/*
Title:      Main JS Page
Author:     Ben Babics, bbabics@organic.com
Notes:      Site functions that are page specific
Modified:   Margaret Beck 01/30/2008
*/



//onload functions
onload_register('setup_menus()');
onload_register('lastBorder()');


//set vars
var arr = new Array();
var children = new Array();
var sec = new Array();
var selected;
var scroll_menu;



function curr_Page(obj, id, child_id)
{	
	//if sub-nav exists make sub-nav id the id
	if (child_id != null)id = child_id;
	//call border function
	if (id==null) id="soft_top_guide_landing";
	//call functions accordingly
	lastBorder(id);
	switch(id)
	{
				
		case "soft_top_guide_landing":
			//make var of the menu on the page
			var menu = document.getElementById('custom_menu');
			menu = menu.getElementsByTagName('ul')[0];
			
			//loop through the menu
			//attach event handler to anchor tags
			add_onclick(menu, 'a', 'trail_rated');
			
			//set first child in menu as selected
			selected = menu.firstChild.firstChild;
			
			//send selected to function
			//func used whenever newly selected
			displayContent(selected, 'trail_rated');
			menu = document.getElementById('custom_menu');
			menu.style.height="25px";
			menu.innerHTML = "<h2>Soft Top Intro</h2>";
			
			//get array of children			
			getChildren('custom_container', 'div');
			
			//array of children to exclude from height adjustment
			sec.push('custom_menu', 'custom_movie');
			
			//remove those children from 'getChildren' array
			spliceChild();
			
			//get difference in height and apply it to divs
			var container = getHei('custom_movie');
			var menu = getHei('custom_menu'); 
			var diff = container - menu;
			
			var set_hei = $('custom_movie').getSize().size.y;
			$('custom_container').setStyles({
					height: set_hei,
					overflow: 'hidden'
			});
			
			setHei(diff);
			//menu = document.getElementById('custom_menu');
			//menu.style.visibility="hidden";
			$('left_nav').style.display = "none";
			break
			
		case "soft_top_2d_sec1":
		 
			var menu = document.getElementById('soft_top_guide');
			//define flash proxy
			flashProxy = new FlashProxy(flash_func_id, "/en/owners/soft_top_guide/swfs/JavaScriptFlashGateway.swf");
			
			//make var of the menu on the page
			menu = menu.getElementsByTagName('ul')[0];
			
			//loop through the menu
			//attach event handler to anchor tags
			add_onclick(menu, 'a', 'scrolling_menu');
			
			//set first child in menu as selected
			selected = menu.firstChild.firstChild;
			
			//setup SCROLLING menus
			setupMenu();
			
			//send selected to function
			//func used whenever newly selected
			displayContent(selected, 'scrolling_menu');
			//get array of children			
			getChildren('custom_container', 'div');
			
			//array of children to exclude from height adjustment
			sec.push('custom_menu', 'custom_movie');
			
			//remove those children from 'getChildren' array
			spliceChild();
			
			//get difference in height and apply it to divs
			var container = getHei('custom_movie');
			var menu = getHei('custom_menu');
			var diff = container - menu;
			
			var set_hei = $('custom_movie').getSize().size.y;
			$('custom_container').setStyles({
					height: set_hei,
					overflow: 'hidden'
			});
			
			setHei(diff);
			break;
		case "soft_top_4d_sec1":
			var menu = document.getElementById('unlimited_soft_top_guide');
			//define flash proxy
			flashProxy = new FlashProxy(flash_func_id, "/en/owners/soft_top_guide/swfs/JavaScriptFlashGateway.swf");
			
			//make var of the menu on the page
			menu = menu.getElementsByTagName('ul')[0];
			
			//loop through the menu
			//attach event handler to anchor tags
			add_onclick(menu, 'a', 'scrolling_menu');
			
			//set first child in menu as selected
			selected = menu.firstChild.firstChild;
			
			//setup SCROLLING menus
			setupMenu();
			
			//send selected to function
			//func used whenever newly selected
			displayContent(selected, 'scrolling_menu');
			//get array of children			
			getChildren('custom_container', 'div');
			
			//array of children to exclude from height adjustment
			sec.push('custom_menu', 'custom_movie');
			
			//remove those children from 'getChildren' array
			spliceChild();
			
			//get difference in height and apply it to divs
			var container = getHei('custom_movie');
			var menu = getHei('custom_menu');
			var diff = container - menu;
			
			var set_hei = $('custom_movie').getSize().size.y;
			$('custom_container').setStyles({
					height: set_hei,
					overflow: 'hidden'
			});
			
			setHei(diff);
			break;			
			
		default:
			//set tracking
			/*var elms = $$('.halfs');
			
				//loop through divs
				for (i=0; i < elms.length; i++)
				{
					var id = removeStr(elms[i].id, 'custom_');
					createLink(elms[i], id, 'left_nav');
				}*/
				
	}
	
	//match height of nav to #contentCont
	navHei();
	
	//wrap nav & contentCont in a master div for bg transparency
	masterCont();
	if(userData.bandwidth == "low") {
		document.getElementById("low_band_copy").style.visibility = "visible";
	}else{
		document.getElementById("low_band_copy").style.visibility = "hidden";
	}
}



// Wraps content of a div inside an anchor so as to link entire div.
// Link automatically takes on full size of div.
function createLink(divID, link, lpos) {
	var div = $(divID);
	
	//var linkWidth = div.getStyle('width') + ' !important';
	var newLink = new Element('a', {
		'styles': {
			'display': 'block',
			'height': div.getStyle('height'),
			'width': div.getStyle('width'),
			'text-decoration': 'none'
		},
		'class': 'divWrapper',
		'href': link,
		'name': '&lpos=' + lpos + '&lid=' + link
	});
	
	newLink.adopt(div.getChildren());
	div.adopt(newLink);
}



function masterCont()
{
	//grab nav & contentCont divs
	var cont = $('content');
	var nav = $('left_menu');
	var contentCont = $('contentCont');
	
	
	//establish content current height
	var before_hei = cont.getStyle('height');
	
		//content is assigned before height
		//cont.style.height = before_hei;  //UNCOMMENT THIS TO TURN ON OPENING ANIMATIONS
		
	
	//create div's
	var blank_div = new Element('div', { 'id': 'blank_div' } );
	var wrapper = new Element('div', { 'id': 'masterCont', 'class': 'autoclear'	} );
	var foot = new Element('div', { 'id': 'masterFoot' } );
	
		//set adoptions
		cont.adopt(blank_div);
		blank_div.adopt(wrapper);
		wrapper.adopt(nav);
		wrapper.adopt(contentCont);
		blank_div.adopt(foot);
		
		
	//establish blank_div current height
	var after_hei = blank_div.getSize().size.y;
	
	//var transitions
	/* UNCOMMENT THIS TO TURN ON OPENING ANIMATIONS */ /*
	var contentFx = new Fx.Styles(cont, {duration: 1000, transition: Fx.Transitions.linear});  //TO TURN OFF OPENING ANIMATIONS
	var wrapperFx = new Fx.Style(wrapper, 'opacity', {duration: 1000, transition: Fx.Transitions.linear});  //TO TURN OFF OPENING ANIMATIONS
	var footerFx = new Fx.Style(foot, 'opacity', {duration: 1000, transition: Fx.Transitions.linear});  //TO TURN OFF OPENING ANIMATIONS
	var navFx = new Fx.Style(nav, 'opacity', {duration: 1000, transition: Fx.Transitions.linear});  //TO TURN OFF OPENING ANIMATIONS
	
		//cue tranisitions
		contentFx.start({ 'height': [before_hei, adispfter_hei] }).chain(function() { wrapperFx.start(0,1); footerFx.start(0,1); navFx.start(0,1) });  //TO TURN OFF OPENING ANIMATIONS
	*/
	
	//reset adoptions
	cont.adopt(wrapper);
	cont.adopt(foot);
		
	//set styles
	contentCont.setStyle('visibility', 'visible');	
	nav.setStyle('visibility', 'visible'); //visiblity remains hidden because IE show's it because of absolute positioning
}

function setEmbedLoaded(){	
	try
	{
		var myLoadMovie = "";
		if (selected.id!=null && selected.id!='' && selected.id != 'undefined') {
			myLoadMovie = selected.id;
		}else{
			myLoadMovie = "soft_top_guide_landing";
		}
		var swfPath = '/en/owners/soft_top_guide/swfs/soft_top_guide/'+myLoadMovie+'.flv';
		var lbPath = '/en/owners/soft_top_guide/images/soft_top_guide/'+myLoadMovie+'.jpg';	
		var flvEndPath = getEndframe(myLoadMovie);
		window.document["custom_movie_flash"].SetVariable('title',myLoadMovie);
		window.document["custom_movie_flash"].SetVariable('poster',lbPath);
		window.document["custom_movie_flash"].SetVariable('flv',swfPath);
		window.document["custom_movie_flash"].SetVariable('swf',flvEndPath);
		if (myLoadMovie == "soft_top_guide_landing"){
			window.document["custom_movie_flash"].SetVariable('disclaimer','0:This is not a substitute for reading the owners manual.|1:This is not a substitute for reading the owners manual.');
		}else{
			//window.document["custom_movie_flash"].SetVariable('disclaimer','0:Jeep is a registered trademark of Chrysler Group LLC.|1:Jeep is a registered trademark of Chrysler Group LLC.');
		}	
		window.document["custom_movie_flash"].change();
	}
	catch (exception)
	{
		//alert("error on movie load "+selected.id);
	}	
}

function setOwnersFLVComplete(){
	alert("setOwnersFLVComplete remove function");
}

function flashSays(what){
	//alert("flashSays "+what);
}

function flashButtonComm(buttonCall){
	switch(buttonCall)
	{
		case "videoFinished":
			window.document["custom_movie_flash"].changeMedia('endframeOn');
		break;
		case "playagain":
			window.document["custom_movie_flash"].changeMedia('restartVideo');
			break;
		case "next":
			if (selected.id!='soft_top_2d_sec6' && selected.id!='soft_top_4d_sec6'){
				//pageTrack("",selected.id);
				displayContent(selected.nextSibling.firstChild, 'scrolling_menu');
				//displayContent(selected.nextSibling.firstChild, 'next_menu');
			}
			break;
		case "menu1":
			//var gToSelectedURL = document.getElementById('menu_'+'soft_top_guide').firstChild.href;
			//setTimeout( "window.location.href = gToSelectedURL", 0 );

			window.location.href=("" + document.getElementById('menu_'+'soft_top_guide').firstChild.href +"");
			break;
		case "menu2":
			//var gToSelectedURL = document.getElementById('menu_'+'unlimited_soft_top_guide').firstChild.href;
			//setTimeout( "window.location.href = gToSelectedURL", 0 );
			window.location.href=("" + document.getElementById('menu_'+'unlimited_soft_top_guide').firstChild.href +"");
			break;
		default:
			//alert("unknown button comm "+buttonCall);
		break;
	}
}
function navHei()
{
	var nav = $('left_menu');
	var cont = $('contentCont');
	var hei = cont.getStyle('height');

	nav.style.height = hei;
}



function headlineDiff(obj)
{
	//set appropriate height
	var pageCopy = $('pageCopy');
	var headline = pageCopy.getStyle('height');
	headline = removeStr(headline, 'px');
	var height = (403 - headline) - 14;
			
		//push obj into array
		arr.push(obj);
			
		setHei(height);
}


function getChildren(obj, el)
{
	var parent = document.getElementById(obj);
	var child = parent.getElementsByTagName(el);
	
	//loop through and push children into array
	for (i=0; i < child.length; i++) arr.push( child[i].id );
}



function spliceChild()
{
	//loop through remove array
	for (i=0; i < sec.length; i++)
	{
		//if you find it remove it		
		for (x=0; x < arr.length; x++) if (arr[x] == sec[i]) arr.splice([x], 1);
	}
}



function replaceStr(val, rep, newVal)
{
	//if val doesn't exist stop
	if (val == null) return false;
	
	//return val with replaced portion of string
	while (val.indexOf(rep) != -1)
	{
		val = val.replace(rep, newVal);
	}
	
	return val;
}



function removeStr(val, rem)
{
	//if val doesn't exist stop
	if (val == null) return false;
	
	//return val with removed portion of string
	if (val.indexOf(rem) != -1) return val.replace(rem, '');	
}



function add_onclick(obj, el, mode)
{
	//create var of child elements and length
	var child = obj.getElementsByTagName(el);
	var len = child.length;
	
	//loop through length
	for (i=0; i < len; i++)
	{
		child[i].href = "#";
		
		//place tracking info from nav
		//into array for menu
		children.push(child[i].name);
		
		//attach onclick event handler and function
		child[i].onclick = function()
		{
			//if we don't need to call a flash function
			displayContent(this, mode);
			return false;
		}
		
	}
}

function getEndframe(id){
	//alert("getEndframe "+id);
	var myEndframe;
	switch(id)
	{
		case "":
			myEndframe = '/shared/flash/owners/endClip3.swf';
		break;
		case "soft_top_guide_landing":
			myEndframe = '/shared/flash/owners/endClip3.swf';
		break;
		case "soft_top_2d_sec6":
			myEndframe = '/shared/flash/owners/endClip1.swf';
		break;
		case "soft_top_4d_sec6":
			myEndframe = '/shared/flash/owners/endClip1.swf';
		break;
		default:
			myEndframe = '/shared/flash/owners/endClip2.swf';
		break;
	}
	return myEndframe;
}
function displayContent(obj, mode)
{
	var curr = obj;
	
	switch(mode)
	{
		case "flash_proxy":
			var curr_id = "custom_" + curr.title;
			var selected_id = "custom_" + selected.firstChild.title;
			var flash = true;
			var typeOfCall = "fselectSystem";
			var curr_path = removeStr(curr_id, 'custom_');
			var lbPath = "/en/owners/soft_top_guide/images/soft_top_guide/" + curr_path + '.jpg';
			break;
			
		case "scrolling_menu":
			var curr_id = "custom_" + curr.parentNode.id;
			var selected_id = "custom_" + selected.id;
			//var curr_id = curr.parentNode.id;
			//var selected_id = selected.id;
			var flash = true;
			var typeOfCall = "getMovie"; 
			var swfPath = "/en/owners/soft_top_guide/swfs/soft_top_guide/" + curr.parentNode.id + '.flv';
			var lbPath = "/en/owners/soft_top_guide/images/soft_top_guide/" + curr.parentNode.id + '.jpg';
			var flvEndPath = getEndframe(selected.id);
			updateMenu(obj);
			pageTrack("",curr.parentNode.id);
			showFlash("/shared/flash/owners/owners_player.swf","custom_movie","&title="+curr.parentNode.id+"&bg_color=000000&flv="+swfPath+"&poster="+lbPath+"&endClip="+flvEndPath+"&disclaimer="+"","true");
			
			//showFlash("/shared/flash/owners/owners_player.swf","custom_movie","&title="+curr.parentNode.id+"&bg_color=000000&flv="+swfPath+"&poster="+lbPath+"&endClip="+flvEndPath+"&disclaimer="+"0:Jeep is a registered trademark of Chrysler Group LLC.|1:Jeep is a registered trademark of Chrysler Group LLC.","true");
			break;
			
		case "trail_rated":
			var curr_id = "custom_" + curr.title;
			var selected_id = "custom_" + selected.firstChild.title;
			var lbPath = "/en/owners/soft_top_guide/images/soft_top_guide/soft_top_guide_landing.jpg";
			break;
			
		case "soft_top_guide_landing":
			var curr_id = "custom_soft_top_guide_landing";
			var selected_id = "custom_soft_top_guide_landing";
			var flash = true;
			var typeOfCall = "getMovie"; 
			var swfPath = "/en/owners/soft_top_guide/swfs/soft_top_guide/soft_top_guide_landing.flv";
			var lbPath = "/en/owners/soft_top_guide/images/soft_top_guide/soft_top_guide_landing.jpg";
			var flvEndPath = getEndframe(selected.id);
			break;
			
		case "next_menu":
			var curr_id = "custom_" + curr.parentNode.id;
			var selected_id = "custom_" + selected.id;
			var flash = true;
			var typeOfCall = "getMovie"; 
			var swfPath = "/en/owners/soft_top_guide/swfs/soft_top_guide/" + curr_id + '.flv';
			var lbPath = "/en/owners/soft_top_guide/images/soft_top_guide/" + curr_id + '.jpg';
			var flvEndPath = getEndframe(curr_id);
			updateMenu(obj);
			//showFlash("/shared/flash/owners/owners_player.swf","custom_movie","&title="+curr_id+"&bg_color=000000&flv="+swfPath+"&poster="+lbPath+"&endClip="+flvEndPath+"&disclaimer="+"0:Jeep is a registered trademark of Chrysler LLC.|1:Jeep is a registered trademark of Chrysler LLC.","true");
			showFlash("/shared/flash/owners/owners_player.swf","custom_movie","&title="+curr_id+"&bg_color=000000&flv="+swfPath+"&poster="+lbPath+"&endClip="+flvEndPath+"&disclaimer="+"","true");
			
			break;
			
		default:
			var curr_id = "custom_" + curr.title;
			var selected_id = "custom_" + selected.firstChild.title;
			var flash = false;
	}
	
	var parent = curr.parentNode;
	var desc;
	
	//check if selected has className 'selected'
	if( checkClass(selected, 'selected') == true )
	{
		//if so remove className 'selected'
		removeClass(selected, 'selected');
		
		//find description div
		desc = document.getElementById(selected_id);
		
		//display none to div
		addClass(desc, 'none');
	}
	
	//assign selected var
	selected = parent;
	//add 'selected' className
	addClass(parent, 'selected');
	
	//strip 'custom_' out of id
	var flash_id = removeStr(curr_id, 'custom_');
	
		if (swfPath) flash_id = swfPath;
	
		//send id to flash movie or write lo-band image		
		var isSafari = "";
		navigator.userAgent.indexOf("Safari")>= 0 ? isSafari = true : isSafari = false;

		var isLTOpera92 = "";
		navigator.userAgent.indexOf("Opera") >= 0 && parseFloat(navigator.appVersion) < 9.2 ? isLTOpera92 = true : isLTOpera92 = false;
		if(UFO.hasFlashVersion(8,0) && (userData.bandwidth != "low" && isLTOpera92 == false) ) {
			if (flash == true) flashProxy.call(typeOfCall, flash_id);
		} else {
			if (flash != false) $('custom_movie').setStyle('background', 'url(' + lbPath + ') right top no-repeat');
		}
	
	//find description div
	desc = document.getElementById(curr_id);
	
	//display div
	removeClass(desc, 'none');
	//desc.style.visibility = "hidden";
}



function setupMenu()
{
	var scroll_menu = document.getElementById('custom_menu');
	scroll_menu = scroll_menu.getElementsByTagName('li')[0];
	
	scroll_menu.className = "autoclear";
	
	var el = scroll_menu.firstChild;
	scroll_menu.removeChild(el);
	
	var prev_button = document.createElement('a');
	var menu_title = document.createElement('span');
	var next_button = document.createElement('a');
	
	prev_button.setAttribute('id', 'prev_butt');
	menu_title.setAttribute('id', 'menu_title');
	next_button.setAttribute('id', 'next_butt');
	
	prev_button.setAttribute('href', '#');
	next_button.setAttribute('href', '#');
	
	scroll_menu.appendChild(prev_button);
	scroll_menu.appendChild(menu_title);
	scroll_menu.appendChild(next_button);
}




function updateMenu(obj)
{
	//escape if SCROLLING menu doesn't exist
	if (!document.getElementById('menu_title')) return false;
	
	//find menu title element
	var menu_title = document.getElementById('menu_title');
	
	//add title
	menu_title.innerHTML = obj.innerHTML;
	
	//check for Previous & Next
	var parent = obj.parentNode;
	
	( parent.previousSibling == null )? switchButton('prev_butt', 'none') : switchButton('prev_butt', 'none', parent.previousSibling.firstChild);
	( parent.nextSibling == null )? switchButton('next_butt', 'none') : switchButton('next_butt', 'none', parent.nextSibling.firstChild);
}



function switchButton(dir, style, obj)
{
	//var button obj
	var button = document.getElementById(dir);
	
	//toggle display style on button
	button.className = style;
	
	//assign tracking
	if (obj) button.setAttribute('name', '&lpos=soft_top_guide&lid=' + obj.parentNode.id);
	
	if (obj != null) button.onclick = function()
	{
		displayContent(obj, 'scrolling_menu');
		return false;
	};
}



function emptyArray(arr)
{
	//while length is larger than 0
	while ( sec.length > 0 )
	{
		//remove children
		sec.pop();
	}
}



function childHeight(el)
{
	var len = sec.length;
	
	for (i=0; i < len; i++)
	{	
		var parent = document.getElementById(sec[i]);
		var hei = parent.clientHeight + 10;
		var child = parent.getElementsByTagName(el);
			
		//matches height of current row
		for (j=0; j < child.length; j++)
		{
			//set height to current row
			child[j].style.height = hei + "px";
			
			//put row in array
			children.push(child[j]);
		}
		
		//put height in array
		arr.push(hei);
	}
}



function getHei(obj)
{
	//get height from obj and return it
	var el = document.getElementById(obj);
	var hei = el.clientHeight;
	return hei;
}



function setHei(hei)
{
	//loop through array
	for (i=0; i < arr.length; i++)
	{
		//set height to current child in array
		var curr = document.getElementById(arr[i]);
		curr.style.height = (hei - 6) + "px";
		curr.style.overflow = "auto";
		curr.style.marginbottom = "6px";
	}
}



function sortHei(a, b)
{
	//sort highest first
	return b - a;
}



function oddChild()
{
	//loop through "children" array
	for (i=0; i < children.length; i++)
	{
		var child = children[i];
		var odd = [i] % 2;
		
		//find if current child is odd number
		//add 'odd' className
		if (odd === 1) addClass(child, 'odd');
	}
}



function setAllHei()
{
	var hei = arr[0] + "px";

/*	
	var heightSum = 0;
	arr.each(function(item) { heightSum += item; });
	
	var contentHeight = $('contentCont').effect('height', {duration: 300, transition: Fx.Transitions.linear});
	contentHeight.start($('contentCont').getSize().size.y + (3 * hei) - heightSum);
*/
	
	for (i=0; i < sec.length; i++)
	{
		var obj = document.getElementById(sec[i]);
		var len = obj.childNodes.length;
		
		for (j=0; j < len; j++)
		{
			var child = obj.childNodes[j];
			child.style.height = hei;
		}
	}
	
}



function lastBorder()
{
	var parent = document.getElementById('pageElements');
	var child = parent.getElementsByTagName('div');
	
	//loop through elements in parent
	for (i=0; i < child.length; i++)
	{
		//find last element
		var gChild = child[i].lastChild;
		
		//assign 'last' className
		addClass(gChild, 'last');
	}
}