/*-------------------------------------------------------------*
 * jquery-vabtc.js						*
 * Author: Jeff Lewis (jeffhlewis@gmail.com)	*
 * Last Modified: 1.2.2009                                 	*
 * This file contains all of the jquery code for the	*
 * various town center of virginia beach website	*
 * sections.							*
 *------------------------------------------------------------	*/

$(document).ready(function() {
	//Initialization Code
	jQuery.easing.def 	= "easeInOutQuint";		//GENERAL - set default easing
	var fadeSpeed		= 400;					//GENERAL - default fading speed
	var maxNewsItems	= 3;					//HOME - max # of items to display in news feed
	var currentNewsItem = 0;					//HOME - current item being displayed in news feed
	var timerDelay		= 6000;					//HOME - # of seconds x 1000 to cycle each news feed
	var keepCycling     = true;					//HOME - flag for whether to keep the timer running	

	//EVENTS - Ajax load for events
	$('body').click(function(event) {
		ajaxContainer = $("ul.eventList");
		ajaxLoader = $(".ajaxLoader");
		if ($(event.target).is('a.ajaxLink')) {
			var linkHref = $(event.target).attr("href");	//Link of new section to load
			ajaxContainer.fadeOut(fadeSpeed, function() {
				ajaxLoader.fadeIn(fadeSpeed/2, function() {
					ajaxContainer.load(linkHref, function() {
						ajaxLoader.fadeOut(fadeSpeed/2, function() {
							ajaxContainer.fadeIn(fadeSpeed);
						});//ajaxLoader.fadeout
					});//ajaxContainer.load
				});//ajaxLoader.fadein
			});//ajaxContainer.fadeto
			return false;
		}//end if
	});//end body.click

	//Cycle Plugin
	$(".cyclePics").cycle({
		fx:    	'scrollLeft',
		timeout:  0
	});
	
	//Cycle Plugin				
	$("#flashContent").cycle({
		fx:    	'fade',
		timeout:  0
	});

	//GENERAL - Process Menu Commands
	function setMenuPosition(position) {
		$sectionElement = $("ul.navigation li a").filter(":eq(" + position + ")");
		$sectionElement.addClass("on");
		$("ul.secondaryNavigation li a").removeClass("on").filter(":contains(" + $sectionElement.text() + ")").addClass("on");
	};

	//HOME - Timer Code for SideBarMenu
	$.timer(timerDelay, function() {
		currentNewsItem = currentNewsItem + 1;			//increment event counter
		if (currentNewsItem == maxNewsItems) {			//if event counter reaches limit...
			currentNewsItem = 0;						//reset to zero
		}
		if (keepCycling) {								//if keepCycling flag is true...
			showEvent("event" + currentNewsItem);		//show the event
		} else {
			$(this).stop();								//otherwise, stop the timer altogether
		}
	});
	
	//HOME - Processes clicks for sidebar menu
	$("ul.sideBarMenu li a.event").click(function() {
		showEvent($(this).attr("id"));					//show the event that was clicked
		keepCycling = false;							//notify timer to stop
		return false;
	});

	//SHOP - DINE - SERVICES - Process Tenant Buttons
	$("ul.tenantListing li a").click(function() {
		if (!$(this).hasClass("on")) {															//prevent viewing items that are already "on"
			var allButtons 	= $("ul.tenantListing li a");										//get all buttons
			var allContent 	= $("ul.tenantDescriptions li");									//get all content
			var buttonId	= $(this).attr("id").substring(6);									//extract ID # from button that was clicked
			allContent.hide();																	//hide all the content
			allButtons.removeClass("on");														//clear buttons of "on" state
			$(this).addClass("on").blur();														//turn event button "on"
			$("ul.tenantDescriptions li[id = tenantDescription" + buttonId + "]").fadeIn(fadeSpeed);	//fade in content for clicked item
		}
		return false;
	});

	//HOME - Show a sideBarMenu event
	function showEvent(eventId) {
		var event = $("ul.sideBarMenu li a.event[id = " + eventId + "]"); 	//the event we want to show
		if (!event.hasClass("on")) {										//prevent viewing items that are already "on"
			var allButtons 	= $("ul.sideBarMenu li a.event");				//get all buttons
			var allContent 	= $("ul.sideBarMenu li div");					//get all content
			allButtons.removeClass("on");									//clear buttons of "on" state
			allContent.hide();												//hide all content
			event.addClass("on").blur().siblings().fadeIn(fadeSpeed * 1.5);	//turn event button "on" and fade in content
		}
	};

	//DINE - SHOP - SERVICES - Enable Tooltips
	$("ul.tenantListing li a").tooltip({
		fixPNG: false,
		track: true,
		delay: 500,
		showURL: false,
		extraClass: "balloon",
		top: -130,
		left: -25
	});

	//LIVE - STAY - Cycle Plugin Settings
	$(".cyclePics").each(function() {
		var nav = $(".cyclePicsNav", this.parentNode);
		$(this).cycle({
			fx:    	'scrollLeft',
			timeout:  0,
			pager: nav
		});
	});

	//LIVE - STAY - Process Button Clicks for Cycle plugin
	$(".cyclePicsNav").each(function() {
		var navLinks = $(".cyclePicsNav a", this.parentNode);
		navLinks.click(function() {
			navLinks.removeClass("on");
			$(this).addClass("on");
			$(this).blur();
			return false;
		}).filter(":first").click();
	});
	
	//DIRECTORY - Sidebar accordion menu - hide & process category clicks
	$("ul.accordionList").hide();
	$("a.accordionTitle").click(function() {
		id = $(this).attr("id").substring(4);
		accordionSwitch(id);
		$(this).blur();
		return false;
	});

	//DIRECTORY - Sidebar accordion menu - process tenant clicks
	$("a.accordionLink").click(function() {
		var linkID = parseInt($(this).attr("id").substring(4));		//get the id of the link clicked					
		$("a.accordionLink").removeClass("on");
		$(this).addClass("on").blur();
		$("#flashContent").cycle(linkID);							//call the cycle plugin to flip the pics
		return false;
	});
	
	//DIRECTORY - Sidebar accordion menu - switch categories
	function accordionSwitch(index) {
		$("ul.accordionList").slideUp(400);
		$("ul.accordionList[id = list" + index + "]").addClass("on").slideDown(400, function() {
			$("a.accordionLink").removeClass("on");					
			$("ul.accordionList[id = list" + index + "] li a").filter(":first").click();
		});
	};
	
	//DIRECTIONS-PARKING - Process mouseclicks on map
	$(".dataColumn li").click(function() {
		var buttons = $(".parking li");								//get all the sidebar buttons
		var links = $(".parking a");								//get all the links
		var linkID = parseInt($(this).attr("id").substring(7));		//get the id of the link clicked
		buttons.removeClass("on");									//turn all buttons "off"
		if ($(this).attr("id").substring(0,4) == "view") {			//if user clicked a view switch button...
			linkID = parseInt($(this).attr("id").substring(4));		//get the view ID
			$(".dataColumn").hide(0);								//hide the sidebar
			$(".dataColumn[id$=" + linkID + "]").fadeIn(fadeSpeed, function() {			//display new sidebar
				$(".dataColumn[id$=" + linkID + "] li").filter(":first").click();	//"click" the first item
			});
		} else {													//if user DIDNT click a view button...
			$(this).addClass("on");									//turn the selected button "on"
			links.blur();											//blur the links (removes bounding box)
			$(".cyclePics").cycle(linkID);							//call the cycle plugin to flip the pics
		}
		return false;
	}).filter(":first").click();									//click the first button on page load

	//DIRECTIONS-PARKING - show only the first parking column
	$(".parking .dataColumn").hide().filter(":first").show();

	//HOME - Code to run at start
	showEvent("event0");		//show the first event

	//GENERAL - Enable scrollTo animation for page anchors
	$.localScroll({duration:600, easing:"swing"});
	
	//DIRECTORY - Switch to first accordion item
	accordionSwitch(0);

});
