$(document).ready(function() {

	var menu = $("#menu");
	var link = menu.find(".first-level .first-level-item");

	link.bind("mouseenter", function(e) {
		var element = $(this).children(".second-level");
		element.children("li").css({opacity : 0});
		element.css({display : "block"});
		element.children("li").animate({opacity : 1}, 300);
	});
	link.bind("mouseleave", function(e) {
		$(this).children(".second-level").children("li").animate({opacity : 0}, 200, function() {
			$(this).parent().css({display: "none"});
		});
	});

	$("#opinion-block").supersleight();
	$("#draggable-cart").draggable();

	$("#opinion-block").hover(
		function() {
			$("#item-see-more").css({ display : "block", opacity : 0 });
			$("#floating-item").animate({ right : 75 }, 400, function() {
				$("#item-see-more").animate({ opacity : 1 });
			});
		},
		function() {
			$("#item-see-more").animate({ opacity : 0 }, 400, function() {
			});
			$("#floating-item").animate({ right : -210 }, 400).dequeue();
		}
	);

	var auth_form = $("#auth-form");
	var auth_form_name = $("#auth-form-name");
	var auth_form_name_default = "Ваше имя";
	var auth_form_password = $("#auth-form-password");
	var auth_form_password_default = "Пароль";

	if ( !($.browser.msie && $.browser.version == "6.0") ) {
		auth_form_password = $(changeInputType(document.getElementById("auth-form-password"), "text"));
	}
	else {
		$(".search").css({ right : 40 });
	}

	auth_form_name.focus(function() {
		if ( $(this).val() == auth_form_name_default ) {
			$(this).val("");
		}
		$(this).css({ color : "#2667af" });
	});
	auth_form_name.blur(function() {
		if ( $(this).val() == "" ) {
			$(this).val(auth_form_name_default);
		}
		$(this).css({ color : "#99b5ce" });
	});

	auth_form_password.focus(function() {
		if ( $(this).val() == auth_form_password_default || $(this).val() == "" ) {
			$(this).val("");
			var changed_object = changeInputType(document.getElementById("auth-form-password"), "password");
			changed_object.focus();
		}
	});

	function changeInputType(oldObject, oType) {
		var newObject = document.createElement('input');
		newObject.type = oType;
		if ( oldObject != null ) {
			if(oldObject.size) newObject.size = oldObject.size;
			if(oldObject.value) newObject.value = oldObject.value;
			if(oldObject.name) newObject.name = oldObject.name;
			if(oldObject.id) newObject.id = oldObject.id;
			if(oldObject.className) newObject.className = oldObject.className;
			oldObject.parentNode.replaceChild(newObject,oldObject);
		}
		return newObject;
	}

	auth_form.submit(function() {
		var errors = false;
		if ( auth_form_name.val() == auth_form_name_default || auth_form_name.val() == "" || ! /^[a-zа-я0-9_\s]+$/i.test(auth_form_name.val()) ) {
			auth_form_name.prev().css({ display : "block", opacity : 0 });
			auth_form_name.prev().animate({ opacity : 1 });
			errors = true;
		}
		else {
			auth_form_name.prev().css({ display : "none" });
		}
		var auth_form_password = $("#auth-form-password");
		if ( auth_form_password.val() == auth_form_password_default || auth_form_password.val() == "" || ! /^[a-z0-9]+$/i.test(auth_form_password.val()) || auth_form_password.val().length < 6 ) {
			auth_form_password.prev().css({ display : "block", opacity : 0 });
			auth_form_password.prev().animate({ opacity : 1 });
			errors = true;
		}
		else {
			auth_form_password.prev().css({ display : "none" });
		}
		if ( errors == true )
			return false;
	});
	$("#auth-form .form-span-enter").click(function() {
		auth_form.submit();
	});

	cart_default_top = $("#draggable-cart").css("top");
	cart_default_left = $("#draggable-cart").css("left");

	$(".show-product").mouseenter(function() {
		var cart_obj = $("#draggable-cart");
		var self = $(this);
		cart_obj.animate({ top : parseInt(cart_default_top)+self.position().top + 90, left : self.width() + 100 }, 400);
	});

	$(".products-main .item").mouseenter(function() {
		var cart_obj = $("#draggable-cart");
		var self = $(this);
		cart_obj.animate({ top : parseInt(cart_default_top)+self.position().top + 500 }, 400);
	});

	$(".header").mouseenter(function() {
    	var cart_obj = $("#draggable-cart");
    	cart_obj.animate({ top : parseInt(cart_default_top), left : parseInt(cart_default_left) }, 400);
    });

	$(".loader").bind("ajaxSend", function() {
		$(this).text("Загрузка данных...");
		$(this).css({ display: "block" });
	}).bind("ajaxComplete", function() {
		$(this).text("");
		$(this).css({ display: "none" });
	});

});
