$(document).ready(function(){

//Popup

//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).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="images/fancy_close.png" class="btn_close" title="Cerrar ventana" alt="Cerrar" /></a>');

    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and 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=20)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

    return false;
});

//Close Popups and Fade Layer
$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
    $('#fade , .popup_block').fadeOut(function() {
        $('#fade, a.close').remove();  //fade them both out
    });
    return false;
});

//Iframe

$("#iframe").fancybox({
    'width': '90%',
    'height': '75%',
    'autoScale': false,
    'transitionIn': 'none',
    'transitionOut': 'none',
    'type': 'iframe'
});

//Galeria

$("a[rel=galeria]").fancybox({
    'transitionIn': 'elastic',
    'transitionOut': 'elastic',
    'titlePosition': 'outside',
    'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
            return '<span id="fancybox-title-over">Imagen ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
        }
    });

//Rollover

    $(".menu").mouseover(function(){
        var nombre=this.id;
        $("#"+nombre).removeClass(nombre);
         $("#"+nombre).addClass(nombre+"_over");
    });

    $(".menu").mouseout(function(){
        var nombre=this.id;
        $("#"+nombre).removeClass(nombre+"_over");
         $("#"+nombre).addClass(nombre);
    });
    
//Tooltip

        $(".tip_trigger").hover(function(){
        tip = $(this).find('.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 + 20; //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;
        }
        //Absolute position the tooltip according to mouse position
        tip.css({  top: mousey, left: mousex });
    });

});

//Link with Popup Windows

function popup(url,ventana,width,height,resizable)
{
                if(!resizable)
                resizable = "yes"
                w=width;
                h=height;
                LeftPosition=(screen.width)?(screen.width-w)/2:100;
                TopPosition=(screen.height)?(screen.height-h)/2:100;
                settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',menubar=no,toolbar=no,scrollbars=yes,titlebar=no,status=no,location=no,directories=no,resizable='+resizable;
                window.open(url,ventana,settings);
}

//Funciones para Contacto

function Validatecontacto(theForm)
{
if (theForm.nombre.value == "")
{
   alert("Por favor llene los datos requeridos en el campo: \"nombre\"");
   theForm.nombre.focus();
   return false;
}
if (theForm.telefono.value == "")
{
   alert("Por favor llene los datos requeridos en el campo: \"telefono\"");
   theForm.telefono.focus();
   return false;
}
var strValue = theForm.email.value;
var strFilter = /^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i;
if (!strFilter.test(strValue))
{
   alert("Por favor introduzca una direccion de correo valida.");
   theForm.email.focus();
   return false;
}
if (theForm.email.value == "")
{
   alert("Por favor llene los datos requeridos en el campo: \"email\"");
   theForm.email.focus();
   return false;
}
if (theForm.empresa.value == "")
{
   alert("Por favor llene los datos requeridos en el campo: \"empresa\"");
   theForm.empresa.focus();
   return false;
}
if (theForm.comentarios.value == "")
{
   alert("Por favor llene los datos requeridos en el campo: \"comentarios\"");
   theForm.comentarios.focus();
   return false;
}
return true;
}
