
var ajax = new Array();

function obterModelo(sel)
{
	var marca = sel.options[sel.selectedIndex].value;
	document.getElementById('modelo').options.length = 0;
	if(marca.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'obterModelos.php?marca='+marca;
		ajax[index].onCompletion = function(){ obterModelos(index) };
		ajax[index].runAJAX();
	}
	
}

function obterModelos(index)
{
	var obj = document.getElementById('modelo');
	eval(ajax[index].response);
}

function obterModelo2(sel)
{
	var marca2 = sel.options[sel.selectedIndex].value;
	document.getElementById('modelo2').options.length = 0;
	if(marca2.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'obterModelos.php?marca='+marca2;
		ajax[index].onCompletion = function(){ obterModelos2(index) };
		ajax[index].runAJAX();
	}
	
}

function obterModelos2(index)
{
	var obj = document.getElementById('modelo2');
	eval(ajax[index].response);
}

function obterModelo3(sel)
{
	var marca3 = sel.options[sel.selectedIndex].value;
	document.getElementById('modelo3').options.length = 0;
	if(marca3.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'obterModelos.php?marca='+marca3;
		ajax[index].onCompletion = function(){ obterModelos3(index) };
		ajax[index].runAJAX();
	}
	
}

function obterModelos3(index)
{
	var obj = document.getElementById('modelo3');
	eval(ajax[index].response);
}

function obterModelo4(sel)
{
	var marca = sel.options[sel.selectedIndex].value;
	document.getElementById('modelo').options.length = 0;
	if(marca.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'obterModelosMotos.php?marca='+marca;
		ajax[index].onCompletion = function(){ obterModelos4(index) };
		ajax[index].runAJAX();
	}
	
}

function obterModelos4(index)
{
	var obj = document.getElementById('modelo');
	eval(ajax[index].response);
}

function obterModelo5(sel)
{
	var marca = sel.options[sel.selectedIndex].value;
	document.getElementById('modelo').options.length = 0;
	if(marca.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'obterModelosBarcos.php?marca='+marca;
		ajax[index].onCompletion = function(){ obterModelos5(index) };
		ajax[index].runAJAX();
	}
	
}

function obterModelos5(index)
{
	var obj = document.getElementById('modelo');
	eval(ajax[index].response);
}

function obterModelo6(sel)
{
	var marca = sel.options[sel.selectedIndex].value;
	document.getElementById('modelo2').options.length = 0;
	if(marca.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'obterModelosMotos.php?marca='+marca;
		ajax[index].onCompletion = function(){ obterModelos6(index) };
		ajax[index].runAJAX();
	}
	
}

function obterModelos6(index)
{
	var obj = document.getElementById('modelo2');
	eval(ajax[index].response);
}

function obterModelo7(sel)
{
	var marca = sel.options[sel.selectedIndex].value;
	document.getElementById('modelo3').options.length = 0;
	if(marca.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'obterModelosBarcos.php?marca='+marca;
		ajax[index].onCompletion = function(){ obterModelos7(index) };
		ajax[index].runAJAX();
	}
	
}

function obterModelos7(index)
{
	var obj = document.getElementById('modelo3');
	eval(ajax[index].response);
}

function obterModelo8(sel)
{
	var marca = sel.options[sel.selectedIndex].value;
	document.getElementById('modelo').options.length = 0;
	if(marca.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'obterModelosMotos.php?marca='+marca;
		ajax[index].onCompletion = function(){ obterModelos8(index) };
		ajax[index].runAJAX();
	}
	
}

function obterModelos8(index)
{
	var obj = document.getElementById('modelo');
	eval(ajax[index].response);
}

function obterModelo9(sel)
{
	var marca = sel.options[sel.selectedIndex].value;
	document.getElementById('modelo2').options.length = 0;
	if(marca.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'obterModelosMotos.php?marca='+marca;
		ajax[index].onCompletion = function(){ obterModelos9(index) };
		ajax[index].runAJAX();
	}
	
}

function obterModelos9(index)
{
	var obj = document.getElementById('modelo2');
	eval(ajax[index].response);
}

function obterModelo10(sel)
{
	var marca = sel.options[sel.selectedIndex].value;
	document.getElementById('modelo3').options.length = 0;
	if(marca.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'obterModelosMotos.php?marca='+marca;
		ajax[index].onCompletion = function(){ obterModelos10(index) };
		ajax[index].runAJAX();
	}
	
}

function obterModelos10(index)
{
	var obj = document.getElementById('modelo3');
	eval(ajax[index].response);
}

function obterModelo11(sel)
{
	var marca = sel.options[sel.selectedIndex].value;
	document.getElementById('modelo').options.length = 0;
	if(marca.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'obterModelosBarcos.php?marca='+marca;
		ajax[index].onCompletion = function(){ obterModelos11(index) };
		ajax[index].runAJAX();
	}
	
}

function obterModelos11(index)
{
	var obj = document.getElementById('modelo');
	eval(ajax[index].response);
}

function obterModelo12(sel)
{
	var marca = sel.options[sel.selectedIndex].value;
	document.getElementById('modelo2').options.length = 0;
	if(marca.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'obterModelosBarcos.php?marca='+marca;
		ajax[index].onCompletion = function(){ obterModelos12(index) };
		ajax[index].runAJAX();
	}
	
}

function obterModelos12(index)
{
	var obj = document.getElementById('modelo2');
	eval(ajax[index].response);
}

function obterModelo13(sel)
{
	var marca = sel.options[sel.selectedIndex].value;
	document.getElementById('modelo3').options.length = 0;
	if(marca.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'obterModelosBarcos.php?marca='+marca;
		ajax[index].onCompletion = function(){ obterModelos13(index) };
		ajax[index].runAJAX();
	}
	
}

function obterModelos13(index)
{
	var obj = document.getElementById('modelo3');
	eval(ajax[index].response);
}

function sack(file) {
	this.xmlhttp = null;

	this.resetData = function() {
		this.method = "POST";
  		this.queryStringSeparator = "?";
		this.argumentSeparator = "&";
		this.URLString = "";
		this.encodeURIString = true;
  		this.execute = false;
  		this.element = null;
		this.elementObj = null;
		this.requestFile = file;
		this.vars = new Object();
		this.responseStatus = new Array(2);
  	};

	this.resetFunctions = function() {
  		this.onLoading = function() { };
  		this.onLoaded = function() { };
  		this.onInteractive = function() { };
  		this.onCompletion = function() { };
  		this.onError = function() { };
		this.onFail = function() { };
	};

	this.reset = function() {
		this.resetFunctions();
		this.resetData();
	};

	this.createAJAX = function() {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				this.xmlhttp = null;
			}
		}

		if (! this.xmlhttp) {
			if (typeof XMLHttpRequest != "undefined") {
				this.xmlhttp = new XMLHttpRequest();
			} else {
				this.failed = true;
			}
		}
	};

	this.setVar = function(name, value){
		this.vars[name] = Array(value, false);
	};

	this.encVar = function(name, value, returnvars) {
		if (true == returnvars) {
			return Array(encodeURIComponent(name), encodeURIComponent(value));
		} else {
			this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
		}
	}

	this.processURLString = function(string, encode) {
		encoded = encodeURIComponent(this.argumentSeparator);
		regexp = new RegExp(this.argumentSeparator + "|" + encoded);
		varArray = string.split(regexp);
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split("=");
			if (true == encode){
				this.encVar(urlVars[0], urlVars[1]);
			} else {
				this.setVar(urlVars[0], urlVars[1]);
			}
		}
	}

	this.createURLString = function(urlstring) {
		if (this.encodeURIString && this.URLString.length) {
			this.processURLString(this.URLString, true);
		}

		if (urlstring) {
			if (this.URLString.length) {
				this.URLString += this.argumentSeparator + urlstring;
			} else {
				this.URLString = urlstring;
			}
		}

		// prevents caching of URLString
		this.setVar("rndval", new Date().getTime());

		urlstringtemp = new Array();
		for (key in this.vars) {
			if (false == this.vars[key][1] && true == this.encodeURIString) {
				encoded = this.encVar(key, this.vars[key][0], true);
				delete this.vars[key];
				this.vars[encoded[0]] = Array(encoded[1], true);
				key = encoded[0];
			}

			urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
		}
		if (urlstring){
			this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
		} else {
			this.URLString += urlstringtemp.join(this.argumentSeparator);
		}
	}

	this.runResponse = function() {
		eval(this.response);
	}

	this.runAJAX = function(urlstring) {
		if (this.failed) {
			this.onFail();
		} else {
			this.createURLString(urlstring);
			if (this.element) {
				this.elementObj = document.getElementById(this.element);
			}
			if (this.xmlhttp) {
				var self = this;
				if (this.method == "GET") {
					totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, true);
				} else {
					this.xmlhttp.open(this.method, this.requestFile, true);
					try {
						this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
					} catch (e) { }
				}

				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState) {
						case 1:
							self.onLoading();
							break;
						case 2:
							self.onLoaded();
							break;
						case 3:
							self.onInteractive();
							break;
						case 4:
							self.response = self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;

							if (self.execute) {
								self.runResponse();
							}

							if (self.elementObj) {
								elemNodeName = self.elementObj.nodeName;
								elemNodeName.toLowerCase();
								if (elemNodeName == "input"
								|| elemNodeName == "select"
								|| elemNodeName == "option"
								|| elemNodeName == "textarea") {
									self.elementObj.value = self.response;
								} else {
									self.elementObj.innerHTML = self.response;
								}
							}
							if (self.responseStatus[0] == "200") {
								self.onCompletion();
							} else {
								self.onError();
							}

							self.URLString = "";
							break;
					}
				};

				this.xmlhttp.send(this.URLString);
			}
		}
	};

	this.reset();
	this.createAJAX();
}


//0 means disabled; 1 means enabled;
var popupStatus2 = 0;

function loadPopup3(){
	//loads popup only if it is disabled
	if(popupStatus2==0){
		$("#popUpOsMeusFavoritos").show();
		popupStatus2 = 1;
	}
}

function loadPopup4(){
	//loads popup only if it is disabled
	if(popupStatus2==0){
		$("#popUpOsMeusFavoritosInfo").show();
		popupStatus2 = 1;
	}
}

function loadPopup5(){
	//loads popup only if it is disabled
	if(popupStatus2==0){
		$("#popUpAsMinhasPesquisas").show();
		popupStatus2 = 1;
	}
}

function loadPopup6(){
	//loads popup only if it is disabled
	if(popupStatus2==0){
		$("#popUpAsMinhasPesquisasInfo").show();
		popupStatus2 = 1;
	}
}

function loadPopup7(){
	//loads popup only if it is disabled
	if(popupStatus2==0){
		$("#popUpUltimosAnuncios").show();
		popupStatus2 = 1;
	}
}

function loadPopup8(){
	//loads popup only if it is disabled
	if(popupStatus2==0){
		$("#utilidadesnd").show();
		popupStatus2 = 1;
	}
}

function loadPopup9(){
	//loads popup only if it is disabled
	if(popupStatus2==0){
		$("#personalizar").show();
		popupStatus2 = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup2(){
	//disables popup only if it is enabled
	if(popupStatus2==1){
		$("#popUpOsMeusFavoritos").fadeOut("fast");
		popupStatus2 = 0;
	}
}

function disablePopup3(){
	//disables popup only if it is enabled
	if(popupStatus2==1){
		$("#popUpOsMeusFavoritosInfo").fadeOut("fast");
		popupStatus2 = 0;
	}
}

function disablePopup4(){
	//disables popup only if it is enabled
	if(popupStatus2==1){
		$("#popUpAsMinhasPesquisas").fadeOut("fast");
		popupStatus2 = 0;
	}
}

function disablePopup5(){
	//disables popup only if it is enabled
	if(popupStatus2==1){
		$("#popUpAsMinhasPesquisasInfo").fadeOut("fast");
		popupStatus2 = 0;
	}
}

function disablePopup6(){
	//disables popup only if it is enabled
	if(popupStatus2==1){
		$("#popUpUltimosAnuncios").fadeOut("fast");
		popupStatus2 = 0;
	}
}

function disablePopup7(){
	//disables popup only if it is enabled
	if(popupStatus2==1){
		$("#utilidadesnd").fadeOut("fast");
		popupStatus2 = 0;
	}
}

function disablePopup8(){
	//disables popup only if it is enabled
	if(popupStatus2==1){
		$("#personalizar").fadeOut("fast");
		popupStatus2 = 0;
	}
}

//centering popup
function centerPopup3(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popUpOsMeusFavoritos").height();
	var popupWidth = $("#popUpOsMeusFavoritos").width();
	//centering
	$("#popUpOsMeusFavoritos").css({
		"position": "absolute",
		"top": 92,
		"left": windowWidth/2-52
	});
	
}

function centerPopup4(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popUpOsMeusFavoritosInfo").height();
	var popupWidth = $("#popUpOsMeusFavoritosInfo").width();
	//centering
	$("#popUpOsMeusFavoritosInfo").css({
		"position": "absolute",
		"top": 72,
		"left": windowWidth/2-42
	});
	
}

function centerPopup5(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popUpAsMinhasPesquisas").height();
	var popupWidth = $("#popUpAsMinhasPesquisas").width();
	//centering
	$("#popUpAsMinhasPesquisas").css({
		"position": "absolute",
		"top": 92,
		"left": windowWidth/2-32
	});
	
}

function centerPopup6(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popUpAsMinhasPesquisasInfo").height();
	var popupWidth = $("#popUpAsMinhasPesquisasInfo").width();
	//centering
	$("#popUpAsMinhasPesquisasInfo").css({
		"position": "absolute",
		"top": 72,
		"left": windowWidth/2-32
	});
	
}

function centerPopup7(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popUpUltimosAnuncios").height();
	var popupWidth = $("#popUpUltimosAnuncios").width();
	//centering
	$("#popUpUltimosAnuncios").css({
		"position": "absolute",
		"top": 92,
		"left": windowWidth/2-14
	});
	
}

function centerPopup8(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#utilidadesnd").height();
	var popupWidth = $("#utilidadesnd").width();
	//centering
	$("#utilidadesnd").css({
		"position": "absolute",
		"top": 12,
		"left": windowWidth/2-350
	});
	
}

function centerPopup9(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#personalizar").height();
	var popupWidth = $("#personalizar").width();
	//centering
	$("#personalizar").css({
		"position": "absolute",
		"top": 12,
		"left": windowWidth/2-350
	});
	
}

$(document).ready(function(){

	$("a#meusFavoritos").click(function(){
		//centering with css
		centerPopup3();
		//load popup
		loadPopup3();
	});
	
	$("a#minhasPesquisas").click(function(){
		//centering with css
		centerPopup5();
		//load popup
		loadPopup5();
	});
	
	$("a#infoMeusFavoritos").mouseover(function(){
		//centering with css
		centerPopup4();
		//load popup
		loadPopup4();
	});
	
	$("a#infoMinhasPesquisas").mouseover(function(){
		//centering with css
		centerPopup6();
		//load popup
		loadPopup6();
	});
	
	$("a#ultimosAnuncios").click(function(){
		//centering with css
		centerPopup7();
		//load popup
		loadPopup7();
	});
	
	$("a#utilidades").click(function(){
		//centering with css
		centerPopup8();
		//load popup
		loadPopup8();
	});
	
	$("a#personalizarnd").click(function(){
		//centering with css
		centerPopup9();
		//load popup
		loadPopup9();
	});
	
	$("a#infoMeusFavoritos").mouseleave(function(){
		disablePopup3();
	});
	
	$("#utilidadesnd").mouseleave(function(){
		disablePopup7();
	});
	
	$("#personalizar").mouseleave(function(){
		disablePopup8();
	});
	
	$("a#infoMinhasPesquisas").mouseleave(function(){
		disablePopup5();
	});
	
	$("#popUpOsMeusFavoritos").mouseleave(function(){
		disablePopup2();
	});
	
	$("#popUpAsMinhasPesquisas").mouseleave(function(){
		disablePopup4();
	});
	
	$("#popUpUltimosAnuncios").mouseleave(function(){
		disablePopup6();
	});
				
	$("a#popUpOsMeusFavoritosFechar").click(function(){
		disablePopup2();
	});
	
	$("a#popUpUltimosAnunciosFechar").click(function(){
		disablePopup6();
	});
	
	$("a#popUpAsMinhasPesquisasFechar").click(function(){
		disablePopup4();
	});
	
	$("#backgroundPopup").click(function(){
		disablePopup2();
	});
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus2==1){
			disablePopup2();
		}
	});
	
	
	$("a.loadpopup").click(function(){
		var windowHeight = document.documentElement.clientHeight;							
		$("#backgroundPopupPesquisa").css({
			"opacity": "0.7"
		});
		$("#backgroundPopupPesquisa").css({
		"height": windowHeight
		});
		$("#backgroundPopupPesquisa").css({
			"display": "block"
		});
	});

});

$(document).ready(function(){
	$('a#ultimosAnunciosMotos').click(function() {
	$('#divUltimosCarros').hide();
	$('#divUltimosMotos').show();
	$('#divUltimosBarcos').hide();
    return false;
    });
	$('a#ultimosAnunciosCarros').click(function() {
	$('#divUltimosCarros').show();
	$('#divUltimosMotos').hide();
	$('#divUltimosBarcos').hide();
    return false;
    });
	$('a#ultimosAnunciosBarcos').click(function() {
	$('#divUltimosCarros').hide();
	$('#divUltimosMotos').hide();
	$('#divUltimosBarcos').show();
    return false;
    });
});
jQuery.fn.styleSwitcher = function(){
	$(this).click(function(){
		loadStyleSheet(this);
		return false;
	});
	function loadStyleSheet(obj) {
		$('body').append('<div id="overlay" />');
		$('body').css({height:'100%'});
			var windowHeight = document.documentElement.clientHeight;							
		$("#backgroundPopupPesquisa").css({
			"opacity": "0.7"
		});
		$("#backgroundPopupPesquisa").css({
		"height": windowHeight
		});
		$("#backgroundPopupPesquisa").css({
			"display": "block"
		});
		$('#backgroundPopupPesquisa')		
			.fadeIn(500,function(){
				$.get( obj.href+'&js',function(data){
					$('#stylesheet').attr('href','css/' + data + '.css');
					cssDummy.check(function(){
						$('#backgroundPopupPesquisa').fadeOut(500,function(){
							$(this).remove();
						});	
					});
				});
			});
	}
	var cssDummy = {
		init: function(){
			$('<div id="dummy-element" style="display:none" />').appendTo('body');
		},
		check: function(callback) {
			if ($('#dummy-element').width()==2) callback();
			else setTimeout(function(){cssDummy.check(callback)}, 200);
		}
	}
	cssDummy.init();
}
