$(document).ready(function(){
	/*
	 * добавляем товар в корзину и переходим к офформлению заказа
	 * */
	$('.popup_basket').click(function (){
		var id = $(this).attr('rel');
		
		var color = $('#color_'+id).attr('value');
		var posit = $('#posit_'+id).attr('value');
		var count = $('#count_'+id).attr('value');
		
		//$(this).attr('href', '/popup_basket.php?item='+id+'&color='+color+'&posit='+posit);
		
		$.get('/includes/ajax/add_basket.php', { id: id, color: color, posit: posit, count: count }, function(json){
			//window.location.href = '/basket/';
			
			docHght = $(document).height();
			$("#darkBg").css("height", docHght);
			$("#darkBg").css("opacity", 0.7);
			$("#darkBg").fadeIn("normal");
			topH = $(document).scrollTop() + 100;
			$("#flyBuy").css("top", topH);
			$("#flyBuy").fadeIn("slow");
			
			$('.basketBox a').html(json);
			
			return false;
		});
		
		return false;
	})
	
	$(".left1 ul a").click (function (){
		var pic_big = $(this).attr('href');
		var pic = $(this).children('img').attr('rel');
		
		$(".left1 ul li").removeClass('active');
		$(this).parent().addClass('active');
		
		$('.left1 .img a').attr('href', pic_big);
		$('.left1 .img a img').attr('src', pic);
		
		return false;
	})
	
	$("#darkBg").click(function(){ 
		$(this).fadeOut("fast");
		$("#flyBuy").fadeOut("fast");
	});
	
	$("#closeBuy").click(function(){ 
		$("#darkBg").fadeOut("fast");
		$("#flyBuy").fadeOut("fast");
		
		return false;
	});
	
	$('.delete').click(function (){
		var id = $(this).attr('id');
		
		$.get('/includes/ajax/del_basket.php', { id: id }, function(json){
			window.location.href = '/basket/';
			
			return false;
		});
	})
	
	$('.b-table .minus').click (function (){
		var id = $(this).attr('id');
		
		$.get('/includes/ajax/inc_dec_basket.php', { id: id, action: 0 }, function(json){
			//window.location.href = '/basket/';
			var node = parseJSON(json);
			
			$('#price_'+node.id).html(node.price_id);
			$('#all_price').html(node.all_price);
			
			return false;
		});
	})
	
	$('.b-table .plus').click (function (){
		var id = $(this).attr('id');
		
		$.get('/includes/ajax/inc_dec_basket.php', { id: id, action: 1 }, function(json){
			//window.location.href = '/basket/';
			var node = parseJSON(json);
			
			$('#price_'+node.id).html(node.price_id);
			$('#all_price').html(node.all_price);
			
			return false;
		});
	})
	
	$('.select select').change(function(){
        myVal = $('option:selected', this).text();
        $('p', $(this).parent()).html(myVal);
    });
	
	$('#sendZak').click (function (){
		$('#target').submit();
	})
	
	$('#autoriz_btn').click (function (){
		$('#autoriz').submit();
	})
	
	$(".e .e1 .pic").fancybox({
		"hideOnContentClick": false,
		"titlePosition"  : "inside",
		"overlayOpacity": "0.7",
		"overlayColor": "black"
	});
	
	$(".left1 .img a").fancybox({
		"hideOnContentClick": false,
		"titlePosition"  : "inside",
		"overlayOpacity": "0.7",
		"overlayColor": "black"
	});
	
	$(".colorList li a").fancybox({
		"hideOnContentClick": false,
		"titlePosition"  : "inside",
		"overlayOpacity": "0.7",
		"overlayColor": "black"
	});
	
	$(".awardsList li a").fancybox({
		"hideOnContentClick": false,
		"titlePosition"  : "inside",
		"overlayOpacity": "0.7",
		"overlayColor": "black"
	});
	
	$(".b-table .pic").fancybox({
		"hideOnContentClick": false,
		"titlePosition"  : "inside",
		"overlayOpacity": "0.7",
		"overlayColor": "black"
	});
	
    // AFTER DOCUMENT LOAD
	rSize();
    $('.cnt .minus').click(function(){
		pointer = $('input', $(this).parent());
		v = $(pointer).val();
		if ( v > 1 ) {
			$(pointer).val(v-1);
		}
	});
	$('.cnt .plus').click(function(){
		pointer = $('input', $(this).parent());
		v = parseInt($(pointer).val());
		$(pointer).val(v+1);
	});
});
$(window).resize(function(){rSize();});


function rSize() {
	h = $('#work_page .contentBlock .rightBlock .left').height() + parseInt($('#work_page .contentBlock .rightBlock .left').css('padding-bottom'));
	$('#lBox').css('height', h);
	$('#rBox').css('height', h);
	//alert($('#work_page .contentBlock .rightBlock .left').height());
}

function parseJSON(text){
	if (text.replace(/\\./g, '@').
			replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
			replace(/(?:^|:|,)(?:\s*\[)+/g, '')) {

		j = eval('(' + text + ')');

		return typeof filter === 'function' ? walk('', j) : j;
    }else{
		return false;
	}
}

