



$(document).ready(function(){

	var curr_prod_id = 0;
	var curr_id_model = '';
	var curr_element;

	

	$('a.ma_link_lexika, a.ma_basket_link_lexika').bind('click',function(e) {
		e.preventDefault();
		var id = $(this).attr('lang');
 		curr_id_model = $(this).attr('id');
		var winH = $(this).offset().top;
		var winW = $(window).width();
		
//		alert($(this).attr('name'));
		if ($(this).attr('name') == -2) $('.attention_about_rezerz',id).css('display','block');
		
		
	    $(id).css('top',winH-$(id).height()/2);
		$(id).css('left',winW/2-$(id).width()/2);
		$(id).fadeIn(1000);
		curr_element = $(this);
		$(id).focus();
		$('.submit').focus();
		

	});

	  

	  

	$('.submit').bind('click',function(){
		//добавляем в корзину товар с помощью ajax
		//alert('f');
		JsHttpRequest.query(
            'hr_gate.php?test=500&r='+Math.random(),
//            'ims/logic/hr_compare.php?r='+Math.random(),
            {
                'sp': "prod_in_basket",
				'add_id': curr_id_model,
				'prod_count': $('input[name=prod_count]').val()
            },
            function(result, errors) {
        	   if (errors) alert(errors);
				if (result) {
					if (result.count_prod) {
						update_count_basket(result.count_prod, result.total_sum);
						if (!empty(curr_id_model) && !empty(result.prod_added_count)) {
							$.cookie("cart_"+curr_id_model, '{"count":'+result.prod_added_count+'}', { path: '/', expires: 7 });
//							$.cookie("cart_11868", null); // * delete cookie
//							alert(curr_id_model + " "+ result.prod_added_count);
						}
					}
					if (result.goBasket) {
//						alert(base_url); return;
						document.location = base_url + "/ordering.htm";}
                }
            },
            true  //disable caching
        );

			var base_url = $("base").attr('href')+lng;

		$(curr_element).html('<a class="compare_ordering" href="'+base_url+'/ordering.htm">'+prod_in_basket+'</a>');
		$(curr_element).attr('title',go_to_basket);
		$(curr_element).unbind();
		$('.window').fadeOut(500);

	}); 

	


	$('.close').bind('click',function(){
		$('.window').fadeOut(500);
	}); 

	

	



});



function update_count_basket(count, sum){
	if (count && $('span#cart_count')) {
		$('span#cart_count').html(count);
		$('span#cart_sum').html(sum);

	}
	if (count && window.opener && !window.opener.closed){
		//alert('opener  ['+count+' шт.]');
		window.opener.$('span#cart_count').html(count);
		window.opener.$('span#cart_sum').html(sum);
	}
}








