// defaults
var mover_offset = 735;
var mover_current = 0;
var mover_pause = 5000;
var mover_animationTime = 750;
var mover_popTime = 300;
var mover_pos = 1;
var mover_max = 1;
var mover_old_pos = 1;
var myOldID = "restaurants-art";
var myOffer = "open";

// function to make color bullets
function makeColorBullets(i,e) {
	
	var myBulletColorStyle = "blue";
	var myTextColorStyle = "gray";
	
	$(e).addClass(myBulletColorStyle);
	$(e).find("li").wrapInner("<span class=" + myTextColorStyle + " />");
	
}

// function for on state of navs
function checkState(i,e) {
	
	myHREF = $(e).attr("href");
	mySRC = $(e).find("img").attr("src");

	if ((myHREF == "area-map/" && pageLabel == "download-maps-listings") || (myHREF == "area-map/" && pageLabel == "locust-map") || parentPageLabel == myHREF.substr(0,myHREF.length - 1) || pageLabel == myHREF.substr(0,myHREF.length - 1)) {
		$(e).find("img").attr("src",mySRC.substr(0,mySRC.lastIndexOf(".")) + "-on" + mySRC.substr(mySRC.lastIndexOf("."),mySRC.length));
		$(e).parent().addClass("selected");
	}
	
}

// highlight
function toggleHighlight(i,e) {
	
	var myURL = $(e).find("img").attr("originalSRC");

	if (!$(e).hasClass("selected")) {
		var newURL = myURL.substr(0,myURL.lastIndexOf(".")) + "-on" + myURL.substr(myURL.lastIndexOf("."),myURL.length);
		$(e).addClass("selected");
	} else { 
		var newURL = myURL;
		$(e).removeClass("selected");
	}
	
	$(e).find("img").attr("src",newURL);
	
}


// function for hovers
function makeHover(i,e) {
	
	var myURL = $(e).find("img").attr("src");
	$(e).find("img").attr("originalSRC",myURL);
	
	if (myURL.indexOf("-on.") == -1) {
		
		var myOnURL = myURL.substr(0,myURL.lastIndexOf(".")) + "-on" + myURL.substr(myURL.lastIndexOf("."),myURL.length);
	
		$(e).hover(function() {
			if (!$(e).hasClass("selected")) $(e).find("img").attr("src",myOnURL);
		},function(){
			if (!$(e).hasClass("selected")) $(e).find("img").attr("src",myURL);
		});
		
	}
	
}

// function to "pop out" the current image
function popOut(pos) {
	
	// pop up
	var myImage = $('#gallery div.image-hold')[pos];
	$(myImage).animate({top:"0px"},mover_popTime,"easeOutExpo");
	
	// kill fades
	$(myImage).find(".gallery-plate-black").fadeOut(mover_popTime);
	$(myImage).find(".gallery-plate-white").fadeOut(mover_popTime);
	
	// show border
	$(myImage).find(".gallery-border").fadeIn(mover_popTime);
		
	// timer
	setTimeout("dropIn(" + pos + ")",mover_pause);
	
}

// function to "drop in" the current image
function dropIn(pos) {
	
	// drop in
	var myImage = $('#gallery div.image-hold')[pos];
	
	// hide border
	$(myImage).find(".gallery-border").fadeOut(mover_popTime);
	
	// animate
	$(myImage).animate({top:"20px"},mover_popTime,"easeOutExpo"/*,function() {
		
		// move the gallery
		moveGallery(pos);
		
	}*/);
	
	// move the gallery
	moveGallery(pos);
	
	// show fades
	$(myImage).find(".gallery-plate-black").fadeIn(mover_popTime);
	$(myImage).find(".gallery-plate-white").fadeIn(mover_popTime);

}

// function to move gallery
function moveGallery(pos) {
	
	// new position
	pos--;
	
	// we have reached the position just prior to the end
	if (pos < 1) {
		
		// copy last image on the right to first place on the left
		$("#gallery .image-hold:last").prependTo("#gallery");

		// get the old first image
		var oldFirstImage = $("#gallery .image-hold")[1];
		
		// position it before the previous first element
		var myNewValue = parseInt($(oldFirstImage).css("left")) - mover_offset;
		$("#gallery .image-hold:first").css("left",myNewValue.toString() + "px");
		
		// reset position
		pos = 1;
		
	}
	
	// advance
	mover_current += mover_offset;
	
	$("#gallery").animate({left:mover_current.toString() + "px"},mover_animationTime,"easeInOutQuad",function() {
		popOut(pos);
	});
	
}

// function for sliding, looping gallery
$.fn.mover = function() {
	
	// set the original maximum
	mover_max = $(this).find('.image-hold').length;
	
	// default left value
	var myLeft = 0;
	
	$(this).find('.image-hold').each(function(i,e) {
	
		// add black and white fades
		$(e).append('<div class="gallery-plate-black"></div>');
		$(e).append('<div class="gallery-plate-white"></div>');
		
		// add borders
		$(e).append('<div class="gallery-border"></div>');
		
		// dim fades
		$(e).find(".gallery-plate-black").css('opacity',0.3);
		$(e).find(".gallery-plate-white").css('opacity',0.7);
		
		// hide borders
		$(e).find(".gallery-border").fadeOut(0);
		
	});
	
	// copy all images to the left twice
	var myImages = $(this).find('.image-hold').clone();
	$(myImages).prependTo("#gallery");
	$(myImages).prependTo("#gallery");
	
	// set the left value
	$(this).find('.image-hold').each(function(i,e) {
	
		// set left value
		myLeft = i * mover_offset;
		$(e).css("left",myLeft.toString() + "px");
		
		// set top value
		$(e).css("top","20px");
		
	});
	
	// move all images left one full set
	mover_current = -(mover_offset * (mover_max - 1) - (238 + 35));
	$("#gallery").css("left",mover_current.toString() + "px");
	
	// set the current position (first image in second set)
	mover_pos = mover_max - 1;
	
	// get the new, created maximum
	mover_max = $(this).find('.image-hold').length;
	
	// fix the current position
	popOut(mover_pos);
	
}

$(document).ready(function() {
	
	$("#gallery").mover();
	
	$("ul#nav a").each(checkState);
	$("ul#util-nav a").each(checkState);
	$("ul#secondary-nav a").each(checkState);
	
	$("ul#nav a").each(makeHover);
	$("ul#util-nav a").each(makeHover);
	$("ul#side-nav a").each(makeHover);
	$("ul#secondary-nav a").each(makeHover);
	$("a#watch-our-virtual-tour").each(makeHover);
	$("a.submit-btn").each(makeHover);
	$("a.download-pdf").each(makeHover);
	$("a#view-carls-bio").each(makeHover);
	$("#map-hold a").each(makeHover);
	$("#map-hold a[href=restaurants-art]").each(toggleHighlight);
	
	$("#page-text ul").each(makeColorBullets);
	
	$("#map-hold a").bind('click',function(e){
		
		e.preventDefault();
		
		var myID = $(this).attr("href");
		
		if (myID != myOldID) {
			
			$("#map-hold a.selected").each(toggleHighlight);
			$(this).each(toggleHighlight);
			
			$("#" + myID).fadeIn(300);
			$("#" + myOldID).fadeOut(300);
		
			myOldID = myID;
			
		}
		
		$(this).blur();
		
	});


	$('a.submit-btn').bind('click',function(e) {
		e.preventDefault();
		$('#plan-form').submit();
	});
	
	$('a#view-carls-bio').bind('click',function(e) {
		e.preventDefault();
		$('#carls-bio').slideToggle();	
		$(this).blur();	
	});
	
	$("[rel=lightbox]").fancybox({overlayOpacity:0.6});
	
	$('a#home-offer-switch').bind('click',function(e) {
		e.preventDefault();
		$('#home-offer .body').slideToggle();
		if (myOffer == "open") {
			$('a#home-offer-switch').removeClass("open").addClass("closed");
			myOffer = "closed";
		} else {
			$('a#home-offer-switch').removeClass("closed").addClass("open");
			myOffer = "open";
		}
		$(this).blur();
	});

});
