﻿// Archivo JScript
function $(a)
{
    return document.getElementById(a);
}

// Archivo JScript
function visualitzaSubmemu(num)
{        
    /* close all submenus */
    var lis = document.getElementsByTagName('li')
    {
        for (var i=0; i<lis.length; i++)
        {
            if (lis[i].id.indexOf('sub') == 0)
            {
                lis[i].style.display = 'none';
            }
        }
    }
    
    var subMenu = document.getElementById('sub' + num);
    if (subMenu != null)
    {
        if (subMenu.style.display == 'none')
        {
            subMenu.style.display = '';
        }
        else
        {
            subMenu.style.display = 'none'
        }
    }
}

function ConfirmDelete()
{
    return confirm('¿Deseas suprimir esta línea?');
}

var map = null;
var geocoder = null;

function preload()
{
    if (GBrowserIsCompatible()) 
    {
        map = new GMap2(document.getElementById("map"));
    }
}

function load() {

  var contactHTML = 'C/ Diputación, 235, 3<sup>º</sup><br />08007 BARCELONA<br /><br /><b>Tel.:</b> 902 32 36 36<br /><b>Fax:</b> 902 36 59 07';
  
  if (GBrowserIsCompatible()) {
  
    var icon = new GIcon();
    icon.image = "images/contract-ico.gif";
    icon.iconSize = new GSize(32, 32);
    icon.iconAnchor = new GPoint(6, 20);
    icon.infoWindowAnchor = new GPoint(5, 1);

  
  
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());       
    map.addControl(new GScaleControl());      
    //map.addControl(new GOverviewMapControl());
    
    map.setCenter(new GLatLng(41.388330, 2.16405), 14);
    //geocoder = new GClientGeocoder();
    
    // Add marker to CV HQ
    var point = new GLatLng(41.388330, 2.16405);
    var marker = new GMarker(point, icon);
    
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(contactHTML);
      });

    map.addOverlay(marker);
  }
}

function AddMarker(x, y, textAddress, icon)
{
    var point = new GLatLng(x, y);
    var marker = new GMarker(point, icon);    
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(textAddress);
      });
      
    return marker;
}

function showAddress(address) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          //alert(address + " not found");
        } else {          
          // the assignment x = y and y = x is correct becauce google keeps ordered pair like [y,x] ... ?!?!
          $('mapX').value = point.y;
          $('mapY').value = point.x;          
          document.forms[0].submit();
          //map.setCenter(point, 13);
          //var marker = new GMarker(point);
          //map.addOverlay(marker);
          //marker.openInfoWindowHtml(address);
        }
      }
    );
  }
}

/**** NEWS ***/
function RotateNews(name)
{
    var array = newsArray[name];
    
    if (array[array.length - 1] == array.length - 1)
    {
        array[array.length - 1] = 0;
    }
    
    // display news
    var newsHolder = document.getElementById(name + '_newsHolder');
    newsHolder.innerHTML = array[array[array.length - 1]];
    
    array[array.length - 1]++;
    
    
    setTimeout("RotateNews('" + name + "')", 4000);
}

// TODO: Optimize with initarray
var newsArray = new Array();
function InitArray()
{
    newsArray = new Array();
}

/**** end NEWS ***/

/*** Reports ****/

function ShowReport(listID)
{
    var list = $(listID);
    window.open('../Herramientas/ReportViewer.aspx?Id=' + list.selectedValue);
    
}

/*** end Reports ***/

/**** Banner multiControl ***/

function LoadBanners(image, link, srcs, time, index)
{
    // Find controls
    var imgctrl = document.getElementById(image);
    var linkctrl = document.getElementById(link);
    
    var source = srcs.split(';');
    
    // Change source
    if (source[index] == '')
    {
        index = 0;
    }
    
    // Fade in 
    fadeOut(image, 100);
    imgctrl.src = 'Images/Enlaces/Banners/' + source[index].split('|')[0].replace(' ','');
    fadeIn(image, 0);
    
    linkctrl.href = source[index].split('|')[1].replace(' ','');
    
    // Rotate news
    index = (index == source.length - 1 ? 0 : index + 1);
    setTimeout("LoadBanners('" + image + "','" + link + "','" + srcs + "'," + time + "," + index + ")", time);
}

function fadeIn(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 10;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
    }
  }
}

function fadeOut(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity > 0) {
      setOpacity(obj, opacity);
      opacity -= 10;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
    }
  }
}

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

/**** end Banner multiControl ***/

function writeHomeVideoPlayer(object)
{
    document.write('<object id="pruebaVideoFlan" width="162" height="155" align="middle" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">\n');
    document.write('<param value="http://www.supermotortv.com/player400.swf" name="movie" />\n');
    document.write('<param value="high" name="quality" />\n');
    document.write('<param value="transparent" name="wmode" />\n');
    document.write('<param value="#ffffff" name="bgcolor" />\n');
    document.write('<param value="20" name="volume" />\n');
    document.write('<param value="peli=' + object + '" name="FlashVars" />\n');
    document.write('<embed width="162" height="155" align="middle" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" name="pruebaVideoFlan" bgcolor="#DBE1E4" quality="high" volume="20" flashvars="peli=' + object + '" src="http://www.supermotortv.com/player400.swf" wmode="transparent" />');
    document.write('</object>\n');
}

function writeTVPlayer(object)
{
    document.write('<object id="pruebaVideoFlan" width="506" height="400" align="middle" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">\n');
    document.write('<param value="http://www.supermotortv.com/player400.swf" name="movie" />\n');
    document.write('<param value="high" name="quality" />\n');
    document.write('<param value="transparent" name="wmode" />\n');
    document.write('<param value="#ffffff" name="bgcolor" />\n');
    document.write('<param value="20" name="volume" />\n');
    document.write('<param value="peli=' + object + '" name="FlashVars" />\n');
    document.write('<embed width="506" height="400" align="middle" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" name="pruebaVideoFlan" bgcolor="#DBE1E4" quality="high" volume="20" flashvars="peli=' + object + '" src="http://www.supermotortv.com/player400.swf" wmode="transparent" />');
    document.write('</object>\n');
}

/* JS per al random d'imatges de la capsalera */

/* Vector de totes les Imatges (ruta a partir del html que es mostrara)*/
var imgPathArray = new Array("./Images/img_random/img01.png",
						 "./Images/img_random/img02.png",
						 "./Images/img_random/img03.png",
						 "./Images/img_random/img04.png",
						 "./Images/img_random/img05.png",
						 "./Images/img_random/img06.png");

var imgCarPathArray = new Array("./Images/img_random/coche01.png",
						 "./Images/img_random/coche02.png",
						 "./Images/img_random/coche03.png");


var imgArray = new Array(imgPathArray.length);
var imgCarArray = new Array(imgCarPathArray.length);

function preloadImages() 
{ 
  for(i=0; i<imgPathArray.length; i++)
  { 
    imgArray[i]=new Image; 
    imgArray[i].src=imgPathArray[i];
  }
   
  for(i=0; i<imgCarArray.length; i++)
  { 
    imgCarArray[i]=new Image; 
    imgCarArray[i].src=imgCarPathArray[i];
  }
}
													 
/* Temps de delay (en milisegons 1000 = 1segon) */
var delayTime = 2000;

/* Contador */
var counter = 0;

/* Rang de Possibilitats */
var minRang = 0;
var maxRang = imgPathArray.length - 1;
		
/* Funcio per iniciar el Timer */
function startSlide()
{
    preloadImages();
	//setInterval("slideShow()",delayTime);
	setInterval("slideShowRandom()",delayTime);
}

/* Funcio per Canviar les imatges */
function slideShow(){	
	if(counter == maxRang)	counter = 0;
	document.getElementById("img01").src = imgArray[counter++];
	if(counter == maxRang)	counter = 0;
	document.getElementById("img02").src = imgArray[counter++];
	if(counter == maxRang)	counter = 0;
	document.getElementById("img03").src = imgArray[counter++];
}

/* Funcio per Canviar les imatges amb Random*/
function slideShowRandom(){
	
	var pos1 = getAleatori();
	var pos2 = getAleatori();
	var pos3 = getAleatori();
	
	var imgSrc01 = document.getElementById("img01");
	var imgSrc02 = document.getElementById("img02");
	var imgSrc03 = document.getElementById("img03");	
	
	// Aixi mai hi haura dues imatges iguals
	while(pos1 == pos2 || pos2 == pos3 || pos3 == pos1){
		pos1 = getAleatori();
		pos2 = getAleatori();
		pos3 = getAleatori();
	}
		
	imgSrc01.src = imgArray[pos1].src;
	imgSrc02.src = imgArray[pos2].src;
	imgSrc03.src = imgArray[pos3].src;
		
	var imgCarSrc = document.getElementById("imgcar");
	var pos4 = getAleatoriMax(imgCarPathArray.length - 1);
	imgCarSrc.src = imgCarArray[pos4].src;
	
	window.status = "Listo";
}

/* Funcio per calcular un nombre aleatori en un rang */
function getAleatori(){
	var numPosibilitats = maxRang - minRang;
	var numAleatori = Math.random() * numPosibilitats;
	var numAleatori = Math.round(numAleatori);
	return parseInt(minRang) + numAleatori;
}

function getAleatoriMax(maxVal){
	var numPosibilitats = maxVal;
	var numAleatori = Math.random() * numPosibilitats;
	var numAleatori = Math.round(numAleatori);
	return numAleatori;
}

/* Abrir e imprimir el PDF de alta*/

var winPDF = null;
var winPDFNumber = 0;

function printPDFAlta(url)
{
  winPDFNumber += 1;
  winPDF = window.open(url,'pdfWindow' + winPDFNumber.toString(),'width=800,height=600,resizable=no,menubar=yes,toolbar=yes,status=yes,location=yes'); 
  if (window.focus) {winPDF.focus()} 
  var x = 0;
  var y = 0;
  if (self.screen.width > 800)
  {
    x = (self.screen.width - 800) / 2;
  }
  else
    x = 0;   
  if (self.screen.height > 768)
  {
    y = (self.screen.height - 600) / 2;
  }
  else
    y = 0;
  winPDF.moveTo(x,y);
}

function agregarFavoritos(){
   if ((navigator.appName=="Microsoft Internet Explorer") && 
         (parseInt(navigator.appVersion)>=4)) {
      var url="http://www.ancove.es/Default.aspx"; 
      var titulo="ANCOVE - Asociación Nacional de Comerciantes de Vehículos";
      window.external.AddFavorite(url,titulo);
   } else { 
      if(navigator.appName == "Netscape") 
         alert("Presione Crtl+D para agregar este sitio en sus Marcadores"); 
   }
} 
