var selectedMenu;

function selectMenu(htmlId){
	selectedMenu = document.getElementById(htmlId);
	selectedMenu.src = "/images/menu/btn"+selectedMenu.id+"_on.png";
}

function menuOver(htmlElement){
	if(htmlElement == selectedMenu){return;}
	htmlElement.src = "/images/menu/btn"+htmlElement.id+"_on.png";
}

function menuOut(htmlElement){
	if(htmlElement == selectedMenu){return;}
	htmlElement.src = "/images/menu/btn"+htmlElement.id+"_off.png";
	if(selectedMenu != null){return;}	
}

function closeTopsLayer(){
	document.getElementById('topsLayer').style.display = "none";
}
function openTopsLayer(){
	document.getElementById('topsLayer').style.display = "block";
}

var layerwaiting;
var layerContentContainer;

function ajaxGetContents(url,layerId,container){
	layerwaiting = document.getElementById(layerId);
	layerwaiting.style.display = "block";
	layerContentContainer = document.getElementById(container);
	
	if(url.indexOf("?") != -1){url = url+"&v="+Math.random();}
	else{url = url+"?v="+Math.random();}
		
	if (window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET",url,true);
		req.send(null);
	}
		// Procura por uma versão ActiveX (IE)
	else if (window.ActiveXObject)
	{
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req)
		{
			req.onreadystatechange = processReqChange;
			req.open("GET",url,true);
			req.send();
		}
	}
}

var kool;
function menuAuto(menuID){
	document.getElementById('Comerciais').src = "/images/menu/btnComerciais_off.png";
	document.getElementById('Ligeiros').src = "/images/menu/btnLigeiros_off.png";
	document.getElementById('Sport').src = "/images/menu/btnSport_off.png";
	document.getElementById('Electricos').src = "/images/menu/btnElectricos_off.png";
	document.getElementById(menuID).src ="/images/menu/btn"+menuID+"_on.png";
	kool = document.getElementById(menuID);

}

function menuOver2(htmlElement){
	if(htmlElement == kool){return;}
	htmlElement.src = "/images/menu/btn"+htmlElement.id+"_on.png";
}

function menuOut2(htmlElement){
	if(htmlElement == kool){return;}
	htmlElement.src = "/images/menu/btn"+htmlElement.id+"_off.png";	
}

/* AJAX SCRIPT RUNNER */
var codeToRun;
var scriptRunnerTimerId;

function runScript(){
	clearTimeout(scriptRunnerTimerId);
	eval(codeToRun);
}
function scriptFilter(htmlString){
	if(String(htmlString).indexOf("<script>") != -1 && String(htmlString).indexOf("</script>") != -1){
		var initPointer = String(htmlString).indexOf("<script>");
		var endPointer = String(htmlString).indexOf("</script>");
		
		var scriptCode = String(htmlString).substr(initPointer + 8, endPointer);
		scriptCode = String(scriptCode).substr(0, String(scriptCode).indexOf("</script>"));
		
		codeToRun = scriptCode;
//		alert(codeToRun);
		scriptRunnerTimerId = setTimeout('runScript()', 50);
		
		var finalHtml = String(htmlString).substr(0, initPointer)+""+String(htmlString).substr(endPointer+9, String(htmlString).length);
		//alert(finalHtml +"   CODE: "+scriptCode);
		return finalHtml;
	}
	return htmlString;
}
/* END AJAX SCRIPT RUNNER */

function processReqChange(){
	// apenas quando o estado for "completado"
	if (req.readyState == 4)
	{
		// apenas se o servidor retornar "OK"
		if (req.status ==200)
		{
			// procura pela div id="pagina" e insere o conteudo
			// retornado nela, como texto HTML
			layerContentContainer.innerHTML = scriptFilter(req.responseText);
		}
		else
		{
			layerContentContainer.innerHTML = "<div style='width:100%;height:300;text-align:center;'>Ocorreu um erro pedimos desculpa pelo incómodo causado.</div>";
		}
	}
	if(req.readyState == 1){
		layerContentContainer.innerHTML = "<div style='width:100%;height:300;text-align:center;padding:100 0 0 0;'><img src='/images/layout/ajaxLoading.gif' alt='A Carregar'/></div>";
	}
}

function checkFichaId(){
	var loc = String(window.location);
	if(loc.indexOf("?openFichaId=") != -1){
		var id = String(loc).substring(String(loc).indexOf("?openFichaId=")+13, String(loc).length);
		ajaxGetContents('ficha.aspx?id='+id+'','topsLayer','fichatops')
	}
}

function checkUsadoFichaId(){
	var loc = String(window.location);
	if(loc.indexOf("?openFichaId=") != -1){
		var id = String(loc).substring(String(loc).indexOf("?openFichaId=")+13, String(loc).length);
		ajaxGetContents('veiculo.aspx?id='+id+'','topsLayer','fichatops')
	}
}

function homeBanner(spot,link,target){
	 AC_FL_RunContent(
	   'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
	   'width', '770',
	   'height', '392',
	   'src','/folder/homepage/'+spot,
	   'quality', 'best',
	   'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
	   'align', 'middle',
	   'play', 'true',
	   'loop', 'false',
	   'scale', 'showall',
	   'wmode', 'transparent',
	   'devicefont', 'false',
	   'id', 'menuTopo',
	   'bgcolor', '#ffffff',
	   'name', 'Spot',
	   'menu', 'false',
	   'allowFullScreen', 'false',
	   'FlashVars', 'linky='+link+'&janela='+target,
	   'allowScriptAccess','sameDomain',
	   'movie','/folder/homepage/'+spot,
	   'salign', ''
	); //end AC code
	
}

function homeSlideBanner(){
 AC_FL_RunContent(
	   'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
	   'width', '770',
	   'height', '392',
	   'src','/flash/bannerList?v=21',
	   'quality', 'best',
	   'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
	   'align', 'middle',
	   'play', 'true',
	   'loop', 'false',
	   'scale', 'showall',
	   'wmode', 'transparent',
	   'devicefont', 'false',
	   'id', 'menuTopo',
	   'bgcolor', '#ffffff',
	   'name', 'Spot',
	   'menu', 'false',
	   'allowFullScreen', 'false',
	   'FlashVars', 'xmlPath=/xml/bannerList.aspx',
	   'allowScriptAccess','sameDomain',
	   'movie','/flash/bannerList?v=21',
	   'salign', ''
	); //end AC code
}

function homeScroll(){
	 AC_FL_RunContent(
	   'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
	   'width', '965',
	   'height', '40',
	   'src','/flash/homeScroll',
	   'quality', 'best',
	   'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
	   'align', 'middle',
	   'play', 'true',
	   'loop', 'false',
	   'scale', 'showall',
	   'wmode', 'transparent',
	   'devicefont', 'false',
	   'id', 'homeScroll',
	   'bgcolor', '#ffffff',
	   'name', 'Spot',
	   'menu', 'false',
	   'allowFullScreen', 'false',
	   'allowScriptAccess','sameDomain',
	   'movie','/flash/homeScroll',
	   'salign', ''
	); //end AC code
	
}

function renderImageFade(imagesArray){
	var flashvar = "";
	for(var i = 0; i < imagesArray.length; i++){
		flashvar += "img"+i+"="+imagesArray[i];
		if(i < imagesArray.length-1){flashvar += "&";}
	}
	
	var flashObj = "";
	
	flashObj +="<object width='420' height='330' VIEWASTEXT>";
		flashObj +="<param name='movie' value='/flash/imageFade.swf'>";
		flashObj +="<param name='allowFullScreen' value='true'>";
		flashObj +="<param name='allowscriptaccess' value='always'>";
		flashObj +="<param name='FlashVars' value='"+flashvar+"'>";
		flashObj +="<embed src='/flash/imageFade.swf' type='application/x-shockwave-flash' ";
				flashObj +="allowscriptaccess='always' FlashVars='"+flashvar+"' ";
				flashObj +="allowfullscreen='true' width='420' height='330'></embed>";
	flashObj +="</object>";
	
	document.getElementById('flashFadeContainer').innerHTML = flashObj;
//	alert(flashObj);
}
function spot(){

	 AC_FL_RunContent(
	   'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
	   'width', '16',
	   'height', '14',
	   'src', '/flash/spot/spot_14022011_3',
	   'quality', 'best',
	   'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
	   'align', 'middle',
	   'play', 'true',
	   'loop', 'false',
	   'scale', 'showall',
	   'wmode', 'transparent',
	   'devicefont', 'false',
	   'id', 'menuTopo',
	   'bgcolor', '#ffffff',
	   'name', 'GaleriaDeImagens',
	   'menu', 'false',
	   'allowFullScreen', 'false',
	   'allowScriptAccess','sameDomain',
	   'movie', '/flash/spot/spot_14022011_3',
	   'salign', ''
	); //end AC code
	
}

function pub(){

	 AC_FL_RunContent(
	   'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
	   'width', '300',
	   'height', '250',
	   'src', '/flash/banner_renault_shooting_western',
	   'quality', 'best',
	   'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
	   'align', 'middle',
	   'play', 'true',
	   'loop', 'false',
	   'scale', 'showall',
	   'wmode', 'transparent',
	   'devicefont', 'false',
	   'id', 'menuTopo',
	   'bgcolor', '#ffffff',
	   'name', 'GaleriaDeImagens',
	   'menu', 'false',
	   'allowFullScreen', 'false',
	   'allowScriptAccess','sameDomain',
	   'movie', '/flash/banner_renault_shooting_western',
	   'salign', ''
	); //end AC code
	
}


var theImages = new Array()

theImages[0] = '/images/layout/header_1.jpg'
theImages[1] = '/images/layout/header_2.jpg'
theImages[2] = '/images/layout/header_3.jpg'
theImages[3] = '/images/layout/header_4.jpg'
theImages[4] = '/images/layout/header_5.jpg'
theImages[5] = '/images/layout/header_6.jpg'
theImages[6] = '/images/layout/header_7.jpg'
theImages[7] = '/images/layout/header_8.jpg'
theImages[8] = '/images/layout/header_9.jpg'
theImages[9] = '/images/layout/header_10.jpg'

var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="'+theImages[whichImage]+'" width="865" height="80">');
}

