jQuery.noConflict();

var isUserKeepMousePressed = false;
function ScrollProductListRight(theObj) {
	jQuery(theObj).animate({"scrollLeft": "+=76px"}, 700, function() {
		if(isUserKeepMousePressed)
			ScrollProductListRight(theObj);
	});
}
function ScrollProductListLeft(theObj) {
	jQuery(theObj).animate({"scrollLeft": "-=76px"}, 700, function() {
		if(isUserKeepMousePressed)
			ScrollProductListLeft(theObj);
	});
}

function urlParam(name) {
  var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
  var what;
  if(results) {what = results[1] || 0;} else {what = 0;};
  return what;
}

function InitLupa() {
	var curProductTypeId = urlParam("product_type_id");
	var curSize = urlParam("size");
	if (curProductTypeId) jQuery("#search_product_types").val(curProductTypeId);
	if (curSize) {
		jQuery("div.search_fields:visible > select.search_params").val(curSize);
		jQuery("select.inputboxattrib").val(curSize);
	}
}

function SetLupaHref() {
	if(jQuery("div.search_fields:visible > select.search_params > option:selected").val() == 'все размеры') {
//		jQuery("a#lupa").attr("href", "index.php?page=shop.browse&option=com_virtuemart&Itemid=11&product_type_id=" + jQuery("#search_product_types option:selected").val() + "&manufacturer_id=0&limitstart=0");
		jQuery("a#lupa").attr("href", jQuery("#search_product_types option:selected").attr("myref"));
	} else {
//		jQuery("a#lupa").attr("href", "index.php?page=shop.browse&option=com_virtuemart&Itemid=11&product_type_id=" + jQuery("#search_product_types option:selected").val() + "&manufacturer_id=0&size=" + jQuery("div.search_fields:visible > select.search_params > option:selected").val() +"&limitstart=0");
		jQuery("a#lupa").attr("href", jQuery("div.search_fields:visible > select.search_params > option:selected").attr("myref"));
	}
}

function RotateGalleryBlocks() {
	theObj1 = jQuery("div.gallery_top_image_1 > img:visible");
	theObj2 = jQuery("div.gallery_top_image_2 > img:visible");
	theObj3 = jQuery("div.gallery_bottom > div:visible");
	theObj1.fadeOut(200, function() {
		if(jQuery(theObj1).next("img").attr("class")) {
			theObj1.next("img").fadeIn();
		} else {
			jQuery("div.gallery_top_image_1 > img:first").fadeIn();
		}
	});
	theObj2.fadeOut(200, function() {
		if(jQuery(theObj2).next("img").attr("class")) {
			theObj2.next("img").fadeIn();
		} else {
			jQuery("div.gallery_top_image_2 > img:first").fadeIn();
		}
	});
	theObj3.fadeOut(100, function() {
		if(jQuery(theObj3).next("div").attr("class")) {
			theObj3.next("div").fadeIn();
		} else {
			jQuery("div.gallery_bottom > div:first").fadeIn();
		}
	});
}

function GetDataWithEmail(emailObj) {
	jQuery.ajax({
		type: "POST",
		url: "index.php?option=com_hello&view=hello&tmpl=component",
		data: "email=" + emailObj.value,
		success: function(msg){
			var form_data;
			var msg_parts = msg.split("<body");
			msg_parts = msg_parts[1].split("</body");
			msg_parts = msg_parts[0].split(">");
			form_data = msg_parts[1].split("|");
//			alert( msg_parts[1] );
			if(form_data[1]) jQuery(emailObj).parents("form").find("input#name").val(form_data[1]); else jQuery(emailObj).parents("form").find("input#name").val('');
			if(form_data[2]) jQuery(emailObj).parents("form").find("input#address").val(form_data[2]); else jQuery(emailObj).parents("form").find("input#address").val('');
			if(form_data[3]) jQuery(emailObj).parents("form").find("input#phone").val(form_data[3]); else jQuery(emailObj).parents("form").find("input#phone").val('');
			CheckForm(emailObj);
		}
	});
 }

function CheckForm(obj) {
	var AllRight = false, IsPhoneRight = false, IsAddressRight = false, IsNameRight = false, IsEmailRight = false;
	var theForm = jQuery(obj).parents("form");
	var thePhone = theForm.find("#phone");
	var re = /^[0-9+()\- ]*$/;
	var theButton = theForm.find('input[type="submit"]');
	var theDigits = thePhone.val().length;
	var theAddress = theForm.find("#address");
	var theName = theForm.find("#name");
	var theEmail = theForm.find("#email");
//	var reg = /^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/;
	var reg = /^[0-9a-z_.\-]+@[0-9a-z_\-^\.]+\.[a-z]{2,6}$/i;

	// проверяем phone
	if (re.test(thePhone.val()) && theDigits!=0) {
		thePhone.removeClass("border_alarm");
		IsPhoneRight = true;
	} else {
		thePhone.addClass("border_alarm");
		IsPhoneRight = false;
	}
	
	// проверяем address
	if (theAddress.val().length > 0) {
		theAddress.removeClass("border_alarm");
		IsAddressRight = true;
	} else {
		theAddress.addClass("border_alarm");
		IsAddressRight = false;
	}

	// проверяем email
	if (reg.test(theEmail.val())) {
		theEmail.removeClass("border_alarm");
		IsEmailRight = true;
//		GetDataWithEmail(theEmail.val());
	} else {
		theEmail.addClass("border_alarm");
		IsEmailRight = false;
	}
	
	// проверяем name
	if (theName.val().length > 0) {
		theName.removeClass("border_alarm");
		IsNameRight = true;
	} else {
		theName.addClass("border_alarm");
		IsNameRight = false;
	}

	AllRight = IsPhoneRight && IsAddressRight && IsNameRight && IsEmailRight;
	if (AllRight) {
		theButton.removeAttr("disabled");
	} else {
		theButton.attr("disabled","true");
	}
}


jQuery(document).ready(function(){
	jQuery("input#email").change(function () {
		GetDataWithEmail(this);
	});
	
	var theMaxHeight = 0;
	if(jQuery("#panel-3 > #left").outerHeight() > jQuery("#panel-3 > #right").outerHeight()) {
		theMaxHeight = jQuery("#panel-3 > #left").outerHeight();
	} else {
		theMaxHeight = jQuery("#panel-3 > #right").outerHeight();
	}
	if(jQuery("#panel-3 > #middle").outerHeight() > theMaxHeight) {
		jQuery("#panel-3 > #middle > div").addClass("border_sides");
	} else {
		jQuery("#panel-3 > #left").addClass("border_right");
		jQuery("#panel-3 > #right").addClass("border_left");
		jQuery("#panel-3 > #left").height(theMaxHeight);
		jQuery("#panel-3 > #right").height(theMaxHeight);
	}
//	jQuery("#panel-3 > #middle > #mainbody").css({"min-height": 786 - jQuery("#panel-3 > #middle > #user1").outerHeight() - jQuery("#panel-3 > #middle > #user2").outerHeight()});
//alert(786 - jQuery("#panel-3 > #middle > #user1").outerHeight() - jQuery("#panel-3 > #middle > #mainbody").outerHeight());
//	jQuery("#panel-3 > #middle > #user2").css({"min-height": 786 - jQuery("#panel-3 > #middle > #user1").outerHeight() - jQuery("#panel-3 > #middle > #mainbody").outerHeight()});
	jQuery("#product_price > .productPrice").css({marginLeft: (jQuery("#product_price > .productPrice").width() - jQuery("#product_price > .productPrice > .priceDigits").width())});
	jQuery("#product_price > .product-Old-Price").css({marginLeft: (jQuery("#product_price > .productPrice").width() - jQuery("#product_price > .productPrice > .priceDigits").width())});
	
	jQuery("#product_middle_left > img").click(function() {
		var mainSrc = jQuery("#product_middle_center_image_main").attr("src");
		var thisSrc = jQuery(this).attr("src");
		jQuery("#product_middle_center_image_main").fadeOut(function() {
			jQuery(this).attr("src",thisSrc).fadeIn().parent().attr("href",thisSrc);
		});
		jQuery(this).fadeOut(function() {
			jQuery(this).attr("src",mainSrc).fadeIn();
		});
	});
	
	jQuery(".jcomments-links").each(function() {
		var theObj = jQuery(this);
		theObj.parent().find("p:last").append(" " + theObj.html());
		theObj.remove();
	});

//	jQuery(".cart_last:last").addClass("border_bottom");
	var theCartLastLast = jQuery(".cart_last:last");
	var theCartLastLastHeight = 0;
	theCartLastLast.parent().children("div").each(function() {
		theCartLastLastHeight += jQuery(this).outerHeight();
	});
	theCartLastLast.children(".cart_item_price").height(theCartLastLast.parent().outerHeight() - theCartLastLastHeight + 36);
	
	// центрируем по вертикали товары в корзине
	jQuery(".cart_item").each(function() {
		var theObj = jQuery(this);
		theObj.find("div").each(function() {
			jQuery(this).css("margin-top", (theObj.height()-jQuery(this).height())/2);
		});
	});
/*	jQuery(".cart_last").each(function() {
		var theObj = jQuery(this);
		theObj.find("div").each(function() {
			jQuery(this).css("margin-top", (theObj.find("div.cart_item_name").outerHeight()-jQuery(this).outerHeight())/2);
		});
	}); */
	// центрируем по вертикали отзывы
	jQuery(".fjeans_post").each(function() {
		var theObj = jQuery(this);
		theObj.find("div").each(function() {
			jQuery(this).css("margin-top", (theObj.height()-jQuery(this).outerHeight())/2);
		});
	});

	// скроллируем сопутствующие товары
	jQuery("#scroll_right").mousedown(function() {
		isUserKeepMousePressed = true;
		ScrollProductListRight(jQuery("#product_list_scroller"));
	});
	jQuery("#scroll_right").mouseup(function() {
		isUserKeepMousePressed = false;
	});
	jQuery("#scroll_left").mousedown(function() {
		isUserKeepMousePressed = true;
		ScrollProductListLeft(jQuery("#product_list_scroller"));
	});
	jQuery("#scroll_left").mouseup(function() {
		isUserKeepMousePressed = false;
	});

	// --
	var theMainPageDivsHeight = 0;
	jQuery("#vmMainPage > div").each(function() {
		theMainPageDivsHeight += jQuery(this).outerHeight();
	});
	jQuery("div.cart_last:last").find("div.cart_item_price").outerHeight(jQuery("div.cart_last:last").find("div.cart_item_price").outerHeight() + (jQuery("#vmMainPage").outerHeight() - theMainPageDivsHeight - 2));
	
	// работаем с search
	jQuery("div#search_fields_" + jQuery("#search_product_types option:selected").val()).show();
	SetLupaHref();
	jQuery("#search_product_types").change(function() {
		jQuery("div.search_fields").hide();
		jQuery("div#search_fields_" + jQuery("#search_product_types option:selected").val()).show();
		SetLupaHref();
	});
	jQuery("select.search_params").change(function() {
		SetLupaHref();
		location.href = jQuery("#lupa").attr("href");
	});
	InitLupa();
	
	// работаем со связкой способов доставки и оплаты
	jQuery("form#without_register_form input[name=shipping_rate_id]:radio").change(function() {
		if(jQuery("form#without_register_form input[name=shipping_rate_id]:radio:checked").attr("code") == "shipping_moscow") {
			jQuery("form#without_register_form input[name=payment_method_id][code=COD]").attr({"enabled":"enabled", "disabled":""}).prev().removeClass("disabled");
		} else {
			jQuery("form#without_register_form input[name=payment_method_id][code=COD]").attr({"disabled":"disabled", "enabled":""}).prev().addClass("disabled");
			jQuery("form#without_register_form input[name=payment_method_id][code=pd4s]").attr("checked", "checked");
		}
	});
	
	// выравниваем высоту блоков в конкурсе
	theMaxHeight = 0;
	jQuery(".contest > ul > li").each(function() {
		if(jQuery(this).height() > theMaxHeight)
			theMaxHeight = jQuery(this).height();
	}).height(theMaxHeight);
	
	// search
	jQuery("#search_left").hover(function() {
		jQuery(this).stop().addClass("shadow4").animate({height: jQuery(this).find(".items").outerHeight()*(jQuery(this).find(".items").length + 1)});
	}, function() {
		jQuery(this).stop().animate({height: '22px'}).removeClass("shadow4");
	});
	
	// pagination
	jQuery(".pagination .pagination_left > a").html('<div class="arrow_left"></div>').parent().show();
	jQuery(".pagination .pagination_right > a").html('<div class="arrow_right"></div>').parent().show();
});
