jQuery(document).ready(function() {
	jQuery('#tabs').tabs();
	jQuery('#eventtabs').tabs();

	jQuery('#registerform').validate( {
		rules: {
			user_password: "required",
			user_confirm_password: {
			equalTo: "#user_password"
    }
  }
	});
	jQuery('#authorize-event').validate();
	jQuery('#quoteform').validate();
	jQuery('#authorize-user').validate();
	jQuery('#addrole-form').validate();
	jQuery("#event_begin").datepicker({ dateFormat: 'mm/dd/yy' });		
	jQuery("#event_end").datepicker({ dateFormat: 'mm/dd/yy' });	
	jQuery("#authorize-event-div").hide();
	jQuery("#subscribe").click(function() {
		jQuery("#authorize-event-div").animate({"height": "toggle"}, { duration: 1000 });;
	});	
	jQuery("#gfoa_category").change(function() {
		var event_type = jQuery("#gfoa_category").val();
		if( event_type == 'chapter-event') {
			jQuery('#gfoa_showhide_event').hide();
			jQuery('#event_fee').val(0);
		} else if(event_type == 'state-event') {
			jQuery('#gfoa_showhide_event').show();
			jQuery('#event_fee').val('');
		}
	});
	
	jQuery ( '#select_chapter' ).css ( 'display' , 'none' );
	jQuery ( '#change_chapter' ).live ( "click" , function() {
		jQuery ( '#select_chapter' ).css ( 'display' , 'inherit' );
		changeChapters();
		return false;
	});
});

function changeChapters () {
	
	jQuery ( '#select_chapter' ).unbind ( 'change' );
	jQuery ( '#select_chapter' ).change ( function() {
		if ( jQuery ( this ).val() != '' ) {
			var chapter = jQuery ( this ).val();
			jQuery.post ( '/wp-content/plugins/gfoa/ajax.php?f=get_events_by_chapter' , { 'select_chapter' : chapter } , function( data ) {
				if ( data ) {
					var html = [];
					if ( chapter == 'all' ) {
						html.push ('<h4>Currently displayed events for all chapters. <a href="" name="change_chapter" id="change_chapter">Change Chapter</a></h4>');
					}
					else {
						html.push ('<h4>Currently displayed events for the ' + chapter + ' chapter. <a href="" name="change_chapter" id="change_chapter">Change Chapter</a></h4>');
					}
					html.push ( "<select name='select_chapter' id='select_chapter' style='display: none'><option value=''></option><option value='all'>All</option><option value='gfoa-mo'>GFOA - MO</option><option value='central'>Central</option><option value='st-louis'>St. Louis</option><option value='southwestern'>Southwestern</option><option value='western'>Western</option></select>" );
					
					if ( data[0] ) {
						for ( x in data ) {
							html.push ( '<div class="event" style="margin: 20px 0;">' );
							html.push ( "<div style='clear:left; margin:0px 8px 0 0; width:37px; height: 48px; padding:0; background:url(/wp-content/plugins/gfoa/css/images/bg-calendar-icon.jpg); float:left; text-align:center;'>" );
							html.push ( '<div style="font-size:10px; font-weight:bold; margin:0; width:100%; color:#fff; line-height:24px;">' + gfoaGetMonth (data[x].event_begin) + '</div>');
							html.push ( '<div style="font-size:24px; font-weight:bold; line-height:18px; color:#555;">' + gfoaGetDate(data[x].event_begin) + '</div>');
							html.push ( '<div style="font-size:10px; line-height:22px; color:#777;">' + gfoaGetDay( data[x].event_begin) + '</div>' );
							html.push ( "</div>" );
							html.push ( "<div style='float:left; margin-left: 5px;'><a href='" + window.location.href +  "?event_id=" + data[x].event_id + "'><h3 style='margin-bottom:4px'>" + data[x].event_title + "</h3></a>" );
							html.push ( data[x].event_category );
							html.push ( "</div><br clear='all' /></div>" );
						}
					}
					else {
						html.push ( '<h5> No Events Found. </h5>' );
					}
					
					jQuery ( '#events' ).html ( html.join('') );
				}
			} , 'json' );
		}
	});


}

function gfoaGetMonth ( date ) {
	var obj = new Date();
	obj.setTime ( date * 1000 );

	var months = ['Jan' , 'Feb' , 'Mar' , 'Apr', 'May' , 'Jun' , 'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec'];
	return months[obj.getMonth()];
}

function gfoaGetDate ( date ) {
	var obj = new Date();
	obj.setTime ( date * 1000 );

	return obj.getDate();
}

function gfoaGetDay ( date ) {
	var obj = new Date();
	obj.setTime ( date * 1000 );
	
	var days = [ 'Sun' , 'Mon' , 'Tue' , 'Wed' , 'Thu' , 'Fri' , 'Sat' ];
	return days[obj.getDay()];
}
