$(function(){
  Cufon.replace('.contacts, .portfolio .link, .reasons .title, .reasons .num, .tarifs .title, .seo .title, .clients .title, .news .title, .news-item .date, .portfolio-list .date, h1, .slogan, .pager a', { fontFamily: 'Myriad Pro' });
  $("#top-menu ul li.active a").append("<div class='arrow'></div>");
  //slidesGlowInit();
  
  $(".slides a").hover(function(){
    if (!$(this).parent().hasClass("disabled")) $(this).find("canvas").stop(true,true).fadeIn(150);
  }, function(){
    if (!$(this).parent().hasClass("disabled")) $(this).find("canvas").stop(true,true).fadeOut(150);
  });
  
  $(".slides").append("<div class='arrow arrow-next'></div><div class='arrow arrow-prev'></div>");
  if (!($.browser.msie && $.browser.version < 9))
    $(".portfolio .slides .arrow").css({opacity: 0.7}).hover(function(){
      $(this).stop(true,true).animate({opacity: 1}, 150);
    }, function(){
      $(this).stop(true,true).animate({opacity: 0.7}, 150);
    });
  $("a[rel='gallery']").fancybox({
    'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
                'speedIn'       : 500,
                'speedIn'       : 400,
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
    'overlayColor'  : '#fff',
		'titlePosition' 	: 'over',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
		    if (currentArray.length > 1) return '<span id="fancybox-title-over">' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
        //else currentOpts.showNavArrows = false;
		}
	});
  
  $(".slides .arrow").click(function(){
    s = $(this).parent();
    sl = s.find("a");
    if (!s.hasClass("disabled"))
    {
      effect = "easeOutQuad";
      speed = 700;
      s.addClass("disabled");
      
      sl0w = sl.eq(0).width();
      sl0h = sl.eq(0).height();
      sl0l = sl.eq(0).css("left").substr(0, sl.eq(0).css("left").length - 2)*1;
      sl0t = sl.eq(0).css("top").substr(0, sl.eq(0).css("top").length - 2)*1;
      
      sl1w = sl.eq(1).width();
      sl1h = sl.eq(1).height();
      sl1l = sl.eq(1).css("left").substr(0, sl.eq(1).css("left").length - 2)*1;
      sl1t = sl.eq(1).css("top").substr(0, sl.eq(1).css("top").length - 2)*1;
      
      sl2w = sl.eq(2).width();
      sl2h = sl.eq(2).height();
      sl2l = sl.eq(2).css("left").substr(0, sl.eq(2).css("left").length - 2)*1;
      sl2t = sl.eq(2).css("top").substr(0, sl.eq(2).css("top").length - 2)*1;

      if ($(this).hasClass("arrow-next"))
      {
        sl.eq(0).css({zIndex: 3});
        sl.eq(1).css({zIndex: 2});
        sl.eq(2).css({zIndex: 1});
        sl.eq(0).animate({width: sl1w, height: sl1h, left: sl1l, top: sl1t}, speed, effect);
        sl.eq(0).find("img").animate({width: sl.eq(1).find("img").width(), height: sl.eq(1).find("img").height()}, speed, effect);        
        sl.eq(1).animate({width: sl2w, height: sl2h, left: sl2l, top: sl2t}, speed, effect);
        sl.eq(1).find("img").animate({width: sl.eq(2).find("img").width(), height: sl.eq(2).find("img").height()}, speed, effect);        
        sl.eq(2).animate({width: sl0w, height: sl0h, left: sl0l, top: sl0t}, speed, effect);
        sl.eq(2).find("img").animate({width: sl.eq(0).find("img").width(), height: sl.eq(0).find("img").height()}, speed, effect, function(){
          $(this).parent().insertBefore($(this).parent().parent().find("a:first"));
          sl = $(this).parent().parent().find("a");
        });
      }
      if ($(this).hasClass("arrow-prev"))
      {
        sl.eq(0).css({zIndex: 1});
        sl.eq(1).css({zIndex: 2});
        sl.eq(2).css({zIndex: 3});
        sl.eq(0).animate({width: sl.eq(2).width(), height: sl.eq(2).height(), left: sl.eq(2).css("left"), top: sl.eq(2).css("top")}, speed, effect);
        sl.eq(0).find("img").animate({width: sl.eq(2).find("img").width(), height: sl.eq(2).find("img").height()}, speed, effect, function(){
          $(this).parent().insertAfter($(this).parent().parent().find("a:last"));
          sl = $(this).parent().parent().find("a");
        });
        sl.eq(1).animate({width: sl.eq(0).width(), height: sl.eq(0).height(), left: sl.eq(0).css("left"), top: sl.eq(0).css("top")}, speed, effect);
        sl.eq(1).find("img").animate({width: sl.eq(0).find("img").width(), height: sl.eq(0).find("img").height()}, speed, effect);
        sl.eq(2).animate({width: sl.eq(1).width(), height: sl.eq(1).height(), left: sl.eq(1).css("left"), top: sl.eq(1).css("top")}, speed, effect);
        sl.eq(2).find("img").animate({width: sl.eq(1).find("img").width(), height: sl.eq(1).find("img").height()}, speed, effect);
      }
      
      setTimeout(function(){
        //slidesGlowInit();
        $(".slides").removeClass("disabled");
      }, speed+50);
    }
  });
  $(".reasons ul li").each(function(i){
    $(this).prepend("<span class='num'>"+(i+1)+"</span>");
  });
  $('.clients ul').jcarousel();
  if (!($.browser.msie && $.browser.version < 9))
    $(".clients .jcarousel-next, .clients .jcarousel-prev").css({opacity: 0.85}).hover(function(){
      $(this).stop(true,true).animate({opacity: 1}, 150);
    }, function(){
      $(this).stop(true,true).animate({opacity: 0.85}, 150);
    });
});

function slidesGlowInit()
{  
  $(".slides a").each(function(){
    img = $(this).find("img");
    $(this).find("canvas").remove();
    $(this).append(img.clone());
    img.eq(0).pixastic("glow", {amount:0.3,radius:1.0});
    $(this).find("canvas").fadeOut(0);
  });
}
