﻿var wait = new Image;
wait.src = "http://www.xfashion.com/design/waiter_middle.gif";
var wait_dark = new Image;
wait_dark.src = "http://www.xfashion.com/design/waiter_middle_dark.gif";

$(document).ready(function () {
	$("body").append("<div id=\"ToolTip\"></div>");
	$("#MainContainer").append("<div id=\"Fader\"></div>");

	$(".w-tooltip").mousemove(function() {
		var w = $(this).outerWidth();
		var h = $(this).outerHeight();
		var offset = $(this).offset();
		var x = offset.left - (100-w)/2;
		var y = offset.top + h + 5;
		$("#ToolTip").css({
			left: x,
			top: y,
			display: 'block'
		}).text($(this).text());
	});
	$(".w-tooltip").mouseout(function() {
		$("#ToolTip").css("display", "none");
	});
	$(".ClickToSee").click(function() {
		var val = $(this).siblings(".ToSee").html();
		$(this).html(val);
		$(this).removeClass("ClickToSee");
	});

	$(".TopMenuDropDown").mouseenter(function() {
		$(this).find("ul").css("display", "block");
	});
	$(".TopMenuDropDown").mouseleave(function() {
		$(this).find("ul").css("display", "none");
	});
	
	$("[id^=topLang-]").click(function() {
		var l = $(this).attr('id').substr(8);
		if (l == 'en') {
			window.location.href = 'http://www.xfashion.com';
		} else if (l == 'de') {
			window.location.href = 'http://www.xfashion-de.com';
		} else if (l == 'ru') {
			window.location.href = 'http://www.xfashion-ru.com';
		}
	});
	$("#A_SignIn").click(function() {
		$("#SignInBox").slideToggle(100, function() {
			$("#LoginContainer").css("display", "block");
			$("#RemindContainer").css("display", "none");
			$(".HeaderErr, .HeaderOk").css("display", "none");
			$("#SignInBox [name='login']").focus();
		});
	});
	$("#SignInBox .cnl").click(function() {
		$("#SignInBox").slideUp(100);
		$("#LoginContainer").css("display", "block");
		$("#RemindContainer").css("display", "none");
		$(".HeaderErr, .HeaderOk").css("display", "none");
		return false;
	});
	$(document).click(function() {
		$("#SignInBox").css("display", "none");
		$("#LoginContainer").css("display", "block");
		$("#RemindContainer").css("display", "none");
		$(".HeaderErr, .HeaderOk").css("display", "none");
	});
	$("#A_SignIn, #SignInBox").click(function(event){
		event.stopPropagation();
	});
	$("#RemindHref").click(function() {
		$("#LoginContainer").css("display", "none");
		$("#RemindContainer").css("display", "block");
		return false;
	});
	$("#DealerLoginForm").submit(function() {
		$("#loginErr").css("display", "none");
		$.post(
				 "/scripts/dealer_login.pl", $(this).serialize()+'&ms='+new Date().getTime(),
				 function(data) {
					if (data.status == 'Ok') {
						document.cookie = "logged="+escape(data.logged)+"; path=/";
						window.location.href = '/'+l+'cp';
					} else {
						$("#loginErr").slideDown(200);
					}
				 },
				 'json'
		);
		return false;
	});
	$("#DealerRemindForm").submit(function() {
		$("#remindErr").css("display", "none");
		$("#remindOk").css("display", "none");
		$.post(
				 "/scripts/dealer_remind_password.pl", $(this).serialize()+'&ms='+new Date().getTime(),
				 function(data) {
					if (data.status == 'Ok') {
						$("#remindOk").slideDown(200);
					} else {
						$("#remindErr").slideDown(200);
					}
				 },
				 'json'
		);
		return false;
	});
	$("#A_LogOut").click(function() {
		document.cookie = "logged=; path=/";
		window.location.href='/';
		return false;
	});
	
	$("[id^='CatSx']").click(function() {
		var s_id = $(this).attr('id').substr(5);
		if (!$("#CatDropBox"+s_id).length) {
			$("<div class='CatDropBox' id='CatDropBox"+s_id+"'><div class='WaiterCenterDark'></div></div>").appendTo($(this).parents("#CatRow"));
			var l = $(this).parents("#CatRow").find("[id^='CatRowLang']").attr('id').substr(11);
			$.post(
					 "/engine/Xf.pl", "t=CATALOGUE.ttc&l="+l+"&s_id="+s_id,
					 function(data) {
						$("#CatDropBox"+s_id).html(data);
						$("#CatDropBox"+s_id).animate({
							height: $("#CatDropBox"+s_id).find(".CatalogueContainer").height()
						}, 400);
					 }
			);
		}
		$("#CatDropBox"+s_id).slideDown(200);
		return false;
	});
	$(document).click(function() {
		$(".CatDropBox").css("display","none");
	});
	$(".CatDropBox").click(function(event){
		event.stopPropagation();
	});
	
	
	$("span.ContactSeller").click(function() {
		$('html,body').animate({ scrollTop: $(".ProdContact").offset().top }, 1000);
		$("[name='ContactSellerForm'] [name='email']").focus();
	});
	
	if ( $(".PicsStock img").length > 1 ) {
		$(".PicsBox").append("<div class=\"S ArrowLeft\"></div><div class=\"S ArrowRight\"></div>");
	}
//	$(".PicsBox").append("<div class=\"S Enlarge\"></div>");
	$(".PicsBox .ArrowLeft").live("click", function() {
		$(this).siblings(".PicsStock").animate({left: '+=330'}, 200,
			function() {
				if (parseInt($(this).css('left')) >= 0) {
					$(this).siblings(".ArrowLeft").css("display", "none");
				}
				else {
					$(this).siblings(".ArrowLeft").css("display", "block");
				}
				$(this).siblings(".ArrowRight").css("display", "block");
			}
		);
	});
	$(".PicsBox .ArrowRight").live("click", function() {
		$(this).siblings(".PicsStock").animate({left: '-=330'}, 200,
			function() {
				if ($(this).width() + parseInt($(this).css('left')) - 350 <= 0) {
					$(this).siblings(".ArrowRight").css("display", "none");
				}
				else {
					$(this).siblings(".ArrowRight").css("display", "block");
				}
				$(this).siblings(".ArrowLeft").css("display", "block");
			}
		);
	});
	var CurrentScroll = 0;
	$(".PicsBox a").lightBox({fixedNavigation: true, imageLoading: '/design/waiter_middle.gif', imageBtnPrev: '/design/BtnLeft.png', imageBtnNext: '/design/BtnRight.png', imageBtnClose: '/design/BtnClose.png', txtImage: '', txtOf: '/'});
/*
	$(".PicsBox .Enlarge").live("click", function() {
		var num = Math.round( parseInt( $(this).siblings(".PicsStock").css("left").replace(/\D/,'') )/330 ) || 0;
		var img = $(this).siblings(".PicsStock").children("img:eq("+num+")");
		$("#EnlargePic").remove();
		$("body").append("<div id=\"EnlargePic\"><img src=\""+img.attr('src').replace('/mid/','/full/')+"\"</div>");
		$("#Fader").css("height", $("body").height()).fadeTo(100, 0.7, function() {
			var w = $("#EnlargePic img").outerWidth();
			var h = $("#EnlargePic img").outerHeight();
			var bodyW = $("body").width();
			x = bodyW/2 - w/2;
			y = 10;
			$("#EnlargePic").css({
				width:w,
				height:h,
				left: x,
				top: y,
				visibility: 'visible'
			});
			CurrentScroll = $('html,body').scrollTop();
			$('html,body').animate({ scrollTop: 0 }, 1000);
		});
	});
*/
	$("#Fader").click(function() {
		$("#Fader").fadeOut(100);
		$("#EnlargePic").fadeOut(100);
		$('html,body').animate({ scrollTop: CurrentScroll }, 1000);
	});
	$(".ListItem").find("img:first").click(function() {
		window.location.href = $(this).parents(".ListItem").find("a.t").attr('href');
	});
	$(".ListItem").mouseenter(function() {
		$(this).find(".ItemSellerContact").css("visibility", "visible");
	});
	$(".ListItem").mouseleave(function() {
		$(this).find(".ItemSellerContact").css("visibility", "hidden");
	});
	
	$(".ListItemV2").click(function() {
		window.location.href = $(this).find("a:first").attr('href');
	});
	
	$(".PagerArrow").click(function() {
		$(this).siblings(".PagerDrop").slideToggle(100);
	});
	$(".Sorter").click(function() {
		$(this).find(".SortDrop").slideToggle(100);
	});
	$(document).click(function() {
		$(".PagerDrop, .SortDrop").css("display", "none");
	});
	$(".PagerDrop, .SortDrop").click(function(event){
		event.stopPropagation();
	});
	$("[name='choose_page']").submit(function() {
		var p = parseInt($(this).find("input[name='go_to_page']:first").val());
		p = (p > 0 ? p : '');
		var url = $(this).find("input[name='page_uri']:first").val().replace(/__page__/, p);
		window.location.href = url;
		return false;
	});
	
	//FIRSTPAGE
	$("#FirstPageProductsBlock .tab_title").click(function() {
		if (!$(this).hasClass("tab_cur")) {
			var tab = $(this).attr("id").substr(4);
			$("#FirstPageProductsBlock .tab_title").removeClass("tab_cur");
			$(this).addClass("tab_cur");
			$("#FirstPageProductsBlock .tab_content").css("display", "none").removeClass("tab_content_cur");
			$("#FirstPageProductsBlock #"+tab).slideDown(500).addClass("tab_content_cur");
		}
	});
});
