$(function() {
	$('ul.navMain').superfish();
	
	$('#kidsLink').hover(
		//Over
		function(){
			
			$('#kidsLink img').stop().animate({top:"-25px"},200);
		},
		//Out
		function(){
			$('#kidsLink img').stop().animate({top:"-10px"},200);
		}
	);
	
	$('#search .text,.text.search').val("")
				
	.blur(function(){
		if ($(this).val().length == 0) {
			$(this).val("");
		};
	})
	
	.focus(function(){
		$(this).val("");
	});
	
	$('.smallBtn').bind("click",{percent:80,lineHeight:15},fontResize);
	$('.mediumBtn').bind("click",{percent:100.01,lineHeight:null},fontResize);
	$('.largeBtn').bind("click",{percent:140,lineHeight:21},fontResize);
	$('.printBtn').click(printPage);
	
	function fontResize(event){
		event.preventDefault();
		var percent = event.data.percent;
		var lineHeight = event.data.lineHeight;
		
		$('.content').css({fontSize:percent+"%"});
		if (lineHeight != null){
			$('p').css({lineHeight:lineHeight+"pt"});
		}else{
			$('p').removeAttr("style");
		}
		
		$().cookie('fontSize',percent,1);
		$().cookie('lineHeight',lineHeight,1);
		
		/**/
		/*$('label').css({lineHeight:"15px"})*/
	}
	
	function printPage(){
		window.print();
	}
	
	DD_roundies.addRule('#Home #sidePanel li', '4px');
	
	function checkCookie()
	{
		if ($().cookie('fontSize') && $().cookie('lineHeight')){
			var percent=$().cookie('fontSize');
			var lineHeight=$().cookie('lineHeight');
		}
		
		if (percent!=null && percent!="")
		{
			$('.content').css({fontSize:percent+"%"});
			if (lineHeight != null){
				$('p').css({lineHeight:lineHeight+"pt"});
			}else{
				$('p').removeAttr("style");
			}
		}
		
		if (percent == 100.01){
			$().cookie("fontSize",null);
			$().cookie("lineHeight",null);
		}
		
	}
	checkCookie();
});


// Easing Plugin 
/*
	 * jQuery EasIng v1.1.2 - http://gsgd.co.uk/sandbox/jquery.easIng.php
	 *
	 * Uses the built In easIng capabilities added In jQuery 1.1
	 * to offer multiple easIng options
	 *
	 * Copyright (c) 2007 George Smith
	 * Licensed under the MIT License:
	 * http://www.opensource.org/licenses/mit-license.php
*/
// t: current time, b: begInnIng value, c: change In value, d: duration

$.extend(jQuery.easing, {
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) {return c/2*t*t + b;}
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1){ return c/2*t*t*t + b;}
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) {return c/2*t*t*t*t + b;}
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) {return c/2*t*t*t*t*t + b;}
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t===0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t===0) {return b;}
		if (t==d) {return b+c;}
		if ((t/=d/2) < 1) {return c/2 * Math.pow(2, 10 * (t - 1)) + b;}
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1){ return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;}
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t===0) {return b;}  
		if ((t/=d)==1) {return b+c;}  if (!p){ p=d*0.3;}
		if (a < Math.abs(c)) { a=c;  s=p/4; }
		else {s = p/(2*Math.PI) * Math.asin (c/a);}
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t===0) {return b;}  
		if ((t/=d)==1) {return b+c;}  if (!p){ p=d*0.3;}
		if (a < Math.abs(c)) { a=c;  s=p/4; }
		else {s = p/(2*Math.PI) * Math.asin (c/a);}
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeOutCool: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t===0) {return b;}  
		if ((t/=d)==1) {return b+c;}  if (!p) {p=d*0.3;}
		if (a < Math.abs(c)) { a=c;  s=p/4; }
		else {s = p/(2*Math.PI) * Math.asin (c/a);}

		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t===0) {return b;}  
		if ((t/=d/2)==2) {return b+c;}  if (!p){ p=d*(0.3*1.5);}
		if (a < Math.abs(c)) { a=c;  s=p/4; }
		else {s = p/(2*Math.PI) * Math.asin (c/a);}
		if (t < 1) {return -0.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;}
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*0.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s === undefined){ s = 1.70158;}
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s === undefined){ s = 1.70158;}
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s === undefined){ s = 1.70158; }
		if ((t/=d/2) < 1) {return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;}
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + 0.75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + 0.9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + 0.984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) {return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * 0.5 + b;}
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * 0.5 + c*0.5 + b;
	}
});

var section;

var lang = 'en';
if ($("body").hasClass("FRCA")) {lang = 'fr';}


// Disable opacity effects for IE 7 & 8 to avoid png artifacts
var effects = !(jQuery.browser.msie && parseInt(jQuery.browser.version,10) > 5);

var navIndices = {
	'Phone_Us': 0,
	'Ask_Us_Online': 1,
	'Info_Booth': 2,
	'Your_Space': 3,
	'Get_Involved': 4
};


$(function() {
	section = $('#wrapper')[0].className.substring(5);
	$('#wrapper').removeClass('nojs').addClass('js');
	if ($.browser.msie){$('#wrapper').addClass('ie');}
});




var navHoversDisabled = false;


var defaultDepth = 1000;
var activeDepth = 1100;
var hoverDepth = 1200;

function initNav() {
	var fadingTo = 'on';
	
	

	// Create needed elements and set intial properties
	$('#navigation > ul > li')
		.each(function() {
			
			var self = this;
			var itemName = this.id;
			
			var active = section == itemName.substring(3);

			$('a.link', this).html('<span class="imgs"><img class="idle" src="/KHP/Teens/images/' + lang + '/navigation/' + (active ? 'hr' : 'off') + '/' + itemName + '.png" />\n<img class="' + fadingTo + '" src="/KHP/Teens/images/' + lang + '/navigation/' + (active ? 'hr' : fadingTo) + '/' + itemName + '.png" /></span>');
			if (effects) {
				$('.idle', this).css('opacity', 1);
				$('.' + fadingTo, this).css('opacity', 0);
			}
			
			
			if (this.id.substring(3) == section){
				$(this).css("z-index", activeDepth);
			}else{
				$(this).css("z-index", defaultDepth);
			}
	});
	
	
	//in ie7 the span wrapper breaks the anchor tag, this is a fix.
	if (jQuery.browser.msie && parseInt(jQuery.browser.version,10) == 7){
	
		$('a.link > .imgs').click(function(e){
			document.location.href = $(e.currentTarget).parent().attr("href");
		});
		
	}
			
		
	//
	// Functions for hover on and off
	//
	var easingImage = 'easeOutExpo';
	var easingFade = 'easeOutExpo';
	var currentItem = null;
	
	
	
	var hovers = [function(event) {
		
		var itemId = $(event.currentTarget).attr("id");
		
		if (navHoversDisabled) {return;}

		$(this).css("z-index",hoverDepth);

		// Image
		
		if (itemId != 'navGet_Involved'){
				
				
			if (jQuery.browser.msie && parseInt(jQuery.browser.version,10) <= 6){
				$('a.link', this).stop().css({
				'height': '110px'
				});
				$(this).stop().css({'top': '178px'});
				
			}else{
				$(this).stop().animate({
				'top': '178px'
				}, 200, easingImage);
				
				$('a.link', this).stop().animate({
				'height': '110px'
				}, 200, 'easeOutQuint' );
				
				setTimeout(function(){
					if (itemId == "navInfo_Booth"){
						$("#navInfo_Booth ul")
						.css({visibility:"visible"})
						.slideDown(200);
					}
				}, 50);
			}
			
			
		}else{
			

			if (jQuery.browser.msie && parseInt(jQuery.browser.version,10) <= 6){
				$('a.link', this).stop().css({
				'height': '110px'
				});
				$(this).stop().css({'top': '178px'});
			}else{
				$(this).stop().animate({
				'top': '178px'
				}, 200, easingImage);
				$('a.link', this).stop().animate({
				'height': '110px'
				}, 200, 'easeOutExpo');
			}
		}
		
		
		// Fade
		if (effects) {
			$('.' + fadingTo, this).stop().animate({
				'opacity': 1
			}, 200);
		}
	
	}, function(event) {
		
		var itemName = this.id;
		var active = ("nav"+section == itemName);
		
		if(active){
			$(this).css("z-index",activeDepth);
		}else{
			$(this).css("z-index",defaultDepth);
		}
		
		$("#navInfo_Booth ul").css({display:"none",visibility:"hidden"});
		
		$(this).stop().animate({
			'top': '124px'
		}, 500, easingImage);
		
		// Image
		if (itemName =="navGet_Involved"){
			$('a.link', this).stop().animate({
			'height': '54px'
			}, 500, 'easeOutExpo');
		}else{
			$('a.link', this).stop().animate({
			'height': '44px'
			}, 500, 'easeOutExpo');
		}
		

		// Fade
		if (effects) {
			$('.' + fadingTo, this).stop().animate({
				'opacity': 0
			}, 900);
		}
		
		
	}];

			
	//
	// Initialize the hovers either all together or one by one
	//
	var initHovers = function(n) {
		/*
			If it's a number:
				Positive: Enable just that one
				Negative: Enable the rest except for that one
				If no argument:
				Enable all of them.		
		*/
		if (jQuery.browser.msie && parseInt(jQuery.browser.version,10) <= 6){
			if (typeof n == 'number')
			{
				if (n === 0 && section != 'home') {
					$('#navigation > ul')
					.hover.apply($('#navigation > ul > li'), hovers)
					.hover.apply($('#navigation > ul > li').not(':eq(' + -n + ')'), hovers);
				} else {
					if (n > -1) {
						$('#navigation > ul > li:eq(' + n + ')')
							.one('mouseover', function() { $('#navigation > ul > li:eq(' + n + ') a').queue([]).stop(); })
							.hover.apply($('#navigation > ul > li:eq(' + n + ')'), hovers);
					}
					else {
						$('#navigation > ul')
						.hover.apply($('#navigation > ul > li').not(':eq(' + -n + ')'), hovers);
					}
				}
			}
			else {
				$('#navigation > ul')
					.hover.apply($('#navigation > ul > li'), hovers);
			}
		}else{
			if (typeof n == 'number')
			{
				if (n === 0 && section != 'home') {
					$('#navigation > ul')
					.hoverIntent.apply($('#navigation > ul > li'), hovers)
					.hoverIntent.apply($('#navigation > ul > li').not(':eq(' + -n + ')'), hovers);
				} else {
					if (n > -1) {
						$('#navigation > ul > li:eq(' + n + ')')
							.one('mouseover', function() { $('#navigation > ul > li:eq(' + n + ') a').queue([]).stop(); })
							.hoverIntent.apply($('#navigation > ul > li:eq(' + n + ')'), hovers);
					}
					else {
						$('#navigation > ul')
						.hoverIntent.apply($('#navigation > ul > li').not(':eq(' + -n + ')'), hovers);
					}
				}
			}
			else {
				$('#navigation > ul')
					.hoverIntent.apply($('#navigation > ul > li'), hovers);
			}
		}

		
	};
	
	//
	// Call it in the context of the li with n == index of the element
	// This bounces a tab down
	//
	var bounceUp = function(options) {
		var self = this;
		var itemName = this.id.substring(3);
		
		
		var e = {
			fadeinLength: 500,
			pulseLength: 600,
			dropLength: 500,
			easing: 'easeOutBounce'
		};
		
		if (options){ e = jQuery.extend(e, options);}
		
		
		if (effects) {
				$('a.link', self)
				.animate({ 'opacity': 1 }, 100, e.easing, function() {
						if (section == itemName) {
							$('.idle', self)[0].src = '/KHP/Teens/images/' + lang + '/navigation/hr/nav' + itemName + '.png';
							//$('img.idle').css("z-index",activeDepth); 
							$(this).parent().css("z-index",activeDepth).animate({ 'top': '124px'}, 500, 'easeOutExpo');
							
						}
					})
				.find('.on')
					.animate({ 'opacity': 0 }, e.pulseLength)
	
				.end();
				if (itemName == "Get_Involved"){
					$('a.link', self).animate({ 'height': '54px'}, 500, 'easeOutExpo');
				}else{
					$('a.link', self).animate({ 'height': '44px'}, 500, 'easeOutExpo');
				}
				
					
		} else {
			if (section == itemName) {
					$('.idle', self)[0].src = '/KHP/Teens/images/' + lang + '/navigation/hr/nav' + itemName + '.png';
					$('.idle', self).css("z-index",activeDepth).show(); 
					$('.on').css("z-index",defaultDepth).hide(); 
					$('a.link', self).parent().animate({ 'top': '124px'}, 500, 'easeOutExpo');
				
			}
			if (itemName == "Get_Involved"){
				$('a.link', self).animate({ 'height': '54px'}, 500, 'easeOutExpo');
			}else{
				$('a.link', self).animate({ 'height': '44px'}, 500, 'easeOutExpo');
			}
		}
		
		// Initialize the hovers at a good point in the animation
		setTimeout(function() {
			initHovers.apply(self, [navIndices[itemName]]);
		}, e.dropLength - 150);
	};

	// If it is not the home page, only drop the active tab
	if (section != 'Home' && section != "About_Us"&& section != "Search") {
		var fadeinLength = 250;
	
		var current = $('#navigation > ul > li:eq(' + navIndices[section] + ')')[0];
		
		$(current).css({
			'top': '154px',
			"z-index":activeDepth 
		});

		// Set the defaults
		$('a.link', current).css({
			'height': '95px'
		});
		
		
		bounceUp.apply(current, [{ 'fadeinLength': fadeinLength, pulseLength: 0 }]);
		initHovers(-navIndices[section]);

		return;
	}	


	// Bounce the tabs up in a wave on the home page
	var timeout = 0;
	
	$('#navigation > ul > li').each(function(n)  {
		var self = this;

		// Set the defaults
		$('a.link', this).css({
			'height': '0px'
			
		});
		
		if (effects) {
			$('a.link', this).css({
				'opacity': 0
			});
		}

		setTimeout(function() {
			bounceUp.apply(self);
		}, timeout += 200);
	});
	
}

$(function() {
	initNav();
});
