function videoGallerySetup(){
    //news lightbox open listener
    $(".history .extras-summary .extras-copy a.video").live('click',function(){
        showNewsVideo($(this).attr('rel'), $(this).siblings('h4').html(), $(this).siblings('h4').html(), $(this).attr('durv'));
        return false;
    });
    //lightbox open listener
    $(".video-gallery .gallery-thumbnail, .video-gallery .gallery-item h4 a").live('click',function(){
        gvShowLargeContent(this);
        return false;
    });
    //lightbox close listener
    $("#video-gallery-lightbox .gallery-lightbox-close").click(function(){
        gvHideLargeContent();
        return false;
    });
    //lightbox prev listener
    $("#video-gallery-lightbox .gallery-lightbox-prev").click(function(){
        gvPrevLargeContent();
        return false;
    });
    //lightbox next listener
    $("#video-gallery-lightbox .gallery-lightbox-next").click(function(){
        gvNextLargeContent();
        return false;
    });
    // change season listener
    $(".change-season").click(function(){
        gvChangeSeason(this);
        return false;
    }); 
    //setup pagination
    gvCreatePagination();
    //show/create first page
    gvShowPage(1);
}
function gvShowLargeContent(targ) {
    var contentIndexHash=String($(targ).attr('href'));
    var contentPosHash=contentIndexHash.indexOf("#");
    if(contentPosHash>=0){
        contentIndexHash=contentIndexHash.substr(contentPosHash);
    }
    var contentIndex=Number(contentIndexHash.substr(1,contentIndexHash.length-1));
    //set content
    gvSetLargeContent(contentIndex);
    //fade in
    $("#video-gallery-lightbox").css({left: '7570px', top: '4489px'}).fadeIn(750);
}
function gvSetLargeContent(itemIndex) {
    gvItemIndexCur=itemIndex;
    //set content
    //$("#video-gallery-lightbox .gallery-lightbox-share").html('SHARE');
    $("#video-gallery-lightbox .gallery-lightbox-content").html(gvItems[gvSeasonNumCur-1][itemIndex].content);
    $("#video-gallery-lightbox .gallery-lightbox-meta-1 span").html(gvItems[gvSeasonNumCur-1][itemIndex].caption);
    $("#video-gallery-lightbox .gallery-lightbox-meta-2 span").html(gvItems[gvSeasonNumCur-1][itemIndex].description);
    $("#video-gallery-lightbox .gallery-lightbox-meta-3 span").html(gvItems[gvSeasonNumCur-1][itemIndex].duration);
    $("#video-gallery-lightbox .gallery-lightbox-page.gallery-lightbox-prev").show();
    $("#video-gallery-lightbox .gallery-lightbox-page.gallery-lightbox-next").show();
}
function gvPrevLargeContent() {
    if(gvItemIndexCur>0){
        gvItemIndexCur=gvItemIndexCur-1;
    }else{
        gvItemIndexCur=gvItems[gvSeasonNumCur-1].length-1;
    }
    gvSetLargeContent(gvItemIndexCur);
}
function gvNextLargeContent() {
    if(gvItemIndexCur<gvItems[gvSeasonNumCur-1].length-1){
        gvItemIndexCur=gvItemIndexCur+1;
    }else{
        gvItemIndexCur=0;
    }
    gvSetLargeContent(gvItemIndexCur);
}
function gvHideLargeContent() {
    $("#video-gallery-lightbox").fadeOut(300,function(){
        //remove content in case of video/audio
        $("#video-gallery-lightbox .gallery-lightbox-content").html('');
    });
}
function gvShowPage(pageNum) {
    gvPageNumCur=pageNum;
    //update pagination
    gvUpdatePagination(pageNum);
    //does page exist?
    if($(".video-gallery.season-"+gvSeasonNumCur+" .video-gallery-page-"+pageNum).length==0){
        //page doesnt exist, create
        gvCreatePage(pageNum);
    }
    //hide current content
    var pageWrapper=$(".video-gallery.season-"+gvSeasonNumCur+" .gallery-pages");
    pageWrapper.fadeOut(500,function(){
        //set position
        var newXPos=0-((pageNum-1)*gvPageWidth);
        pageWrapper.css({
            left:newXPos+'px'
        })
        pageWrapper.fadeIn(500);
    });
}
function gvCreatePage(pageNum) {
    var pageIndex=pageNum-1;
    var pagePosX=pageIndex*gvPageWidth;
    var itemIndexStart=pageIndex*gvItemsPP;
    var itemIndexStop=Math.min(gvItems[gvSeasonNumCur-1].length-1,itemIndexStart+gvItemsPP);
    //create content
    var itemStr='<div class="gallery-page video-gallery-page-'+pageNum+'" style="left:'+pagePosX+'px">';
    for(var i=itemIndexStart;i<=itemIndexStop;i++){
        itemStr+='<div class="gallery-item video-gallery-item">';
        itemStr+='<a class="gallery-thumbnail" title="'+gvItems[gvSeasonNumCur-1][i].description+'" href="#'+i+'"><img src="'+gvItems[gvSeasonNumCur-1][i].thumb+'" /></a>';
        itemStr+='<h4><a href="#'+i+'">'+gvItems[gvSeasonNumCur-1][i].caption+'</a> <span class="meta">'+gvItems[gvSeasonNumCur-1][i].duration+'</span></h4>';
        itemStr+='</div>';
    }
    itemStr+='<div class="gallery-list-end"></div>';
    itemStr+='</div>';
    //create element
    $(".video-gallery.season-"+gvSeasonNumCur+" .gallery-pages").append(itemStr);
}
function gvUpdatePagination(newPageNum) {
    //update paging left
    if(newPageNum>1){
        $(".video-gallery-pagination.season-"+gvSeasonNumCur+" li .page-left").parent('li').show();
    }else{
        $(".video-gallery-pagination.season-"+gvSeasonNumCur+" li .page-left").parent('li').hide();
    }
    //update paging right
    if(newPageNum<gvPageNumMax){
        $(".video-gallery-pagination.season-"+gvSeasonNumCur+" li .page-right").parent('li').show();
    }else{
        $(".video-gallery-pagination.season-"+gvSeasonNumCur+" li .page-right").parent('li').hide();
    }
    //unset old active page
    $(".video-gallery-pagination.season-"+gvSeasonNumCur+" li a").removeClass('active');
    //set new active page
    $(".video-gallery-pagination.season-"+gvSeasonNumCur+" li a.page-num-"+newPageNum).addClass('active');
}
function gvCreatePagination() {
    var pageNums=Math.ceil(gvItems[gvSeasonNumCur-1].length/gvItemsPP);
    if(pageNums>1){
        //create pagination content
        var pStr='';
        for(var p=1;p<=pageNums;p++){
            pStr+='<li><a class="page-num page-num-'+p+'" href="#'+p+'">'+p+'</a></li>';
            //update max page number
            gvPageNumMax=p;
        }
        //create pagination elements
        $(".video-gallery-pagination.season-"+gvSeasonNumCur+" li").first().after(pStr);
        //setup click event
        $(".video-gallery-pagination.season-"+gvSeasonNumCur+" li a").live('click',function(){
            gvClickPage(this);
            return false;
        });
    }else{
        $(".video-gallery-pagination.season-"+gvSeasonNumCur+"").hide();
    }
}
function gvClickPage(targ) {
    var el=$(targ);
    var newPageNum=0;
    if(el.hasClass('page-left')){
        //prev
        newPageNum=gvPageNumCur-1;
    }else if(el.hasClass('page-right')){
        //next
        newPageNum=gvPageNumCur+1;
    }else{
        //number
        var pageHash=String(el.attr('href'))
        var contentPosHash=pageHash.indexOf("#");
        if(contentPosHash>=0){
            pageHash=pageHash.substr(contentPosHash);
        }
        newPageNum=Number(pageHash.substr(1,pageHash.length-1));
    }
    //within bounds?
    if(newPageNum>=1 || newPageNum<=gvPageNumMax){
        gvShowPage(newPageNum);
    }
}

function gvChangeSeason(targ){
    // Get target season info
    var seasonNumTarg = $(targ).parent('li').attr('rel');
    var seasonPlsTarg = $(targ).attr('rel');
    
    // Unset old active season
    $("#temp-menu-items li a").removeClass('active');
    // Set new active season
    $(targ).addClass('active');
    
    // Hide all paginations & galleries and show preloader
    $(".video-gallery.gallery").hide(300);
    $(".video-gallery-pagination").hide(300);
    //$("#video-gallery-preloader").show(300);
    
    // Store actual season info
    $(".video-gallery.gallery.season-"+gvSeasonNumCur).data('gvPageNumCur',gvPageNumCur);
    $(".video-gallery.gallery.season-"+gvSeasonNumCur).data('gvPageNumMax',gvPageNumMax);
    
    // If gallery is empty, proceed to fill with videos    
    if($(".video-gallery.gallery.season-"+seasonNumTarg+" .gallery-pages").is(":empty")){ 
        // Init gallery info
        gvItems[seasonNumTarg-1] = new Array();
        gvPageNumCur = 1;   
        gvPageNumMax = 1;
        gvSeasonNumCur = seasonNumTarg;
        // Get Youtube videos
        $.ajax({
            url: "https://gdata.youtube.com/feeds/api/playlists/"+seasonPlsTarg,
            dataType: 'json',
            data: {
                'v': 2, 
                'alt': 'jsonc', 
                'max-results': 50
            },
            async: false,
            success: 
            function(response){
                $.each(response.data.items, function(i, item) {
                    gvItems[seasonNumTarg-1].push({
                        caption: item.video.title,
                        thumb:'http://img.youtube.com/vi/'+item.video.id+'/0.jpg',
                        content:'<iframe width="720" height="350" src="http://www.youtube.com/embed/'+item.video.id+'?rel=0&amp;autoplay=1&amp;showinfo=0&amp;hd=1" frameborder="0" allowfullscreen></iframe>',
                        description: item.video.description,
                        duration: formatTime(item.video.duration) 
                    });
                });         
            }
        });
        //setup pagination
        gvCreatePagination();
        //show/create first page
        gvShowPage(1); 
    } else {
        // Gallery already filled, restore
        gvSeasonNumCur = seasonNumTarg;
        gvPageNumCur = $(".video-gallery.gallery.season-"+gvSeasonNumCur).data('gvPageNumCur');
        gvPageNumMax = $(".video-gallery.gallery.season-"+gvSeasonNumCur).data('gvPageNumMax');
    }
    // Hide preloader
    //$("#video-gallery-preloader").hide(300);
    $(".video-gallery.gallery.season-"+seasonNumTarg).show();
    $(".video-gallery-pagination.season-"+seasonNumTarg).show();
}
function formatTime(value) {
    var mm = Math.floor(value / 60);
    if (mm < 10) mm = "0" + mm;
    var ss = value % 60;
    if (ss < 10) ss = "0" + ss;
    return mm + ":" + ss;
}
function showNewsVideo(id, titulo, desc, duration){
    //set content
    $("#video-gallery-lightbox .gallery-lightbox-content").html('<iframe width="720" height="350" src="http://www.youtube.com/embed/'+id+'?rel=0&amp;autoplay=1&amp;showinfo=0&amp;hd=1" frameborder="0" allowfullscreen></iframe>');
    $("#video-gallery-lightbox .gallery-lightbox-meta-1 span").html(titulo);
    $("#video-gallery-lightbox .gallery-lightbox-meta-2 span").html(desc);
    $("#video-gallery-lightbox .gallery-lightbox-meta-3 span").html(duration);
    $("#video-gallery-lightbox .gallery-lightbox-page.gallery-lightbox-prev").hide();
    $("#video-gallery-lightbox .gallery-lightbox-page.gallery-lightbox-next").hide();
    
    //fade in
    $("#video-gallery-lightbox").css({left: '4460px', top: '2280px'}).fadeIn(750);
}
