﻿var tcfId = 552; //214 // TemplateContainerFunction, utilizzata per la chiamata alla proxypage per la preview dinamica
var undef;
var current = 1;
var rnd = Math.floor(Math.random() * 1000001);
var idUser = 0;
var idLang = 'it-IT';
var playerFlashvars = {};
var playerParams = {};
var playerAttributes = {
    styleclass: "swfvideo"
};

var spett = {
    count: 1,
    current: 1,
    loop: null,
    pause: 10000,
    refresh: function () { $('#spettacoli').scrollTo('#sp' + spett.current, 0); },
    next: function () { spett.goTo(spett.current + 1); },
    prev: function () { spett.goTo(spett.current - 1); },
    goTo: function (n) {
        spett.reset(); // resetto il contatore per lo slideshow
        spett.closeMainVideo(); // chiudo il video dello spettacolo corrente se aperto
        var direction = (n > spett.current) ? 1 : 0;
        if (n > spett.count) {
            n = 1;
        } else if (n < 1) {
            n = spett.count;
        }

        spett.HUD.hideYArrow();
        spett.HUD.hideXArrow();

        if (direction == 1) {
            $('#sp' + spett.current).after($('#sp' + n));
        } else {
            $('#sp' + spett.current).before($('#sp' + n));
        }
        $('#spettacoli').scrollTo('#sp' + spett.current);

        $('#spettacoli').scrollTo('#sp' + n, 1000, function () {
            spett.HUD.showXArrow();
            spett.loadData();
        });
        spett.current = n;
    },
    loadData: function () {
        
        if($('#sp' + spett.current).attr('skip') != 1){
        	var ajaxurl;
        var cntId = $('#sp' + spett.current).attr('alt').replace('cnt', '');
        if (idUser > 0) {
            ajaxurl = '/proxypage.aspx?ts=&t=T&o=' + tcfId + '&idUser=1&idContent=' + cntId + '&idLang=' + idLang;
        } else {
            ajaxurl = '/static/v2010/generated/hp/panel/' + cntId + '.html?rnd=' + rnd;
            //ajaxurl = 'ajax/hp-sp'+ spett.current +'-panels.htm?rnd=' + rnd
        }

        $('#toppanel').html('');
        $('#bottompanel .center').html('');
        $.ajax({ url: ajaxurl,
            success: function (data) {
                $('#temp').html(data);
                $('#toppanel').html($('#temp #ajaxtop').html());
                $('#bottompanel .center').html($('#temp #ajaxbottom').html());
                $('#temp').html('');
                spett.HUD.showYArrow();
                $('.cal .day[title]').tipsy(); //tipsy non gestisce gli eventi live, deve riagganciarlo ogni volta
                $('.col3 .cal:gt(1)').hide();
                $('.col3 .mese a:first').css('font-weight', 'bold');
                $('.col2 .text').jScrollPane();
                $('.col1 .text').jScrollPane({ verticalGutter: 20 });
                //setTimeout("$('.col2 .text').jScrollPane();$('.col1 .text').jScrollPane({verticalGutter:40});",200);

            }
        });
        }
    },
    toggleTopPanel: function (force) {
        var opened = $('#toppanel').data('open');
        if (force === 0 || (force == undef && opened == 1)) {
            $('#toppanel').data('open', 0).animate({
                'marginTop': '-360px'
            }, 1000, 'swing', function () {
                $('#prev').fadeIn();
                $('#next').fadeIn();
                $('#down a').fadeIn();
                $('.prossimamenteLabel').fadeIn();
            });
            $('#up img').attr('src', '/includes/v2010/img/hp/arrow_up.gif');
            $('.logo').animate({
                'top': '80px'
            }, {
                "duration": 1000,
                "easing": "swing"
            });
            $('#up').animate({
                'top': '-=360px'
            }, {
                "duration": 1000,
                "easing": "swing"
            });
            spett.play();
        } else {
            spett.closeMainVideo(); //chiudo il video principale se aperto
            $('#toppanel').data('open', 1).animate({
                'marginTop': 0
            }, 1000, 'swing', function () {
                $('#prev').hide();
                $('#next').hide();
                $('#down a').fadeOut();
                $('.prossimamenteLabel').fadeOut();
            });
            $('#up img').attr('src', '/includes/v2010/img/hp/arrow_down.gif');
            $('.logo').animate({
                'top': '+=360'
            }, {
                "duration": 1000,
                "easing": "swing"
            });
            $('#up').animate({
                'top': '+=360px'
            }, {
                "duration": 1000,
                "easing": "swing"
            });
            spett.stop();
        }
    },
    toggleBottomPanel: function (force) {
        var H = $('#bottompanel').height();
        var opened = $('#bottompanel').data('open');
        if (force === 0 || (force == undef && opened == 1)) {
            $('#bottompanel').data('open', 0).animate({
                'bottom': '-=' + H + 'px'
            }, 1000, 'swing', function () {
                $('#prev').fadeIn();
                $('#next').fadeIn();
                $('#up').fadeIn();
                $('.prossimamenteLabel').fadeIn();
            });
            $('#overlay').fadeOut(800);
            $('#down img').attr('src', '/includes/v2010/img/hp/arrow_down.gif');
            $('#slider').animate({
                'top': '+=' + H + 'px'
            }, 1000, 'swing');
            spett.play();
        } else {
            spett.closeMainVideo(); //chiudo il video principale se aperto
            $('#bottompanel').data('open', 1).animate({
                'bottom': '+=' + H + 'px'
            }, 1000, 'swing', function () {
                $('#prev').fadeOut();
                $('#next').fadeOut();
                $('#up').fadeOut();
                $('.prossimamenteLabel').fadeOut();
            });
            $('#overlay').fadeTo(800, 0.8);
            $('#down img').attr('src', '/includes/v2010/img/hp/arrow_up.gif');
            $('#slider').animate({
                'top': '-=' + H + 'px'
            }, 1000, 'swing');
            spett.stop();
        }
    },
    toggleProssimamente: function (force) {
        var opened = $('.prossimamente').data('open');
        if (force === 0 || (force == undef && opened == 1)) {
            $('.prossimamente').data('open', 0).animate({
                'marginLeft': '-540px'
            }, 1000, 'swing', function () {
                $('#next').fadeIn();
                $('#up').fadeIn();
                $('#down a').fadeIn();
            });
            $('.prossimamenteLabel').animate({
                'left': '0'
            }, {
                "duration": 1000,
                "easing": "swing"
            });
            $('.logo').animate({
                'left': '-=540'
            }, {
                "duration": 1000,
                "easing": "swing"
            });
            $('#overlay').fadeOut(800);
            spett.play();
            $('#bottom .sponsor').show();
            $('#bottom .time').show();
        } else {
            spett.closeMainVideo(); //chiudo il video principale se aperto
            $('.prossimamente').data('open', 1).animate({
                'marginLeft': 0
            }, 1000, 'swing', function () {
                $('#next').hide();
            });
            $('#down a').fadeOut();
            $('#up').fadeOut();
            $('.prossimamenteLabel').animate({
                'left': '540px'
            }, {
                "duration": 1000,
                "easing": "swing"
            });
            $('.logo').animate({
                'left': '+=540'
            }, {
                "duration": 1000,
                "easing": "swing"
            });
            $('#overlay').fadeTo(800, 0.8);
            spett.stop();
            $('#bottom .sponsor').hide();
            $('#bottom .time').hide();
        }
    },
    HUD: {
        showXArrow: function () {
            $('#next').fadeIn('fast');
            $('#prev').fadeIn('fast');
        },
        hideXArrow: function () {
            $('#next').fadeOut('fast');
            $('#prev').fadeOut('fast');
        },
        showYArrow: function () {
            $('#up').fadeIn('fast');
            $('#down a').fadeIn('fast');
        },
        hideYArrow: function () {
            $('#up').fadeOut('fast');
            $('#down a').fadeOut('fast');
        }
    },
    play: function () {
        spett.stop();
        spett.loop = setInterval("spett.next()", spett.pause);
    },
    stop: function () { clearInterval(spett.loop); },
    reset: function () {
        spett.stop();
        spett.play();
    },
    openMainVideo: function () {
        $('#sp' + spett.current + ' .inscena .video').show(); //.slideDown('fast');
        var poster = $('#sp' + spett.current + ' .inscena .thumbvideo a').attr('name');
        var videoSrc = $('#videoSp' + spett.current).attr('href');
        spett.stop();
        //if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
        //	$('#videoSp' + spett.current).html('<video src="'+videoSrc+'" style="width: 100%; height: 100%; z-index: 1;" ></video>')
        //} else {
        flow.open('videoSp' + spett.current, videoSrc, poster);
        //}		

        /*swfobject.embedSWF($('#videoSp' + spett.current).attr('title'),
        "videoSp" + spett.current,
        "499",
        "308",
        "6.0.0",
        "expressInstall.swf",
        playerFlashvars, playerParams, playerAttributes);*/


    },
    closeMainVideo: function () {
        spett.play();
        $('#sp' + spett.current + ' .inscena .video').hide();
        $('#videoSp' + spett.current).empty();
        $('.inscena .thumbvideo a').one('click', function () {
            spett.openMainVideo();
            return false;
        });
    },
	galleryLoadImage:function(obj) {
	    $('#videoFull').hide();
	    $('#mmgalleryfull').removeClass('videofull');
	    $('#mmgalleryfull .full').addClass('loading');
	    // rimuovo l'eventuale immagine precedente
	    $('#mmgalleryfull .full .image').replaceWith('<div class="image" id="imageFull"> </div>');
	
	    var img = new Image();
	    $(img).load(function () {
	        $(this).hide();
	        $(this).attr('class', 'image');
	        $('#mmgalleryfull .full').removeClass('loading');
	        $('#mmgalleryfull .full .image').attr('src', $('.image', obj).attr('longdescr')).replaceWith(this);
	        $(this).fadeIn();
	    })
	    .error(function () {
	        // in caso di errore....
	    })
	    .attr('src', $('.image', obj).attr('longdescr'));
	    $('#mmgalleryfull .full .dida').html($('.dida', obj).html());
	},
	galleryLoadVideo:function(obj) {
	    var src = $('.image', obj).attr('longdescr');
	    $('#videoFull').attr('href', src);
	    $('#mmgalleryfull .full .image').hide();
	
	    $('#videoFull').show();
	    $('#mmgalleryfull').addClass('videofull');
	    // rimuovo l'eventuale immagine precedente
	    $('#imageFull').empty(); //replaceWith('<div class="image" id="imageFull"> </div>');
	    $('#mmgalleryfull').show();
	
	    flow.open('videoFull', src);
	    $('#mmgalleryfull .full .dida').html($('.dida2', obj).html());
	}
};

var layout = {
	setFullW:function(o) {
	    $(o).width($('#hpviewport').width());
	},
	setFullH:function(o) {
		if($.browser.opera){
			$('#spettacoli').css('position','static');
			$(o).height('100%');
		}
		else{
		    //var done = 0;
		    var h = $('#hpviewport').height();
		    $(o).height(h - 75);
		    /*if (h > 2000 && done === 0) { //hack per Chrome
		        setTimeout("$(o).height($(window).height());", 200);
		        done = 1;
		    } else {
		        $(o).height(h);
		    }*/
		}
	},
	resizeImg:function() {
	    var imgDefW = 1200;
	    var imgw = $('img.bg').width();
	
	    if (imgw <= 0) { imgw = imgDefW; }
	    var x = (imgw / 2) - ($(window).width() / 2);
	    $('img.bg').css('marginLeft', x * -1);
	},
	resizeItems: function() {
	    layout.setFullW('#hpviewport .spettacolo');
	    layout.setFullH('#hpviewport .spettacolo');
	    layout.resizeImg();
	    spett.refresh();
	},
	fixFooter:function() {
    if ($(window).height() < 600) {
        $('#hpviewport').height('560px');
    } else {
        $('#hpviewport').height('');
    }
}
}
$(window).load(function () {layout.resizeItems();});
//$(document).ready(function() {
$(document).ready(function() {
    spett.count = $('#spettacoli .spettacolo').size();
    $('#slider').width('8000px');
    layout.resizeItems();

    spett.HUD.hideYArrow();
    spett.loadData(); //la prima volta carico i panel manualmente 

    if (spett.count > 1) {
        spett.play();
    } else {
        $('#next').hide();
        $('#prev').hide();
    }

    $(window).resize(function () {
        layout.resizeItems();
    });


    // IE7 hack. Doppio set scroll per fixare il bug di posizionamento
    if (parseInt($.browser.msie && $.browser.version) == 7) {
        $(window).resize(function () {
            spett.refresh();
        });
    }

    // IE8 hack. fix footer
    if (parseInt($.browser.msie && $.browser.version) == 8) {
        layout.fixFooter();
        $(window).resize(function () {
            layout.fixFooter();
        });
    }

    setTimeout("layout.resizeItems();", 300);
    

    $('#up').click(function () {
        spett.toggleTopPanel();
        return false;
    });

    $('#down a').click(function () {
        spett.toggleBottomPanel();
        return false;
    });

    $('#next').click(function () {
        spett.next();
        return false;
    });

    $('#prev').click(function () {
        spett.prev();
        return false;
    });

    $('.prossimamenteLabel').click(function () {
        spett.toggleProssimamente();
        return false;
    });

    $('#toppanel .mm .item[class*=video]').live('click', function () {
        spett.galleryLoadVideo(this);
        return false;
    });

    $('#toppanel .mm .item:not([class*=video])').live('click', function () {
        spett.galleryLoadImage(this);
        $('#mmgalleryfull').fadeIn('fast');
        return false;
    });


    $('#mmbtnclose').live('click', function () {
        $('#videoFull').empty();
        $('#mmgalleryfull').fadeOut('fast');
        return false;
    });

    $('.inscena .thumbvideo a').one('click', function () {
        spett.openMainVideo();
        return false;
    });

    $('.inscena .video .close').click(function () {
        spett.closeMainVideo();
        return false;
    });

    $('.col3 .mese a').live('click', function () {
        $('.col3 .mese a').css('font-weight', 'normal');
        $(this).css('font-weight', 'bold');
        $('.col3 .cal').hide();
        $('.col3 .cal').eq($('.col3 .mese a').index($(this))).show();
        return false;
    });

 if ($('dl.castperdata').size() > 0) {
  $('dl.castperdata').parents('.box').hide()
}



});
