// jQuery kickoff code


// jCarousel code taken from http://sorgalla.com/projects/jcarousel/
// jQuery 1.2.6 or greater is required


	var sidebarLoaded = false;
	var isRequirements = false

	$(document).ready(function() {
	
		
		// setup the sidebar
		 setupSidebar();
				
	
		// utility script to apply table row highlighting
		// to the search results table
		// make table rows clickable
		// extract the href from the link in the last column
		$(".searchResults tr:not('.PagerHeader'):not('.GridHeader')").mouseover(function(){
			$(this).addClass("tableOver")
		}).mouseout(function(){
			$(this).removeClass("tableOver")
		}).click(function() {
			location.href = $("td:last a", $(this)).attr("href");
		});
			
		
		
		// if there is a gallery on the page, initialise it
		if($('ul.gallery').length >= 1){
			$('ul.gallery').galleria({history: false}).removeClass("loading");
			// page height can be changed when the gallery is created
			// call the sidebar function again
			setSidebarHeight();
		}
		
		// start the header cycle if present
		if($('#headerCycle').length >= 1){
			$('#headerCycle').cycle()
		}
		
	});
	
	
	// called with an onscroll handler on the sidebar divs.
	// saves the scrollTop position for the div into a cookie	
	function SetDivPosition(whatElement){
		var intY = document.getElementById(whatElement).scrollTop;
		document.cookie = "yPos" + whatElement + "=!~" + intY + "~!"; 
	}

	// utility function to make cookie retireval by name easier
	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
		return null;
	}

	
	
	
	
	function setupSidebar(){
		
		// set the initial sidebar height to improve presentation 
		// while the content gets ajax'd in
		setSidebarHeight();
		
		// retrieve the content
		// async flag set so that sidebar events are not 
		// hooked up until after content has loaded.
		$.ajax({
			url: "jcarousel.ashx",
			data: window.location.search.substring(1),
			async: true, 
			cache: false,
			success: function(html){				
				$("#ajaxLoader").remove();
				$("#sidebarBody").append(html);					
								   
				// assign the onclick function to the subnav
				$("ul#subnav li").click(function(){
					toggleState($(this))				
				});				
				
				
				// reset the sidebar height once the content is loaded in.
				setSidebarHeight();
				
				
				
				// if requirements flag set, toggle the tabs
				if(isRequirements){
					toggleState($("ul#subnav li:eq(1)"))
				}
				
				// set the sidebars scroll positions once the content has loaded				
				persistSidebarScroll();	
			}
		});		
	}
	
	// retrieves the sidebar scroll positions from the cookie and resets them
	// could be improved, sidebar names are hard coded
	function persistSidebarScroll(){
	
		var strCook = readCookie("yPosmycarouselDynamicDisposals") ? readCookie("yPosmycarouselDynamicDisposals") : 0;
		var strCook2 = readCookie("yPosmycarouselDynamicRequirements") ? readCookie("yPosmycarouselDynamicRequirements") : 0;   
               
               
		if(strCook.length>=0){
			var intS = strCook.indexOf("!~");
			var intE = strCook.indexOf("~!");
			var strPos = strCook.substring(intS+2,intE);
			document.getElementById("mycarouselDynamicDisposals").scrollTop = strPos;
		}
        
        if(strCook2.length>=0){
			var intS = strCook2.indexOf("!~");
			var intE = strCook2.indexOf("~!");
			var strPos = strCook2.substring(intS+2,intE);
			document.getElementById("mycarouselDynamicRequirements").scrollTop = strPos;
		}								
	}
	
	
	function setSidebarHeight(){
	
		// set the sidebar height dynamically based on the wrapper height
		// to ensure a constant overlap on the footer, regardless of page length		
		
		var sidebarHeaderHeight = $('#sidebarHeader').height();
		var sidebarFooterHeight = $('#sidebarFooter').height()
		
		var sidebarY = $('#sidebar').position().top
		var footerY = $('#footer').position().top
		var diff = footerY-sidebarY		
		
		// expand the outer wrapper to fit the new height
		$('.carousel').height(diff-(sidebarHeaderHeight+sidebarFooterHeight)-9)
		$('#sidebarBody').height(diff-(sidebarHeaderHeight)-9)
		
	}
		
	function toggleState(whatElement){	
					
		var indexClicked = $("ul#subnav li").index(whatElement)
		
		// check and see if the button is already active
		// if not, go ahead and make the changes.
		// toggle the active classes on each of the menus
		
		// needs to be less dependent on specific classnames
		// IE6 bug was causing issues with multiple classes
		// on the LI's

		if(indexClicked == 0 && whatElement.hasClass("salesActive") == false){
			whatElement.toggleClass("salesActive").next().toggleClass("aquisitionsActive");
			$("div.carousel").toggle();			
		} else if(indexClicked == 1 && whatElement.hasClass("aquisitionsActive") == false){
			whatElement.toggleClass("aquisitionsActive").prev().toggleClass("salesActive");
			$("div.carousel").toggle();			
		}	
		
		// need to re-call this function after toggling 
		// the tabs as scrolltop gets set to 0 when hidden
		persistSidebarScroll();	
	}