// Groovy Flash HTML resources. Copyright 2005 James Gretton - Igloo Web Design . com (unless otherwise stated). All rights reserved.
var HOST = "gwsmotors";
var FLA = "gwsmotors";
var htmlWaiting;
var currentPage = checkLoc();
var saveNavPage = "";


//add onload event to div:
function init(){
	var showFlash = !checkRef();
	if (showFlash) displayWhich("flash");
	else displayWhich("html");
}


// Detection from www.dithered.com
var ua        = navigator.userAgent.toLowerCase(); 
var is_pc_ie  = ( (ua.indexOf('msie') != -1 ) && ( ua.indexOf('win') != -1 ) && ( ua.indexOf('opera') == -1 ) && ( ua.indexOf('webtv') == -1 ) );

// Returns true if refered from url site.
function checkRef(){
	url = HOST;
	if (checkParams()) 
		return true;
	var ref = unescape(document.referrer);
	if (ref != ""){
		var begin = ref.indexOf('/') + 2;
		ref = ref.substring(begin,ref.length);
		var end = ref.indexOf('/');
		if (end < 0) end = ref.length;
		ref = ref.substring(0,end);
		if(ref.indexOf(url) != -1 ){
			return true;
		}else{
			return false;
		}
	}else{
		return false;
	}
}

function checkParams(){
	//Checks for get data on header.
	url = HOST;
	var loc = unescape(document.location.href);
	//check for root of page
	loc.substring(loc.indexOf('?'), loc.length);
	if ( loc.indexOf('disp=html') != -1 ){
		return true;
	}else
		return false;
}

function checkLoc(){
	url = HOST;
	var loc = unescape(document.location.href);
	//check for root of page
	if (loc.indexOf('.html') == -1){
		return "";
	}
	// remove . from domain name
	if (loc.indexOf(url) != -1){
		loc = loc.substring(loc.indexOf(url) + url.length, loc.length);
	}
	// trim down to just file name (without .html)
	loc = loc.substring(loc.lastIndexOf('/') + 1, loc.lastIndexOf('.'));
	//add .html if necessery
	if (loc == ""){
		return "";
	}else{
		return loc + ".html";
	}
}


//Swap between flash and html
function displayWhich(which){
	var h = document.getElementById('htmldiv');	
	var f = document.getElementById('flashdiv');		
	// Display the flash div, and set the page variable
	if (which == 'flash' && f){
		if(h) h.style.display = 'none';
		if(f) f.style.display = 'block';
		if (saveNavPage)
			setFlashVariables(FLA, saveNavPage)
	}
	// Display the html div, changing html page if neccesery.
	if (which == 'html'){
		//Do page change.
		if (checkHtmlWaiting())
		  return;
		if(h) h.style.display = 'block';
		if(f) f.style.display = 'none';
		scrollbars(true);
	}
}

function checkHtmlWaiting(){
	if (!htmlWaiting) return false;
	if (htmlWaiting == currentPage) return false;
	//Otherwise call goto
	gotoHtmlWaiting();
	return true;
}

function gotoHtmlWaiting(){
	location.href = htmlWaiting;
}

function setHtmlWaiting(p){
	htmlWaiting = p;
}

//Mustard's Code
function setFlashVariables(movieid, flashquery){
	var i,values;
	if(is_pc_ie){
		var chunk = flashquery.split("&");
		for(i in chunk){
			var m = document.getElementById('flashcars');
			values = chunk[i].split("=");
			if (m){
				m.SetVariable('NAVFRAME_'+values[0],values[1]);
			}
		}
	}else{
		var divcontainer = "flash_setvariables_"+movieid;
		if(!document.getElementById(divcontainer)){
			var divholder = document.createElement("div");
			divholder.id = divcontainer;
			document.body.appendChild(divholder);
		}
		document.getElementById(divcontainer).innerHTML = "";
		var divinfo = "<embed src='/gateway.swf' FlashVars='lc="+movieid+"&fq="+escape(flashquery)+"' width='0' height='0' type='application/x-shockwave-flash'></embed>";
		document.getElementById(divcontainer).innerHTML = divinfo;
	}
	//Save Nav Page
	saveNavPage = flashquery;
}