function add_to_cart(obj) {
	 
	  // ADDONS
	var addon='';
	$.each($(".addon"),function(){
	if ($(this).attr('value')) addon=addon+"&addon["+$(this).attr('id')+"]="+$(this).attr('value')
	});
	//addons END;
	
	$('#cart_dialog').dialog('open');
	$.getJSON("./data/cart.php","cs=ש&action=add_product&pid="+$(obj).attr('product_id')+addon,function (data) {
		
		show_cart_data(data);
		
	});
	
	
	
}




function plus_cart(obj) {
	// $('#cart_table').html("");
	
	//alert() ;
	$('#cart_dialog').dialog('open');
	$.getJSON("./data/cart.php","cs=ש&action=more_product&key="+$(obj).attr('product_index'),function (data) {
		
		show_cart_data(data);
		
	});
	
}

function minus_cart(obj) {
	 //$('#cart_table').html("");
	
	//alert() ;
	$('#cart_dialog').dialog('open');
	$.getJSON("./data/cart.php","cs=ש&action=less_product&key="+$(obj).attr('product_index'),function (data) {
		
		show_cart_data(data);
		
	});
	
}




function remove_from_cart(obj) {
	 //$('#cart_table').html("");
	
	//alert() ;
	$('#cart_dialog').dialog('open');
	$.getJSON("./data/cart.php","cs=ש&action=remove_product&key="+$(obj).attr('product_index'),function (data) {
		
		show_cart_data(data);
		
	});
	
}






function show_cart(){
	
	$('#cart_dialog').dialog('open');

}



function draw_cart(){
	
	$("#minicart_table").hide();
	$.getJSON("./data/cart.php","cs=ש",function (data) {
		
		show_cart_data(data);
		
	});


}

function show_cart_data(data){
	
	var carthtml='';
	var minicarthtml='';
	
		if (data.subtotal==0 || data.subtotal=='-') carthtml='הסל ריק';
		else {
				carthtml=carthtml+='<TR class="ui-widget-header ui-corner-all cart_header"><TD></TD><TD>שם המוצר</TD><TD colspan=3 style="text-align:center;">כמות</TD><TD>מחיר</TD><TD>סה"כ</TD></TR>';
				
				$.each(data.products,function (index,value){
					if (this.amount>1) minicarthtml=minicarthtml+'<div class=minicart_item>'+this.amount+' יחידות <span class=minicart_name>'+this.name+'</span> ב: <span class=minicart_price>'+this.price+' </span>ליחידה, סה"כ: <span class=minicart_price>'+this.total+'</span></div>';
					else minicarthtml=minicarthtml+'<div class=minicart_item><span class=minicart_name>'+this.name+'</span> במחיר: <span class=minicart_price>'+this.total+'</span></div>';
					
					carthtml=carthtml+'<TR class="ui-widget-header ui-corner-all cart_product"><TD><SPAN class=remove_from_cart product_index='+index+' ><span class="ui-icon ui-icon-closethick"></span></TD><TD class=cart_name>'+this.name+'</TD><TD ><span product_index='+index+' class="minus_cart ui-icon ui-icon-minusthick"></span></td><td>'+this.amount+'</td><td><span product_index='+index+' class="plus_cart ui-icon ui-icon-plusthick"></span></TD><TD>'+this.price+'</TD><TD>'+this.total+'</TD></TR>';
					
				});
		minicarthtml=minicarthtml+'<div class=minicart_total>סה"כ: '+data.subtotal+' </div>';
		minicarthtml=minicarthtml+'<div id=minicart_cash><img src="images/templates/chabadshop/he/cash.jpg"></div>';
		
		carthtml=carthtml+'<TR class="ui-widget-header ui-corner-all cart_total"><TD></TD><TD class=cart_name></TD><TD ></td><td></td><td></TD><TD>סה"כ</TD><TD>'+data.subtotal+'</TD></TR>';
		$("#minicart_table").show();
		}
		
		$('#cart_table').html(carthtml);
		$('#cart_container').html(minicarthtml);

}



function searchSuggest() {


		if (typeof $("body").data("before_search") == 'undefined' || $("body").data("before_search")=='') {
		
				$("body").data("before_search",$(".main_col_container").html());

				
			}
			
if 	($("#search_input").val().length==0) {
	
					$(".main_col_container").html($("body").data("before_search"));
					$("body").data("before_search",'');
					return
	
}

			
if 	($("#search_input").val().length<2) {
	
					return;
}




// start search ///
$(".main_col_container").html("<div id=close_and_searchname><div id=closesearch_container class='ui-state-default ui-corner-all'></div><span id='searchname'></span></div><div id=catpics_container></div><div id=category_item></div><div id=products_items></div>");


			$("#closesearch_container").html("<A id='close_search'>סגור חיפוש</a>");
			$("#searchname").html("מחפש: "+$("#search_input").val());
			

				$('#closesearch_container').click(function(){

					$(".main_col_container").html($("body").data("before_search"));
					$("body").data("before_search",'');
				});
			
			

		$.getJSON("./data/dataserver.php","cs=ש&action=search_product&search="+$("#search_input").val(),function (data) {
				
			var products_search='';
			products_search=products_search+'<ul>';
			
			
			
			$.each(data,function (index,value){
					
					products_search=products_search+'<li><span class="product_catalog_item  ui-corner-all"><a href="?template=prod&prod_id='+this.id+'"><IMG src="images/products/160/'+this.id+'.jpg"><DIV class="mover"></DIV><DIV class="product_name">'+this.name+'</DIV><DIV class="product_desc">'+''+'</DIV><DIV class="product_price">'+this.price+'</DIV></a><DIV class="product_buttons"><a href="?template=prod&prod_id='+this.id+'&cat=$cat"><span class="product_details ui-state-default ui-corner-all">פרטים</span></a><span class="cart_add product_buy ui-state-default ui-corner-all" product_id="'+this.id+'">הוסף לסל</span></DIV></span></li>';
			
			});
			products_search=products_search+'</ul>';
			$("#products_items").html(products_search);
			
			if (data.length==0) $("#products_items").html("<div align=center>לא נמצאו מוצרים התואמים את החיפוש</div>");
		});
		
		
		 
		$.getJSON("./data/dataserver.php","cs=ש&action=search_lable_name&search="+$("#search_input").val(),function (data) {
				
			var lable_search='';
			lable_search=lable_search+'<ul>';
			
			
			
			$.each(data,function (index,value){
					
					lable_search=lable_search+'<li><span class="catpic"><a href="?cat='+this.lable_id+'"><img border="0" src="images/cats/'+this.pic+'.jpg"><div class="catpic_name">'+this.lable_name+'</div></a></span></li>';
					
			});
			lable_search=lable_search+'</ul>';
			$("#catpics_container").html(lable_search);
			
		});
		



}


/////////////////////////////////////////////////////##
var timeout_reserver = null;

			


		
			
			$(function(){ 
				$.ajaxSetup({
				contentType: "application/x-www-form-urlencoded;charset=windows-1255",
				cache:false
				});
				
				
				
				
			
			
				
				draw_cart();
			
				
				
				
	
$(".catpic img").live('mouseover mouseout', function(event) {
					  if (event.type == 'mouseover') {
						
						$(this).animate({
		
								 opacity: 0.5								
								
						});

						
					  } else {

							$(this).animate({

							 opacity: 1
							
							});

						
					  }
});


$(".product_catalog_item a img").live('mouseover mouseout', function(event) {
					  if (event.type == 'mouseover') {
						
						$(this).animate({
		
								 opacity: 0.75								
								
						});

						
					  } else {

							$(this).animate({

							 opacity: 1
							
							});

						
					  }
});
				
			
				
				$(".cart_add").live("click",function(){add_to_cart(this)});
				$(".remove_from_cart").live("click",function(){remove_from_cart(this)});
				$(".plus_cart").live("click",function(){plus_cart(this)});
				$(".minus_cart").live("click",function(){minus_cart(this)});
				$("#open_cart").click(function(){show_cart()});
				$("#minicart_cash").click(function(e){e.stopPropagation();window.location='?template=checkout';});
				
				
				$('#search_input').bind("keyup",function(){
				
				if (timeout_reserver != null) clearTimeout(timeout_reserver);
				timeout_reserver =setTimeout('searchSuggest()', 500); 
				
	
				
				});
				
				
				
				$("#minicart_table").live("click",function(){show_cart()});
				
				
				$("#open_audiodisk").live("click",function(){
				
					$('#general_dialog').dialog('open');
					$('#general_dialog').html ('<object type="application/x-shockwave-flash" data="/dwplayer/dewplayer-playlist.swf" width="340" height="400"><param name="wmode" value="transparent" /><param name="movie" value="/dwplayer/dewplayer-playlist.swf" /><param name="flashvars" value="xml=/php/mp3xml.php?id='+$(this).attr('prod_id')+'&amp;autostart=1"/></object>');
				});
				
				
				 
				
				
				$("#open_video").live("click",function(){
					
				$('#general_dialog').html ('<div id="container_92">Loading the player ...</div>');
					jwplayer("container_92").setup({ flashplayer: "./jwplayer/player.swf", file: $(this).attr('video_name'), height: 400, width: 550 }); 
					
					
					$('#general_dialog').dialog('open');
				});
				
				
				




				$('#cart_dialog').dialog({
					autoOpen: false,
					modal: true,
					width: 880,
					height: $(window).height()*0.8,
					buttons: {
						"חזור לחנות": function() { 
							$(this).dialog("close"); 
						},
					
					
					
					
						"עבור לקופה": function() { 
							$(this).dialog("close");
							window.location='https://secure.chabadshop.co.il/?template=checkout&phpsess='+$('body').data('phpsess');
						}
						
						
						
					}
					
				});
				
				
				$('#general_dialog').dialog({
					autoOpen: false,
					modal: true,
					width: 880,
					height: $(window).height()*0.8,
					buttons: {
						"סגור": function() { 
							$(this).dialog("close"); 
						}
						
						
					}
					
				});

		
		

				
				

			});

			
