jQuery.fn.equalHeights = function() {
	jQuery(this).each(function(){
		var currentTallest = 0;
		jQuery(this).children().each(function(i){
			if (jQuery(this).height() > currentTallest) {currentTallest = jQuery(this).height();}
		});
    currentTallest=currentTallest+"px";
		jQuery(this).children('div').css({'min-height': currentTallest}); 
	});
	return this;
};

function Scroller(classScroll) {
  /*jQuery(classScroll).jScrollPane({
      showArrows : true,
      scrollbarWidth : 11,
      scrollbarMargin : -1
    }).parent().mouseover(function(){
                  jQuery(this).children('.jScrollPaneTrack').show();
                }).mouseout(function(){
                    jQuery(this).children('.jScrollPaneTrack').hide();
                  });*/
}

function hideShowDesc(id) {
  jQuery(id).mouseover(function(){
    jQuery(id+" .featured-entry").css('display','block')
  });
  jQuery(id).mouseout(function(){
    jQuery(id+" .featured-entry").css('display','none')
  })
}
        
function center_img_o(obj,nw){
    var ml;
    var w=obj.width();
    ml=Math.ceil((w-nw)/2);
    ml=ml*-1;
    var ml1=ml+'px';
    obj.css({'marginLeft' : ml1});
    obj.css({'marginRight' : ml1});
}

function center_img_v(obj,nh){
    var mt;
    var h=obj.height();
    mt=Math.ceil((h-nh)/2);
    mt=mt*-1;
    var mt1=mt+'px';
    obj.css({'marginTop' : mt1});
    obj.css({'marginBottom' : mt1});
}

function resizeAndCenter(id,width,height,src) {
  jQuery("#r-"+id).load(function() {
    var rbox=width/height;
    var img=new Image();
    img.src=src;
    var rimg=img.width/img.height;
    var mleft=((height*rimg)-width)/2;
    mleft=mleft*(-1);
    if(rimg>rbox){
      jQuery("#r-"+id).attr("height",height).css("margin-left",mleft+"px");
    }
    else {
      var rimg=img.height/img.width;
      var mtop=((width*rimg)-height)/2;
      mtop=mtop*(-1);
      jQuery("#r-"+id).attr("width",width).css("margin-top",mtop+"px");
    }
  });
}

function overlay_box_photo() {
  /**
     * Funzione che muove le immagini della photo gallery
     * IMPORTANTE!! Funziona solo su classe .image-overlay
     */
    var $fPhoto = jQuery('.image-overlay').parent();
    $fPhoto.css('background-color','#000000');
    jQuery('.zoom-icon, .more-icon').css({'opacity':'0','visibility':'visible'});
    
    $fPhoto.hover(function(){
        jQuery(this).stop(true, true).animate({top: -10}, 500).find('img.f-photo-img').stop(true, true).animate({opacity: 0.7},500);
        jQuery(this).find('.zoom-icon').stop(true, true).animate({opacity: 1},400);
        jQuery(this).find('.more-icon').stop(true, true).animate({opacity: 1},400);
    }, function(){
        jQuery(this).find('.zoom-icon').stop(true, true).animate({opacity: 0},400);
        jQuery(this).find('.more-icon').stop(true, true).animate({opacity: 0},400);
        jQuery(this).stop(true, true).animate({top: 0}, 500).find('img.f-photo-img').stop(true, true).animate({opacity: 1},500);
    });
}

function addClassKeyColor(){
      /*jQuery('#last-match-result h2, 
      #box-matches-result h2, 
      .box-home.left .fragments.left, 
      .team-slideshow h2, 
      #left-col-team .team-roster li .entry-title, 
      #team-posts li h3, 
      .box-last-photos h2, 
      .box-album-photogallery h2, 
      #box-ricerca h2, 
      #search-result h4, 
      #desc-boxVideo p strong, 
      #box-albums-video h4, 
      #videogallery-thumb h4, 
      .event-date div, 
      .more-info-shop, 
      #matches-result div.result-match, 
      #search-result h4, 
      #desc-boxVideo strong, 
      .box-home.right .ui-widget-header, 
      .box-home-right, .box-prezzo-shop, 
      .shop-page-details .right-col h4, 
      .box-prezzo-shop-details, 
      .box-desc-shop-details h3, 
      #box-nav, 
      .contacts-group-title').addClass('keycolor_bg');*/
      jQuery('#search-result h3, #p-details .entry-info .entry-author .author, #p-details .entry-info .entry-cat a, #desc-boxVideo span, #box-albums-video ul li div p span, div.desc-videogallery p span, #videogallery-thumb h3, #search-result h3, #p-details .entry-info .entry-cat abbr, .box-home.left .ui-widget-header li.ui-tabs-selected a, .ui-tabs .fragments ul.last-articles span, #middle-home ul#mv-media li.event-home h3, .box-title-single h3, .single-page .right-col .list-article li a, .single-page .right-col h5, .archive-group h4, .archive-group .entry-author a, .box-top-shop-details h2, .single-page .left-col .entry-info .entry-author a, .single-page .left-col .entry-info a, .photo-gallery-details h4').addClass('keycolor_txt');
      jQuery('#last-match-result h2, #box-matches-result h2, .team-slideshow h2, #left-col-team .team-roster li .entry-title, #team-posts li h3, .box-last-photos h2, .box-album-photogallery h2, #box-ricerca h2, #search-result h4, #desc-boxVideo p strong, #box-albums-video h4, #videogallery-thumb h4, .event-date div, .more-info-shop, #matches-result div.result-match, #search-result h4, #desc-boxVideo strong, .archive-title, .box-prezzo-shop, .shop-page-details .right-col h4, .box-prezzo-shop-details, .box-desc-shop-details h3, #nav .sf-menu, .contacts-group-title').addClass('keycolor_fg');
      jQuery('#top-home-left .featured, #box-matches-result .jScrollPaneContainer, #matches-result .clear, #middle-home ul#mv-media li.event-home a, .notices-page .box-notice, .location-info, #map_canvas, #result-last-match .last-match, #result-last-match .latest-match, .box-album-details div.sub-box-album-details, #picture-box .img-wrap, #p-details .sub-p-details, #boxVideo, #box-albums-video ul li, .box-video-thumb div.int, #box-albums-video ul, #event-description, .event-picture, .event-locandina, .documents-page .entry-content table, .box-shop, .single-page .left-col .entry-content, .single-page .right-col .list-article li .box-list-content, .single-page .right-col .list-article li .entry-content, .archive-group, .single-page .left-col, #middle-home ul#mv-media li.last-article-home .img-last-article-home, .box-top-shop-details .box-img div, .contacts-group').addClass('keycolor_border');
    }
    
function getGameInfo(path,m_id){
  jQuery('#gameInfoBox').html("").dialog("destroy");
  jQuery('#gameInfoBox').load(path+'/matches_page.php?m_id='+m_id, function() {
    jQuery('#gameInfoBox').dialog({
      modal: true,
      open: initialize,
      width: 1032,
      height: 500,
      show: "fade",
      hide: "fade"
    });
    jQuery('#map_wrapper').addClass('bg-color2');
  })
}

jQuery(document).ready(function(){
    
    overlay_box_photo();

    jQuery(".tabs").tabs();
    
    jQuery('#mv-media').jcarousel({
            vertical: false,
            animation: 1000,
            scroll: 1
    });
    jQuery('#middle-index .jcarousel-container-horizontal').addClass('jcarousel-home');
    
    jQuery('#calendar-team').jcarousel({
            vertical: false,
            animation: 1000,
            scroll: 1
    });
    jQuery('#box-calendar-team .jcarousel-container-horizontal').addClass('jcarousel-team');
    
    jQuery(".fancybox-last-video-home").fancybox({
                  'type': "iframe",
                  'autoDimension': true,
                  'showCloseButton': true,
                  'autoScale': true,
                  'frameWidth': 622,
                  'frameHeight': 374,
                  'hideOnContentClick': false,
                  'overlayColor': '#000',
                  'overlayOpacity': '0.7'
                });
                
    Scroller('#fragment-1 div');
                  
    jQuery('.tab-scroll').click(function(){
      var href=jQuery(this).attr("href");
      var childHref=jQuery(href).children(".content-fragment");
      if(!jQuery(href).children().hasClass("jScrollPaneContainer") && jQuery(href).children().children("div").height()>259) {
        Scroller(childHref);
      }
    });
    
    /*jQuery('.roster-avatar-wrapper').hover(function(){
        jQuery(this).find('div.roster-player').fadeIn(500);
    }, function(){
        jQuery(this).find('div.roster-player').fadeOut(500);
    });*/
    
    /*jQuery(".click").click(function(){
        window.location = jQuery(this).attr("url");
    });*/
    
    var tName=jQuery('#box-matches-home .no-logo, #box-matches-team .no-logo, #last-match-result .no-logo, #box-matches-home .box-game, #box-matches-team .box-game');
    tName.each(function(){
      center_img_v(jQuery(this),51);
    });
    center_img_v(jQuery('#last-match-result .middle'),117);

    var player=jQuery('ul.team-roster li ul.list-player li .roster-player');
    player.each(function(){
      center_img_v(jQuery(this),63);
    });

    jQuery('.left-col-shop').addClass('eq');
    
    var heightBoxPhotoDetails=jQuery('.sub-box-album-details').parent().height();
    jQuery('.sub-box-album-details').css('height',heightBoxPhotoDetails-12+'px');
    
    var heightBoxShop=jQuery('.box-shop-shadow').height();
    jQuery('.box-shop').css('height', heightBoxShop-7+'px');
    
    jQuery(window).load(function(){addClassKeyColor();})
    
    jQuery('.eq').equalHeights();

    jQuery('#btn-submit-login').addClass('bg-color fg3');
    
});

function gamesCarousel(starPoint){
  jQuery('#box-matches-home').jcarousel({
          vertical: true,
          animation: 500,
          scroll: 1,
          start: starPoint
  });
  jQuery('#inner-box-2 .jcarousel-container-vertical').addClass('jcarousel-home');
}

function gamesCarouselTeam(starPoint){
  jQuery('#box-matches-team').jcarousel({
          vertical: true,
          animation: 500,
          scroll: 1,
          start: starPoint
  });
  jQuery('#box-result-last-match .jcarousel-container-vertical').addClass('jcarousel-team');
}

function showRanking(){
  jQuery('#content-rank-home').children().hide();
  var showRank = jQuery('#team-rank').val();
    jQuery(showRank).show();
}

function SizeVideo() {
  jQuery('#boxVideo div object, #boxVideo div object embed').attr('height','382').attr('width','590');
}

function fancySearch() {
  jQuery('.zoom-icon.fancybox').fancybox();
}

jQuery(document).ready(function(){

  jQuery('#carousel-shop').jcarousel({
          vertical: true,
          animation: 500,
          scroll: 1
  });
  jQuery('.shop-page .entry-content .jcarousel-container-vertical').addClass('jcarousel-shop');
  
  jQuery('#list-article').jcarousel({
          vertical: true,
          animation: 500,
          scroll: 1
  });
  jQuery('.single-page .left-col .jcarousel-container-vertical').addClass('jcarousel-articles');
  
  jQuery('#last-media').jcarousel({
          vertical: true,
          animation: 500,
          scroll: 1
  });
  jQuery('#box-albums-video .jcarousel-container-vertical').addClass('jcarousel-album-video');
  
  jQuery('#thumb-video').jcarousel({
          vertical: false,
          animation: 500,
          scroll: 1
  });
  jQuery('#videogallery-thumb .jcarousel-container-horizontal').addClass('jcarousel-thumb-video');
  
  jQuery('#album-photo').jcarousel({
            vertical: false,
            animation: 1000,
            scroll: 1
  });
  jQuery('.box-album-photogallery .jcarousel-container-horizontal').addClass('jcarousel-album-photo');

  jQuery('#photo-details').jcarousel({
            vertical: false,
            animation: 1000,
            scroll: 1
  });
  jQuery('.box-first-step-photo .jcarousel-container-horizontal').addClass('jcarousel-photo-details');
  
  jQuery(window).load(function(){
    SizeVideo();
  })
})
