$(document).ready(function() {
	$("a[rel=img_group]").fancybox({
	    'transitionIn'      : 'elastic',
	    'transitionOut'     : 'elastic',
	    'titlePosition'     : 'over',
	    'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
					}
	});
});
$(function(){
	if ($.browser.opera) $('body').addClass('opera');
})
Cufon.replace('h2.title strong');
Cufon.replace('h2.title2');
window.onload = function () {
    $(".logos .logo").each(function(index){
        if(index%3 == 0){
            setImageHeight(index, ".logos .logo");    
        }
    });
	 $(".partnerItem .photo").each(function(index){
        if(index%3 == 0){
            setImageHeight(index, ".partnerItem .photo");    
        }
    });
	 
	checkResolution();
	if($("body").hasClass("home")){
		initSlider(".slider", 4);	
	}
	else{
		initSlider(".slider", 3);	
	}
}
var resizeTimeout;
window.onresize = function () {	
	resizeTimeout = setTimeout(checkResolution, 200);
}
function setImageHeight(index, name){
	var img1 = $(name).eq(index);
	var img2 = $(name).eq(index + 1);
	var img3 = $(name).eq(index + 2);
	
	img1.height(Math.max(img1.height(), img2.height(), img3.height()));
	img2.height(Math.max(img1.height(), img2.height(), img3.height()));
	img3.height(Math.max(img1.height(), img2.height(), img3.height()));
	
	img1.find("img").eq(0).css("margin-top", (img1.height() - img1.find("img").eq(0).height())/2 + "px");
	img2.find("img").eq(0).css("margin-top", (img2.height() - img2.find("img").eq(0).height())/2 + "px");
	img3.find("img").eq(0).css("margin-top", (img3.height() - img3.find("img").eq(0).height())/2 + "px");
}

function manageAnswer(tag){
	var parent = $(tag).parents(".questionItem").eq(0);
	if(parent.hasClass("opened")){
		parent.removeClass("opened");
	}
	else{
		parent.addClass("opened");
	}
}
function toogleBlock(tag){
	var parent = $(tag).parents(".toggleBlock").eq(0);
	if(parent.hasClass("closed")){
		parent.removeClass("closed");
	}
	else{
		parent.addClass("closed");
	}
}

var blockSlider, stepSlider, slides, nameSlider, current, idInterval;
function initSlider(name, num){
	nameSlider = name;
	blockSlider = $(nameSlider);
	stepSlider = blockSlider.find(".infoItem").eq(0).width() + parseInt(blockSlider.find(".infoItem").eq(0).css("margin-right"));
	var stepCount = 1;
	var count = blockSlider.find(".infoItem").size();
	current = 0;
	slides = blockSlider.find(".slides");
	slides.css("left", 0);
	slides.stop(true, true);
	var showNumber = num;
	var timeanimation = 1000;
	
	var isOvered = false;

	blockSlider.find(".arrowLeft").click(function(){
		clearTimeout(idInterval);
		slides.stop(true, true);
		moveRight();										
	});
	blockSlider.find(".arrowRight").click(function(){
		clearTimeout(idInterval);
		slides.stop(true, true);
		moveLeft();										
	});
	
	blockSlider.mouseover(function(){
		clearTimeout(idInterval);
		isOvered = true;
	});
	blockSlider.mouseout(function(){
		startAnimation();
		isOvered = false;
	});
	
	startAnimation();
	
	function startAnimation(){
		idInterval = setTimeout(function(){
			slides.stop(true, true);
			moveLeft();
		}, 2000);
	}
	function moveRight(){
		clearTimeout(idInterval);
		current --;
		
		$(".slides .infoItem").eq($(".slides .infoItem").size() - 1).insertBefore($(".slides .infoItem").eq(0));
		slides.css("left", parseInt(slides.css("left")) - stepSlider + "px");
		
		slides.animate({
			left: "+=" + stepSlider
		}, timeanimation, function() {
			if(!isOvered){
				startAnimation();
			}
		});
	}
	function moveLeft(){
		clearTimeout(idInterval);
		current ++;
		
		slides.animate({
			left: "-=" + stepSlider
		}, timeanimation, function() {
			$(".slides .infoItem").eq(0).appendTo($(".slides"));
			slides.css("left", parseInt(slides.css("left")) + stepSlider + "px");
			
			if(!isOvered){
				startAnimation();
			}
		});
	}
}
function checkResolution(){
	if($(document).width() < 1186){
		$("body").addClass("smallResolution");
	}
	else{
		$("body").removeClass("smallResolution");
	}
	
	current=0;
	blockSlider = $(nameSlider);
	stepSlider = blockSlider.find(".infoItem").eq(0).width() + parseInt(blockSlider.find(".infoItem").eq(0).css("margin-right"));
	slides = blockSlider.find(".slides");
	slides.css("left", 0);
	slides.stop(true, true);
	
	clearTimeout(resizeTimeout);
}
