// JavaScript Document

var nmCmpsCd = 0;
var sn = 1;
var markersArray = [];

$(document).ready(function () {	
	$(".menuNome").mouseover(function() {
		$(this).next().show(0,function() {
			if($(this).children(".menuDropSub").width()-30 < $(this).children(".menuDropNome").width()) {
				$(this).children(".menuDropSub").width($(this).children(".menuDropNome").width()+30)
			}
			var largura= $(this).children(".menuDropSub").width()-$(this).children(".menuDropNome").width();
			$(".menuDropSubDum",$(this).children(".menuDropSub"))
				.width(largura)
				.mouseover(function() {
					$(this).parent().parent().parent().find(".menuDrop").slideUp(250);
				});
			 
		});
		$(this).parent().hover(function() {
		}, function(){
			$(this).parent().find(".menuDrop").slideUp(250);
		});
	});

	$(".menuDropSubEach").mouseover(function() {
		$(this).addClass("active");
	}).mouseout(function() {
		$(this).removeClass("active");
	});
	
	$(".coverAll")
		.height($(document).height())
		

	$(".lghtBxCls").click(function() {
		$(this).parent().parent().fadeOut("fast")
		$(".coverAll").fadeOut("fast");
	});
	
	

	// ADICIONAR CAMPOS PARA POR CÓDIGOS

	$(".holderCod .addCod").click(function() {
		nmCmpsCd++;
		var newName = "input[name=codigo"+nmCmpsCd+"]";
		$(this).parent().append('<input style="display:none" type="text" class="codigo" name="codigo'+nmCmpsCd+'">');
		$(newName).slideDown();
	});

	// /ADICIONAR CAMPOS PARA POR CÓDIGOS

	//  TOOL TIP

	/*$("body").append('<div class="toolTip"></div>');

	$("*[boxii]").mouseover(function() {
		$(".toolTip").html($(this).attr("boxii")).show();
	}).mousemove(function(e) {
		var altu = $(".toolTip").height();
		var comp = $(".toolTip").width();
		$(".toolTip").css({
			"top":e.pageY-altu-20,
			"left":e.pageX-15
		});
	}).mouseout(function() {
		$(".toolTip").hide();
	});*/

	// /TOOL TIP

	//  DROPMENU SIMULAÇÃO

	//$("#dropSim1, #dropSim2, #dropSim3").msDropDown({mainCSS:'dd2'});

	//  /DROPMENU SIMULAÇÃO

	// MAPS
	$(".ofListContLinha").live('click', function() {
		$(".ofListContLinha").removeClass("active");
		$(this).addClass("active");

		var coord = $(this).attr("coord");
		var id_o = $(this).attr("id");

		var aux_coord = coord.split(",");
		animateTo(aux_coord[0],aux_coord[1],15)
		$(".listaCont").hide();
		$(".listaCont[id="+id_o+"]").show();
	}).live('mouseover', function() {
		$(this).addClass("hover");
	}).live('mouseout', function() {
		$(this).removeClass("hover");
	});
	// /MAPS
	
	$(window).bind("resize",function(){
		if($(".lghtBx").is(":visible")){
			var isto = $(".lghtBx");
			var boxheight = $(isto).height();
			var stageheight = 0;
			if (window.innerHeight) {
				stageheight=window.innerHeight;
			}
			else if (document.documentElement && document.documentElement.clientHeight) {
				stageheight=document.documentElement.clientHeight;
			}
			else if (document.body) {
				stageheight=document.body.clientHeight;
			}
			var top = (stageheight/2)-(boxheight/2);
			$(isto).css("top",top);
			}
	});
	
	$(".menuDropSub").each(function(){
		$(this).append("<div class='menuDropSubDum'></div>");
	});

	var MAXNROWS = 6;
	$('.tableScroll .table')
		.filter(function(){return ($("tbody tr",this).length > MAXNROWS);})
        .children("table")
		.fixedHeaderTable({ footer: false, cloneHeadToFoot: false, fixedColumn: false, themeClass:'generictable' });
});

function teste() {
	sn=1;
}

function activateMap(id){
	getFlash('map').activateMap(id);
	alterarConteudoOnSel(id);
}

function activateFromMap(o){
	var selectMap = $("#dropCid_msddHolder").html();
	$("#dropCid_msddHolder").next().remove();
	$("#dropCid_msddHolder").remove();
	$(".cidadeDropHolder").html(selectMap);
	$("#dropCid option").attr("selected","")
	$("#dropCid option[value="+o.id+"]").attr("selected","selected")
	$("#dropCid").msDropDown().data("dd");
	alterarConteudoOnSel(o.id);
}

function openLightBox(idlightbox){
	var box = $("#"+idlightbox);
	$(".coverAll").fadeTo(1000,0.7);
	$(box).fadeIn(function(){
		var boxheight = $(this).height();
		var stageheight = 0;
		if (window.innerHeight) {
			stageheight=window.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight) {
			stageheight=document.documentElement.clientHeight;
		}
		else if (document.body) {
			stageheight=document.body.clientHeight;
		}
		var top = (stageheight/2)-(boxheight/2);
		$(this).css("top",top);
	});
	$("select",$(box)).msDropDown().data("dd");
}

function animateTo(lat,lng,zoom) {
	map.setZoom(zoom);
	map.panTo(new google.maps.LatLng(lat,lng))
}

function alterarConteudoOnSel(id) {	
	deleteOverlays();
	$(".ofListContHold").html("");
	$.ajax({
		type:"GET",
		url:"xml/getXML.php",
		dataType:"xml",
		data:{local:id-1},
		success:function(xml) {
			$(".ofListContHold").html("");
			$(".listaCont").remove();
			var navTo = $(xml).find("lista").attr("navTo");
			var aux_navTo = navTo.split(",");
			if ($(xml).find("oficina").length>12) {
				$(".ofListContHold").css("overflow-y","scroll");
			} else {
				$(".ofListContHold").css("overflow-y","hidden");
			}
			$(xml).find("oficina").each(function(){
				var id_o = $(this).attr("id");
				var coord = $(this).attr("coord");
				var nome = $(this).find("parc").text();
				var dist = $(this).attr("dist");
				var codp = $(this).attr("codp");
				var rua = $(this).find("rua").text();
				var local = $(this).find("local").text();
				var concelho = $(this).find("conc").text();
				var cont = $(this).find("cont").text();
				var mail = $(this).find("mail").text();
				var hora = $(this).find("hora").text();
				var xxt = $(this).find("xxt").text();
				
				$(".ofListContHold").append('<div id="'+id_o+'" coord="'+coord+'" class="ofListContLinha">'+
                                            	'<div class="ofListCont1">'+nome+'</div>'+
                                                '<div class="ofListCont2">'+dist+'</div>'+
                                                '<div class="ofListCont3">'+concelho+'</div>'+
												'<div class="ofListCont4">'+codp+'</div>'+
												'<div class="ofListCont5">'+local+'</div>'+
                                            '</div>');
				$(".listaContactos").append('<div id="'+id_o+'" style="display:none" class="listaCont">'+
												'<div class="listaContEach">'+
													'<div class="listaContTxt">'+nome+'</div>'+
													'<div class="listaDiasUteis">'+rua+'</div>'+
													'<div class="listaContTxt">'+codp+' '+dist+', '+local+' '+concelho+'<br>'+cont+'<br>'+mail+'</div>'+
												'</div>'+
												'<div class="listaContEach listaContHorario">'+
													'<div class="listaContTxt">Horário</div>'+
													'<div class="listaDiasUteis">'+codp+'</div>'+
													'<div class="listaContTxt">'+xxt+'</div>'+
												'</div>'+
											'</div>');
				var aux_coord = coord.split(",");
				var marker = new google.maps.Marker({
					position: new google.maps.LatLng(aux_coord[0],aux_coord[1]), 
					map: map, 
					title:nome
				});
				markersArray.push(marker);

				google.maps.event.addListener(marker, 'click', function() {
					animateTo(aux_coord[0],aux_coord[1],17)
					$(".ofListContLinha").removeClass("active");
					$(".ofListContLinha[id="+id_o+"]").addClass("active");
	
					$(".listaCont").hide();
					$(".listaCont[id="+id_o+"]").show();
				});
				
			});
			map.setZoom(10);
			animateTo(aux_navTo[0],aux_navTo[1],13)			
		},
		complete:function() {
			$(".listaContactos").show(0);
			$(".listaCont:eq(0)").show(0);
		}
	});
}

function getFlash(movieName) {
	 if (navigator.appName.indexOf("Microsoft") != -1) {
		 return window[movieName];
	 } else {
		 return document[movieName];
	 }
}

function deleteOverlays() {
  if (markersArray) {
    for (i in markersArray) {
      markersArray[i].setMap(null);
    }
    markersArray.length = 0;
  }
}
