if(location.hash=="#cc") {
	empty_basket();
}
$(function() {
	var prods=get_prods(),i;
	$(".add_to_basket").each(function(i) {
		if($(this).attr('id')) {
			id = $(this).attr('id').match(/(\d+)/)[0];
			for(i=prods.length-1;i>=1;--i)
				if(prods[i].id==id) {
					//mark_as_added(this);
					break;
				}
		}
	});
	$("#basket_table tr:even td").css("background","#F2E8F0");
	$("#basket_form #delivery_address").focus(function() {
		if($("#delivery_address").val() == "")
			$("#delivery_address").val($("#address").val());
	});
	$("#credit_name").focus(function(){
		if($("#credit_name").val()=='')
			$("#credit_name").val($("#fname").val());//$("#credit_name").val($("#fname").val()+' '+$("#lname").val());
	});
	$(".small_prod_list .prod").css("float","left"); //M$ IE7
});

/*ddaccordion.init({
	headerclass: "submenuheader",
	contentclass: "submenu",
	collapseprev: true, 
	defaultexpanded: [],
	animatedefault: false,
	persiststate: true,
	toggleclass: ["", ""],
	togglehtml: ["suffix", "", ""],
	animatespeed: "normal"
});*/

function add_to_basket(id, o, q, a) {
	var i;
	if(q == undefined)
		q = 1;
	if(a == undefined)
		a = '';
	else if(a == '*') {
		var $at = $('.attribute');
		if($at.length > 0) {
			a = '(';
			for(i=0;i<$at.length;++i) {
				if(i>0)
					a+= ', ';
				a += $at.get(i).title + " ";
				a += $at.get(i).value;
			}
			a += ')';
		}
	}

	var prods=get_prods(),i;
	for(i=1;i<prods.length;++i)
		if(prods[i].id==id && prods[i].a==a)
			break;
	if(i<prods.length) {
		prods[i].q = parseInt(prods[i].q) + parseInt(q);
	} else {
		prods[i] = {"id" : id, "q" : q, "a" : a} 
	}
	set_prods(prods);
	if(o!=undefined) {
		mark_as_added(o);
		light_basket_button();
	}
	update_basket_table(id,prods[i].q,a)
}
function sub_from_basket(id, a) {
	if(a == undefined)
		a = '';
	var prods=get_prods(),i;
	for(i=1;i<prods.length;++i)
		if(prods[i].id==id && prods[i].a==a)
			break;
	if(i<prods.length && prods[i].q>0) {
		var new_q = prods[i].q-1;
		if(prods[i].q>1) {
			prods[i].q--;
		} else {
			var j,k,new_prods=[];
			for(j=0,k=0;j<prods.length;++j,++k) {
				if(j!=i)
					new_prods[k] = prods[j];
				else
					--k;
			}
			prods = new_prods;
		}
		set_prods(prods);
		update_basket_table(id,new_q,a)
	}
}

function basket_form_check() {
	if($("#fname").val() == "") {
		alert("יש להזין שם.");
		return false;
/*	} else if($("#lname").val() == "") {
		alert("יש להזין שם משפחה.");
		return false;*/
	} else if(!/^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z0-9_\-\.]+$/.test($("#email_address").val())) {
		alert("יש להזין כתובת דוא\"ל תקינה.");
		return false;
/*	} else if($("#phone_number").val() == "") {
		alert("יש להזין מספר טלפון.");
		return false;*/
	} else if($("#cellphone_number").val() == "") {
		alert("יש להזין מספר טלפון נייד.");
		return false;
	} else if($("#address").val() == "") {
		alert("יש להזין שם רחוב.");
		return false;
	} else if($("#address_num").val() == "") {
		alert("יש להזין מספר בית.");
		return false;
	} else if($("#address_city").val() == "") {
		alert("יש להזין שם עיר.");
		return false;
/*	} else if($("#delivery_address").val() == "") {
		alert("יש להזין כתובת למשלוח.");
		return false;*/
	} else if($("#credit_where_1").is(':checked') && $("#credit_name").val() == "") {
		alert("יש להזין שם בעל כרטיס האשראי.");
		return false;
	} else if($("#credit_where_1").is(':checked') && $("#credit_id").val() == "") {
		alert("יש להזין מספר תעודת זהות.");
		return false;
	} else if($("#credit_where_1").is(':checked') && $("#credit_num").val() == "") {
		alert("יש להזין מספר כרטיס אשראי.");
		return false;
	} else if(!$("#conditions").is(':checked')) {
		alert("יש לאשר קריאת התקנון.");
		return false;
	} else
		return true;
}

/****/

function show_hide_credit() {
	if($("#credit_where_1").is(':checked')) {
		$("#credit_tbody").show();
	} else {
		$("#credit_tbody").hide();
	}
}

function update_basket_table(prod_id,prod_q,prod_a) {
	if($("#basket_table").length==0)
		return;
	var i;
	var trs = $("#basket_table .tr_"+prod_id);
	for(i=0;i<trs.length;++i) {
		tr = $(trs.get(i));
		if(tr.get(0).title == prod_a)
			break;
	}
	if(i < trs.length) {
		tr.children("td").eq(3).text(prod_q);
		v=parseInt(tr.children("td").eq(1).text());
		tr.children("td").eq(5).text(prod_q*v + ' ₪');
		if(prod_q == 0) {
			tr.remove();
		}
	}
	var total=0, count=0;
	var td = $("#basket_table td:nth-child(6n)");
	for(i=td.length-1; i>=0; --i)
		total+=parseInt(td.eq(i).text());

	td = $("#basket_table td:nth-child(6n+4)");
	for(i=td.length-1; i>=0; --i)
		count+=parseInt(td.eq(i).text());
	/*
	if(count<=3) {
		total+=count*mail_price_up_to_3;
		$("#mail_price_for_this_basket").text(count*mail_price_up_to_3);
	} else {
		$("#mail_price_for_this_basket").text(0);
	}
	*/
	//"mail_price_up_to_3" now is const value for each inv.
	total+=mail_price_up_to_3;
	$("#mail_price_for_this_basket").text(mail_price_up_to_3);
	
	$("#basket_total_total").text(total);
	$("#basket_total_count").text(count);

	/*var si = $("#credit_backnum").get(0).selectedIndex;
	if(total>399) {
		if(total>699) {
			$("#credit_backnum").parent().html('<select name="credit_backnum" id="credit_backnum"><option value="1">תשלום אחד</option><option value="2">שני תשלומים</option><option value="3">שלושה תשלומים</option></select>');
		} else {
			$("#credit_backnum").parent().html('<select name="credit_backnum" id="credit_backnum"><option value="1">תשלום אחד</option><option value="2">שני תשלומים</option></select>');
			si = Math.min(si, 1);
		}
	} else {
		$("#credit_backnum").parent().html('<select name="credit_backnum" id="credit_backnum"><option value="1">תשלום אחד</option></select>');
		si = 0;
	}
	$("#credit_backnum").get(0).selectedIndex = si;*/
}
$(function(){
	if($("#basket_table").length==1)
		update_basket_table(0,0);
});
function empty_basket() {
	$.cookie('basket', JSON.stringify([ { "id" : 0, "q" : 0, "a" : '' } ]), { expires: 3, path: '/'});
	location.href=location.href;
}
function get_prods() {
	var prods;
	if($.cookie('basket')==undefined || $.cookie('basket')=='') {
		prods = [ { "id" : 0, "q" : 0, "a" : '' } ];
	} else {
		prods = JSON.parse($.cookie('basket'));
	}
	return prods;
}
function set_prods(prods) {
	$.cookie('basket', JSON.stringify(prods), { expires: 3, path: '/'});
}
function mark_as_added(o) {
	if(o!=undefined)
		$(o).html($(o).html().replace("ket","ket-v").replace(/הוסף לסל/g,"התווסף")).removeAttr("onclick");
}
function light_basket_button() {
	$("#basket_link").animate({ 
		opacity: 0
	}, 500, 'linear', function() {
		$("#basket_link").css("border-color","yellow");
		$("#basket_link").animate({ 
			opacity: 1
		}, 1000, 'linear', function() {
			$("#basket_link").animate({ 
				opacity: 0
			}, 1000, 'linear', function() {
				$("#basket_link").css("border-color","#6B133D");
				$("#basket_link").animate({ 
					opacity: 1
				}, 500);
			});
		});
	});
}

function send2friend() {
	$("#send2friend").remove();
	$(".prod_page").before(
		"<div id='send2friend' style='text-align:center;width:300px;margin:0 auto; display:none'>" +
			"<table>" +
				"<tr>" +
				"	<th>שם השולח/ת:</th>" +
				"	<td><input type=\"text\" id=\"send2friend_s_name\" /></td>" +
				"</tr>" +
				"<tr>" +
				"	<th>כתובת דוא\"ל השולח/ת:</th>" +
				"	<td><input type=\"text\" id=\"send2friend_s_email\" /></td>" +
				"</tr>" +
				"<tr>" +
				"	<th>שם הנמען/ת:</th>" +
				"	<td><input type=\"text\" id=\"send2friend_f_name\" /></td>" +
				"</tr>" +
				"<tr>" +
				"	<th>כתובת דוא\"ל הנמען/ת:</th>" +
				"	<td><input type=\"text\" id=\"send2friend_f_email\" /></td>" +
				"</tr>" +
				"<tr>" +
				"	<th colspan=\"2\"><button onclick=\"send2friend_do()\">שליחה</button></th>" +
				"</tr>" +
			"</table>" +
		  "</div>"
	);
	$("#send2friend").slideDown();
}

function send2friend_do() {
	$.ajax({
		type: "POST",
		url: "/ajax/send2friend.php",
		data:	"toe="+$("#send2friend_f_email").val()+
				"&ton="+$("#send2friend_f_name").val()+
				"&frome="+$("#send2friend_s_email").val()+
				"&fromn="+$("#send2friend_s_name").val()+
				"&url="+location.href,
		complete: function(){
			$("#send2friend").html("האימייל נשלח.");
		}
	});
	$("#send2friend").html("בשליחה...");
}

/****/

/* scrollers */

var scrlSpeed=2;
scrlSpeed=(document.all)?scrlSpeed:Math.max(1,scrlSpeed-1); //decreasing speed for mozilla

function initScroll(container,object,dir) {
	if (document.getElementById(container) != null){
		var contObj=document.getElementById(container);
		var obj=document.getElementById(object);
		contObj.style.visibility = "visible";
		contObj.scrlSpeed = scrlSpeed;
		contObj.style.overflow='hidden';
		if(contObj.style.position!='absolute' && contObj.style.position!='fixed')
			contObj.style.position='relative';
		obj.style.position='absolute';
		obj.style.left='0';
		obj.style.top='0';
		if(dir=='r'||dir=='l') {
			obj.style.whiteSpace='nowrap'; //!!!
			widthObject=obj.offsetWidth;
			widthContainer=contObj.offsetWidth;
			if(dir=='r')
				obj.style.left=parseInt(-widthObject)+"px";
			else
				obj.style.left=parseInt(widthContainer)+"px";
		} else {
			heightObject=obj.offsetHeight;
			heightContainer=contObj.offsetHeight;
			if(dir=='d')
				obj.style.top=parseInt(-heightObject)+"px";
			else
				obj.style.top=parseInt(heightContainer)+"px";
		}
		interval=setInterval("objScroll('"+ container +"','"+ object +"','"+ dir +"')",50);
		contObj.onmouseover = function(){
			contObj.scrlSpeed=0;
		}
		contObj.onmouseout = function(){
			contObj.scrlSpeed=scrlSpeed;
		}	
	}
}
function objScroll(container,object,dir){
	var contObj=document.getElementById(container);
	var obj=document.getElementById(object);
	if(dir=='r'||dir=='l') {
		widthObject=obj.offsetWidth;
		widthContainer=contObj.offsetWidth;
		if(dir=='r') {
			if (parseInt(obj.style.left)<widthContainer){
				obj.style.left=parseInt(obj.style.left)+contObj.scrlSpeed+"px";
			} else {
				obj.style.left=parseInt(-widthObject)+"px";
			}
		} else {
			if (parseInt(obj.style.left)>-widthObject){
				obj.style.left=parseInt(obj.style.left)-contObj.scrlSpeed+"px";
			} else {
				obj.style.left=parseInt(widthContainer)+"px";
			}
		}
	} else {
		heightObject=obj.offsetHeight;
		heightContainer = contObj.offsetHeight;
		if(dir=='d') {
			if (parseInt(obj.style.top)<heightContainer){
				obj.style.top=parseInt(obj.style.top)+contObj.scrlSpeed+"px";
			} else {
				obj.style.top=parseInt(-heightObject)+"px";
			}
		} else {
			if (parseInt(obj.style.top)>-heightObject){
				obj.style.top=parseInt(obj.style.top)-contObj.scrlSpeed+"px";
			} else {
				obj.style.top=parseInt(heightContainer)+"px";
			}
		}
	}
} 

function update_from_where(val) {
	$("#delivery_address").val(val);
	val==''? $("#delivery_address").show() : $("#delivery_address").hide();
}

$(document).ready(function(){
	$("#delivery_address").hide();
	//initScroll('left_roller_cont','left_roller','r');
});

function bookmark() {
	var title = document.title, url = location.href;
	if (window.sidebar) { //FF
		window.sidebar.addPanel(title, url, "");
	} else if( window.external ) { //IE
		window.external.AddFavorite(url, title);
	}
}

function homepage() {
	var url = 'http://bathandbeauty.co.il/';
	if (document.all) {
		document.body.style.behavior='url(#default#homepage)';
		document.body.setHomePage(url);
	} else if (window.sidebar) {
		if(window.netscape) {
			try {  
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
			} catch(e) {  
				alert("הדפדפן חוסם את הפעול. על מנת לאפשר אותה יש להזין בשורת הכתובת about:config, ולשנות את ערך השדה signed.applets.codebase_principal_support ל-true");  
			}
		} 
		var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
		prefs.setCharPref('browser.startup.homepage', url);
	}
}
/*
$(positionBanners);
$(window).resize(positionBanners);
$(window).scroll(positionBanners);
function positionBanners() {
	if($(window).width()>=780+100*2) {
		$(".banner_left,.banner_right").css("top",$(window).scrollTop()+"px").css("display","block");
	} else {
		$(".banner_left,.banner_right").css("display","none");
	}
}
*/