$(document).ready(function(){
        /*Вставка баннера на главную страницу*/
        if($('div#main_page_banner').length)
                $('div#main_page_banner').load('ban.php');
         
		$("#vertical-multilevel-menu").slideToggle("slow");		 


	function deleteFromBasketAjax(id){
		jsAjaxUtil.LoadData('/catalog/deletefrombasket.php?&id='+id,x_deleteCallback);
	}
	function x_deleteCallback(data){
		alert('Deleted');
	}
	
	$('.item input[type=checkbox].commercial_chechbox').click(function(){
		if(this.checked == true){
			$(this).attr("checked", "checked");

		}else{
			$(this).removeAttr("checked");
		}
		 var cb_id = $(this).attr("id");
		 var obj = document.getElementById(cb_id);


	     if(this.checked == true)
	     {
			jsAjaxUtil.LoadData('/commercial_offer/add2commoffer.php?&id='+cb_id+'&ajax_request_variable=1',resultcom);
	     } //if checked
	     else
	     {
			jsAjaxUtil.LoadData('/commercial_offer/scleanByID.php?id='+cb_id+'&ajax_request_variable=1',resultcom);
		 }
	});
	function resultcom(data)
	{
		if (data.length>1){
			data = eval('('+data+')');
			if(data.check==1)
			{
				if($("#rightCommerceBlock").length>0){//если элемент уже присутствует в DOM - добавляем только блок товара, если же нет - добавляем в DOM весь блок коммерческих предложений с первым товаром
					if($("#rightCommerceBlock .prod:last").hasClass("light"))
						  $("#rightCommerceBlock .prod:last").after("<div id='comm_offer"+data.id+"' class='prod'><a style='float:right;  margin-right: 10px;' title='Удалить' onclick='deleteFromCommOfferAjax("+data.id+"); return false;' href='#'><img alt='Удалить' src='/images/new/icon-delete.png'/></a><div class='name'>"+data.name+"</div></div>")
					else
						 $("#rightCommerceBlock .prod:last").after("<div id='comm_offer"+data.id+"' class='prod light'><a style='float:right; margin-right: 10px;' title='Удалить' onclick='deleteFromCommOfferAjax("+data.id+"); return false;' href='#'><img alt='Удалить' src='/images/new/icon-delete.png'/></a><div class='name'>"+data.name+"</div></div>")
				}
				else{
					$(".rightSidebar:last").append("<a name='commerce'>&nbsp</a><div id='rightCommerceBlock' class='commercial_div commerce' style='margin-bottom: 15px;'><a name='commerce'> </a><div class='wrapper' style='overflow: hidden;'><a name='commerce'>  </a><h2><span class='commercial'><a name='commerce'>&nbsp;</a></span></h2><a name='commerce'>   </a><div class='itemBasket'><div id='comm_offer"+data.id+"' class='prod'><a style='float:right; margin-right: 10px;' title='Удалить' onclick='deleteFromCommOfferAjax("+data.id+"); return false;' href='#'><img alt='Удалить' src='/images/new/icon-delete.png'/></a><div class='name'>"+data.name+"</div></div><div class='buy_it'><a href='/commercial_offer/' class='btn'><span>Сформировать</span></a></div></div></div></div>");
				}
			}
			else
			{
				$('#comm_offer'+data.id).remove();
				if($("#rightCommerceBlock .prod").length<=0)
					$('#rightCommerceBlock').remove();	
			}
		}
	}
	
	
	$('.item input[type=checkbox].comparing_chechbox').click(function(){
		if(this.checked == true){
			$(this).attr("checked", "checked");

		}else{
			$(this).removeAttr("checked");
		}
		 var cb_id = $(this).attr("id");
		 var obj = document.getElementById(cb_id);


	     if(this.checked == true)
	     {
	    	 jsAjaxUtil.LoadData('/catalog/ajax_compare.php?action=ADD_TO_COMPARE_LIST&id='+cb_id,result);
	     } //if checked

	      else
	      {
	    	  jsAjaxUtil.LoadData('/catalog/ajax_compare.php?action=DELETE_FROM_COMPARE_LIST&id='+cb_id,result);
	      }
	     

	});

	

	function result(data)
	{
		if (data.length>1){
			data = eval('('+data+')');
			if(data.check==1)
			{
				if($("#block-catalog-compare-list").length>0)
				{
					$("#block-catalog-compare-list .prod:last").after("<div id='compare"+data.id+"' class='prod'><input type='hidden' name='ID[]' value='"+data.id+"'/><a style='float:right; margin-right: 10px;' title='Удалить' href='?action=DELETE_FROM_COMPARE_LIST&id="+data.id+"' rel='nofollow'><img alt='Удалить' src='/images/new/icon-delete-a.png'/></a><div class='name'><a href='"+data.link+"'>"+data.name+"</a></div></div>");
					document.getElementById('button_compare').style.display='block';
				}
				else
				{
					$("#compare-block").html("<div id='block-catalog-compare-list'><div id='compare"+data.id+"' class='prod'><input type='hidden' name='ID[]' value='"+data.id+"'/><a style='float:right; margin-right: 10px;' title='Удалить' href='?action=DELETE_FROM_COMPARE_LIST&id="+data.id+"' rel='nofollow'><img alt='Удалить' src='/images/new/icon-delete-a.png'/></a><div class='name'><a href='"+data.link+"'>"+data.name+"</a></div></div></div>");
				}
			}
			else
			{
				$('#compare'+data.id).remove();
				if($("#block-catalog-compare-list .prod").length<=0)
				{
					$('#block-catalog-compare-list').remove();	
					document.getElementById('button_compare').style.display='none';
				}
				else if($("#block-catalog-compare-list .prod").length<=1)
					document.getElementById('button_compare').style.display='none';
			}
		}
		var checkednum = 0;
	    var n = $("input[type=checkbox]");
	    var i = 0;
	    for(i=0;i<n.length;i++){
	    	if (n[i].checked == true)
	    		checkednum++;
	    }
	    
	    var path = "/catalog/compare.php";
	    
	    if(checkednum>1){ 
	    	if($("input[type=checkbox].mod").length>0){//если мы на жмем на ссылку на каоточке товара в модификациях
	    	   $(".transform").html(' <a href="/catalog/compare.php?prod_type=33" >сравнить</a>');
	    	   
	    	}
                else{
                    $(".transform").html(' <a href="/catalog/compare.php" >сравнить</a>');
	    	   $(".compare_link").html('<a href="/catalog/compare.php" >Моделей в сравнении</a>&nbsp;'+checkednum);
	    	
                }
	    }
	    else { 
	    	
	    	$(".transform").html('');
	    	$(".compare_link").html('Моделей в сравнении '+checkednum);
	    	
	    }
	    //alert(data);
	}

	
	
	
	
	
		
		
	
  $(".radio_label").click(function () {
	$("span", this).toggleClass("checked");
});
  

 function res(){ 
  	var width_all = 0;
  	var width_menu = $('.menu').width();
	$(".menu li").each(function(){
	  width_all += $(this).width();	  
	});	
   var padd_all = $(".menu li").length;
   var padd = (width_menu - width_all)/padd_all/2-1;
   $(".menu li").css('paddingLeft', padd);
   $(".menu li").css('paddingRight', padd);
  }
  
//  res();
  window.onresize = function(){
//        res();
        divHider(207, "new");
	divHider(207, "spec");
        divHider(207, "lead");
        
  };
 
  $(".search").hover(function() {
		  $(this).css('background-image', 'url(/images/search-bg.png)');							
	
	});

   if ($(".description .model-photo img").height()<90)
  		{
												
		$(".description .model-photo img").css('marginTop',65);	
		$(".description .model-photo img").css('marginBottom',25)	
												
	}

        $(".sub_menu li:not(.actived)").hover(function() {
		  $(this).addClass('actived');							
	
	}, function() {
		  $(this).removeClass('actived');		
	});
	
	$(".sub_menu li").hover(function() {
		  $(".und", this).fadeIn('fast');
	}, function() {
		  $(".und", this).fadeOut('fast');
	});
	
  	$(".container .item:last").css('background-image', 'none');	
	$(".new .item:last").css('background-image', 'none');	
	$(".menu ul li:last").css('background-image', 'none');
	$(".pagination a:last").css('background-image', 'none');
	$(".tabs li:last a").addClass('lasted');
	
	$('a.modal_link').click(function (e) {
		e.preventDefault();
		$('#modal').modal({onOpen: modalOpen});
	});	
		 

  	divHider(207, "new");
	divHider(207, "spec");
        divHider(207, "lead");
  
  	$('#container1').tabs({ /*fxSlide: true,*/ fxFade: true, fxSpeed: 'normal' });
});


function checkToggle_(obj,addurl,delurl){
	if($(obj).attr('checked')==true) 
		window.location=delurl;
	else 
		window.location=addurl;
}




var divCount = 0;
var divCounter = 0;
function divHider(div_width, cl){
    divCount = parseInt($("."+cl).width()/div_width);
    divCounter = 0;
	if(cl!="spec")
		$("."+cl+" .item").each(divHiderHandler);
	$($(".new .item").get(divCount-1)).css('background-image', 'none');	
        $($(".lead .item").get(divCount-1)).css('background-image', 'none');		

}
var divHiderHandler = function(){
	divCounter++;
	if(divCounter > divCount) $(this).hide();
	else this.style.width = (100/divCount)-1+"%";
}



function reDraw(arg){
	var inpList = document.getElementsByTagName('span');
	for(i = 0; i < inpList.length; i++){
		if(inpList[i].className == 'radioInp'){
			inpList[i].style.backgroundPosition = '0 -25px';
		}
	}
	
	
	document.getElementById('sp_' + arg).style.backgroundPosition = '0 0';
	
}


function modalOpen (dialog) {
	dialog.overlay.fadeIn('slow', function () {
		dialog.container.fadeIn('slow', function () {
			dialog.data.slideDown('slow');
		});
	});
}
function addToCompare(id)
{
	jsAjaxUtil.LoadData('/catalog/ajax_compare.php?action=ADD_TO_COMPARE_LIST&id='+id,result_c);
}
function result_c(data)
{
	if (data.length>1)
	{
		data = eval('('+data+')');
		if(data.check==1)
		{
			if($("#block-catalog-compare-list").length>0)
			{
				$("#block-catalog-compare-list .prod:last").after("<div id='compare"+data.id+"' class='prod'><input type='hidden' name='ID[]' value='"+data.id+"'/><a style='float:right; margin-right: 10px;' title='Удалить' href='?action=DELETE_FROM_COMPARE_LIST&id="+data.id+"' rel='nofollow'><img alt='Удалить' src='/images/new/icon-delete-a.png'/></a><div class='name'><a href='"+data.link+"'>"+data.name+"</a></div></div>");
				document.getElementById('button_compare').style.display='block';
			}
			else
			{
				$("#compare-block").html("<div id='block-catalog-compare-list'><div id='compare"+data.id+"' class='prod'><input type='hidden' name='ID[]' value='"+data.id+"'/><a style='float:right; margin-right: 10px;' title='Удалить' href='?action=DELETE_FROM_COMPARE_LIST&id="+data.id+"' rel='nofollow'><img alt='Удалить' src='/images/new/icon-delete-a.png'/></a><div class='name'><a href='"+data.link+"'>"+data.name+"</a></div></div></div>");
			}
		}
		else
		{
			$('#compare'+data.id).remove();
			if($("#block-catalog-compare-list .prod").length<=0)
			{
				$('#block-catalog-compare-list').remove();	
				document.getElementById('button_compare').style.display='none';
			}
			else if($("#block-catalog-compare-list .prod").length<=1)
				document.getElementById('button_compare').style.display='none';
		}
	}
}
