$(document).ready(function() {
	moveHeaderRightBlock();
	$(window).resize(function() {
		moveHeaderRightBlock();
	});
	
	$("a.dropdown").click(function() {
		var menuTimeOut;
		if($(this).parent().hasClass("dropdown_open")) {
			$(this).parent().removeClass("dropdown_open").children("a.dropdown").removeClass("selected");
			$(this).parent().children("div.submenu").fadeOut(200);
		} else {
			$(".dropdown_open").each(function() {
				$(this).removeClass("dropdown_open").children("a.dropdown").removeClass("selected");
				$("div.submenu").hide();
			});
			$(this).addClass("selected").parent().addClass("dropdown_open");
			$(this).parent().children("div.submenu").fadeIn(200).hover(
				function() {
					clearTimeout(menuTimeOut);
				}, 
				function() {
					var subMenu = $(this);
					menuTimeOut = setTimeout(function(){
						subMenu.fadeOut(200).parent().removeClass("dropdown_open").children("a.dropdown").removeClass("selected");
					}, 1000);
					subMenu.parent().hover(
						function() {
							clearTimeout(menuTimeOut);
						}, 
						function() {
							var subMenu = $(this).children("div.submenu");
							clearTimeout(menuTimeOut);
							menuTimeOut = setTimeout(function(){
								subMenu.fadeOut(200).parent().removeClass("dropdown_open").children("a.dropdown").removeClass("selected");
							}, 1000);
						}
					);
				}
			);
		}
		
		return false;
	});
	
	$("div.submenu a").click(function() {
		$(this).parents("div.submenu").find("a").removeClass("selected_filter");
		$(this).addClass("selected_filter");
		$(this).parents("li.dropdown_open").children("a.dropdown").text($(this).text());
		$(this).parents("li.dropdown_open").children("div.submenu").fadeOut(200);
		$(this).parents("li.dropdown_open").removeClass("dropdown_open").children("a.dropdown").removeClass("selected");
		
		if($(this).attr("href").charAt(0) == "/") {
			return true;
		} else {
			var $paramString = "?" + $(this).attr("href").slice(1);
			$("a.selected_filter").not($(this)).each(function() {
				if($paramString != "?") {
					$paramString += "&";
				}
				$paramString += $(this).attr("href").slice(1);
				
			});
			if(location.pathname == "/en/") {
				$.get("/en/ajax/portfolio.php"+$paramString, function(data) {
					$(".home_grid").fadeOut(400, function() {
						var $data = $(data);
						$data.hide();
						$(this).replaceWith(function() {
							return $data.fadeIn(400);
						});
					});
				});

			} else {
				$.get("/ajax/portfolio.php"+$paramString, function(data) {
					$(".home_grid").fadeOut(400, function() {
						var $data = $(data);
						$data.hide();
						$(this).replaceWith(function() {
							return $data.fadeIn(400);
						});
					});
				});
			}
		}
		
		return false;
	});
	
	$(".header .search input").focus(function() {
		$(this).animate({"width":"120px"});
		if($(this).val() == '' || $(this).val() == 'Поиск' || $(this).val() == 'Search') {
			$(this).val('');
		}
	});
	
	$(".header .search input").blur(function() {
		$(this).animate({"width":"54px"});
		if($(this).val() == '' || $(this).val() == 'Поиск' || $(this).val() == 'Search') {	
			if(location.pathname.indexOf("/en/") == 0) {
				$(this).val('Search');
			} else {
				$(this).val('Поиск');
			}
		}
	});
	
	$(".case img").each(function() {
		if($(this).attr("alt")) {
			$(this).after('<div class="image_description">'+$(this).attr("alt")+'</div>');
		}
	});
	
  var i = 0;
	$(".case img").each(function() {
		$(this).hide();
		var img = $(this);
    var src = img.attr("src");

    i = i < 7 ? i + 1 : 1;
    var host = 'http://i' + i + '.' + location.host.replace('www.', '');
    
		if ($.browser.msie) {
      src = src + "?" + new Date().getTime();
		}
    
    console.info(src);
    img.attr("src", host + src);
		$(this).load(function() {
			var newWidth = img.width();
			if(newWidth >= 620) {
				newWidth = 620;
				img.replaceWith('<a href="'+host + src+'" target="_blank"><img src="' + host + '/thumb/'+newWidth+'x0xCUT'+src+'" /></a>');
			}
			img.show();
		});
	});
	
	$("a.fancybox").fancybox({
		'overlayShow'	: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
});

function moveHeaderRightBlock() {
	// itemWidth = width + margin-left
	var itemWidth = 162;
	$(".header .r").css({
		"margin-right": (($(".middle").width() + 28) % itemWidth)+"px"
	});
}
