//Funciones para crear el scroller
var theHandle = []; var theRoot = []; var theThumb = []; var theScroll = []; var thumbTravel = []; var ratio = [];

function instantiateScroller(count, id, right, top, width, height, speed){
	if(document.getElementById) {
		theScroll[count] = new ypSimpleScroll(id, right, top, width, height, speed);
	}
}
function createDragger(count, handler, root, thumb, minX, maxX, minY, maxY){

		var imagen_arriba ='<img src="img/up.gif" width="15" height="15">';
		var imagen_abajo = '<img src="img/dn.gif" width="15" height="15">';
		var imagen_cuadro ='<img src="img/thumb.gif" width="15" height="15">';
		var buttons = '<div class="up" id="up'+count+'"><a href="#" onmouseover="theScroll['+count+'].scrollNorth(\''+count+'\')" onmouseout="theScroll['+count+'].endScroll()" onclick="return false;">' + imagen_arriba + '</a></div><div class="dn"  id="dn'+count+'""><a href="#" onmouseover="theScroll['+count+'].scrollSouth(\''+count+'\')" onmouseout="theScroll['+count+'].endScroll()" onclick="return false;">' + imagen_abajo + '</a></div><div class="thumb" id="'+thumb+'" style="left: 135px; top: 15px;"><a href="#" onclick="theScroll['+count+'].endScroll()">' + imagen_cuadro + '</a></div>';
		document.getElementById(root).innerHTML = buttons + document.getElementById(root).innerHTML;

		theRoot[count]   = document.getElementById(root);
		theThumb[count]  = document.getElementById(thumb);
		var thisup = document.getElementById("up"+count);
		var thisdn = document.getElementById("dn"+count);
		theThumb[count].style.left = parseInt(minX+345) + "px";
		thisup.style.left = parseInt(minX+345) + "px";
		thisdn.style.left = parseInt(minX+345) + "px";
		theThumb[count].style.border =0;
		theThumb[count].style.top = parseInt(minY) + "px";
		thisup.style.top = 0 + "px";
		thisdn.style.top = parseInt(minY+maxY) + "px";
		//thisdn.style.top = 15 + "px";

		theScroll[count].load();

		//Drag.init(theHandle[count], theRoot[count]); //not draggable on screen
		Drag.init(theThumb[count], null, minX+345, maxX+345, minY, maxY);
		
		// the number of pixels the thumb can travel vertically (max - min)
		thumbTravel[count] = theThumb[count].maxY - theThumb[count].minY;

		// the ratio between scroller movement and thumbMovement
		ratio[count] = theScroll[count].scrollH / thumbTravel[count];

		theThumb[count].onDrag = function(x, y) {
			theScroll[count].jumpTo(null, Math.round((y - theThumb[count].minY) * ratio[count]));
		}
}
function addLoadEvent(fn) {
      var old = window.onload;
      if (typeof window.onload != 'function') {
         window.onload = fn;
      }
      else {
         window.onload = function() {
         old();
         fn();
         }
      }
   }
//Funcion para crear el mapa de google.maps
function loadGMap() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(43.258549, -2.944679), 15);
		map.disableScrollWheelZoom();
		//map.setUIToDefault();
		icon = new GIcon();
		icon.image = "http://www.google.com/gmm/images/blue_dot_circle.png";
		icon.iconSize = new GSize(38, 38);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);
		var point = new GLatLng(43.258549, -2.944679);
		map.addOverlay(new GMarker(point, icon));
	}	
}
//Funciones para detectar la rueda del raton
function handle(delta) {
	theScroll['0'].endScroll();
       if (delta < 0){
		theScroll['0'].scrollSouth('0');
	}

    else{
		theScroll['0'].scrollNorth('0');
	}
	timerScroll=setTimeout("theScroll['0'].endScroll()",400);
}
function wheel(event){
    var delta = 0;
    if (!event) /* For IE. */
            event = window.event;
    if (event.wheelDelta) { /* IE/Opera. */
            delta = event.wheelDelta/120;
            /** In Opera 9, delta differs in sign as compared to IE.
            */
            if (window.opera)
                    delta = -delta;
    } else if (event.detail) { /** Mozilla case. */
            /** In Mozilla, sign of delta is different than in IE.
            * Also, delta is multiple of 3.
            */
            delta = -event.detail/3;
    }
    /** If delta is nonzero, handle it.
    * Basically, delta is now positive if wheel was scrolled up,
    * and negative, if wheel was scrolled down.
    */
    if (delta)
            handle(delta);
    /** Prevent default actions caused by mouse wheel.
    * That might be ugly, but we handle scrolls somehow
    * anyway, so don't bother here..
    */
    if (event.preventDefault)
            event.preventDefault();
	event.returnValue = false;
}
//Event Listeners
if (window.addEventListener)
    /** DOMMouseScroll is for mozilla. */
    window.addEventListener('DOMMouseScroll', wheel, false);
	/** IE/Opera. */
	window.onmousewheel = document.onmousewheel = wheel;

//Load Events
addLoadEvent(function(){
		
		if(theScroll.length>0) {
		for(var i=0;i<theScroll.length;i++){
			createDragger(i, "handle"+i, "root"+i, "thumb"+i, theScroll[i].clipW, theScroll[i].clipW, 15, theScroll[i].clipH-30);
		}
		if(document.getElementById("map")){
			loadGMap();
		}
	}
})
