$(document).ready(function() {
	$("#menu-head .cms").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
	var w = $(window).width();
	
	$(".phone-link").click(function(){
		$("#phone-head").fadeOut(150).fadeIn(150).fadeOut(150).fadeIn(150).fadeOut(150).fadeIn(150);
		return false;
	});
	
	$("#carousel-units li:eq(0)").addClass("unit-prev");
	$("#carousel-units li:eq(1)").attr("id","current-unit");
	$("#carousel-units li:eq(2)").addClass("unit-next");
	
	$("#carousel-units li.unit-next").css("left",w);

/*	
	$("#current-unit").hover(function(){
		$(this).addClass("current-unit-active");
		$("#carousel").css("overflow","visible");
	},function(){
		$(this).removeClass("current-unit-active");
		$("#carousel").css("overflow","hidden");
	});
*/

	var lsize = $('#carousel-units li').size();

	$("#carousel-units li_").click(function(){

		if($(this).attr("class") == "unit-prev"){
			$("#current-unit").animate({left:w},1000);
			$(this).animate({left:w/2},1000);
		} 
		
		if ($(this).attr("class") == "unit-next") {
			$("#current-unit").animate({left:0},1000);
			$(this).animate({left:w/2},1000);
		}
		
		$("#carousel-units li").attr("id","").removeClass("unit-prev").removeClass("unit-next").removeClass("current-unit-active");
		$("#carousel-units li .current-unit-top,#carousel-units li .current-unit-bottom,#carousel-units li h1").remove();
		
		$(this).append('<div class="current-unit-top"></div><div class="current-unit-bottom"></div>').prepend("<h1>" + $('.h1',this).text() + "</h1>");
		
		//console.log($(this).prev().children(".h1").text())
		$(this).prev().addClass("unit-prev");
		
		$(this).next().addClass("unit-next");
		
		
		
		$("#carousel-controls-left p").text($(".unit-prev .h1").text());
		$("#carousel-controls-right p").text($(".unit-next .h1").text())
/*
		var pthis = $(this).prev();
		var nthis = $(this).next();
		
		$("#carousel-controls-left").click(function(){
			pthis.click();
		});
		
		$("#carousel-controls-right").click(function(){
			nthis.click();
		});
*/		

		$(this).attr("id","current-unit");
		$(this).hover(function(){
			if($(this).attr("id") == "current-unit"){ //error hover
				$(this).addClass("current-unit-active");
				$("#carousel").css("overflow","visible");
			}
		},function(){
			$(this).removeClass("current-unit-active");
			$("#carousel").css("overflow","hidden");
		});
		
		$("#carousel-units li.unit-next").css("left",w);
	
		
	});
	
/*  */
	var
		carusel = $("#carousel-units");
		caruselLen = $("li", carusel).length;
	
// РќСѓРјРµСЂР°С‚РѕСЂ + РєР»РёРє РїРѕ Р›Р. Р‘СЂСЋСЃ Р›Рё!
	function refresh_gallery () {
	$("li", carusel)		
		.each(function (i) {
			$(this).attr('elIndex', i);
		})
		.click(function () {
			
			var el = $(this), shift;
		
			if (el.hasClass('unit-prev')) {
				shift = 'left';
			} else if (el.hasClass('unit-next')) {
				shift = 'right';
			} else {			
				return;
			}
			
			__shift(shift);				
		});
	}
	refresh_gallery();
	$('#carousel-units li a').click(function(){$(this).parent().click();return false;});
	$('#carousel-units #current-unit a').unbind('click');				
// РљР»РёРє РїРѕ СЃС‚СЂРµР»РєР°Рј
	$("#carousel-controls-left, #carousel-controls-right").click(function () {
		
		var el = $(this);
			
		if (el.attr('id') == 'carousel-controls-left') {
			shift = 'left';
		} else if (el.attr('id') == 'carousel-controls-right') {
			shift = 'right';
		} else {			
			return;
		}

		__shift(shift);	
	});

// РЎРґРІРёРі
	if (!jQuery.browser.opera) 
	$(document).bind("keydown", function(e) {
        var key = e.keyCode;

        if (key == 37 && e.ctrlKey) {
			shift = 'left';
        } else if (key == 39 && e.ctrlKey) {
			shift = 'right';
        } else {
			return;
		}

		__shift(shift);
	});

	function __shift(shift)
	{		
		var elCenter = $('#current-unit');
			
//		console.log('Ground shift: %s', shift);

		if (shift == 'left') {
			__shiftLeft(elCenter);
		}
	
		if (shift == 'right') {
			__shiftRight(elCenter);
		}		
	}

	function __shiftRight(el)
	{
		if (el.attr('elIndex')*1 + 2 >= caruselLen) {
			$("#carousel-controls-right p").text('');
		}

		if (el.next().next().next().length < 1) {
					var elIndex = parseInt(el.attr('elIndex')) + 3;
					var eq =  elIndex - lsize;
					var html = $('#carousel-units li:eq('+eq+')').html();
					$('#carousel-units').append('<li id="" class="" elindex="' + elIndex + '" style="display: none; left: '+ (w + el.width())+'px;">' + html + '</li>');
				    refresh_gallery();
			//return;
		}
		
		if (el.next().length < 1) {
//			console.warn('РђС…С‚СѓРЅРі!!!');
			return;
		}
		
		
		
		
		$("#carousel-units li .current-unit-top,#carousel-units li .current-unit-bottom, #carousel-units li h1").remove();
		
		
		el.prev().animate({ 
			left: -el.width()
		}, 1000, function () {
			$(this)
				.hide()
				.removeClass('unit-prev');
		});

		el.animate({ 
			left: 0
		}, 1500, function () {
			$(this)
				.attr('id', '')
				.addClass('unit-prev');
				
			$("#carousel-controls-left p").text($(this).text());
		}).children('a').click(function(){$(this).parent().click();});

		el.next().animate({ 
			left: w/2
		}, 1500, function () {
			__makeHover($(this));
			$(this)
				.attr('id', 'current-unit')
				.removeClass('unit-next')
				.append('<div class="current-unit-top"></div><div class="current-unit-bottom"></div>')
				.prepend("<h1>" + $('.h1',this).text() + "</h1>");
		}).children('a').unbind('click');

		$('li:eq('+ (el.attr('elIndex')*1 + 2) +')', carusel)
			.show()
			.addClass('unit-next')
			.css('left', w + el.width())
			.animate({ 
				left: w
			}, 1500, function () {
				$(this)				
					.addClass('unit-next');
				$("#carousel-controls-right p").text($(this).text());
			
			}).children('a').click(function(){$(this).parent().click();});
		
		
	}
	
	function __shiftLeft(el)
	{

		if (el.attr('elIndex')*1 <= 1) {
			$("#carousel-controls-left p").text('');
		}
		
		if (el.prev().prev().length < 1) {
					var elIndex = parseInt(el.attr('elIndex')) - 2;
					var eq = lsize + elIndex;
					var html = $('#carousel-units li:eq('+eq+')').html();
					$('#carousel-units').prepend('<li id="" class="" elindex="' + elIndex + '" style="display: none; left: -520px;">' + html + '</li>');
					refresh_gallery();
					/*var ii = 0;
					$('#carousel-units li').each(function(){
						$(this).attr('elindex', ii++);									  
					});*/
				   // $('#carousel-units li:first').remove();
			//return;
		}
		
		if (el.prev().length < 1) {
//			console.warn('РђС…С‚СѓРЅРі!!!');
			return;
		}
				
		$("#carousel-units li .current-unit-top,#carousel-units li .current-unit-bottom, #carousel-units li h1").remove();
		
		el.next().animate({ 
			left: w + el.width()
		}, 1000, function () {
			$(this)
				.hide()
				.removeClass('unit-next');
		});

		el
		//.prepend("<h1>" + $('.h1',el).text() + "</h1>")
		.animate({ 
			left: w
		}, 1500, function () {
			$(this)
				.attr('id', '')
				.addClass('unit-next');
			$("#carousel-controls-right p").text($(this).text());
		}).children('a').click(function(){$(this).parent().click();});
		
		el.prev().animate({ 
			left: w/2
		}, 1500, function () {
			__makeHover($(this));
			$(this)
				.attr('id', 'current-unit')
				.removeClass('unit-prev')
				.append('<div class="current-unit-top"></div><div class="current-unit-bottom"></div>')
				.prepend("<h1>" + $('.h1',this).text() + "</h1>");
		}).children('a').unbind('click');		
		
		$('li:eq('+ (el.attr('elIndex')*1 - 2) +')', carusel)
			.show()
			.addClass('unit-prev')
			.css('left', -el.width())
			.animate({ 
				left: 0
			}, 1500, function () {
				$("#carousel-controls-left p").text($(this).text()||'ghjgjg');
			})
			.children('a').click(function(){$(this).parent().click();});

	}	

	function __makeHover(el)
	{
		
		el.hover(function(){
			if(el.attr("id") == "current-unit") {
				$(this).addClass("current-unit-active");
				$("body").css("overflow-x","hidden");
				$("#carousel").css("overflow","visible");
			}
		},function(){
			if(el.attr("id") == "current-unit") {
				$(this).removeClass("current-unit-active");
				$("body").css("overflow-x","auto");
				$("#carousel").css("overflow","hidden");
			}
		});
	}
	
	__makeHover($("#current-unit"));
	$("#carousel-controls-left p").text($(".unit-prev .h1").text());
	$("#carousel-controls-right p").text($(".unit-next .h1").text());

/* Р Р°Р·РІРѕСЂР°С‡РёРІР°РµС‚ С„РѕСЂРјРѕС‡РіСѓ */
	$(".feedback-form-popup").hide();
	
	$(".feedback-form-popup-link").click(function(){
		$(document).bind("keydown", function(e) {
	        var key = e.keyCode;
	        if (key == 27) {
				$(".feedback-form-popup")
				.slideUp();
				return false;
	        }
		});
	
		$(".feedback-form-popup")
		.slideToggle();
		return false;
	});
	
});




// ie min-width
var d = document;
var winIE = (navigator.userAgent.indexOf("Opera")==-1 && (d.getElementById && d.documentElement.behaviorUrns)) ? true : false;

function bodySize(){
	if(winIE && d.documentElement.clientWidth) {
		sObj = d.getElementsByTagName("body")[0].style;
		sObj.width = (d.documentElement.clientWidth<960) ? "960px" : "100%";
	}
}

function init(){
	if(winIE) { bodySize(); }
}
onload = init;
if(winIE) { onresize = bodySize; } 
