// JavaScript Document

$(document).ready(function() {
        
		/*// Movemos el banner de ofertas a la parte de arriba
		var contenido = $("#contenedor_banner").html();
		$("#contenedor_banner").html("");
		$("#contenedor_ofertas").html(contenido);*/
		 
		$("#banner .closebutton").click(function(){
                $("#banner").animate({'margin-top':'-72px'}, 500);
        });
        
        $(".selectBox").find("input:checked").parent().addClass('active');
        
        $(".selectBox input").click(function(){
                
                if(!$(this).is(':checked'))
                   {
                          $(this).parent("label").removeClass('active');
                          
                   }else{
                           $(this).parent("label").addClass('active');
                }

        });
        
/* Tooltips
--------------------------------------------------------------------------------------------------------*/
        /*$(".tip_trigger").hover(function(){
                tip = $(this).next('.tip');
                tip.show(); //Show tooltip
        }, function() {
                tip.hide(); //Hide tooltip                
        }).mousemove(function(e) {
                var mousex = e.pageX - 20; //Get X coodrinates
                var mousey = e.pageY + 15; //Get Y coordinates
                var tipWidth = tip.width(); //Find width of tooltip
                var tipHeight = tip.height(); //Find height of tooltip
                
                //Distance of element from the right edge of viewport
                var tipVisX = $(window).width() - (mousex + tipWidth);
                //Distance of element from the bottom of viewport
                var tipVisY = $(window).height() - (mousey + tipHeight);
                  
                if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
                        mousex = e.pageX - tipWidth - 20;
                } if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
                        mousey = e.pageY - tipHeight - 20;
                } 
                tip.css({  top: mousey, left: mousex });
        });*/
        
        
        $('.tip_trigger').live('mouseover mouseout mousemove click', function(e) {
                tip = $(this).next('.tip');  
                if (e.type == 'mouseover') {
                        tip.show(); //Show tooltip
                } else if(e.type == 'mouseout' || e.type == 'click'){
                        tip.hide(); //Hide tooltip      
                }
                else {
                        var mousex = e.pageX - 20; //Get X coodrinates
                        var mousey = e.pageY + 15; //Get Y coordinates
                        var tipWidth = tip.width(); //Find width of tooltip
                        var tipHeight = tip.height(); //Find height of tooltip
                        
                        //Distance of element from the right edge of viewport
                        var tipVisX = $(window).width() - (mousex + tipWidth);
                        //Distance of element from the bottom of viewport
                        var tipVisY = $(window).height() - (mousey + tipHeight);
                          
                        if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
                                mousex = e.pageX - tipWidth - 20;
                        } if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
                                mousey = e.pageY - tipHeight - 20;
                        } 
                        tip.css({  top: mousey, left: mousex });                          
                }
        });
        
        
        
        
        
/* Poup
--------------------------------------------------------------------------------------------------------*/
        //When you click on a link with class of poplight and the href starts with a # 
        $('a.poplight[href^=#]').click(function() {
                var popID = $(this).attr('rel'); //Get Popup Name
                var popURL = $(this).attr('href'); //Get Popup href to define size
                                
                //Pull Query & Variables from href URL
                var query= popURL.split('?');
                var dim= query[1].split('&');
                var popWidth = dim[0].split('=')[1]; //Gets the first query string value

                //Fade in the Popup and add close button
                $('#' + popID).show().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close" title="Cerrar">Cerrar</a>');
                
                //Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
                var popMargTop = ($('#' + popID).height() + 80) / 2;
                var popMargLeft = ($('#' + popID).width() + 80) / 2;
                
                //Apply Margin to Popup
                $('#' + popID).css({ 
                        'margin-top' : -popMargTop,
                        'margin-left' : -popMargLeft
                });
                
                //Fade in Background
                $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
                $('#fade').css({'filter' : 'alpha(opacity=40)'}).fadeIn(); //Fade in the fade layer 
                
                return false;
        });
        
        
        //Close Popups and Fade Layer
        $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
                $('#fade').fadeOut(); 
                $('.popup_block').hide(); //fade them both out
                $('#fade, a.close').remove();  
                return false;
        });
        
        /* TABS Y ACORDEONES
        --------------------------------------------------------------------------------------------------------*/
        $(".acordeon" ).accordion({autoHeight: false});
        $(".tab").tabs();
        
        // Los botones los convertimos en botones
        $("button,.boton").button();
        
        // Botones para imprimir
        $(".imprimir").click(function(){
                self.print();
        });
        
        /*if($("#contenedor_buscador") && $("#buscador_real")){
                $("#contenedor_buscador").html($("#buscador_real").html());
                $("#buscador_real").html("");
        }*/     
        
});

function geolocalizar_dispositivo() {
	if(navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(geolocalizar_dispositivo_ok,geolocalizar_dispositivo_error, {enableHighAccuracy:true,maximumAge:300000});
	}
	else {
		alert("No hay información sobre la ubicación");
	}
}

function geolocalizar_dispositivo_error(error) {
	alert(error.code + ' ' + error.message);
}

function geolocalizar_dispositivo_ok(position) {
	$("#latitud_dispositivo").val(position.coords.latitude);
	$("#longitud_dispositivo").val(position.coords.longitude);
	if($("#mapa_dispositivo")){
		$("#mapa_dispositivo").attr('src','http://dev.virtualearth.net/REST/v1/Imagery/Map/Road/'+position.coords.latitude+','+position.coords.longitude+'/17?mapSize=500,500&mapVersion=v1&key='+BING_MAPS_KEY);
	}
	else {

	}
}
