var ultima_caixa = '';
var sent = '';

if (getCookie('posicao') == '')
	setCookie('posicao', 0, 1);

if (getCookie('pagina') == '')
	setCookie('pagina', 1, 1);

function moveTo(pagina) {
	if (pagina != 'home') {
		$('#placa').css('display', 'none');
		$('#destaques_box').css('display', 'none');
	}
	if (ultima_caixa != '') {
		$('#' + ultima_caixa).css('display', 'none');
	}
	$('#destaques_box').css('display', 'none');
	$('#empresa_box').css('display', 'none');
	$('#servicos_box').css('display', 'none');
	$('#viaturas_box').css('display', 'none');
	$('#financiamento_box').css('display', 'none');
	$('#informacoes_box').css('display', 'none');
	$('#contactos_box').css('display', 'none');
	$('#gps_box').css('display', 'none');
	$('#contactos_box').css('display', 'none');

	/*
	 * if(ultima_caixa=='noticias_box') $('#seta_noticias').fadeOut();
	 * if(ultima_caixa=='newsletter_box') $('#seta_newsletter').fadeOut();
	 */
	var p = getCookie('pagina');
	if (getCookie('pagina') == '3') {
		$('#link_empresa').css('color', '#FFFFFF');
		$('#link_servicos').css('color', '#FFFFFF');
		$('#link_viaturas').css('color', '#FFFFFF');
		$('#link_financiamento').css('color', '#FFFFFF');
		$('#link_contactos').css('color', '#FFFFFF');
	} else {
		$('#link_empresa').css('color', '#1A1A1A');
		$('#link_servicos').css('color', '#1A1A1A');
		$('#link_viaturas').css('color', '#1A1A1A');
		$('#link_financiamento').css('color', '#1A1A1A');
		$('#link_contactos').css('color', '#1A1A1A');
	}
	switch (pagina) {
	case 'home':
		$('#mudancas').css('margin-top', getCookie('posicao') + 'px');
		$('#mudancas').animate( {
			marginTop : '0px'
		}, 1000);
		setTimeout("document.location.href = 'index.php?action=home'", 1100);
		setCookie('posicao', 0, 1);
		break;

	case 'noticias':
		$('#mudancas').css('margin-top', getCookie('posicao') + 'px');
		$('#mudancas').animate( {
			marginTop : '0px'
		}, 1000);
		setCookie('posicao', 0, 1);
		$('img.image').attr('src', 'img/' + p + '/fundo.jpg');
		setTimeout(
				"$('#noticias_box').fadeIn();getContent('noticias', 'noticias_box', '');",
				500);
		ultima_caixa = 'noticias_box';
		break;

	case 'newsletter':
		$('#mudancas').css('margin-top', getCookie('posicao') + 'px');
		$('#mudancas').animate( {
			marginTop : '0px'
		}, 1000);
		setCookie('posicao', 0, 1);
		$('img.image').attr('src', 'img/' + p + '/fundo.jpg');
		setTimeout(
				"$('#newsletter_box').fadeIn();getContent('newsletter', 'newsletter_box', '');",
				500);
		ultima_caixa = 'newsletter_box';
		break;

	case 'empresa':
		$('#mudancas').css('margin-top', getCookie('posicao') + 'px');
		$('#mudancas').animate( {
			marginTop : '20px'
		}, 1000);
		setCookie('posicao', 20, 1);
		setTimeout("$('#link_empresa').css('color', '#8CC63F')", 1000);
		$('img.image').attr('src', 'img/' + p + '/empresa.jpg');
		setTimeout("$('img.image').attr('src', 'img/" + p + "/fundo.jpg');",
				2500);
		setTimeout(
				"$('#empresa_box').css('display', 'block');getContent('empresa', 'empresa_box', '');",
				2900);
		ultima_caixa = 'empresa_box';
		break;

	case 'servicos':
		$('#mudancas').css('margin-top', getCookie('posicao') + 'px');
		$('#mudancas').animate( {
			marginTop : '55px'
		}, 1000);
		setCookie('posicao', 55, 1);
		setTimeout("$('#link_servicos').css('color', '#8CC63F')", 1000);
		$('img.image').attr('src', 'img/' + p + '/servicos.jpg');
		setTimeout("$('img.image').attr('src', 'img/" + p + "/fundo.jpg');",
				2500);
		setTimeout(
				"$('#servicos_box').css('display', 'block');getContent('servicos', 'servicos_box', '');",
				2900);
		ultima_caixa = 'servicos_box';
		break;

	case 'viaturas':
		$('#viaturas_box').css('display', 'none');
		$('#financiamento_box').css('display', 'none');
		$('#informacoes_box').css('display', 'none');
		$('#mudancas').css('margin-top', getCookie('posicao') + 'px');
		$('#mudancas').animate( {
			marginTop : '95px'
		}, 1000);
		setCookie('posicao', 95, 1);
		setTimeout("$('#link_viaturas').css('color', '#8CC63F')", 1000);
		$('img.image').attr('src', 'img/' + p + '/viaturas.jpg');
		setTimeout("$('img.image').attr('src', 'img/" + p + "/fundo.jpg');",
				2500);
		setTimeout(
				"$('#viaturas_box').css('display', 'block');getContent('viaturas', 'viaturas_box', '');",
				2900);
		ultima_caixa = 'viaturas_box';
		setTimeout("viaturas('', '', '', '')", 3100);
		break;
		
	case 'viaturas_voltar':
		$('#viaturas_box').css('display', 'none');
		$('#financiamento_box').css('display', 'none');
		$('#informacoes_box').css('display', 'none');
		$('#mudancas').css('margin-top', getCookie('posicao') + 'px');
		$('#mudancas').animate( {
			marginTop : '95px'
		}, 1000);
		setCookie('posicao', 95, 1);
		setTimeout("$('#link_viaturas').css('color', '#8CC63F')", 1000);
		$('img.image').attr('src', 'img/' + p + '/fundo.jpg');
		$('#viaturas_box').html('');
		setTimeout(
				"$('#viaturas_box').css('display', 'block');getContent('viaturas', 'viaturas_box', '');",
				500);
		ultima_caixa = 'viaturas_box';
		setTimeout("viaturas('', '', '', '')", 800);
		break;

	case 'financiamento':
		$('#mudancas').css('margin-top', getCookie('posicao') + 'px');
		$('#mudancas').animate( {
			marginTop : '120px'
		}, 1000);
		setCookie('posicao', 120, 1);
		$('img.image').attr('src', 'img/' + p + '/financiamento.jpg');
		setTimeout("$('#link_financiamento').css('color', '#8CC63F')", 1000);
		setTimeout("$('img.image').attr('src', 'img/" + p + "/fundo.jpg');",
				2500);
		setTimeout(
				"$('#financiamento_box').css('display', 'block');getContent('financiamento', 'financiamento_box', '');",
				2900);
		ultima_caixa = 'financiamento_box';
		break;

	case 'contactos':
		$('#mudancas').css('margin-top', getCookie('posicao') + 'px');
		$('#mudancas').animate( {
			marginTop : '155px'
		}, 1000);
		setCookie('posicao', 155, 1);
		setTimeout("$('#link_contactos').css('color', '#8CC63F')", 1000);
		$('img.image').attr('src', 'img/' + p + '/fundo.jpg');
		$('#contactos_box').css('display', 'block');
		setTimeout("getContent('contactos', 'contactos_box', '')", 1000);

		setTimeout("$('#gps').fadeOut();", 1600);
		setTimeout("$('#gps_box').fadeIn();", 2200);
		ultima_caixa = 'contactos_box';
		break;
	}
}

function abreViatura(id, page) {
	$('#placa').css('display', 'none');
	$('#destaques_box').css('display', 'none');
	$('#informacoes_box').css('display', 'none');
	$('#financiamento_box').css('display', 'none');
	$('#viaturas_box').css('display', 'block');
	// getContent('viaturas', 'viaturas_box', '');
	$('#viaturas_box')
			.html(
					'<div id="vtr_title"><img src="img/viaturas.png" alt="" /></div><div id="vtr"></div><div id="vtr_content"></div>');
	viatura(id, '', page);
	setTimeout("$('#scroll-pane').jScrollPane({dragMaxHeight:0, showArrows:true, scrollbarWidth:15});$('.lightbox').lightBox();",500);
}

function abreInformacoes(id, page, sent) {
	$('#viaturas_box').css('display', 'none');
	$('#informacoes_box').css('display', 'block');
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			$('#informacoes_box').html(xmlhttp.responseText);
		}
	}
	xmlhttp.open("GET", "includes/informacoes_handler.php?id=" + id + "&page="
			+ page + "&sent=" + sent, true);
	xmlhttp.send();
}

function abreFinanciamento(id, page, sent) {
	$('#viaturas_box').css('display', 'none');
	$('#financiamento_box').css('display', 'block');
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			$('#financiamento_box').html(xmlhttp.responseText);
		}
	}
	xmlhttp.open("GET", "includes/financiamento_handler.php?id=" + id
			+ "&page=" + page + "&sent=" + sent, true);
	xmlhttp.send();
}

function getContent(pagina, caixa, sent) {

	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			$('#' + caixa).html(xmlhttp.responseText);
		}
	}
	xmlhttp.open("GET", "includes/" + pagina + "_handler.php?sent=" + sent,
			true);
	xmlhttp.send();
}

function setCookie(name, value, days) { // set cookie value
	var expireDate = new Date()
	// set "expstring" to either future or past date, to set or delete cookie,
	// respectively
	var expstring = expireDate.setDate(expireDate.getDate() + parseInt(days))
	document.cookie = name + "=" + value + "; expires="
			+ expireDate.toGMTString() + "; path=/";
}
function getCookie(Name) { // get cookie value
	var re = new RegExp(Name + "=[^;]+", "i"); // construct RE to search for
												// target name/value pair
	if (document.cookie.match(re)) // if cookie found
		return document.cookie.match(re)[0].split("=")[1] // return its value
	return ""
}

function destaques() {

	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			$('#destaques_box').html(xmlhttp.responseText);
		}
	}
	xmlhttp.open("GET", "destaques.php", true);
	xmlhttp.send();
	$('#destaques_box').fadeIn(1500);

}

function viaturas(page, marca, modelo, combustivel) {
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			$('#vtr_content').html(xmlhttp.responseText);
		}
	}
	xmlhttp.open("GET", "viaturas.php?page=" + page + "&marca=" + marca
			+ "&modelo=" + modelo + "&combustivel=" + combustivel, true);
	xmlhttp.send();
}

function viatura(id, aviso, page) {
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			$('#vtr_content').html(xmlhttp.responseText);
		}
	}
	xmlhttp.open("GET", "viatura.php?id=" + id + "&aviso=" + aviso + "&page="
			+ page, true);
	xmlhttp.send();
}

function close_cont() {
	$('#gps_box').fadeOut();
	setTimeout("document.location.href='index.php?action=home'", 1000);
}

function ver_mapas() {
	var str = '<img src="img/mapas.png" alt="" usemap="#mapa" style="float:left;z-index:0;" />';
	str += '<a onclick="this.blur();close_cont();" style="z-index:1000;margin-top:-30px;float:left;margin-left:515px;">';
	str += '<img src="img/close.png" alt="fechar" style="" /></a>';
	str += '<map id="mapa" name="mapa">';
	str += '<area shape="rect" coords="20,100,200,200" href="#" onclick="change_mapa(1);" alt="" />';
	str += '<area shape="rect" coords="300,100,530,200" href="#" onclick="change_mapa(2);" alt="" />';
	str += '</map>';
	$('#gps_box').html(str);
}

function change_mapa(value) {
	if (value == 1) { // olhalvas
		var str = '<img src="img/olhalvas.png" alt="" style="float:left;z-index:0;" /> ';
		str += '<a onclick="this.blur();" href="img/mapa_helcar_olhalvas.pdf" target="_blank" style="float:left;z-index:9000;margin-top:-50px;margin-left:15px;">';
		str += '<img src="img/imprimir.png" alt="imprimir" style="" /></a>';
		str += '<a href="javascript:openwindow(1);" onclick="this.blur();" href="#" style="float:left;z-index:9000;margin-top:-30px;margin-left:20px;*margin-left:-70px;">';
		str += '<img src="img/google.png" alt="google" style="" /></a>';
		str += '<a onclick="this.blur();ver_mapas();" style="z-index:9000;float:left;margin-top:-35px;margin-left:505px;">';
		str += '<img src="img/close_mapa.png" alt="fechar" style="" /></a>';
		$('#gps_box').html(str);
	} else if (value == 2) { // eufemia
		var str = '<img src="img/eufemia.png" alt="" style="float:left;z-index:0;" />';
		str += '<a onclick="this.blur();" href="img/mapa_helcar_eufemia.pdf" target="_blank" style="float:left;z-index:9000;margin-top:-50px;margin-left:15px;">';
		str += '<img src="img/imprimir.png" alt="imprimir" style="" /></a>';
		str += '<a  href="javascript:openwindow(2);" onclick="this.blur();" style="float:left;z-index:9000;margin-top:-30px;margin-left:20px;*margin-left:-70px;">';
		str += '<img src="img/google.png" alt="google" style="" /></a>';
		str += '<a onclick="this.blur();ver_mapas();" style="z-index:9000;float:left;margin-top:-35px;margin-left:505px;">';
		str += '<img src="img/close_mapa.png" alt="fechar" style="" /></a>';
		$('#gps_box').html(str);
	}
}

function limpa_form_cont() {
	$('#nome').val('');
	$('#morada').val('');
	$('#email').val('');
	$('#telefone').val('');
	$('#mensagem').val('');
}

function mudar_ambiente(valor) {
	switch (valor) {
	case 1:
		$('.image').fadeOut(600);
		setTimeout("$('.image').attr('src', 'img/1/fundo.jpg')", 400);
		setTimeout("$('.link').css('color', '#1A1A1A');", 900);
		setCookie('pagina', 1, 1);
		$('.image').fadeIn(600);
		break;

	case 2:
		$('.image').fadeOut(600);
		setTimeout("$('.image').attr('src', 'img/2/fundo.jpg')", 400);
		setTimeout("$('.link').css('color', '#1A1A1A');", 900);
		setCookie('pagina', 2, 1);
		$('.image').fadeIn(600);
		break;

	case 3:
		$('.image').fadeOut(600);
		setTimeout("$('.image').attr('src', 'img/3/fundo.jpg');", 400);
		setTimeout("$('.link').css('color', '#FFFFFF');", 900);
		setCookie('pagina', 3, 1);
		$('.image').fadeIn(600);

		break;

	case 4:
		$('.image').fadeOut(600);
		setTimeout("$('.image').attr('src', 'img/4/fundo.jpg')", 400);
		setTimeout("$('.link').css('color', '#1A1A1A');", 900);
		setCookie('pagina', 4, 1);
		$('.image').fadeIn(600);
		break;
	}
}
