var contentDivActive = 0;
var divVisible = 0;
var divModalActive;
var menuActive;
var lastMediaDiv;

function showDivMedia (div)
{
    if (document.getElementById && document.getElementById(div)!= null)
	    {
		    var style2 = document.getElementById(div).style;
		    style2.display = "block";
		    
		    //hideLastMedia();
		    lastMediaDiv=div;
	    }
    
}

function hideLastMedia ()
{
    if (document.getElementById && document.getElementById(lastMediaDiv)!= null)
	    {
		    var style2 = document.getElementById(lastMediaDiv).style;
		    style2.display = "none";
		    
	    }
    
}
function setColor(div)
{
    var style2 = document.getElementById(div).style;
    style2.backgroundImage = "url(menu/_hover.png)";
    style2.color = "#fff";
    menuActive = "";
    menuActive = div;
}

function swapBackground1(div)
{
    
	if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		if (style2.backgroundImage == "")
		{	   
			    style2.backgroundImage = "url(menu/_hover.png)";
			    style2.color = "#fff";
			    menuActive = div;
		}
		else
		{
		    if(div != menuActive)
		    {
		    style2.color = "#ccc";
			style2.backgroundImage= "";
			}
		}
	}
}

function swapBackground2(div)
{

    

	if (document.getElementById && document.getElementById(div)!= null)
	{
	    ;debugger
		var style2 = document.getElementById(div).style;
		if (style2.backgroundImage == "")
		{
			style2.backgroundImage = "url(menu/_hover.png); ";
			style2.backgroundRepeat="no-repeat";
			
			menuActive = div;
		}
		else
		{
		 if(div != menuActive && document.getElementById(menuActive) != null)
		    {
			    var style3 = document.getElementById(menuActive).style;
			    style3.backgroundImage = "";
			}
			
		}
	}
	
}

function openModalDiv(div)
{
   
	if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		style2.display = "block";
		divModalActive = div;
	}
	
}

function closeModalDiv()
{
    if( document.getElementById && document.getElementById(divModalActive)!= null)
	{
	     var style21 = document.getElementById(divModalActive).style;
	     style21.display = "none";
	}
}

function addBookmark(url, title) {
        if (window.sidebar) { // firefox
              window.sidebar.addPanel(title, url,"");
        } else if( document.all ) { //MSIE
                window.external.AddFavorite( url, title);
        }
}

function hideDivContent(div)
{
	if (document.getElementById && document.getElementById(div)!= null)
	{
	    var style2 = document.getElementById(div).style;
		if (style2.display == "block" || style2.display == "")
		    style2.display == "none"
		else
		    var style3 = document.getElementById(contentDivActive).style;
		    style3.display == "none"
	}
}

function showDivContent(div, menuDiv)
{
	if (document.getElementById && document.getElementById(div)!= null)
	{
		    
	    if(contentDivActive == 0)
	    {
	        
	        hideDiv(menuDiv)
	        showDiv(div);
	        contentDivActive = div;
	        
	        
	    }
		
		else
		{ 
		    if(div != contentDivActive)
		    {
		        hideDiv(contentDivActive);
    		    showDiv(div);
		        contentDivActive = div;  
		    }
		    else
		    {
		        contentDivActive = div;
		    }
		}
	}
}




function hideDiv(div)
{
	if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		style2.display = "none";
	}
}

function showDiv(div)
{
	if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		style2.display = "block";
		
	}
}

function showHideDiv(div)
{
	if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		if (style2.display == "none" || style2.display == "")
		{
			style2.display = "block";
		}
		else
		{
			style2.display = "none";
		}
	}
}

function hideDivNow(div) 
{    
	if (document.getElementById && document.getElementById(div)!= null)
	{
	    divVisible = 0;
		var style2 = document.getElementById(div).style;
		style2.display = "none";	
	}
}

function showHideDivRedBorder(div)
{
	if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		if (style2.border == null || style2.border == "")
		{
			style2.border = "solid 2px #FF0000";
		}
		else
		{
			style2.border = "";
		}
	}
}

function hideDivRedBorder(div)
{
	if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		style2.border = "";
	}
}

function showDivStockDescription(obj, div) {              
    obj = document.getElementById(obj);
    
    //alert(obj);
    if (document.getElementById && document.getElementById(div)!= null)
	{   
		var style2 = document.getElementById(div).style;
		var leftMargin = 15;
		var topMargin = 5;
		style2.display = "block";	
		
		menuPos =  findPos (obj); // (mouseX(event) - leftMargin);
		style2.left = (menuPos[0] + leftMargin) + "px";
		style2.top = (menuPos[1] + topMargin) + "px";
//		alert(menuPos);
//		alert(style2.left);
//		alert(style2.top);
	}
}




function hideDivStockDescription(div) {
    clearTimeout(divStockTimeout);
    if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		style2.display = "none";
	}
}

function hideDivs(divArray){
    for(i=0 ;i<divArray.length ; i++){
         var div=divArray[i];
         hideDiv(div);
    }
}

function initializec(latitud,longitud,zoom) 
{
      // Inicializa el mapa de los contactos	 		
      if (GBrowserIsCompatible()) {	     	 
		
        var map = new GMap2(document.getElementById("map_canvasc"));  
        map.setCenter(new GLatLng(latitud, longitud), zoom); // Centro y zoom
    
		// Marcadores de zoom y movimiento
        map.addControl(new GMapTypeControl());
		map.addControl(new GLargeMapControl());
						
		// Toma los datos de las coordenadas del xml para generar los puntos
            GDownloadUrl("index.aspx?p=GeneratorXmlContact", function(data) {
          	var xml = GXml.parse(data);
          	var markersc = xml.documentElement.getElementsByTagName("marker");
		 	 //Toma las coord del punto
         	 for (var i = 0; i < markersc.length; i++) {
         	    var point = new GLatLng(parseFloat(markersc[i].getAttribute("lat")),
                                    parseFloat(markersc[i].getAttribute("lng")));
                var icono = markersc[i].getAttribute("icono");
				var name = markersc[i].getAttribute("name");			
				var morada = markersc[i].getAttribute("morada");	
				var phone = markersc[i].getAttribute("phone");			
				var email = markersc[i].getAttribute("email");				

		 	 	map.addOverlay(createMarkerContacts(point,icono,name,morada,phone,email));		  				
         	 }
          });
      }//end if
	  }//end initialize	  
	 
function createMarkerContacts(point,icono,name,morada,phone,email) 
{		
		  		  
    	  /* Crear un nuevo icono	            
	  	      var icon = new GIcon();
	          icon.image = icono;
    	      //icon.shadow = "http://www.google.com/mapfiles/turkeyshadow.png";
      	      icon.iconSize = new GSize(25, 25);
              //icon.shadowSize = new GSize(91, 62);
              icon.iconAnchor = new GPoint(17, 39);
              icon.infoWindowAnchor = new GPoint(15, 5);
              var marker = new GMarker(point,icono);*/
          
              var marker = new GMarker(point);
          
    	  GEvent.addListener(marker,"click", function() {
	        //Formato de la tabla en HTML
		    var myHtml = '<div style="padding-left:12px; width:200px;"><table><tr><td valign=top;><span class="Titulo">' + name + '</span><br/><td></tr><tr><td><span class="Complementos">'+ morada +'</span></td></tr><tr><td><span class="Complementos">'+ phone +'</span></td></tr><tr><td><span class="Complementos">'+ email +'</span></td></tr></table></div>';        
			marker.openInfoWindowHtml(myHtml);
    	  });
		  return marker;
}	          	
        
function initialize() 
{
		
   if (GBrowserIsCompatible()) {	  	 
		
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(39.68551, -8.085938), 6); // Centro y zoom. Fijo en el inicio        

		// Marcadores de zoom y movimiento
        map.addControl(new GMapTypeControl());
		//map.addControl(new GOverviewMapControl());
		map.addControl(new GLargeMapControl());
						
		// Toma los datos de las coordenadas del xml para generar los puntos
          GDownloadUrl("index.aspx?p=GeneratorXml", function(data) { 
          	var xml = GXml.parse(data);
          	var markers = xml.documentElement.getElementsByTagName("marker");
		 	 //Toma las coord del punto
         	 for (var i = 0; i < markers.length; i++) {
         	   var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                    parseFloat(markers[i].getAttribute("lng")));

		 	 // Toma el resto de datos del Xml
  				var icono = markers[i].getAttribute("icono");
				var name = markers[i].getAttribute("name");
				var tlf = markers[i].getAttribute("tlf");	
				var morada = markers[i].getAttribute("morada");	
				var image = markers[i].getAttribute("image");			
				var email = markers[i].getAttribute("email");
						
				map.addOverlay(createMarker(point, name,morada,tlf,image,email,icono));//i + 1,		  				
         	 }
          });
      }//end if
}//end initialize
	  
function initialize2(latitud,longitud,zoom) 
{
	  // Creo inicializar 2 para generar el mismo mapa pero cambiando el centro!! 
	 		
      if (GBrowserIsCompatible()) {	  	 
		
        var map = new GMap2(document.getElementById("map_canvas"));        
        map.setCenter(new GLatLng(latitud,longitud), zoom); // Centro y zoom

		// Marcadores de zoom y movimiento
        map.addControl(new GMapTypeControl());
		//map.addControl(new GOverviewMapControl()); Map aux
		map.addControl(new GLargeMapControl());
						
		// Toma los datos de las coordenadas del xml para generar los puntos
          GDownloadUrl("index.aspx?p=GeneratorXml", function(data) {
          	var xml = GXml.parse(data);
          	var markers = xml.documentElement.getElementsByTagName("marker");
		 	 //Toma las coord del punto
         	 for (var i = 0; i < markers.length; i++) {
         	   var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                    parseFloat(markers[i].getAttribute("lng")));

		 	 // Toma el resto de datos del Xml
  				var icono = markers[i].getAttribute("icono");
				var name = markers[i].getAttribute("name");
				var tlf = markers[i].getAttribute("tlf");	
				var morada = markers[i].getAttribute("morada");	
				var image = markers[i].getAttribute("image");			
				var email = markers[i].getAttribute("email");
						
				map.addOverlay(createMarker(point, name,morada,tlf,image,email,icono));//i + 1,		  				
         	 }
          });
      }//end if
}//end initialize

	// Crea las  marcas con un texto determinado  
function createMarker(point,name,morada,tlf,image,email,icono) 
{
	  // Crear un nuevo icono
	  	  var icon = new GIcon();
	      icon.image = icono;
    	  //icon.shadow = "http://www.google.com/mapfiles/turkeyshadow.png";
      	  icon.iconSize = new GSize(25, 25);
          //icon.shadowSize = new GSize(91, 62);
          icon.iconAnchor = new GPoint(17, 39);
          icon.infoWindowAnchor = new GPoint(15, 5);
		  
    	  var marker = new GMarker(point,icon);
	     
    	  GEvent.addListener(marker,"click", function() {
	        //Formato de la tabla en HTML
		var myHtml = '<table><tr><td><img src='+ image +'>'+'</td><td><div ><table><tr><td valign=top;>'+'<span class="Titulo">' + name + '</span><br/><td></tr><tr><td><span class="Complementos">'+ morada +'</span></td></tr><tr><td><span class="Complementos">'+ tlf + '</span></td></tr><tr><td><span class="Complementos">'+ email +'</span></td></tr></table></div></td></tr></table>'
        
			marker.openInfoWindowHtml(myHtml);
    	  });
		  return marker;
}	        
