/*AJAX Javascript Functions*/
var info_link_hover = false; //variable for whether we're hovering over the info link
var dropdown_hover = false; //variable for whether we're hovering over the dropdown

$(document).ready(function() { //when the document is loaded
    // Prepare
    var History = window.History; // Note: We are using a capital H instead of a lower h
    if ( !History.enabled ) {
         // History.js is disabled for this browser.
         // This is because we can optionally choose to support HTML4 browsers or not.
        return false;
    }

    // Bind to StateChange Event
    History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
        var State = History.getState(); // Note: We are using History.getState() instead of event.state
        //History.log(State.data, State.title, State.url);
    });

	/*Cylce banner backgrounds for pages:*/
	$('#banner-page-bg').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
	
	/*Cylce billboard backgrounds for events:*/
	$('#billboard-container-churchlife').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});

	/*Information Drop-Down:*/
	$('#info-link').hover(function() { //when the info link is hovered over
		info_link_hover = true; //set the hovering flag to true
		//if(!dropdown_open) { //if the dropdown isn't already open
		$('#drop-down').fadeIn(200); //toggle the Information menu
	}, function() { //when the info link hovering is ended
		setTimeout(function() { //make it wait a little bit in case we've moved to the info link
			if(!dropdown_hover) { //if we're not hovering over the info link area
				$('#drop-down').fadeOut(200); //toggle the menu
			}
		},
		10); //make it pause for 250 ms
		info_link_hover = false; //no longer hovered over
	});

	$('#info-link').click(function() { //when the INFORMATION link is clicked
		$('#drop-down').fadeToggle(200); //toggle the Information menu
	});

	$('#drop-down').hover(function() { //when the mouse goes onto the dropdown menu
		dropdown_hover = true;
	}, function() { //when the mouse goes off the dropdown menu
		dropdown_hover = false; 
		setTimeout(function() { //make it wait a little bit in case we've moved to the info link
			if(!info_link_hover) { //if we're not hovering over the info link area
				$('#drop-down').fadeOut(200); //toggle the menu
			}
		},
		10); //make it pause for 250 ms
	});

	/*Ministry Menu AJAX:*/
	/*Apply slide scrollers:*/
	$('.page_event_wrapper').imageSlider(); //apply the image slider to the events
	$('.page_group_wrapper').imageSlider(); //apply the image slider to the groups

	/*On loading, get the current location based on hash:*/
	pathArray = window.location.pathname.split( '/' ); //get the URL path parts
	/*Get the starting state (if any):*/
	var State = History.getState(); // Note: We are using History.getState() instead of event.state
	start_selected = $('.menu-selected').first().children().attr('id');
	if(State.data.state === undefined) { //if it's undefined (no state set)
		if(pathArray.length > 2) { //if we have at least a partial path
			active_content = pathArray[pathArray.length-2]; //set the active content to the current page
			if(!$('#'+active_content).parent().hasClass('menu-selected')) { //if the desired state is not the selected one already
				$base_menu_item = $('.menu-bar').find('> ul').find('> li').find('> a').first();
				base_menu_id = $base_menu_item.attr('id'); //get the ID
				$('#page-'+base_menu_id).hide(); //hide the first area
				$base_menu_item.parent().removeClass('menu-selected'); //turn off the selected class
				$('#page-'+active_content).show(); //show the new area
				$('#'+active_content).parent().addClass('menu-selected'); //toggle the selected class (to on)
				$('.page_event_wrapper').imageSlider(false); //apply the image slider to the events (without new arrows)
				$('.page_group_wrapper').imageSlider(false); //apply the image slider to the groups (without new arrows)
			}
		} else { //if not, it's a home page
			active_content = ""; //it's the home page
		}
	} else {
		active_content = State.data.state; //otherwise, use the appropriate state
		if(!$('#'+active_content).parent().hasClass('menu-selected')) { //if the desired state is not the selected one already
			$('#page-'+start_selected).hide(); //hide the first area
			$('#'+start_selected).parent().removeClass('menu-selected'); //turn off the selected class
			$('#page-'+active_content).show(); //show the new area
			$('#'+active_content).parent().addClass('menu-selected'); //toggle the selected class (to on)
			$('.page_event_wrapper').imageSlider(false); //apply the image slider to the events (without new arrows)
			$('.page_group_wrapper').imageSlider(false); //apply the image slider to the groups (without new arrows)
		}
	}

	/*Deal with clicks on the menu:*/
	$('.page-menu').click(function() { //when a page menu link is clicked
		/*Determine which link was clicked:*/
		var lid = $(this).attr('id'); //get the link ID
		var lname = $(this).attr('title'); //get the link name (for AJAX)
		if(lid == pathArray[1]) { //if the lid is that of the base
			History.pushState({state: lid}, "Living Faith Fellowship | "+lname, "http://livingfaithfellowship.com/"+lid+"/"); //just go straight to that lid
			_gaq.push(['_trackPageview', '/'+lid+'/']); //track this page view

		} else { //otherwise, append the base			
		    History.pushState({state: lid}, "Living Faith Fellowship | "+lname, "http://livingfaithfellowship.com/"+pathArray[1]+"/"+lid+"/"); //go to the base/lid
			_gaq.push(['_trackPageview', '/'+pathArray[1]+'/'+lid+'/']); //track this page view
		}

		/*Determine if content should be changed and act:*/
		if(active_content != lid) { //if the clicked button is not already active
			$('#page-'+active_content).hide(); //hide the active area
			$('#'+active_content).parent().removeClass('menu-selected'); //toggle the selected class (to off)
			$('#page-'+lid).show(); //show the new area
			$(this).parent().addClass('menu-selected'); //toggle the selected class (to on)
			active_content = lid; //save off the new active content
			$('.page_event_wrapper').imageSlider(false); //apply the image slider to the events (without new arrows)
			$('.page_group_wrapper').imageSlider(false); //apply the image slider to the groups (without new arrows)
		}
		return false; //return false to avoid jumping on the page
	});

	/*
	var d = new Date(); //set up a date object
	var current_year = d.getFullYear(); //get the current year
	var current_month = d.getMonth()+1; //get the current month (add one to convert from JS to PHP numbering)
	$('#calendar').load('http://livingfaithfellowship.com/ajcal?lff_ajcal_year='+current_year+'&lff_ajcal_month='+current_month+'&lff_ajcal_post_type=devotional'); //load the calendar

	/*Calendar AJAX Work:*//*
	$('.cal_month').click(function() { //When the calendar's previous-month link was clicked
		var month = $(this).attr('name');
		alert(month);
		//$('#calendar').load('http://livingfaithfellowship.com/wp-content/themes/Wolcott-2/calendar_ajax.php?year='+current_year+'&month=

	});
*/
});

