jQuery.noConflict();

jQuery(document).ready(function($){
	$("img.ph").parent("a").prettyPhoto();
	$("a.ph").prettyPhoto();
	$("input[type=text][id*=phone]").masker("+7 (999) 999 9999");
	var trigger = $("#over_msg").overlay({
		mask: {
			color: '#000',
			loadSpeed: 200,
			opacity: 0.5
		},
		closeOnClick: true,
		closeOnEsc: true,
		load: false,
		fixed: true,
		top: '45%',
		onBeforeLoad: function (){
			showwork();
		},
		onLoad: function () {
			hidework();
		}
	});

	$("#over_msg .close").live("click", function() {
		$("#over_msg").overlay().close();
	});

	var trigger = $("#over_ajax").overlay({
		mask: {
			color: '#000',
			loadSpeed: 200,
			opacity: 0.5
		},
		closeOnClick: true,
		closeOnEsc: true,
		load: false,
		fixed: true,
		top: '20%',
		onBeforeLoad: function (){
			showwork();
		},
		onLoad: function () {
			hidework();
		}
	});

	$("#over_ajax .close").live("click", function() {
		$("#over_ajax").overlay().close();
	});

	$(".add-to-basket-from-list").click(function(){
		showwork();
			var id = $(this).attr('rel');
			$.ajax({
				type: "POST",
				url: "/basket/add/"+id+"/",
				data: 'count=1&id='+id,
				async: false,
				success: function(text){
					shownotice('Товар успешно добавлен в корзину');
					hidework();
					showbasket();
				}
			});
		return false;
	});
	$(".addtobasket").click(function(e) {
		if ($(".one-count-input").val().match(/[0-9]+/) && $(".one-count-input").val() != 0) {
			showwork();
			var id = $(this).attr('rel');
			var data = $("form#addtobasket").serialize();
			$.ajax({
				type: "POST",
				url: "/basket/add/"+id+"/",
				data: data,
				async: false,
				success: function(data){
					shownotice('Товар успешно добавлен в корзину', e);
					$(".one-count-input").val(1);
					hidework();
					showbasket();
				}
			});
		} else {
			shownotice('Нужно указать какое количество Вам не обходимо', e);
		}
		return false;
	});

	$(".can_preview_item").live('mouseover', function() {
		var rel = $(this).attr('rel');
		if (rel != undefined) {
			var info = rel.split(":");
			$(this).parent().css("position", "relative").prepend("<img src='/uploads/catalog_items/"+info[0]+"/small/"+info[1]+"' style='position: absolute; left: -200px; top: 20px; border: 1px solid #ccc; padding: 3px; background: #eee;' id='can_preview_"+info[0]+"' />");
		}
	}).live('mouseout', function() {
		var rel = $(this).attr('rel');
		if (rel != undefined) {
			var info = rel.split(":");
			$("#can_preview_"+info[0]).detach();
		}
	});

	$("input.one-size-checkbox, input.one-color-checkbox, input.counting").uniform();
	$("input[type=text], input[type=password], select, textarea").uniform();

	$(".one-count-input").keyup(function() {
		var string = $(this).val();
		if (!string.match(/^[0-9]+$/) || string == 0) {
			$(this).val("");
		}
	}).blur(function() {
		var string = $(this).val();
		if (!string.match(/^[0-9]+$/) || string === "") {
			$(this).val("0");
		}
	});
	$(".number_of_item").live('blur', function(event) {
		var string = $(this).val();
		var rel = $(this).attr("rel");
		if (event.type != "focusout") var id = rel;
		if (!string.match(/^[0-9]+$/)) {
			$(this).val("");
		}
				showwork();
				$.ajax({
				   type: "GET",
				   url: "/basket/updatecount/"+rel+"/"+$(this).val()+"/",
				   dataType: 'html',
				   async: false,
				   success: function(data){
					   $(".update_basket_link").hide();
					   updatebasket(id);
				   }
				});
	});
	$(".number_of_item").live('keyup', function(){
		$(".update_basket_link").show();
	});
	$("#back-to-the-catalog").click(function(e){
		location.replace("/catalog/");
		e.preventDefault();
	});

	$("#addtobasket").submit(function(e){
		e.preventDefault();
	});

	/* CATALOG */
	$(".tabs_heading a").click(function() {
		var rel = $(this).attr('rel');
		$(".tabs_heading a").removeClass('active');
		$(".price_place").text($(this).attr('id'));
		$(this).addClass('active');
		$(".one_tab").hide();
		$("#tab-"+rel).show();
		return false;
	});
	$(".uline a").click(function() {
		var rel = $(this).attr('rel');
		$(this).parent().children("a").removeClass('active');
		$(this).addClass('active');
		$(this).parent().parent().find(".toogle_divs div.p").hide();
		$("."+rel).show();
		if (rel != 'opinions_form')
		return false;
	});

	$(".next-image").click(function() {
		if ($(this).hasClass('disabled')) return false;
		$(".prev-image").addClass("disabled");
		$(".next-image").addClass("disabled");
		var all = $(".one-image").size();
		var now = parseInt($(".images-place-holder").css("left")) / -270 + 1;
		var count = now + 1;
		if (now < all) {
			$(".active-image-num").text(count);
			$(".images-place-holder").animate({"left": '-=270'}, null, function() {
				$(".prev-image").removeClass("disabled");
				$(".next-image").removeClass("disabled");
			});
		}
		if (now > 0) {
			$(".prev-image").show();
		}
		if (now == all - 1) {
			$(this).hide();
		}
		return false;
	});
	$(".prev-image").click(function() {
		if ($(this).hasClass('disabled')) return false;
		$(".prev-image").addClass("disabled");
		$(".next-image").addClass("disabled");
		var all = $(".one-image").size();
		var now = parseInt($(".images-place-holder").css("left")) / -270 + 1;
		var count = now - 1;
		if (now > 0) {
			$(".active-image-num").text(count);
			$(".images-place-holder").animate({"left": '+=270'}, null, function() {
				$(".prev-image").removeClass("disabled");
				$(".next-image").removeClass("disabled");
			});
		}
		if (now - 1 < all) {
			$(".next-image").show();
		}
		if (count == 1) {
			$(this).hide();
		}
		return false;
	});

	if ($(".main-slider").size() > 0) {
		$(".main-slider").each(function () {
			var slider = $(this);
			var id  = "#" + slider.attr('id');
			var items = slider.find(".items");
			var all = $(id+" > .scrollable > .items > .small-item").size();
			var max = all - 3;
			var max_left = max * 216;
			slider.find("a.prev").hide();
			if (all <= 3) {
				$(id+" > a.browse").hide();
			} else {
				$(id+" > a.next").fadeIn();
			}
			$(id+" > a.next").click(function() {
				items.animate({"left": '-=216'}, null, function() {
					left = parseInt(items.css("left")) * -1;
					if (left > 0) {
						$(id+" > a.prev").show();
					}
					if (left >= max_left) {
						$(id+" > a.next").hide();
					}
					if (left > max_left) {
						items.css("left", "-"+max_left+"px");
					}
				});
			});
			$(id+" > a.prev").click(function() {
				items.animate({"left": '+=216'}, null, function() {
					left = parseInt(items.css("left")) * -1;
					if (left == 0) {
						$(id+" > a.prev").hide();
					}
					if (left < max_left) {
						$(id+" > a.next").show();
					}
					if (left < 0) {
						items.css("left", "0px");
					}
				});
			});
		});
	}
	if ($(".main-slider-2-line").size() > 0) {
		$(".main-slider-2-line").each(function () {
			var slider = $(this);
			var id  = "#" + slider.attr('id');
			var items = slider.find(".items");
			var all = $(id+" > .scrollable > .items > .initems ").size();
			
			var max = all - 1;
			var max_left = max * 650;
			
			slider.find("a.prev").hide();
			if (all <= 1) {
				$(id+" > a.browse").hide();
			} else {
				$(id+" > a.next").fadeIn();
			}
			$(id+" > a.next").click(function() {
				items.animate({"left": '-=650'}, null, function() {
					left = parseInt(items.css("left")) * -1;
					if (left > 0) {
						$(id+" > a.prev").show();
					}
					if (left >= max_left) {
						$(id+" > a.next").hide();
					}
					if (left > max_left) {
						items.css("left", "-"+max_left+"px");
					}
				});
			});
			$(id+" > a.prev").click(function() {
				items.animate({"left": '+=650'}, null, function() {
					left = parseInt(items.css("left")) * -1;
					if (left == 0) {
						$(id+" > a.prev").hide();
					}
					if (left < max_left) {
						$(id+" > a.next").show();
					}
					if (left < 0) {
						items.css("left", "0px");
					}
				});
			});
		});
	}

	$(".ajax_notice").click(function() {
		$(this).stop().hide().html('');
	});

	$(".select-payment input").change(function(event){
		var rel = $(this).attr('value');
		$(".explane div").addClass('hidden');
		$("#ex-"+rel).removeClass('hidden');
	});

	/*$("#qiwi-allowed, #post-allowed").change(function() {
		var type = ($(this).attr('id') == 'qiwi-allowed'?1:0);
		showwork();
		$.ajax({
			type: "GET",
			url: "/basket/updatepayment/"+type+"/",
			dataType: 'html',
			async: false,
			success: function(data){
				updatebasket();
			}
		});
	});*/

	/* CATALOG */


	/* FAQ */
	$(".faq-open").click(function() {
		var rel = $(this).attr('rel');
		$("#faq-"+rel).toggleClass("hidden");
	});
	/* FAQ */

});
function shownotice(string, e) {
	jQuery(".ajax_notice").stop().html(string).show().delay(1000).fadeOut();
}
function showbasket() {
		showwork();
		jQuery.ajax({
			type: "GET",
			url: "/basket/block/",
			async: false,
			success: function(data){
				jQuery(".basket_block").html(data);
				hidework();
			}
		});
}
function showwork() {
	jQuery(".ajax_work").show();
}
function hidework() {
	jQuery(".ajax_work").hide();
}
function updatebasket(id) {
	showwork();
	jQuery.ajax({
		type: "GET",
		url: "/basket/listing/",
		dataType: 'json',
		async: false,
		success: function(data){
			jQuery(".basket_result").html(data.text);
			if (!data.qiwi) {
				jQuery("#qiwi-allowed, #post-allowed").attr("disabled", true);
				jQuery("#sber-allowed").prop("checked", true);
				jQuery(".explane div").addClass('hidden');
				jQuery("#ex-sber").removeClass('hidden');
			} else {
				jQuery("#qiwi-allowed, #post-allowed").attr("disabled", false);
				jQuery(".explane div").addClass('hidden');
				jQuery("#ex-"+jQuery(".select-payment input:checked").attr("value")).removeClass('hidden');
			}
			hidework();
		}
	});
}
// VALIDATE
function validate_email(string) {
	return string.match(/^[-a-z0-9!#$%&'*+\/=?^_`{|}~]+(?:\.[-a-z0-9!#$%&'*+\/=?^_`{|}~]+)*@(?:[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])?\.)*(?:aero|arpa|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|[a-z][a-z])$/i);
}
function validate_phone(string) {
	string = string.replace(/\s/gi, "");
	string = string.replace("(", "");
	string = string.replace(")", "");
	return string.match(/^\+[0-9]{11}$/i);
}
// VALIDATE
