$(document).ready(function() {
	
	setTimeout(function() {
	  if (location.hash) {
		window.scrollTo(0, 0);
	  }
	}, 1);

	// Change the image of hoverable images
	$(".imgHoverable").hover( function() {
	   var hoverImg = HoverImgOf($(this).attr("src"));
	   $(this).attr("src", hoverImg);
	 }, function() {
	   var normalImg = NormalImgOf($(this).attr("src"));
	   $(this).attr("src", normalImg);
	 }
	);	
	function HoverImgOf(filename){
	   var re = new RegExp("(.+)\\.(gif|png|jpg)", "g");
	   return filename.replace(re, "$1-over.$2");
	}	
	function NormalImgOf(filename){
	   var re = new RegExp("(.+)-over\\.(gif|png|jpg)", "g");
	   return filename.replace(re, "$1.$2");
	}
	
	$('#coda-slider-1').codaSlider({
		autoSlide: false,
		crossLinking: false,
		dynamicArrows: false,
		dynamicTabs: false,
		firstPanelToLoad: 1,
		panelTitleSelector: "h1.title",
		slideEaseDuration: 0,
		externalTriggerSelector: "a.xtrig"
	});
	
	$('#coda-slider-2').codaSlider({
		autoSlide: true,
		autoSlideInterval: 8000,
		crossLinking: false,
		dynamicArrows: false,
		dynamicTabs: false,
		firstPanelToLoad: 1,
		panelTitleSelector: "h1.title",
		slideEaseDuration: 400
	});
	
	var clickedItem = "noname"	;

	$("#topnav a.xtrig").click(function() {	
		if( ($(this).attr("name") == clickedItem) || (clickedItem == "noname")){			
			//panel is already in correct position, so just open megamenu
			$('#megamenu a.closer').hide();
			$("#megamenu").delay(100).slideToggle(function(){				
				if(($('#megamenu').css("display")) == "none")
				{
					$('#topnav').find('li').removeClass('current');
				}
				$('#megamenu a.closer').show();
			});
			clickedItem = $(this).attr("name");
		}
		else if (($("#megamenu").css("display")) == "none"){
			//delay to get slide to correct panel while hidden, then open megamenu
			$('#megamenu a.closer').hide();
			$("#megamenu").delay(100).slideToggle(function(){
				$('#megamenu a.closer').show();
			});
			clickedItem = $(this).attr("name");
		}
		else {			
			clickedItem = $(this).attr("name");
			//alert("scroll sideways!");
		};		
	});	
	
	megamenuIntent = {
        sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)
        interval: 200, // number = milliseconds for onMouseOver polling interval
        timeout: 800, // number = milliseconds delay before onMouseOut
        over: function() {
            if(($("#megamenu").css("display")) == "block"){				
				$('#megamenu a.closer').hide();
				$("#megamenu").stop().slideToggle(function(){
					$("#megamenu").css("display","none");
					$("#megamenu").css("height","324px");
					$('#topnav').find('li').removeClass('current');
					$('#megamenu a.closer').show();
				});				
			}
        }, // function = onMouseOver callback (REQUIRED)
        out: function() {

		} // function = onMouseOut callback (REQUIRED)
    }
	
	$('#middle').hoverIntent(megamenuIntent);
	
	$('#openerlink').click(function(){
		if(($("#megamenu").css("display")) == "none"){				
			$('#topnavlink1').click();
			$('#megamenu a.closer').hide();
			$("#megamenu").stop().slideToggle(function(){
				$("#megamenu").css("display","block");
				$("#megamenu").css("height","324px");
				$('#megamenu a.closer').show();
			});				
		}
	});
	
	$('#closerlink').click(function(){
		
		if(($("#megamenu").css("display")) == "block"){				
			$('#megamenu a.closer').hide();
			$("#megamenu").stop().slideToggle(function(){
				$("#megamenu").css("display","none");
				$("#megamenu").css("height","324px");
				$('#megamenu a.closer').show();
				$('#topnav').find('li').removeClass("current");
			});	
		}
	});
	
	$('.megacontent a').click(function(event){
		if( $(this).hasClass('ecosystem') )
		{
			//leave for modal
		}
		else {
			var bblinker = $(this).attr("href");
			event.preventDefault();
			if(($("#megamenu").css("display")) == "block"){				
				$('#megamenu a.closer').hide();
				setTimeout(function() {
					$("#megamenu").stop().slideToggle(function(){
						$("#megamenu").css("display","none");
						$("#megamenu").css("height","324px");
						$('#megamenu a.closer').show();
						$('#topnav').find('li').removeClass("current");
						setTimeout(function() {
							if(($('#megamenu').css("display")) == "none"){
								window.location = bblinker;
							}
						},100);
					});
				},200);
			}
		}
	});
	
	//big button function for homepage info panels
	$(".infopanel").click(function(){
		if( $(this).hasClass('ecosystem') )
		{
			//leave for modal
		}
		else {
			var linktosection = $(this).attr("id");
			window.location = linktosection;
			return false;
		}
	});
	
	$(".fadecolor").hover(function() { //On hover...
		var thumbOver = $(this).find("img").attr("src"); //Get image url and assign it to 'thumbOver'
	//Set a background image(thumbOver) on the <a> tag - Set position to bottom
		$(this).find("div.sectionbg").css({'background' : 'url(' + thumbOver + ') no-repeat left bottom'});
	//Animate the image to 0 opacity (fade it out)
		$(this).find("img").stop().fadeTo('normal', 0 , function() {
			$(this).hide() //Hide the image after fade
		});
	} , function() { //on hover out...
		//Fade the image to full opacity 
		$(this).find("img").stop().fadeTo('normal', 1).show();
	});	
	
	//big button function for section panels
	$(".landingpanel").click(function(){
		var linktosection = $(this).attr("id");
		window.location = linktosection;
		return false;
	});
	
	//big button function for section panels
	$(".sectionpanel").click(function(){
		var linktoproduct = $(this).attr("id");
		window.location = linktoproduct;
		return false;
	});
	
	$(".tabpanel").hide(); //Hide all content
	$("#tabcontent ul li a:first").addClass("active").show(); //Activate first tab
	$(".tabpanel:first").show(); //Show first tab content
	
	//count bbtabs (limit now is 3 or 4)
	function bbtabscount(){
		var ul = document.getElementById('bbtabs');
		var bbtabscounter=0, bbtabsused=0;
		if (ul != null){
			while(ul.getElementsByTagName("li")[bbtabscounter++]) bbtabsused++;
		};
		if(bbtabsused==3){
			$("#bbtabs li a").css("width","215px");
			$("#bbtabs li a").css("margin-right","5px");
		}
		else if (bbtabsused==4){
			$("#bbtabs li a").css("font-size","15px");
			$("#bbtabs li a").css("width","150px");
			$("#bbtabs li a").css("margin-right","10px");
			$("#bbtabs li a:eq(0)").css("width","157px");		
			$("#bbtabs li a:eq(3)").css("width","157px");
			$("#bbtabs li a:eq(0)").css("margin-right","0px");		
			$("#bbtabs li a:eq(3)").css("margin-right","0px");
		}
		else
		{
			//do nothing
		};
	}
	
	bbtabscount();			
		
	//tab content animation
	startingheight = $("#tabinfo").height() + "px";
	$("#tabinfo").css("height",startingheight);//set the height so animation is smooth
	
	$("#bbtabs li a").bind('click', function() {
		if ($(this).hasClass('active')){
			return false;
		}
		else {
			$("#bbtabs li a").removeClass("active"); //Remove any "active" class
			$(this).addClass("active"); //Add "active" class to selected tab
			var parenttag = this.parentNode.getAttribute('class');
			if (parenttag == "first"){
				$("#bbtabs li").removeClass("current");
				$("#bbtabs li").removeClass("lastcurrent");
				$("#bbtabs li:first").addClass("firstcurrent");
			}
			else if (parenttag == "middle"){
				$("#bbtabs li").removeClass("firstcurrent");
				$("#bbtabs li").removeClass("current");
				$("#bbtabs li").removeClass("lastcurrent");
				$("#bbtabs li:eq(1)").addClass("current");
			}
			else if (parenttag == "middle2"){
				$("#bbtabs li").removeClass("firstcurrent");
				$("#bbtabs li").removeClass("current");
				$("#bbtabs li").removeClass("lastcurrent");
				$("#bbtabs li:eq(2)").addClass("current");
			}
			else if (parenttag == "last"){
				$("#bbtabs li").removeClass("firstcurrent");
				$("#bbtabs li").removeClass("current");
				$("#bbtabs li:last-child").addClass("lastcurrent");
			};
			var activeTab = $(this).attr("href");
			$(".tabpanel").hide();		
			var newheight = ($(activeTab).height()) + 50;
			$("#tabinfo").animate({"height": newheight + "px"}, function() {							
				$(activeTab).show(); //wait for tab resize then show the active ID content
			});	
			return false;
		};		
	});
	
	$("a.morebio").click(function(e){
		pde(e);
		var closestid = $(this).closest("div").attr("id"); //going to scroll to this at the end
		var expandthisbio = $(this).attr("href"); //expand this later
		var closesta = $(this).closest("a"); //closest a tag
		$("p.expandtext").css("height","110px"); //set all p to collapsed state
		$("a.morebio").show(); //show all the expand links
		$("#"+expandthisbio).css("height","auto"); //expand selected text
		closesta.hide(); //hide the more link
		if((closesta.is(':hidden') == true)){
			setTimeout(function() {
				goToByScrollfast(closestid);
			},50);
		}
		return false;
	});
	
	$("a.moretext").click(function(e){
		pde(e);
		var closestid = $(this).closest("div").attr("id"); //going to scroll to this at the end
		var expandthistext = $(this).attr("href"); //expand this later
		var closesta = $(this).closest("a"); //closest a tag
		$("div.expandtext").css("height","100px"); //set all other divs to collapsed state
		$("#"+expandthistext).css("height","auto"); //expand selected text
		$("a.moretext").show(); //show all the expand links
		closesta.hide(); //hide the more link
		if((closesta.is(':hidden') == true)){
			setTimeout(function() {
				goToByScrollfast(closestid);
			},50);
		}
		return false;
	});
	
	$('ul.relevant li p').expander({
	  slicePoint: 420,
	  preserveWords: true,
	  expandText: '<span class="morejumper">More &raquo;</span>',
	  expandPrefix: '...&nbsp;&nbsp;&nbsp;',
	  userCollapseText: '&laquo; Less'
	});
	
	function pde(e)//Function to prevent Default Events
	{
		if(e.preventDefault)
			e.preventDefault();
		else
			e.returnValue = false;
	}
	
	function goToByScroll(id){
     	pde(id);
		setTimeout(function() {
			$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
		},650);
	}
	
	function goToByScrollfast(id){
     	pde(id);
		setTimeout(function() {
			$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
		},1);
	}
	
	$(window).hashchange( function(){
	var tabID = window.location.hash;
	switch(tabID){
		case '#tab1':
			$('a[href="#tab1"]').click();
			break;
		case '#tab2':
			$('a[href="#tab2"]').click();
			break;
		case '#tab3':
			$('a[href="#tab3"]').click();
			break;
		case '#tab4':
			$('a[href="#tab4"]').click();
			break;
	}
	})
	
	$(window).hashchange();

    if((window.location.hash != '')&&(document.getElementById("tabcontent")==null)){ 
		goToByScroll(window.location.hash.substr(1));
	}
	
	$(".ecosystem").fancybox({
		'transitionIn'		:	'fade',
		'transitionOut'		:	'fade',
		'speedIn'		:	400, 
		'speedOut'		:	200, 
		'overlayColor'		:	'#000',
		'overlayOpacity'	:	0.80,
		'height'		:	620,
		'width'			:	1080,
		'scrolling'		:	'auto',
		'autoscale'		:	true,
		'href'			:	'/themes/BatBlue/ecosystem/index.html'
	});
	
	$(".playvideo").fancybox({
		'transitionIn'		:	'fade',
		'transitionOut'		:	'fade',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayColor'		:	'#000',
		'overlayOpacity'	:	0.70,
		'height'		:	400,
		'width'			:	600,
		'scrolling'		:	'auto',
		'autoscale'		:	true,
	});
	
});

