//SLIDING SUB-NAVIGATION (For items with sub-links)

var dhtmlgoodies_slideSpeed = 10;	// Higher value = faster
var dhtmlgoodies_timer = 10;		// Lower value = faster

var objectIdToSlideDown = false;
var leftnav_subctiveId = false;
var dhtmlgoodies_slideInProgress = false;

var activeMenu = null;

function rolloverUpdate(obj, dir)
{
	//alert( obj.id +", "+ activeMenu +", "+ activeMenu.id );
	if( activeMenu != undefined && obj.id == activeMenu.id )
		return;
		
	if( dir == "over" )
		dir = "_over";
	else
		dir = "";

	//alert( "rolloverUpdate: "+ obj );
	$(obj.id).className = "leftnav_main"+dir;
	
	//alert( obj.id +", "+ dir +", "+ "leftnav_main"+dir );
}

function showHideContent(e,inputId)
{
	if(dhtmlgoodies_slideInProgress)return;
	dhtmlgoodies_slideInProgress = true;
	if(!inputId)inputId = this.id;

	// if there's an active menu, revert it's background image	
	if( activeMenu != null )
	{
		activeMenu.className = "leftnav_main";
		activeMenu = null;
	}

	// if a main nav item was clicked
	//alert( "inputId: "+ inputId +", curr main nav: "+ readCookie("skeeter_main_nav") ); //+", "+ $(inputId).firstDescendant().innerHTML );
	eraseCookie("skeeter_main_nav");
	createCookie("skeeter_main_nav", inputId)
	//alert( "new main nav id: "+ readCookie("skeeter_main_nav") );
		
	// update the background image of the clicked main menu item
	//if( !$(inputId) )
	//	return; 
	//alert( inputId +", "+ $(inputId) );
	$(inputId).addClassName('leftnav_main_on');
	//$(inputId).style.backgroundImage = "url('images/leftnav_bg_on.gif')";
	activeMenu = $(inputId);
		
	// if there isn't a chosen sub-nav, don't slide content in
	//if( readCookie("skeeter_sub_nav") == "" || readCookie("skeeter_sub_nav") == null )
	//	return;
	
	inputId = inputId + '';
	var numericId = inputId.replace(/[^0-9]/g,'');
	var answerDiv = $('leftnav_sub' + numericId);

	objectIdToSlideDown = false;
		
	if(answerDiv && (!answerDiv.style.display || answerDiv.style.display=='none')){		
		if(leftnav_subctiveId &&  leftnav_subctiveId!=numericId){			
			objectIdToSlideDown = numericId;
			slideContent(leftnav_subctiveId,(dhtmlgoodies_slideSpeed*-1));
		}else{
			
			answerDiv.style.display='block';
			answerDiv.style.visibility = 'visible';
			
			slideContent(numericId,dhtmlgoodies_slideSpeed);
		}
	}else{
		slideContent(numericId,(dhtmlgoodies_slideSpeed*-1));
		leftnav_subctiveId = false;
	}	
}

function slideContent(inputId,direction)
{
	var obj = $('leftnav_sub' + inputId);
	
	//if( obj == null )
	//	return;

	var contentObj = $('leftnav_subc' + inputId);

	//alert( inputId +", "+ direction +", "+ obj);
	var height = obj.clientHeight;
	if(height==0)height = obj.offsetHeight;
	height = height + direction;
	rerunFunction = true;
	if(height>contentObj.offsetHeight){
		height = contentObj.offsetHeight;
		rerunFunction = false;
	}
	if(height<=1){
		height = 1;
		rerunFunction = false;
	}

	obj.style.height = height + 'px';
	var topPos = height - contentObj.offsetHeight;
	if(topPos>0)topPos=0;
	contentObj.style.top = topPos + 'px';
	if(rerunFunction){
		setTimeout('slideContent(' + inputId + ',' + direction + ')',dhtmlgoodies_timer);
	}else{
		if(height<=1){
			obj.style.display='none'; 
			if(objectIdToSlideDown && objectIdToSlideDown!=inputId){
				$('leftnav_sub' + objectIdToSlideDown).style.display='block';
				$('leftnav_sub' + objectIdToSlideDown).style.visibility='visible';
				slideContent(objectIdToSlideDown,dhtmlgoodies_slideSpeed);				
			}else{
				dhtmlgoodies_slideInProgress = false;
			}
		}else{
			leftnav_subctiveId = inputId;
			dhtmlgoodies_slideInProgress = false;
		}
	}
}

/*
var activeSubNav;
function slideSubNav()
{
	var elToSlide = this.id.replace('leftnav_main', 'leftnav_sub');
	//alert( this.id +", "+ this.innerHTML +", "+ elToSlide +", "+ $(elToSlide).innerHTML);
	Effect.toggle(elToSlide, 'slide');
	//return false;
}
*/

function initShowHideDivs()
{
	//checkUrl();

	/*
	var mainNavButtons = $$(".main");
	mainNavButtons.each(function(el, i){
		// process the main nav item
		el.onclick = slideSubNav;
		el.id = "leftnav_main"+i;
		el.addClassName('leftnav_main');

		// process the .sub content container		
		var subContentDiv = el.next();
		subContentDiv.id = "leftnav_sub"+ i;
		//subContentDiv.hide();
		subContentDiv.setStyle({
			display: 'none'
		});
	*/		
		/*
		// process the inner content container
		var subSubContentDiv = subContentDiv.firstDescendant();
		subSubContentDiv.id = "leftnav_subc"+i;
		subSubContentDiv.addClassName('sub_content');
		subSubContentDiv.setStyle({
			top: (0 - subSubContentDiv.offsetHeight)+"px"
		});
		
		subContentDiv.setStyle({
			display: 'none',
			height: '1px'
		});
		*/
	//});
	
	var divs = document.getElementsByTagName('DIV');
	var divCounter = 1;
	for(var no=0;no<divs.length;no++){
		if(divs[no].className=='main')
		{
			divs[no].onclick = showHideContent;
			divs[no].id = 'leftnav_main'+divCounter;
			divs[no].className = "leftnav_main";
			
			//alert( divs[no].innerHTML +", "+ divs[no].id );

			var answer = divs[no].nextSibling;
			while(answer && answer.tagName!='DIV')
			{
				answer = answer.nextSibling;
			}
			answer.id = 'leftnav_sub'+divCounter;	
			contentDiv = answer.getElementsByTagName('DIV')[0];
			contentDiv.style.top = 0 - contentDiv.offsetHeight + 'px'; 	
			contentDiv.className='sub_content';
			contentDiv.id = 'leftnav_subc' + divCounter;
			answer.style.display='none';
			answer.style.height='1px';

			divCounter++;
		}
	}

	wireUpSoloMainNavLinks(divCounter);
	
	manageNavPersistence();
}

function rightTrim(sString)
{
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	
	return sString;
}

function manageTopNav(goToUrl, section)
{
	
	//alert( goToUrl +", "+ section );
	switch( section )
	{
		// clear out the cookies when the products landing page is chosen
		case "events":
			eraseCookie("skeeter_main_nav");
			eraseCookie("skeeter_sub_nav");
			eraseCookie("skeeter_sub_nav_text");
			
			createCookie("skeeter_main_nav", "leftnav_main2");
			break;
		case "privacy_policy":
			eraseCookie("skeeter_main_nav");
			eraseCookie("skeeter_sub_nav");
			eraseCookie("skeeter_sub_nav_text");
			
			createCookie("skeeter_main_nav", "leftnav_main6");
			break;
		case "news":
		case "bass_federation":
			eraseCookie("skeeter_main_nav");
			eraseCookie("skeeter_sub_nav");
			eraseCookie("skeeter_sub_nav_text");
			
			createCookie("skeeter_main_nav", "leftnav_main3");
			break;
		case "team":
		case "home_real_money":
		case "buying_guide":
		case "my_boat":
		case "my_extras":
		case "about":
		case "contact":
			eraseCookie("skeeter_main_nav");
			eraseCookie("skeeter_sub_nav");
			eraseCookie("skeeter_sub_nav_text");
			
			createCookie("skeeter_main_nav", "leftnav_main1");
			break;
		case "products":
			eraseCookie("skeeter_main_nav");
			eraseCookie("skeeter_sub_nav");
			eraseCookie("skeeter_sub_nav_text");
			break;
	}
	
	location.href = goToUrl;
}

// check to see if we're on the main products page. If so, delete the cookies
function checkUrl(goToUrl)
{
	var url = location.href.substring(0, location.href.length-1);
	var url_parts = url.split("/");
	
	//alert( url_parts[url_parts.length-1] ); //url +", "+ url_parts[url_parts.length-1] +", "+ url_parts[3] );

	switch( url_parts[url_parts.length-1] )
	{
		// clear out the cookies when the products landing page is chosen
		case "team":
		case "about":
		case "gallery":
			eraseCookie("skeeter_main_nav");
			eraseCookie("skeeter_sub_nav");
			eraseCookie("skeeter_sub_nav_text");
			
			createCookie("skeeter_main_nav", "leftnav_main1");
			break;
		case "contact":
		case "my_boat":
		case "skeeter_innovation":
		case "products":
		case "events":
			//alert("CLEAR COOKIES");
			eraseCookie("skeeter_main_nav");
			eraseCookie("skeeter_sub_nav");
			eraseCookie("skeeter_sub_nav_text");
			break;
	}
	
	if( goToUrl != undefined )
		location.href = goToUrl;
}

// handle sub-nav clicks
function manageSubNav(goToUrl, parentID, buttonText)
{
	//alert( goToUrl +", "+ elId ); // +", "+ el.id );
	eraseCookie("skeeter_sub_nav");
	createCookie("skeeter_sub_nav", parentID);
	eraseCookie("skeeter_sub_nav_text");
	createCookie("skeeter_sub_nav_text", escape(buttonText));
	
	location.href = goToUrl;
}

// handle large photo clicks on main product page
function managePhotoNav(goToUrl, section)
{
	//alert( goToUrl +", "+ section );

	// find the main nav item
	var main_nav_links = $$("div.leftnav_main");
	//alert( main_nav_links.length );
	main_nav_links.each(function(el, i){
			var txt_node = el.firstDescendant();
			var subnav_container = el.next(".sub").firstDescendant().firstDescendant().firstDescendant();
			//var subnav_node = subnav_container.childElements();
			
			//alert( "Main Nav: "+ el.id +", "+ el.innerHTML ); //+", "+ subnav_container.innerHTML ); //+", "+ txt_node.innerHTML );

			if( txt_node.innerHTML == section )
			{
				eraseCookie("skeeter_main_nav");
				createCookie("skeeter_main_nav", el.id)
				
				// get sub-nav item and text
				var subnav_text = subnav_container.firstDescendant().firstDescendant();
				//alert( "Sub Nav: "+ subnav_container.id +", "+ subnav_text.innerHTML );

				eraseCookie("skeeter_sub_nav");
				createCookie("skeeter_sub_nav", subnav_container.id);
				eraseCookie("skeeter_sub_nav_text");
				createCookie("skeeter_sub_nav_text", escape(subnav_text.innerHTML));
			}
		});
		
	location.href = goToUrl;
	
	//return false;
}

var activeMainNavCookie;
var activeSubNavCookie;
var activeSubNavCookieText;
function manageNavPersistence()
{
	// fetch the active main- and sub-nav items
	activeMainNavCookie = readCookie("skeeter_main_nav");
	activeSubNavCookie = readCookie("skeeter_sub_nav");
	activeSubNavCookieText = unescape(readCookie("skeeter_sub_nav_text"));
	//alert( activeMainNavCookie +", "+ activeSubNavCookie +", "+ activeSubNavCookieText );

	// if a main-nav item has been clicked, persist the display on other pages
	if( activeMainNavCookie != null && activeMainNavCookie != "" )
	{
		$(activeMainNavCookie).className = "leftnav_main_on";
		$(activeMainNavCookie).firstDescendant().setStyle({
			paddingTop: '7px'
		});

		activeMenu = $(activeMainNavCookie);
		//alert( $(activeMainNavCookie).innerHTML +", "+ $(activeMainNavCookie).next().hasClassName('sub') );
		
		// if the active menu has a child to be displayed, do so
		if( $(activeMainNavCookie).next() && $(activeMainNavCookie).next().hasClassName('sub') )
			showHideContent(null, activeMainNavCookie);
	}

	// if there's an active sub-nav, style it and disable the click
	//alert( "active sub-nav: "+ activeSubNavCookie );
	if( activeSubNavCookie != null && activeSubNavCookie != "" )
	{
		$(activeSubNavCookie).update(activeSubNavCookieText.replace(/%20/g, " "));
		
		//alert( $(activeSubNavCookie).childNodes[0] );
		//Element.addClassName(activeSubNavCookie, "activeSubNav");
		$(activeSubNavCookie).setStyle({
			paddingTop: '7px',
			paddingBottom: '7px',
			paddingLeft: '4px',
			textDecoration: 'underline',
			textAlign: 'center',
			color: '#ff7e00'
		});
		//$(activeSubNavCookie).childNodes[0].setStyle("text-decoration", "underline");
		//$(activeSubNavCookie).childNodes[0].setStyle("color", "#ff7e00");
		
		//$(activeSubNavCookie).onclick = null;
	}
}

function wireUpSoloMainNavLinks(numAccordionNav)
{
	$$('.menulink').each(function(el, i) {
		el.id = "leftnav_main"+ (i+numAccordionNav);
		//alert( el.innerHTML +", "+ el.id );
	});
}

// handle non-accordion main-nav clicks
function manageMainNav(elID, goTo)
{
	//alert( elID +", "+ goTo );
	//return;
	
	//alert( elID +", "+ goTo ); // +", "+ el.id );
	eraseCookie("skeeter_main_nav");
	createCookie("skeeter_main_nav", elID);
	eraseCookie("skeeter_sub_nav");
	eraseCookie("skeeter_sub_nav_text");
	
	location.href = goTo;
}

//###################################
// COOKIE METHODS
//###################################
function createCookie(name,value)
{
	var date = new Date();
	date.setTime(date.getTime()+(30*24*60*60*1000));
	var expires = ";expires="+date.toGMTString();
	
	var domain = location.href.match( /:\/\/(www\.)?([^\/:]+)/ );
	domain = domain[2] ? domain[2] : '';
	//alert( domain );

	document.cookie = name +"="+ value + expires +";domain="+ domain +";path=/;";
}

function readCookie1(name)
{
	//alert(name +" :: "+ document.cookie);
	var cookie;
	var _cookie = document.cookie;
	if( _cookie.indexOf('PHPSESSID=') > -1 )
	{
		cookie = _cookie.substring(_cookie.indexOf('PHPSESSID='));
	}
	//alert("Cookie: "+ cookie);
	_cookie = cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
	
	//alert(_cookie);
	return (_cookie ? unescape(_cookie[2]) : null);
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}


function readCookie(cookie_name)
{
	var cookie = document.cookie;

	// split it into key-value pairs
	var cookie_pieces = cookie.split(';');

	// for each of those pairs, split into key and value
	for(var i=0; i<cookie_pieces.length; i++)
	{
		// get the cookie piece and trim it
		var piece = trim(cookie_pieces[i]);

		// find the location of the '=' and split the string
		var a = piece.indexOf('=');
		if (a == -1)
		{
			// there was no '=' - so we have a key and no value
			var key = piece;
			var value = '';
		}
		else
		{
			// we found an '=' - split the string in two
			var key = piece.substr(0,a);
			var value = piece.substr(a+1);
		}

		if( key == cookie_name )
			return value;

		// now display our cookies
		//alert('Key: ' + key + "  Value : "+ value);
	}
}

function trim(str){
	
	// trim off leading spaces
	while (str.charAt(0) == ' '){
		str = str.substring(1);
	}
	
	//trim off trailing spaces
	while (str.charAt(str.length-1) == ' '){
		str = str.substring(0,str.length-1);
	}

	return str;
}

window.onload = initShowHideDivs;