$(document).ready(function() {
    onContentReady();
});

function onContentReady(context) {
    if (context === undefined) {
        context = $('.Table_01')[0];
    }
    


    $('.mask', context).ajaxError(function() {
        $(this).fadeOut();
    });
    setButtonOnMouseOver(context);
    setButtonPointer(context);
    setModalWindows(context);
    simpleSlide();
    $('.auto-slider').each( function() {
        var grupo = $(this).attr('rel');
        window.setInterval("simpleSlideAction('.right-button', '" + grupo + "');", 15000);
    setDatepiker($(".datepicker", context));
});
}

function setButtonOnMouseOver(context){
    $('input[name=boton], img[name=boton], input[tagname=boton]', context)
    .each(function() {
        var src = $(this).attr('src');
        var ext = src.slice(-4);
        var nombre =  src.slice(0,-4);
        var hover = nombre.concat('_h', ext);
        this.imagenOver = new Image();
        this.imagenOver.src = hover;
        this.imagenOut = new Image();
        this.imagenOut.src = src;
    })
    .mouseover(function() {
        $(this).attr('src',this.imagenOver.src);
    })
    .mouseout(function(){
        $(this).attr('src',this.imagenOut.src);
    });

}

function setButtonPointer(context){
    $('input[name=boton], img[name=boton], input[tagname=boton]', context).css('cursor','pointer');
}


function setModalWindows(context){
    //select all the a tag with name equal to modal
    $('a[name=modal]', context).click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
                        
        //Get the A tag
        var id = $(this).attr('href');
     
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
     
        //Set height and width to mask to fill up the whole screen
        $('#mask').css({
            'width':maskWidth,
            'height':maskHeight
        });
         
        //transition effect     
        $('#mask').fadeIn(1000);    
        $('#mask').fadeTo("slow",0.8);  
     
        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
//        $(id).css('top',  $('#body').scrollTop() + 100);
        $(id).css('top',  winH/2-$(id).height()/2  + $(document).scrollTop() - 100);
        $(id).css('left', winW/2-$(id).width()/2);
     
        //si ya está cargada, la limpio
        $('.window').hide();
        
        //load content
        $(id).html($(id+'_a').html());
        
        //transition effect
        $(id).fadeIn(2000); 
     
        //thickbox
        tb_init('a.thickbox, area.thickbox, input.thickbox');
        //modal windows
        setModalWindows(id);
    });
     
    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
    });     
     
    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
        $('.window').html('');
    });         

}

function acomodarFooter(archivo){
    var footer_bottom;
    switch(archivo)
    {
    case "inicio":
      footer_bottom = 22;
      break;
    case "home":
      footer_bottom = 22;
      break;      
    case "discos":
      footer_bottom = 40;
      break;
    case "biografia":
      footer_bottom = 25;
      break;
    case "multimedia":
      footer_bottom = 24;
      break;
    case "contacto":
      footer_bottom = 15;
      break;
    case "registro":
      footer_bottom = 14;
      break;      
    }

    $('.footer').css('bottom',footer_bottom +'px');
    $('.publicidad').css('bottom',footer_bottom + 25 +'px');


}

function setearTapaDiscos(){ /*no se usa más*/
    $('.tapa_disco').click(function(){
        link = $(this).attr('id') + '.php';
    
        $.get(link,
            function(data) {
                $('.temas_discos').html(data);
                startPlayer();
//                $.getScript("js/soundmanager2.js");
//                $.getScript("js/360player.js");
//                $.getScript("js/berniecode-animator.js");
//                $.getScript("js/excanvas.js");
            },
            'html'
        );        
    });
}

function startPlayer(){
    soundManager.url = 'swf/'; // path to directory containing SM2 SWF
    soundManager.useFastPolling = true; // increased JS callback frequency, combined with useHighPerformance = true

    threeSixtyPlayer.config.scaleFont = (navigator.userAgent.match(/msie/i)?false:true);
    threeSixtyPlayer.config.showHMSTime = true;

    // enable some spectrum stuffs

    threeSixtyPlayer.config.useWaveformData = true;
    threeSixtyPlayer.config.useEQData = true;

    // enable this in SM2 as well, as needed

    if (threeSixtyPlayer.config.useWaveformData) {
        soundManager.flash9Options.useWaveformData = true;
    }
    if (threeSixtyPlayer.config.useEQData) {
        soundManager.flash9Options.useEQData = true;
    }
    if (threeSixtyPlayer.config.usePeakData) {
        soundManager.flash9Options.usePeakData = true;
    }

    // favicon is expensive CPU-wise, but can be used.
    if (window.location.href.match(/hifi/i)) {
        threeSixtyPlayer.config.useFavIcon = true;
    }

    if (window.location.href.match(/html5/i)) {
        // for testing IE 9, etc.
        soundManager.useHTML5Audio = true;
    }
}

function setDatepiker($elem) {
    $elem.datepicker({
        showOn: "both",
        buttonImage: "../imagenes/calendario.png",
        buttonImageOnly: true,
        dateFormat: 'dd-mm-yy',
        changeMonth: true,
        changeYear: true,
        yearRange: '1950:c',
        hideIfNoPrevNext: true
        
    });
}


