var animationSpeed = 400;
var acceptHeaderChanges = true;
var hdrMustHover;
var hdrMustHoverLeftPos;
var bgColor = '#6d9a27';


$(document).ready(function(){
	$("#content .content .cntBottom.fixed .sidePadded").each(function(){
		$(this).jScrollPane({
			scrollbarWidth:15,
			dragMinHeight:41,
			dragMaxHeight:41
		})
	});
	$("#mainPageHeader .content .panel").css("opacity", "0.9");
	
			$(".fob-buttom").hover(
  function () {
    $(this).css('background',bgColor);

  },
  function () {
    $(this).css('background','#888');
  }
);
	
	
	
	
	$("#mainPageHeader .title").click(function() {
			hdrTitleClick(this);
	});
	$("#mainPageHeader .title").mouseover(function () {
		hdrMouseOver(this);
	});
	$("#mainPageHeader .title").mouseout(function () {
		hdrMouseOut(this);
	});
});

function hdrMouseOver(element)
{
var parentP = $(element).parent();
	if ($(parentP).hasClass("active") == false) {
		hdrPrevMustHover = hdrMustHover;
  	hdrMustHover = element;
		hdrMustHoverLeftPos = $(element).offset().left;
  	setTimeout(function() {  	
			if (hdrMustHover && hdrMustHoverLeftPos == $(hdrMustHover).offset().left)
				hdrTitleClick(hdrMustHover);
  		
  	}, 600);
  }
}

function hdrMouseOut(element)
{
	if (element == hdrMustHover) {
		hdrMustHover = null;
	}
}

function hdrTitleClick(element)
{	
	if (acceptHeaderChanges) {
		acceptHeaderChanges = false;
		
  	var oldElt = $("#mainPageHeader .block.active"); // Элемент, который будем скрывать
  	var newElt = $(element).parent(); // Элемент, который будем показывать
  	var olWidth = oldElt.children(".content").width(); // Прежняя ширина картинки
		var previous = false; // Возвращаемся ли мы назад

		
  	newElt.children(".content").css("display", "block");
  	newElt.children(".content").css("width", "0");
  	newElt.children(".content").css("opacity", "0");
		
		if ($('#mainPageHeader .block').index(newElt.get(0)) < $('#mainPageHeader .block').index(oldElt.get(0))) 
			previous = true;
			
		
		// 

		// Если анимация идёт справа налево, то поведение немного другое
		if (previous) {
			newElt.children(".title").css("display", "none");
			oldElt.children(".title").css("display", "block");
			oldElt.children(".title").css("opacity", "0");
		}
		
		if (newElt.hasClass("green")) {
			changeEltColor("#header .mMenu ul li.right a", "green");
			changeEltColor(".fpTwoCols .readMoreButton a", "green");
			bgColor = '#6d9a27';
			
		}
		else if (newElt.hasClass("orange")) {
			changeEltColor("#header .mMenu ul li.right a", "orange");
			changeEltColor(".fpTwoCols .readMoreButton a", "orange");
			bgColor = '#f99617';
		}
		else if (newElt.hasClass("blue")) {
			changeEltColor("#header .mMenu ul li.right a", "blue");
			changeEltColor(".fpTwoCols .readMoreButton a", "blue");
			bgColor = '#2f6baf';

		}
		
		
		$(".fob-buttom").hover(
  function () {
    $(this).css('background',bgColor);

  },
  function () {
    $(this).css('background','#ccc');
  }
);
		
		if (previous) {
			oldElt.children(".title").animate({
				opacity:1
			}, animationSpeed)
		}
		
		newElt.children(".title").animate({
			opacity:0
		}, animationSpeed);
		
		oldElt.children(".content").animate({
  		//width: 0,
  		opacity: 0
  	}, animationSpeed);
		
  	newElt.children(".content").animate({
  		width: olWidth,
  		opacity: 1
  	}, {
			duration: animationSpeed,
			easing: "swing",
			queue: true,
			step: function(){
				oldElt.children(".content").css("width", (olWidth - $(this).width()) + "px");
				return false;
			},
			complete: function(){
			
				newElt.children().css({
					"display": "",
					"opacity": ""
				});
				newElt.addClass("active");
				
				oldElt.children().css({
					"display": "",
					"opacity": ""
				});
				oldElt.removeClass("active");
				newElt.children(".title").removeClass("moved");
				
				acceptHeaderChanges = true;
			}
		});
  	
  }
}

function changeEltColor(jqRule, newColor)
{
	var elt = $(jqRule);
	
	if (elt) {
  
  	var appended = elt.clone().appendTo(elt.parent());
  	
  	appended.addClass("appended");
  	
  	appended.css("opacity", "0.0");
  	appended.addClass(newColor);
  	
  	appended.animate({
  		opacity: 1.0
  	}, animationSpeed, "", function(){
  		elt.remove();
  		appended.removeClass("appended");
  		appended.parent().removeClass("green");
  		appended.parent().removeClass("orange");
  		appended.parent().removeClass("blue");
  		appended.parent().addClass(appended.attr("class"));
  		appended.removeAttr("class");
  	});
  }
}
