Shadowbox.init();
var TICKER_CONTENT = null;
var TICKER_SPEED = 1;
var timeOutId = '';

$(document).ready(function(){
    $logo = $('a#logo-mask');
    $('ul#menu_menu li').hover(function(){
        $logo.hide();
    }, function(){
        $logo.show();
    });
    /*$("div#menu_menu ul li").hover( function() {
        $(this).addClass("iehover");
    }, function() {
        $(this).removeClass("iehover");
    } );*/
    
    $("ul.sf-menu").superfish({
        pathClass:  'current',
        /*onShow: function(){
            $logo.hide();
        },
        onHide: function() {
            $logo.show();
        },*/
        delay: 0
    });
    $('ul#menu_menu li').reverseOrder();
    //$('ul#menu_menu li ul li').reverseOrder();
    
    $('div.timetable div.activities').selectoul({
        callback: getActivities
    });
    
    var optionsPopeye = {
        /*caption:    false,
        navigation: 'permanent',*/
        direction:  'left'
    }
    $('div.notice-gallery, div.event-gallery').popeye(optionsPopeye);

    $(".slidetabs").tabs(".images > div", {
        // enable "cross-fading" effect
        effect: 'fade',
        fadeOutSpeed: "slow",

        // start from the beginning after the last tab
        rotate: true
    // use the slideshow plugin. It accepts its own configuration
    }).slideshow({
        autoplay: true,
        interval: 7000
    });

    $('a.btn_login').click(function(){
        $('#slide_login').slideDown();
        $('.slide_login_hidden').slideUp('fast',function(){        });
    });
    $('a.btn_login_close').click(function(){
        $('#slide_login').slideUp('1000');
    /*$('#slide_login').slideUp('1000',function(){
            $('.slide_login_hidden').slideDown('1000');
        });*/
    });

    //howtocome
    $('#search').click(function(){
        var inicio = $('#direccion').val();
        var fin = 'Cami Tarragona, 1, Reus';
        window.open("http://maps.google.com/maps?lsm=1&daddr=" + fin + "&geocode=&dirflg=&saddr=" + inicio + "&f=li&hl=es");
    });

    $('.datepicker').datepicker({
        dateFormat: 'dd-mm-yy',
        showOn: "button",
        buttonImage: "/img/calendar.png",
        buttonImageOnly: true/*,
        firstDay: 1,
        monthNames: ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'],
        dayNamesMin: ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sá']*/
    });

    $('#ReservationDay').change(function(aux){
        var old_value = aux.target.defaultValue
        var href = window.location.href;
        if(href.indexOf('/' + old_value) > 0) {
            href = href.replace('/' + old_value, '');
        }
        window.location.href = href + '/' + $(this).val();
    });

    /*$('div.results').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
                speed:    300,
    timeout:  6000
	});*/
    if($('div.results div').length > 1) {
        $("div.results").tabs("div.results > div", {
            // enable "cross-fading" effect
            effect: 'fade',
            fadeOutSpeed: "slow",

            // start from the beginning after the last tab
            rotate: true
        // use the slideshow plugin. It accepts its own configuration
        }).slideshow({
            interval: 6000,
            autoplay: true
        });
    }

    /*$('ul#activities-list li a').click(function(event){
        event.preventDefault();
        $('ul#activities-list a').removeClass('selected');
        $(this).addClass('selected');
        getActivities();
    });

    $('a#expand-activities-list').click(function(event){
        event.preventDefault();
        $(this).toggleClass('hover');
        if($(this).hasClass('hover')){
            $('div.timetable div.activities').slideDown(500);
        } else {
            $('div.timetable div.activities').slideUp(500);
        }
    });*/

    $('#dayActivity').change(function(){
        getActivities();
    });
    getActivities();

    $("ul.tabs").tabs("div.panes > div");
    $('div.activity').hover(function(event){
        $('div.activity').removeClass('obscure');
        $('div.activity:not(div.' + $(this).attr('rel') + ')').addClass('obscure');
    },function(event){
        //$('div.activity:not(div.' + $(this).attr('rel') + ')').removeClass('obscure');
        });

    getPoll();

    if($("div.promo-container").length > 0) {        
        $("div.promo-container").overlay({
            top: 120,
            mask: {
                color: '#000',
                loadSpeed: 200,
                opacity: 0
            },
            load: true,
            onLoad: function(){
                $('div.promo-container').galleria({
                    width:660,//650,//650
                    height:420,//$('.promo-container ul.banners li img').length > 1 ? 420 : 400,//350,//300?400?
                    autoplay:true,
                    /*thumbnails: $('.promo-container ul.banners li img').length > 1 ? "" : false,
                    showImagenav: $('.promo-container ul.banners li img').length > 1 ? "" : false*/
                    extend: function(options) {
                        var gallery = this; // "this" is the gallery instance                        
                        $(gallery._target).append('<div class="button-close"><a href="#">&nbsp;</a></div>');
                        $(gallery._target).find('div.button-close a').click(function(e){
                            e.preventDefault();
                            $("div.promo-container").data("overlay").close();                            
                        });
                        if($(gallery._target).find('div.galleria-thumbnails-container div.galleria-image img').length <= 1){
                            $(gallery._target).find('div.galleria-container').css('height','360px');
                            $(gallery._target).find('div.galleria-images').css('bottom','5px');
                            $(gallery._target).find('div.galleria-stage').css('bottom','5px');
                            $(gallery._target).find('div.galleria-thumbnails-container').css('height','0px').css('display','none');
                        }

                    }
                });
            },
            onClose: function(){
                stopGallery();                
            }
        });
    }
    function stopGallery(){
        var gallery = Galleria.get(0);
        gallery.pause();
    }
    $('a.showhide').click(function(event){
        event.preventDefault();
        var $con = $(this).parents('.sch');
        if($con.find('a.all').hasClass('on')){
            $con.find('div.activity').toggleClass('d-none');
        }

        if(!$(this).hasClass('all')) {
            $con.find('a.all').removeClass('on');
        } else {
            $con.find('a:not(.all)').removeClass('on');
        }

        $(this).toggleClass('on');
        if(!$(this).hasClass('on')) {
            $con.find('div.activity.' + $(this).attr('rel')).addClass('d-none');
        }

        if($(this).hasClass('all') || $con.find('ul a.on').length < 1) {
            $con.find('div.activity').removeClass('d-none');
            $con.find('div.activity').removeClass('obscure');
            $con.find('a.all').addClass('on');
            //$('#ActivityId').val('all');
            $con.find('input.activities-for-print').val('all');
        } else {
            var activities = '';
            $.each($con.find('a.on'), function(){
                if(activities != '') {
                    activities += ',';
                }
                activities += $(this).attr('rel');
                $con.find('div.activity.' + $(this).attr('rel')).removeClass('d-none');
            });
            //$('#ActivityId').val(activities);
            $con.find('input.activities-for-print').val(activities);
            $con.find('div.activity.' + $(this).attr('rel')).removeClass('obscure');
            $con.find('div.activity:not(.' + $(this).attr('rel') + ')').addClass('obscure');
        }
        /*$con.find('div.activity').removeClass('d-none obscure');
        if($(this).attr('rel') != 'all') {
            $con.find('div.activity:not(div.' + $(this).attr('rel') + ')').toggleClass('d-none');
        }*/
    });

    $('div.panes a.print').click(function(event) {
        event.preventDefault();
        window.open($(this).attr('href') + '/activity_id:' + $(this).parent().find('input.activities-for-print').val());
    });
});

function getActivities() {
    var day = $('#dayActivity').val();
    var activity = $('div.timetable div.activities .hidval').val();
    //var activity = $('ul#activities-list li a.selected').attr('rel');
    $.ajax({
        url: $('#urlGetActivities').val(),
        type: 'POST',
        data: {
            day: day,
            activity_id: activity
        },
        success: function(response){
            $('#marquee').html(response);

            $('#marquee a').click(function(event){
                event.preventDefault();
            });

            TICKER_CONTENT = document.getElementById("TICKER").innerHTML;
            TICKER_RIGHTTOLEFT = false;
            TICKER_SPEED = 1;
            TICKER_STYLE = "font-family:Arial;";
            TICKER_PAUSED = false;
            clearTimeout(timeOutId)
            ticker_start();
            
        /*$("#scroller").scrollable({
                circular: true,
                speed: 4000
            }).autoscroll({
                autoplay: true
            }).navigator();
            $('#marquee a').click(function(event){
                event.preventDefault();
            });*/
        }
    });
}

function ticker_start() {
    var tickerSupported = false;
    TICKER_WIDTH = document.getElementById("TICKER").style.width;
    //var img = "<img src=ticker_space.gif width="+TICKER_WIDTH+" height=0>";
    var img = '';

    // Firefox
    if (navigator.userAgent.indexOf("Firefox")!=-1 || navigator.userAgent.indexOf("Safari")!=-1) {
        document.getElementById("TICKER").innerHTML = "<TABLE  cellspacing='0' cellpadding='0' width='100%'><TR><TD nowrap='nowrap'>"+img+"<SPAN style='"+TICKER_STYLE+"' ID='TICKER_BODY' width='100%'>&nbsp;</SPAN>"+img+"</TD></TR></TABLE>";
        tickerSupported = true;
    }
    // IE
    if (navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Opera")==-1) {
        document.getElementById("TICKER").innerHTML = "<DIV nowrap='nowrap' style='width:100%;'>"+img+"<SPAN style='"+TICKER_STYLE+"' ID='TICKER_BODY' width='100%'></SPAN>"+img+"</DIV>";
        tickerSupported = true;
    }
    if(!tickerSupported) document.getElementById("TICKER").outerHTML = "";
    else {
        document.getElementById("TICKER").scrollLeft = TICKER_RIGHTTOLEFT ? document.getElementById("TICKER").scrollWidth - document.getElementById("TICKER").offsetWidth : 0;
        document.getElementById("TICKER_BODY").innerHTML = TICKER_CONTENT;
        document.getElementById("TICKER").style.display="block";
        TICKER_tick();
    }
}

function TICKER_tick() {
    if(!TICKER_PAUSED) document.getElementById("TICKER").scrollLeft += TICKER_SPEED * (TICKER_RIGHTTOLEFT ? -1 : 1);
    if(TICKER_RIGHTTOLEFT && document.getElementById("TICKER").scrollLeft <= 0) document.getElementById("TICKER").scrollLeft = document.getElementById("TICKER").scrollWidth - document.getElementById("TICKER").offsetWidth;
    if(!TICKER_RIGHTTOLEFT && document.getElementById("TICKER").scrollLeft >= document.getElementById("TICKER").scrollWidth - document.getElementById("TICKER").offsetWidth) document.getElementById("TICKER").scrollLeft = 0;
    timeOutId = window.setTimeout("TICKER_tick()", 30);
}

function getPoll(vote, poll, showresults){
    vote = !isset(vote) ? null : vote;
    poll = !isset(poll) ? null : poll;
    showresults = !isset(showresults) ? false : showresults;

    $('div.poll-place div.content').hide();
    $('div.poll-place div.loading').fadeIn();

    $.ajax({
        url: $('#urlGetPoll').val(),
        type: 'POST',
        data: {
            answer_id: vote,
            poll_id: poll,
            showresults: showresults
        },
        success: function(response){
            if(response != '') {
                $('div.poll-place div.loading').hide();
                $('div.poll-place div.content').html(response);
                $('div.poll-place div.content').fadeIn();
                $('div.poll a.vote').click(function(event){
                    event.preventDefault();
                    var vote = $(this).parents('div.container').find('input:checked').val();
                    var poll = $(this).parents('div.poll').attr('rel');
                    getPoll(vote, poll);
                });
                $('div.poll a.results, div.poll a.back').click(function(event){
                    event.preventDefault();
                    var poll = $(this).parents('div.poll').attr('rel');
                    getPoll(null, poll, true);
                });
                $('div.poll a.back').click(function(event){
                    event.preventDefault();
                    var poll = $(this).parents('div.poll').attr('rel');
                    getPoll(null, poll);
                });
            } else {
                $('div.poll-place').hide();
            }
        }
    });
}

function isset(value){
    return !(typeof(value) == 'undefined' || value === null);
}
