function getURLParam(strParamName,caps){
	 //alert('getURLParam');
	  var strReturn = "";
	  var strHref = window.location.href;
	  if ( strHref.indexOf("?") > -1 ){
		if(caps!=false){
			var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		}else{
			var strQueryString = strHref.substr(strHref.indexOf("?"));
		}
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
		  if (
			aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
			var aParam = aQueryString[iParam].split("=");
			strReturn = aParam[1];
			break;
		  }
		}
	   //alert(strReturn);
	  }
	  return strReturn;
}
function sendGa(page){
	try {
		pageTracker._trackPageview(page);
	} catch(err) {}
}

//global stuff
$(document).ready(function() {
	$("a.print").click(function(){
		window.print();
	});
	//turn sub nav bar on and off takes
	function subNavToggle(navName,pageLoad){
		//see if the sub nav is there
		if($("#subNav").find("."+navName).html() != null){
			//if first page load just show it, if not animate it
			if(pageLoad==true){
				$("#subNav").show();
				$("#subNav").find("."+navName).css({
					"top": "0px"
				}).siblings().css({
					"top": "-21px"
				});
			}else{
				$("#subNav").slideDown();
				$("#subNav").find("."+navName).animate({
					"top": "0px"
				}).siblings().animate({
					"top": "-21px"
				});
			}
		}else{
			//no subnav so hide it
			$("#subNav").slideUp();
		}
	}
	//mouse off event...
	function mainNavCleanUp(pageLoad){
		//set main nav item from body id
		var mainNav = $("#mainNav a."+$("body").attr("id"));
		
		//test if this mainNav is in the html
		if(mainNav.html() != null){
			//it is in the html so activate it
			if(pageLoad!=true){
				mainNav.animate({"backgroundPosition":"50% 4px"});
			}else{
				mainNav.css({"backgroundPosition":"50% 4px"});
			}

			//turn off others
			mainNav.siblings().animate({"backgroundPosition":"50% 40px"});
			subNavToggle($("body").attr("id"),pageLoad);

		}else{
			//nope the mainNav is not here, close all nav systems down
			$("#subNav").slideUp();
			$("#mainNav a").animate({"backgroundPosition":"50% 40px"});
		}
	}
	//set nav items up to fade in when needed
	$("#mainNav a").css({"backgroundPosition":"50% 40px"});
	//
	$("#mainNav a").hoverIntent(function(){
		$(this).animate({"backgroundPosition":"50% 4px"});
		$(this).siblings().animate({"backgroundPosition":"50% 40px"});

		subNavToggle($(this).attr("class"),false);
	},function(){}).css();

	$("#headHitTest").mouseleave(function(){
		mainNavCleanUp(false);
	});
	//ensure that the nav is looking proper
	mainNavCleanUp(true);



	$(".newWindow").click(function(){
		window.open($(this).attr("href"));
		return false;
	});
	
	
	$("a.thumb").click(function(){
		$('html, body').animate({scrollTop: $($(this).attr("href")).offset().top}, 1000);
		return false;
	});
	$("a.returnToTop").click(function(){
		$('html, body').animate({scrollTop: 0}, 1000);
		return false;
	});
	
});

//home page
$(document).ready(function() {
	// select #flowplanes and make it scrollable. use circular and navigator plugins
	var homeScrollInterval = 5000;
	$("#homeFeature").scrollable({
		size: 1,
		clickable: false,
		onSeek: function(){
			//test if we are at the end of the list of scrollable things
			if(($("#homeFeature").scrollable().getIndex()+1) == $("#homeFeature").scrollable().getSize()){
				//if we are use the same timeout and scroll to start if needed.
				setTimeout('$("#homeFeature").scrollable().seekTo(0)',homeScrollInterval);
			}			
			if(($("#homeFeature").scrollable().getIndex()+1) == 1){
				//$("#homeFeature").scrollable().stop();
			}
		}
	}).navigator({
		navi: "#homeFeatureNav",
		naviItem: 'div',
		activeClass: 'current'
	}).autoscroll({
		autoplay: false,
		interval: homeScrollInterval
	});
	
	//set each pane with proepr left align
	$("#homeTabs .pane").each(function(){
		$(this).css({
			"left": ($(this).parent().position().left*-1)
		});
	});
	//use hoverintent to prevent changing tabs if you did not mean to
	$("#homeTabs ul.tabs li").hoverIntent(function(){
		$(this).find("a").addClass("current");
		$(this).find(".pane").slideDown().fadeIn();
	},function(){
		$(this).find("a").removeClass("current");
		$(this).find(".pane").slideUp().fadeOut();
	});
	//fix the height of the box to prevent page jump
	//$("#homeTabs .panes").height($("#homeTabs .panes .pane").height());
	$("#homeTabsPanes").width($("#homeTabs").width());
	
	
	$("#homeTabs .pane").click(function(){
		window.location = $(this).find("a").attr("href");
	}).css({"cursor":"pointer"});
	
	
});

//working together
$(document).ready(function() {
	if($("body").is("#workingTogether")){
		$("#howCollaborationBeginsAccordion .panes").css({
			"position":"relative"
		});
	
		var widthMessure=145;
	
		function sliders(elem,firstRun){
			//get this index
			var index = $("#howCollaborationBeginsAccordion .panes .pane").index($(elem));
			//stop previous animations
			$("#howCollaborationBeginsAccordion .panes .pane").stop(true,false);
			//set this one to proper location
			$(elem).animate({
				"left": (index)*widthMessure,
				"width":"300px"
			}).find("div div").fadeIn();
			//fix any to the left
			$("#howCollaborationBeginsAccordion .panes .pane:lt("+index+")").each(function(){
				$(this).animate({
					"left":$(this).data("pane")*widthMessure,
					"width":"170px"
				});
			}).find("div div").fadeOut();
			//fix any to the right
			$("#howCollaborationBeginsAccordion .panes .pane:gt("+index+")").each(function(){
				$(this).animate({
					"left":130+$(this).data("pane")*widthMessure,
					"width":"170px"
				});
			}).find("div div").fadeOut();
		}
	
		$("#howCollaborationBeginsAccordion .panes .pane").each(function(i){
			$(this).css({
				"position": "absolute",
				"width":"400px",
				"left":(widthMessure*i)+"px",
				"top":"0px"
			});
			$(this).data("pane",i);
		}).hoverIntent(function(){
			sliders(this,false);
		},function(){
	
		}).css("cursor","pointer");
		sliders($("#howCollaborationBeginsAccordion .panes .pane:first"),true);
	}
});
//contact us
$(document).ready(function() {
		// select one or more elements to be overlay triggers
		 $("#form").overlay({
		    api: true,
			color: '#ccc',
		    top: 50,
		    speed: "slow",
		    expose: {
		    	color: '#000000',
				opacity: 0.7
		    }
		});
		 
		//assign the title tag as the data for example 
		$("#contactUs div.example").each(function(){
			$(this).siblings("input").data("example",$(this).html()).val($(this).html()).addClass("example");
		});
		//swap in out the example on focus / blur
		$("#contactUs input.example").focus(function(){
			if($(this).val() == $(this).data("example")){
				$(this).val('').removeClass("example");
			}
		}).blur(function(){
			if($(this).val() == ''){
				$(this).val($(this).data("example")).addClass("example");
			}
		});
		$("form#contactUs").submit(function(){
			$("form#contactUs input").each(function(){
				if($(this).val() == $(this).data("example")){
					$(this).val('').removeClass("example");
				}
			});
			return true;
		});
		
		
		 
		$(".contactsInfo a.emailMe, .sendContact").click(function(){
			sendGa(window.location+"?sendemail=true");
			//apply proper title depending on who we are emailing 
			if($(this).siblings("h2").html() == null){
				var theContact = 'GENERAL';
				var contactTitle = "Encapsys";
				var jobTitle = '';
			}else{
				var theContact = $(this).siblings("h2").html();
				var contactTitle = theContact;
				var jobTitle = "";
				//var jobTitle = "&nbsp; - " +$(this).siblings(".jobTitle").html();
			}
			
			
			
			//$("input#phone[value=]").val("").addClass("example");
			$("input#application[value=]").val("").addClass("example");

			$("#form .title h2").html("Email "+ contactTitle);
			$("#form .title h2").append("<span>"+jobTitle+"</span>");
			
			
			
			$("#contactName").val(theContact);

			$("#contactUs").show();
			$("#successMessage").hide();

			$("#form").overlay().load();
			return false;
		}).css("cursor","pointer");
		
		if(getURLParam("sendemail") == "true"){
			$("#form").overlay().load();
		}
		
		
		
		$("form#contactUs").validate({
			submitHandler: function(form) {
				jQuery(form).ajaxSubmit({
					success:function(responseText,status) {
						$("#contactUs").slideUp();
						$("#successMessage").slideDown();
						setTimeout('$("#form").overlay().close()',7000);
				    },
				    url: 'contact.aspx'
				});
			}
		});		
});

$(document).ready(function() {
	
	if($("body").is("#ourTech") && $("#evaporationTriggeredCapsuleRupture").html() != null){
		$("#evaporationTriggeredCapsuleRupture").overlay({
		    api: true,
			color: '#ccc',
		    top: 50,
		    speed: "slow",
		    onLoad: function(){
		    	sendGa(window.location+"?videooverlay=true");
		    },
		    expose: {
		    	color: '#000000',
				opacity: 0.7
		    }
		});
		if(getURLParam("videooverlay") == "true"){
			$("#evaporationTriggeredCapsuleRupture").overlay().load();
		}
		
		$("#cellVideo").click(function(){
			$("#evaporationTriggeredCapsuleRupture").overlay().load();
		}).css({"cursor":"pointer"});

		$("[rel=overlay]").overlay({
			target: '#simpleOverlay',
			expose: '#000000',
			onLoad: function(){
				sendGa(window.location+"?overlay="+this.getTrigger().attr("href"));
			}
		}).gallery({
			speed: 800
		});
		
		
		if(getURLParam("overlay",false)!=''){
			$("a[href="+getURLParam('overlay',false)+"]").click();
		}
		



	}
});
