﻿// CLEAR DEFAULT 
jQuery.fn.resetDefaultValue=function(){function _clearDefaultValue(){var _$=$(this);if( _$.val()==this.defaultValue){_$.val('');}};function _resetDefaultValue(){var _$=$(this);if(_$.val()==''){_$.val(this.defaultValue);}};return this.click(_clearDefaultValue).focus(_clearDefaultValue).blur(_resetDefaultValue);}
// EQUAL HEIGHT
function equalHeight(el){var size = 0;$(el).each(function (){if ($(this).height() > size){size = $(this).height();}});$(el).height(size);}

//TIMELINE - VERIFY INDEX PANEL TO DISABLE NAVIGATION
function timelineNavigation(){
  //GET LAST PANEL INDEX
  var lastPanelIndex = $('.timeline-item:last').index()-1; 
  //GET FIRST PANEL INDEX
  var firstPanelIndex = $('.timeline-item:first').index()+1;
  
  var indexActual;
  $('.arrow').removeClass('disabled');
  $('.dummyNav').remove();
  //GET ACTUAL INDEX
  $('.timeline-item').each(function(){
    if ($(this).hasClass('activePage')){
      indexActual = $(this).index();
    }
  });
  
  if(indexActual == lastPanelIndex){$('.arrow.forward').addClass('disabled');$('<span class="dummyNav" style="z-index: 1000; position: absolute; top: 8px; right: 0px; height: 387px; width: 32px"></span>').appendTo($('.timeline-area'));}
  if(indexActual == firstPanelIndex){$('.arrow.back').addClass('disabled'); $('<span class="dummyNav" style="z-index: 1000; position: absolute; top: 8px; left: 0px; height: 387px; width: 32px"></span>').appendTo($('.timeline-area'));}
}

//COUNT HOW MANY EVENTS ITEMS IN EACH PANEL AND CALCULATE WIDTH
function calculateEventsWidth(){
  $('.panel').each(function(){
    if ($(this).children('.event').size() == 2){$(this).children('.event:eq(1)').css('width','388px');
    } else if ($(this).children('.event').size() == 1){ $(this).children('.event:eq(0)').css('width','597px');}
  });
}

//TIMELINE - VERIFY WHAT PANEL IS ACTIVE, AND ACTIVATE SEASON LINK
function isActive(){
  $(".seasons-links a").each(function(){
    if ($(this).text() == $('.timeline .activePage').attr('title')){
      $(".seasons-links a").removeClass('active');
      $(this).addClass('active');
    }
  });
}

//TIMELINE - KNOW WHERE TO GO WHEN SEASON LINK IS CLICKED  
function gotoPanel(index){
  var count = 0;
  var gotopanel;
  $('.timeline-item').each(function() {
    if (!$(this).hasClass('cloned') && $(this).hasClass('newseason')){
      if ($(this).attr('title') == index){
        gotopanel = count;
      }
    }
    count++;    
  });
  
  $('.timeline').anythingSlider(gotopanel);
}

$(document).ready(function () {
    //ORDERED LIST STYLING, ONLY WHEN INSIDE A CLEANSTYLE
    $('.cleanStyle ol li').wrapInner('<span class="inner"></span>').addClass('ol-number');
    //BLOCKQUOTE STYLING, ONLY WHEN INSIDE A CLEANSTYLE
    $('.cleanStyle blockquote').wrapInner('<span class="inner"></span>');
    //SELECT BOX CUSTOM STYLING
    if ($('.custom').length) { $('.custom').dropkick(); }

    //SET EQUAL HEIGHT IN FEATURED CONTENT IN HOMEPAGE
    if ($('.homepage-highlights-item-content').length) {
        
        if ($('.adaptHeight').length) { $('.adaptHeight').height($('.adaptHeight').parent().parent().height()); }
        $('.adaptHeight').each(function () {
            var containerWidth = $(this).parent().parent().width();
            var imageWidth = $(this).width();
            var width = (containerWidth - imageWidth) - 30;
            $(this).parent().prev().width(width);
        });
        equalHeight('.homepage-highlights-item-content');
    }

    //TABS INIT
    if ($('.tabs-homepage').length) {
        $('.tabs-homepage ul').show();
        $('.tabs-homepage h2').hide();
        $('.tabs-homepage').tabs();
    }
    if ($('.content-tabs').length) {
        $('.content-tabs h2.toHide').hide(); $('.content-tabs').tabs();

        $('.content-tabs a').height($('.ui-tabs-nav').height());

        if ($.browser.msie && parseInt($.browser.version) == 7) {
            $('.ui-tabs-nav a').wrapInner('<span class="inner"></span>');
            $('.content-tabs li .inner').each(function () {
                if (($(this).height() < $(this).parent().height()) && ($(this).height() <= 50)) {
                    $(this).css({ 'display': 'block', 'padding-top': ($(this).parent().height() / 2) - ($(this).height() / 2) - 2 });
                }
            });
        }
    }

    if ($('.accordion').length) {
        $('.accordion-content').hide();
        $('.accordion .title').addClass('accordion-title');
        $('.accordion-title').click(function () {
            if ($(this).hasClass('active')) {
                $(this).removeClass('active');
                $(this).next('.accordion-content').slideUp();
            } else {
                $('.accordion-content').slideUp();
                $('.accordion-title').removeClass('active');
                $(this).addClass('active');
                $(this).next('.accordion-content').slideDown();
            }

        });
    }

    if ($('.slideshow').length) {
        if ($('.slideshow-item').size() == 1) { $('.slideshow-item').addClass('onlyone-item'); }
        $('.homepage-shadow').show();
        $('.slideshow').height(360);
        $('.slideshow').anythingSlider({
            theme: "default",
            easing: 'swing',
            delay: 10000,
            animationTime: 500,
            autoPlayLocked: true,
            autoPlay: true,
            pauseOnHover: true,
            buildArrows: false,
            buildNavigation: true,
            buildStartStop: false
        });
    }


    if ($('.featured').length && !$('.featured').hasClass('no-slide')) {
        if ($('.featured .highlight').size() == 1) { $('.featured').addClass('onlyone-item'); }
        $('.featured').height(219);
        $('.featured').anythingSlider({
            theme: "default",
            easing: 'swing',
            delay: 10000,
            animationTime: 500,
            autoPlayLocked: true,
            autoPlay: true,
            pauseOnHover: true,
            buildArrows: false,
            buildNavigation: true,
            buildStartStop: false
        });
    }



    if ($('#timeline').length) {
        $('#timeline').addClass('timeline-area');
        $('.timeline-shadow').show();
        $('.timeline-years').show();
        $('.timeline .detail').hide();
        $('.timeline h2').remove();
        $('.timeline').anythingSlider({
            startPanel: 1,
            buildArrows: true,
            buildNavigation: false,
            forwardText: "&gt;",
            backText: "prev",
            autoPlay: false,
            stopAtEnd: true,
            delay: 10000,
            animationTime: 500,
            easing: 'swing',
            autoPlayLocked: false,
            buildStartStop: false,
            onSlideComplete: function (slider) {
                isActive();
                timelineNavigation();
            }
        });
        calculateEventsWidth();
        timelineNavigation();

        //CREATE SEASON LINKS
        $('.newseason').each(function () {
            if (!$(this).hasClass('cloned')) {
                if ($(this).attr('title') != '') {
                    $('<a href="#" title="" class="link">' + $(this).attr('title') + '</a>').appendTo('.seasons-links');
                }
                $(".seasons-links a:first").addClass('active');
            }
        });

        //WHEN SEASON IS CLICKED
        $(".seasons-links a").click(function () {
            gotoPanel($(this).text());
            $(".seasons-links a").removeClass('active');
            $(this).addClass('active');
            return false;
        });

        //EVERYTIME THE PAGINATION CHANGES
        $(".timeline-area .arrow").click(function () {
            isActive();




        });

        //WHEN CLICK ON EVENT SHOW SHADOWBOX
        $(".timeline .event").click(function () {
            $('#dialog').attr('title', $(this).children('h3').text());
            $('.ui-dialog-title').text($(this).children('h3').text());
            $('#dialog').html($(this).children('.detail').html());

            $("#dialog").dialog({
                modal: true,
                draggable: false,
                resizable: false,
                width: 610,
                height: 350,
                close: function (event, ui) {
                    $('#dialog').attr('title', '');
                }
            });
            return false;
        });
    }

    //CLEAN INPUT VALUES ON CLICK; IF INPUT LEFT EMPTY THE ORIGINAL VALUE WILL SHOW
    $('.clean-input').resetDefaultValue();

    //GALLERY MODAL BOX
    //  if ($('.gallery').length){
    //    $('.gallery a').click(function(){
    //      $('#gallery-dialog').attr('title','');
    //        
    //      if ($(this).children('.info').children('.type').attr('rel') == 'video'){
    //        $($(this).children('.info').children('.type').html()).appendTo('#gallery-dialog');
    //        $('<div class="details"><div class="dimensions goRight">'+$(this).children().next().next().children('.dimensions').text()+'</div><div class="info-detail"><div class="name">'+$(this).children().next('.name').text()+'</div><div class="info">'+$(this).children().next().next().children().next('.label').text()+'</div><div class="detail">'+$(this).children().next().next().children().next('.copyright').text()+'</div></div></div><div class="clearFix"></div>').appendTo('#gallery-dialog');  
    //      } else {
    //        $('<img src="'+$(this).children('.info').children('.type').html()+'" title="'+$(this).children().next('.name').text()+'" alt="'+$(this).children().next('.name').text()+'"/>').appendTo('#gallery-dialog');
    //        $('<div class="details"><div class="dimensions goRight">'+$(this).children().next().next().children('.dimensions').text()+'</div><div class="info-detail"><div class="name">'+$(this).children().next('.name').text()+'</div><div class="info">'+$(this).children().next().next().children().next('.label').text()+'</div><div class="detail">'+$(this).children().next().next().children().next('.copyright').text()+'</div></div></div><div class="download"><div class="goLeft"><input type="checkbox" id="agree" /><label for="agree">Sim, tomei conhecimento e concordo com as <a href="#" title="">condições de utilização</a> das imagens</label></div><a href="#" title="" class="blue-button goRight disable">Download</a><div class="clearFix"></div></div>').appendTo('#gallery-dialog');  
    //      }
    //      
    //      $( "#gallery-dialog" ).dialog({
    //        modal: true,
    //        dialogClass: 'gallery-dialog',
    //        draggable: false,
    //        resizable: false,
    //        width: 730,
    //        close: function(event, ui) {
    //          $('#gallery-dialog').attr('title','');
    //          $('#gallery-dialog').text('');
    //        }
    //      });
    //      return false;
    //    });
    //    
    //    $('#agree').live('click', function() {
    //      if ($(this).attr('checked')){
    //        $('.download a.blue-button').removeClass('disable');
    //      } else { 
    //        $('.download a.blue-button').addClass('disable');
    //      }
    //    });
    //  }    


    if ($('.more-downloads').length) {
        $('.more-downloads').hide();
        $('.archive-item .link').click(function () {
            if ($(this).next('.more-downloads').hasClass('open')) {
                $(this).next('.more-downloads').slideUp().removeClass('open');
                $(this).removeClass('open');
                $(this).parent().parent().removeClass('active');
            } else {
                $(this).next('.more-downloads').slideDown().addClass('open');
                $(this).addClass('open');
                $(this).parent().parent().addClass('active');
            }
            return false;
        });


    }


    if ($('.circular-graphic-table').length) {
        $('.circular-graphic-table tbody tr').hover(function () {
            $('.circular-graphic .image').addClass($(this).attr('class'));
        }, function () {
            $('.circular-graphic .image').attr('class', 'image');
        });
    }


    if ($('.map-europe').length) {
        $('.map-europe').show();
        $('.map-europe .portugal').click(function () {
            $('.map-europe').children().removeClass('active');
            $(this).addClass('active');
            $(".dialog-portugal").dialog({
                modal: true,
                draggable: false,
                resizable: false,
                width: 500,
                dialogClass: 'map-dialog',
                height: 345,
                close: function (event, ui) {
                    $('.map-europe').children().removeClass('active');
                }
            });
        });
        $('.map-europe .polonia').click(function () {
            $('.map-europe').children().removeClass('active');
            $(this).addClass('active');
            $(".dialog-polonia").dialog({
                modal: true,
                draggable: false,
                resizable: false,
                width: 358,
                dialogClass: 'map-dialog',
                height: 285,
                close: function (event, ui) {
                    $('.map-europe').children().removeClass('active');
                }
            });
        });
    }

    if ($('.toggle-advanced-filters').length) {
        $('.toggle-advanced-filters').click(function () {

            if ($('.advanced-filters').hasClass('open')) {
                $(this).removeClass('up').addClass('down');
                $('.advanced-filters').slideUp();
                $('.advanced-filters').removeClass('open').addClass('closed');
                $('.toggle-advanced-filters').text('Mais opções');
            } else {
                $(this).removeClass('down').addClass('up');
                $('.advanced-filters').slideDown();
                $('.advanced-filters').removeClass('close').addClass('open');
                $('.toggle-advanced-filters').text('Menos opções');
            }

        });
    }

    if ($('.downloads').length) {
        var itemheight = 0;
        $('.download-file').each(function () {
            $('.download-file-item').each(function () {
                if ($(this).height() > itemheight) { itemheight = $(this).height() };
            });
            $('.download-file-item').height(itemheight);
        });
    }
});
