$(document).ready(function(){
	var currentImageActive = 1;
	var currentWindowPosition = 0;
	var totalPosts = 3;
	var maxSize = totalPosts*506-10;
	$(".post-navigation").hover(
		function() {
			var whichArrow = $(this).attr("rel");
			$(this).attr("src","/stg/wp-content/themes/peak/art/"+whichArrow+"_on.png");
		}, function() {
			var whichArrow = $(this).attr("rel");
			$(this).attr("src","/stg/wp-content/themes/peak/art/"+whichArrow+".png");
		}
	)

	$(".post-navigation-static").hover(
		function() {
			var whichArrow = $(this).attr("rel");
			$(this).attr("src","/stg/wp-content/themes/peak/art/"+whichArrow+"_on.png");
		}, function() {
			var whichArrow = $(this).attr("rel");
			$(this).attr("src","/stg/wp-content/themes/peak/art/"+whichArrow+".png");
		}
	)
	
	if (startUpSlide == "Yes") {
		var currentImageActive = 2;
		var firstPostWidth = $(".posts .post:first").width();
		$(".post-starter").width((firstPostWidth/3)-12);
		//alert(firstPostWidth);
		var finalMarginStart = (900 - firstPostWidth)+30;
		//alert(finalMarginStart);
		$(".peak-window div.posts").css({'margin-left':'-'+finalMarginStart+'px'});
		currentWindowPosition = (firstPostWidth/2+235)*-1;
	}

	if (startUpSlide2 == "Yes") {
		var currentImageActive = 2;
		var firstPostWidth = 538;
		var finalMarginStart = 450;
		//alert(finalMarginStart);
		$(".peak-window div.posts").css({'margin-left':'-'+finalMarginStart+'px'});
		currentWindowPosition = -450;
	}

	$(".post-navigation").click(function(){
		var currentMargin = $(".peak-window").attr("marginLeft");
		var whichArrow = $(this).attr("rel");
		var activeItem = currentImageActive;
		var totalSize = $(".post").size();
		if (totalSize == 0) {
			totalSize = $(".about-us-post").size();
		}
		//alert(activeItem+"/"+totalSize+" Starting point:"+currentWindowPosition);
		if (whichArrow == "arrow_r") {
			if ((activeItem)==totalSize) {
				currentImageActive = 1;
				if (startUpSlide == "Yes") {
					currentWindowPosition = 30;
				} else {
					if ($(this).hasClass("aboutPage")){
						currentWindowPosition = 0;
					} else {
						currentWindowPosition = 98;
					}
				}
			} else {
				currentImageActive = currentImageActive+1;
				if ($(this).hasClass("aboutPage")){
					currentWindowPosition = currentWindowPosition-900;
				} else {
					currentWindowPosition = currentWindowPosition-($("#width-"+activeItem).width()+52);
				}
			}
		} else {
			currentImageActive = currentImageActive-1;
			if ($(this).hasClass("aboutPage")){
				currentWindowPosition = currentWindowPosition+900;
			} else {
				currentWindowPosition = currentWindowPosition+($("#width-"+(activeItem-1)).width()+52);
			}
		}
		//alert("Current position: "+currentWindowPosition+", Current Image: "+currentImageActive+" but active item is "+activeItem+", Current Div Width: "+$("#width-"+(activeItem)).width());
		//alert($("#width-4").width());

		$(".peak-window div.posts").animate({
			marginLeft:(currentWindowPosition)+'px'
		})
		
		if (currentWindowPosition > -100) {
			$(".arrowLeft").fadeOut(400);
		} else {
			$(".arrowLeft").fadeIn(400);
		}
		if (currentWindowPosition < maxSize) {
			//$(".arrowRight").fadeOut(400);
		} else {
			//$(".arrowRight").fadeIn(400);
		}
		
	})
	
	$(".pe-images").each(function(){
		$(this).attr("alt","");
	})

	$(".arrowLeftStatic").hover(function(){
		$(".tooltipLeft").fadeIn(300);
	}, function(){
		$(".tooltipLeft").fadeOut(300);
	})

	$(".arrowRightStatic").hover(function(){
		$(".tooltipRight").fadeIn(300);
	}, function(){
		$(".tooltipRight").fadeOut(300);
	})
	
	$(".toolTipText").each(function(){
		//alert($(this).attr("innerHTML").replace("Peak Experience ",""));
		$(this).attr("innerHTML",$(this).attr("innerHTML").replace("Peak Experience ","").replace(/^..(.*)/, "$1"));
	})
	$("#site-map .lcp_catlist li a").each(function(){
		//alert($(this).attr("innerHTML").replace("Peak Experience ",""));
		$(this).attr("innerHTML",$(this).attr("innerHTML").replace("Peak Experience ","").replace(/^..(.*)/, "$1"));
	})

	$(".post-navigation-pe").click(function(){
		var currentMargin = $(".peak-window-experience").attr("marginLeft");
		var whichArrow = $(this).attr("rel");
		var activeItem = currentImageActive;
		var totalSize = 700;
		var totalPageSize = $(".pe-pages").size();
		if (whichArrow == "arrow_r") {
			if ((activeItem)==totalSize) {
				currentImageActive = 1;
				currentWindowPosition = 0;
			} else {
				currentImageActive = currentImageActive+1;
				currentWindowPosition = currentWindowPosition-900;
			}
		} else {
			currentImageActive = currentImageActive-1;
			currentWindowPosition = currentWindowPosition+900;
		}
		//alert("Current position: "+currentWindowPosition+", Current Image: "+currentImageActive+" but active item is "+activeItem+", Current Div Width: "+$("#width-"+(activeItem)).width());
		//alert($("#width-4").width());
		$(".pe-page-"+currentImageActive+" img").each(function(){
			$(this).attr("src",$(this).attr("rel"));
		})

		$(".peak-window-experience div.posts").animate({
			marginLeft:(currentWindowPosition)+'px'
		})
		
		if (currentWindowPosition > -100) {
			$(".arrowLeft").fadeOut(400);
		} else {
			$(".arrowLeft").fadeIn(400);
		}
		if (totalPageSize <= currentImageActive) {
			$(".arrowRight").fadeOut(400);
		} else {
			$(".arrowRight").fadeIn(400);
		}
		
	})

	$(".peak-navigation li").hover(function(){
		if ($(this).hasClass("activeNav")){ } else {
			$(this).css('background-image', $(this).css('background-image').replace(".png","_hov.png"));
		}
	}, function(){
		if ($(this).hasClass("activeNav")){ } else {
			$(this).css('background-image', $(this).css('background-image').replace("_hov",""));
		}
	})
	
	
	$(".emailAddress").hover(function(){
		$(this).css({'color':'#716150'});
	},function(){
		$(this).css({'color':'#46332d'});
	})
	
	$(".emailAddress").click(function(){
		window.location = "mailto:"+$(this).attr("innerHTML");
	})
	

	// Startup scripts
	if (currentWindowPosition == 0) {
		$(".arrowLeft").hide();
	} else {
		$(".arrowLeft").show();
	}
	
	$(".post-experience").hover(function(){
		$(this).css({"background-image":"url(/stg/wp-content/themes/peak/art/pe_list_bg_on.png)"});
	}, function(){
		$(this).css({"background-image":"url(/stg/wp-content/themes/peak/art/pe_list_bg.png)"});
	})
	
	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
			&& location.hostname == this.hostname) {
				var $target = $(this.hash);
				$target = $target.length && $target
				|| $('[name=' + this.hash.slice(1) +']');
				if ($target.length) {
					var targetOffset = $target.offset().top;
					$('html,body')
					.animate({scrollTop: targetOffset}, 300);
					return false;
				}
		}
	});
	
	$(".showSubmitForm").click(function(){
		$(".showWhatArePeakExperiences").removeClass("activeNav");
		if($(this).hasClass("activeNav")){
			$(this).removeClass("activeNav");
		} else {
			$(this).addClass("activeNav");
		}
		$(".explainPE").slideUp(300,function(){
			if ($('.submitFormArea').is(':hidden')){
				$(".submitFormArea").show(300);
			} else {
				$(".submitFormArea").slideUp(300);
			}
		})
	})

	$(".showWhatArePeakExperiences").click(function(){
		$(".showSubmitForm").removeClass("activeNav");
		if($(this).hasClass("activeNav")){
			$(this).removeClass("activeNav");
		} else {
			$(this).addClass("activeNav");
		}
		$(".submitFormArea").slideUp(300,function(){
			$(".explainPE").slideToggle(300);
		})
	})

	$(".submitButton").hover(function(){
		$(this).attr("src","/stg/wp-content/themes/peak/art/submit_on.png");
	}, function(){
		$(this).attr("src","/stg/wp-content/themes/peak/art/submit.png");
	})
	
	$(".submitForm").click(function(){
		//alert('submit');
		$(this).parent()[0].submit();
	})


	$(".setCookie").click(function(){
		//Create (or update) the value of a cookie to expire in 2 days:
		$.cookie('over21', 'Yes', { expires: .3 });
		$(".AgeVerifyModal").hide();
		$(".MessageArea").hide();
	})
	
	$(".noEnter").click(function(){
		window.location.replace("http://www.facebook.com/pages/Peak-Organic-Brewing-Company/18292863559");
	})

	$(".deleteCookie").click(function(){
		// Delete a cookie:
		$.cookie('over21', null);
	})
	
	// Get the value of a cookie:
	var over21Cookie = $.cookie('over21');
	if(over21Cookie != "Yes"){
		$(".AgeVerifyModal").show();
		$(".MessageArea").show();
	}
	
	// Fix PNG for IE
	$(document).pngFix();
	$('.lightboxImage').lightBox();
	
	var timedTwitter = setTimeout("changeTwitterAccount(1);",10000);
})

function changeTwitterAccount(whatIsCurrent){
	var newTwitterAccount = whatIsCurrent+1;
	var totalTwitters = $(".twitterAcct").size();
	if (newTwitterAccount > totalTwitters) {
		newTwitterAccount = 1;
	}
	$(".twitterAcct").hide();
	$("#twitter"+newTwitterAccount).show();
	setTimeout("changeTwitterAccount("+newTwitterAccount+");",10000);
}

function limitChars(textid, limit, infodiv)
{
	var text = $('#'+textid).val();	
	var textlength = text.length;
	if(textlength > limit)
	{
		$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else
	{
		$('#' + infodiv).html((limit - textlength) +' characters remaining');
		return true;
	}
}

$(function(){
 	$('#message').keyup(function(){
 		limitChars('message', 150, 'charlimitinfo');
 	})
});
