$(document).ready(function(){
	$("#krestik_auth,.avt").click(function(){
		auth_toggle()
	})
	
	$(".addcart-a").click(function(e){
		$("#popup_basket").find(".form-avt").css("top",e.pageY - 10)
		$("#popup_basket").find(".form-avt").css("left",e.pageX - 320)
		$("#popup_price").html($(this).parent().find(".diskNPrice").html() + ' руб.')
		$("#popup_id").html($(this).parent().find(".diskID").html())
		$("#popup_name").html($(this).parent().parent().find(".name").html())
		$("#popup_img").html('<img src="'+$(this).parent().find(".diskImg").html()+'" style="max-width:112px; max-height:161px;" />')
		$(this).html("добавлен в корзину")
		$(this).attr("class","added")
		var href = $(this).attr("href")
		$(this).attr("href","#")

		$.get("/catalog/add2basket.php"+href,{},function(r){ 

			basket_toggle()
			if(parseInt($("#basketQuantity").html())){
				$("#basketQuantity").html(parseInt($("#basketQuantity").html()) + 1)
				$("#basketPrice").html(parseInt($("#basketPrice").html()) + parseInt($("#popup_price").html()))
			}else{
				$(".cart").find(".b2").find("div").html('<b>В вашей корзине:</b><br>товара: <span id="basketQuantity">1</span> шт.<br>на сумму: <span id="basketPrice">'+parseInt($("#popup_price").html())+'</span>руб.<br><div><a href="/personal/order.php" class="posm">оформить</a> / <a href="/personal/basket.php" class="posm">посмотреть</a></div>')
			}
		})
	})
	
	$(".addcart-a-d").click(function(e){
		
		$("#popup_basket").find(".form-avt").css("top",e.pageY - 10)
		$("#popup_basket").find(".form-avt").css("left",e.pageX - 320)
		$("#popup_price").html($(this).parent().find(".diskNPrice").html() + ' руб.')
		$("#popup_id").html($(this).parent().find(".diskID").html())
		$("#popup_name").html($(this).parent().find(".diskName").html())
		$("#popup_img").html('<img src="'+$(this).parent().find(".diskImg").html()+'" style="max-width:112px; max-height:161px;" />')
		$(this).html("добавлен в корзину")
		$(this).attr("class","added-d")
		var href = $(this).attr("href")
		$(this).attr("href","#")
		$.get("/catalog/add2basket.php"+href,{},function(){ 
			basket_toggle()
			if(parseInt($("#basketQuantity").html())){
				$("#basketQuantity").html(parseInt($("#basketQuantity").html()) + 1)
				$("#basketPrice").html(parseInt($("#basketPrice").html()) + parseInt($("#popup_price").html()))
			}else{
				$(".cart").find(".b2").find("div").html('<b>В вашей корзине:</b><br>товара: <span id="basketQuantity">1</span> шт.<br>на сумму: <span id="basketPrice">'+parseInt($("#popup_price").html())+'</span>руб.<br><div><a href="/personal/order.php" class="posm">оформить</a> / <a href="/personal/basket.php" class="posm">посмотреть</a></div>')
			}
		})
	})
	
	$("#krestik_basket,#krestik_continue").click(function(){
		basket_toggle()
	})
	
	$(".podzakaz-a, .podzakaz-a-d").click(function(e){
		if($("#username").html()){
			$("#popup_message").find(".form-avt").css("top",e.pageY)
			$("#popup_message").find(".form-avt").css("left",e.pageX - 670)
			$("#offerid").html($(this).find(".diskID").html())
			$("#offerdisk").html($(this).find(".diskName").html())
			message_toggle()
			$("#sendoffer").css("display","block")
			$("#sendoffer").focus()
		}else{
			auth_toggle()
			$("#userLogin").focus()
		}
	})

	
	$("#sendoffer").click(function(){
		$("#authloader2").css("display","block")
		$.post("/catalog/preoffer.php",{"ID":$("#offerid").html(),"DISK":$("#offerdisk").html(),"NAME":$("#offername").html(),"EMAIL":$("#offeremail").html(),"PHONE":$("#offermobile").html(),"TEXT":$("#offertext").val()},function(r){

			$("#offeransw").html("Отправлено. Спасибо!")
			$("#authloader2").css("display","none")
			$("#offeransw").css("display","block")
			$("#sendoffer").css("display","none")
		})
	})
	
	$("#sendfb").click(function(){
		$("#authloader3").css("display","block")
		$.post("/fb.php",{"NAME":$("#fb_name").val(),"EMAIL":$("#fb_email").val(),"TEXT":$("#fb_msg").val()},function(r){

			$("#fbansw").html("Отправлено. Спасибо!")
			$("#authloader3").css("display","none")
			$("#fbansw").css("display","block")
			$("#sendfb").css("display","none")
		})
	})
	
	$("#krestik_message").click(function(){
		message_toggle()
	})

	$("#krestik_feedback").click(function(){
		feedback_toggle()
	})
	
	$("input[name=auth]").click(function(){
		$("#authloader").toggle()
		$.get("/ajax/login.php",{"login":$("input[name=login]").val(), "pass":$("input[name=pass]").val()}, function(r){
			if(r == "ok"){
				$("#authfail").css("display","none")
				if($("#frombasket").html() == 'Y'){
					window.location.href = "/personal/order.php"
				}else{
					window.location.href = $("#phpself").html()
				}
			}else{
				$("#authfail").css("display","block")
				$("input[name=pass]").val("")
			}
			$("#authloader").toggle()
		})
	})
	
	$(".item").click(function(){
		$(".body").html("<a href='"+$(this).parent().find(".imghref").html()+"'><img src='"+$(this).attr("href")+"'  width='415' border='0' height='238' /></a>")
		$(".text").html($(this).attr("alt"))
	})
	
	$(".link2fullview").click(function(){
		var id = $(this).attr("id")
		var iblock = $(".iblock"+id).html()
		
		$("#fullview_iblock"+iblock).html("<img src='"+$(this).attr("href")+"' style='max-width:200px; max-height:287px' /><span class='cena'>цена: <b id='cena_iblock"+iblock+"'>"+$("#cena"+id).html()+"</b> руб.</span>")
		
		$("#link_iblock"+iblock).html($("#name"+id).html())
		$("#link_iblock"+iblock).attr("href",$("#name"+id).attr("href"))
	})
	
	$(".block2").click(function(){
		$("#img_toggle_main").html('<a href="'+$(this).find(".img_link").html()+'"><img border="0" src="'+$(this).find(".img_toggle").html()+'" alt="'+$(this).find(".img_name").html()+'" style="max-width:229px; max-height:226px" /></a>')
	})
	
})

function auth_toggle(){
	$("#popup_auth").toggle()
	$(".fon-a").toggle()
}

function basket_toggle(){
	$("#popup_basket").toggle()
	$(".fon-a").toggle()
}

function message_toggle(){
	$("#popup_message").toggle()
	$(".fon-a").toggle()
	$("#authloader2").css("display","none")
	$("#offeransw").css("display","none")
}

function feedback_toggle(){
	$("#popup_feedback").toggle()
	$(".fon-a").toggle()
	//$("#authloader2").css("display","none")
	//$("#offeransw").css("display","none")
}
