// JavaScript Document

var emailRegEx = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
var searchBar = true
var categoryData = [];
var categoryCount = 0;
var homeImagePrefix = "j";
var homeIndex = 2;
var newsInterval;

$(document).ready(function() {
	initSearch();
	initCategorysMenu();
	initTooltip();
	initFilter();
	initRollingNews();
	initproductListHeader();
	initContactValidation();
	initJoinUsValidation();
	initHomeSlideShow();
 //  var a = {44:"4"};
 //  var b = $.JSON.encode(a)
 //  var c = $.JSON.decode(b)
//	alert( c.json );  // GETS the value of the cookie
	
//	initShoppingCartClac();
	
//	initForms();
//	$(".box").corner();
});

// =========================
// = NUMBERS ONLY FUNCTION =
// =========================
function numbersOnly(e){
		var unicode = e.charCode ? e.charCode : e.keyCode
		//if the key isn't the backspace, delete, tab keys, shift, left, right (which we should allow)
		if (unicode!=8  && unicode!=9 && unicode!=16 && unicode!=37 && unicode!=39 ) {
			if (unicode<48||unicode>57) //if not a number
			return false //disable key press
		}
}

/***********************************************************/
/******************** Tooltips   ***************************/
/***********************************************************/

function initTooltip() {
	$(".caption").hide();
	$(".imagesOptions img").mouseover(function() {
		$(this).parents().children(".caption").slideDown("fast");
	});
	$(".imagesOptions img").mouseleave(function() {
		$(this).parents().children(".caption").slideUp("fast");
	});
}


/***********************************************************/
/******************** Shopping Cart   **********************/
/***********************************************************/
function initShoppingCartClac() {
	$(".productAmount").change(function() {
		var row = $(this).parents(".productRow");
		var price = row.children(".price").text();
 		var amount = $(this).value;
		// var subTotal:float =  price * amount;
		// alert(subTotal)
	});
}
/***********************************************************/
/******************* Categorys Filter **********************/
/***********************************************************/
function filterType(filterTypeObj, keepSelected) {
	var i;
	if (categoryData.length > 1) {
		for(i = 0; i < categoryCount; i++) {
			categoryData[i].appendTo("#categories");
		}
		categoryData = [];
		categoryCount = 0;
	}
	var kindId = $("#kinds option:selected").val();
	
	$("#categories option").each(function(){
		if ($(this).attr("id") != kindId && $(this).val() != 0) {
			$(this).removeAttr("selected");
			categoryData[categoryCount] = $(this).detach();
			categoryCount++;
		}
	})
	if (kindId != 0) {
		$("#categories").removeAttr("disabled");
	} else {
		$("#categories").attr("disabled", true);
		
	}
}

function initFilter() {
	categoryData = [];
	categoryCount = 0;
	if ($("#kinds option:selected").val() == 0) {
		$("#categories").attr("disabled", true);
	} else {
	 	filterType($(this), true);
	}

	$("#kinds").change(function(){
	 	filterType($(this));
	});
}

/***********************************************************/
/******************** Categorys Menu **********************/
/***********************************************************/
function initSearch() {
	if ($("#idSearchBox").hasClass("showFirst")) {
		$("#typeSearchBox").hide();
		$("#idSearchBox").show();
	} else {
		$("#typeSearchBox").show();
		$("#idSearchBox").hide();
	}
	$(".searchLink").click(
		function() {
			switchSearchBars();
			//alert($(this).parent().attr("id"));
		}
	)
}

function switchSearchBars() {
	$("#idSearchBox").toggle(searchBar)
	$("#typeSearchBox").toggle(!searchBar)
	searchBar = !searchBar;
    if (!searchBar) {
   		$(".focusedInput").focus();
	}
//	if (searchBar.attr("id") == "idSearchBox")
}
/***********************************************************/
/******************** Categorys Menu **********************/
/***********************************************************/
function initCategorysMenu() {
	var color;
	$(".categoryListWrapper").hide()
	
	var open = false;
	$(".kindTabList li").hover(
		function (ev) {
			if (!open) {
				color = activateCategoryList($(this).attr('id').charAt(7));
				if ($(this).attr('id').charAt(7) == 1) {
					homeImagePrefix = "p";
					homeIndex = 1;
				} else {
					homeImagePrefix = "j";
					homeIndex = 1;
				}
				open = true;
			}
		}, 
		function (ev) {
			if (open) {
				deActivateCategoryList($(this).attr('id').charAt(7), color);
				open = false;
			}
		}
	);
	
	// $(".kindTabList li").mouseover(function() {
	// 	color = activateCategoryList($(this).attr('id').charAt(7));
	// 		if ($(this).attr('id').charAt(7) == 1) {
	// 			homeImagePrefix = "p";
	// 			homeIndex = 1;
	// 		} else {
	// 			homeImagePrefix = "j";
	// 			homeIndex = 1;
	// 		}
	// 	});
	//  $(".kindTabList li").mouseout(function() {
	// 	deActivateCategoryList($(this).attr('id').charAt(7), color);
	// 	});
}


function activateCategoryList(kindId) {
	var kindTabHeader = "#kindTab"+kindId+" .kindTabHeader";
	var categoryList = "#categoryListWrapper"+kindId;
	var color = $(kindTabHeader).css('background-color');
//	$(kindTabHeader).css('background-color', '#fcfcfc');
//	$(kindTabHeader).css('color', '#46282a');
	$("#categoryListWrapper"+kindId).stop(true, true);
	$("#categoryListWrapper"+kindId).slideDown(300);
	//alert($("#categoryListWrapper"+kindId).css("display"));
	return color;
}

function deActivateCategoryList(kindId, color) {
	var kindTabHeader = "#kindTab" + kindId + " .kindTabHeader";
	var categoryList = "#categoryListWrapper" + kindId;
	var opColor;
	if (color == "rgb(130, 100, 76)" || color == "#82644c") {
		opColor = "rgb(246, 239, 221)";
	} else {
		opColor = "#46282a";
	}
//	$(kindTabHeader).css('background-color', color);
//	$(kindTabHeader).css('color', opColor);
//	$(categoryList).stop();
	$(categoryList).slideUp(200);
}


// ================
// = Rolling News =
// ================

function initRollingNews() {
	var tempElement;
	$(".newsRollItem:first").css("top", "0px");
	var newsId = $(".newsRollItem:first").attr("data-newsId");
	//$("#newsItemLink").attr("href", "page.php?id=" + newsId);
	startNewsRoll();
	$(".rollingNews").parent().mouseover(function(){
		clearInterval(newsInterval);
	});
	$(".rollingNews").parent().mouseout(function(){
		startNewsRoll();
	});
}

function replaceNewsElement(tempElement) {
	tempElement.remove();
	tempElement.css("top","200px");
	$(".rollingNews").append(tempElement);
}

function startNewsRoll() {
	newsInterval = setInterval(function(){
		tempElement = $(".newsRollItem:first");
		tempElement.animate({"top": "-200px"}, 2500, function(){
			replaceNewsElement($(this));
		});
		tempElement.next().animate({"top": "0px"}, 2500, function() {
			newsId = $(this).attr("data-newsId");
		//	$("#newsItemLink").attr("href", "page.php?id=" + newsId);
		});
	}, 4000);
}
// =================================
// = productListHeader show / hide =
// =================================

function initproductListHeader() {
	$(".productShowCase").hover(function() {
		$(this).find(".productListHeader").slideDown("fast");
	});
	$(".productShowCase").mouseleave(function() {
		$(this).find(".productListHeader").slideUp("slow");
	});
}

/***********************************************************/
/************************* Forms ***************************/
/***********************************************************/
function initForms() {
	// setting only numbers to phone
/*	$(".stdForm").find(".valPhone").keypress(function(evOb) {
		var charCode = (evOb.which) ? evOb.which : evOb.keyCode
        if ( (charCode > 31) && (charCode < 48 || charCode > 57) && (charCode != 46) ) {
			 return false;
		}
		return true;
	})

	//
	$(".stdForm .valRequired").change(function() {
		validateRequired($(this))
	});
	$(".stdForm .valEmail").change(function() {
		validateEmail($(this))
	});

	// setting the submit function
	$(".stdForm").submit(function() {
		$(this).find(".valRequired").each(function() {
			validateRequired($(this))
		})
		$(this).find(".valEmail").each(function() {
			validateEmail($(this))
		});
		isOk = !($(this).find("input").hasClass("valError"));
		return isOk;
	})
	*/
}

function validateRequired(field) {
	field.val() ? field.removeClass("valError") : field.addClass("valError") ;
}

function validateEmail(field) {
	field.val().match(emailRegEx) ? field.removeClass("valError") : field.addClass("valError") ;
}

// ======================
// = Contact Valitation =
// ======================

function isEmail(emailAddress) {
 	var emailReg = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (emailReg.test(emailAddress) == false) {
		return false;
	}
 	return true;
}

function numbersonly(e){
	var unicode = e.charCode ? e.charCode : e.keyCode
	if (unicode!=8 && unicode!=46 && unicode!=9 && unicode!=16 && unicode!=37 && unicode!=39 ) {
 		if (unicode<48||unicode>57) {
			return false;
		}			
	}
 }

function initContactValidation() {
	$(".contactUsTable input[name='cell']").keypress(function(e) {
		return numbersonly(e);
	});
	$(".contactUsTable input[name='phone']").keypress(function(e) {
		return numbersonly(e);
	});
	$(".contactValidate").click(function() {
		if ($(".contactUsTable input[name='private']").val() == "" || $(".contactUsTable input[name='last']").val() == "" || $(".contactUsTable input[name='email']").val() == "" || $(".contactUsTable input[name='cell']").val() == "") {
			alert("חובה למלא את כל השדות המסומנים בכוכבית");
			return false;
		} else {
			if (!isEmail($(".contactUsTable input[name='email']").val())) {
				alert("כתובת הדואר האלקטרוני אינה תואמת את התבנית הנדרשת");
				return false;
			}
			
		}
	});
}

function initJoinUsValidation() {
	$(".joinUsTable input[name='cell']").keypress(function(e) {
		return numbersonly(e);
	});
	$(".joinUsTable input[name='phone']").keypress(function(e) {
		return numbersonly(e);
	});
	$(".joinUsTable input[name='homeNum']").keypress(function(e) {
		return numbersonly(e);
	});
	$(".joinUsValidation").click(function() {
		if ($(".joinUsTable input[name='privateName']").val() == "" || $(".joinUsTable input[name='lastName']").val() == "" || $(".joinUsTable input[name='email']").val() == "" || $(".joinUsTable input[name='cell']").val() == "") {
			alert("חובה למלא את כל השדות המסומנים בכוכבית");
			return false;
		} else {
			if (!isEmail($(".joinUsTable input[name='email']").val())) {
				alert("כתובת הדואר האלקטרוני אינה תואמת את התבנית הנדרשת");
				return false;
			}
			
		}
	});
}

/***********************************************************/
/***********************************************************/
/***********************************************************/

// =====================
// = Home Gallery Show =
// =====================
function initHomeSlideShow() {
	$(".homeImageToRemove").remove();
	setInterval(function() {
		replaceHomeImages();
		homeIndex++;
		
		if ((homeIndex == 9 && homeImagePrefix == "j") || (homeIndex == 8 && homeImagePrefix == "p")) {
			homeIndex = 1;
		}
	}, 2500);
}

function replaceHomeImages() {
	$(".homeImageWrapper").append("<div style=\"display:none;\" class=\"homeImageTop\"><img src=\"Images/home/" + homeImagePrefix + "_" + homeIndex + ".jpg\" /></div>");
	$(".homeImageTop").fadeIn(1000, function() {
		$(".homeImageBottom").remove();
		$(this).removeClass("homeImageTop").addClass("homeImageBottom");
	});
}



