
var __qValidatorRegExEmail = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		
	function check_email(email)
	{
		return __qValidatorRegExEmail.test(email);
	};
	
	function cancelFunc(form_name)
	{
		var form = jQuery("[name='"+ form_name +"']");
		form.find("input").val("");
		var register_val = (form_name == "form_register") ? "Register" : "Save"; 
		form.find(".submit_register").val(register_val);
		form.find("textarea").val("");
	};
	
	function noLoggedInWishlist(where)
	{
		var textAlert = (where) ? ((where == "no_buy") ? 
		"This cake is not on stock!" : 
		"You cannot add this cake to your wishlist\n beacuse you are not logged in!") :
		"You don't have access to the wishlist\n because you are not logged in!";
		alert(textAlert);
	}
	
	function submitUpdateForm()
	{
		var form = jQuery("[name='update_shopping_cart']");
		var list = jQuery(".quantity_input");
		
		var cakes = "";
		var string_has_zero = "";
		var cakes_f_n_d = [];
		for (var i = 0; i < list.length; i++)
		{
			var itm = jQuery(list[i]);
			if (itm.val() == 0)
			{
				cakes_f_n_d.push(itm.attr("name"));
				string_has_zero = "zero";
				var cake = itm.parent().parent().find(".cake_cart_title").find("a").text();
				cakes += (cakes.length != 0) ? ", " + cake : cake;
			}
		}
		
		if (string_has_zero.length > 0)
		{
			jQuery(".submit_update_form_hidden").val("update_form");
			var c = confirm("Are you sure you want to remove " + cakes + " from your list?");
			if (c)
				form.submit();
			else
			{
				for (var i = 0; i < cakes_f_n_d.length; i++)
				{
					jQuery("[name='" + cakes_f_n_d[i] + "']").val("");
					form.submit();
				}
			}
				
		}
		else
		{
			jQuery(".submit_update_form_hidden").val("update_form");
			form.submit();
		}
	}
	
	function addRemoveNewsletter(email, action)
	{
		var email_error = check_email(email);
	
		if (!email_error)
		{
			alert("The email adress is not valid!\n\n");
			return;
		}
		
		jQuery.ajax({
			   type: "POST",
			   url: "_subscribe.php",
			   data: "email="+email+"&action="+action,
			   success: function(msg){
			    if (msg == "user_not_found")
			    	alert("We are sorry but\n you are not a subscriber to our newsletter");
			    else if (msg == "user_already_subscriber")
			    	alert("You already are a subscriber to our newsletter");
			    else if (msg == "db_errror")
			    	alert ("We are sorry!\nBecause of a sistem error\nyour subscritpion was not processed!\nPlease try again later!");
			    else if (msg == "succes")
			    {
			    	if (action == "unscribe")
			    		alert("Succes!\nYou are not longer a subscriber to our newsletter");
			    	else
			    		alert("Succes!\nYou are now a subscriber to our newsletter");
			    }
			    else if (msg == "date_insuficiente")
			    	alert("Unsufficient data");
			   // else {
			    //	alert(msg);
			    //}
			   }
			 });
	};
	
	function validateForm(formName)
	{
		var form = jQuery("[name='" + formName + "']");
		var steps = form.find(".desing_your_cake_step_container");
		var is_form_completed = true;
		
		for (var i = 0; i < steps.length; i++)
		{
			var el = jQuery(steps[i]);
			var cond = (el.find("input[type='radio']").length > 0);
			//alert(el.find("textarea").length);
			
			if (el.find("input[type='radio']").length > 0)
			{
				if (el.find("input[type='radio']:checked").length == 0)
				{
					
					is_form_completed = false;
					break;
				}
			}
			else if (el.find("textarea").length > 0)
			{
				if (!el.find("textarea").val())
				{
					
					is_form_completed = false;
					break;
				}
			}
		}
		
		if (!is_form_completed)
			alert("You must complete the entire form!");
		else
			document.forms[formName].submit();
	}
	
	/*
	function getCakePage(id)
	{
		if (!id)
		return false;
		jQuery.ajax({
			   type: "POST",
			   url: "_cakes.php",
			   data: "id="+id,
			   success: function(msg)
			   {
			   		var rightContainer = jQuery('.right_container');
			   		var table_list = rightContainer.find(" > table");
			   		table_list.hide();
			   		jQuery(table_list[1]).before(msg);
			   		//alert(msg);
			   }
			 });
	}
		*/

jQuery(document).ready(function () {
	 jQuery('.slideshow').cycle({
		fx: 'fade',
		speed: 3500 // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
	
	if (jQuery(".slideshow").length > 0)
	{
		var imgArr = [];
		var pics_list = jQuery(".slideshow").find("img");
		for (var i = 0; i < pics_list.length; i++)
		{
			var pic = jQuery(pics_list[i]);
			src = pic.attr("src");
			imgArr.push(src);
		}
		jQuery.imgpreload(imgArr, function () {});	
	}
	
	
	
	jQuery(".lightbox").lightBox({avoidDuplicates: true});
	
	var inTransition = false;
	
	jQuery(".small_photos a").click(function () {
		
		if (inTransition)
			return;
		
		var attr = jQuery(this).attr("_attr");
		var clicked_rel = jQuery(this).attr("_rel");
		var big_photo_rel = jQuery(".big_photo_container").find(".big_photo").find("img").attr("_rel");
		var href = jQuery(this).attr("_href");
		
		if (clicked_rel != big_photo_rel)
		{
			//var html = jQuery(".big_photo_container").find(".big_photo").html();
			//html += "<img class='big_img inv' src=" + attr + " alt=''_rel='"+clicked_rel+"' style='opacity:0; filter:alpha(opacity=0);'/>";
			//jQuery(".big_photo_container").find(".big_photo").html(html);
			jQuery(".big_photo_container").find(".big_photo").append("<img class='big_img inv' src=" + attr + " alt=''_rel='"+clicked_rel+"' style='opacity:0; filter:alpha(opacity=0);'/>");
			jQuery(".big_photo_container").find(".big_photo").attr("href", href);
			var type = jQuery(this).attr("type");
				
			var second_pic = jQuery(".big_photo_container").find(".big_photo").find(".inv");
			
			second_pic.removeClass("inv");
			
			if (type == "Portrait") 
				second_pic.css({"padding-left" : "115px"});
			else
				second_pic.css({"padding-left" : "0px"});
			
			var visible_img = jQuery(".big_photo_container").find(".big_photo").find(".visible");
		
			if(!inTransition)
			{
				inTransition = true;
				/*
				if ( jQuery.browser.msie )
				{
					visible_img.fadeOut(500, function() {
						visible_img.remove();
					});
					
					visible_img.fadeIn(500, function () {
						second_pic.addClass("visible");
						inTransition = false;
					});	
				}
				else
				{
				*/
					visible_img.animate({opacity: 0}, 1200, function() {
						visible_img.remove();
					});
					
					
					second_pic.animate({opacity: 1}, 1200, function () {
						second_pic.addClass("visible");
						inTransition = false;
					});	
				//}
			}
		}
	});
	
	var is_login_show = false;
	
	jQuery.expr[':'].focus = function( elem ) {
  			return elem === document.activeElement && ( elem.type || elem.href );
		};

	
	jQuery(".login").hover(
		function() {
			if (!is_login_show)
			{
				jQuery(".login_form_container").show();
				//background-color: #A1160B;
				jQuery(".login").css({"background-color": "#A1160B"});
				is_login_show = true;
			}
		}, 
		function () {
			var inputUsr = jQuery(".login_form_container").find("input[type='text']");
			var inputPass = jQuery(".login_form_container").find("input[type='password']");
			var val1 = inputUsr.val();
			var val2 = inputPass.val();
			if ((!val1) && (!val2) && (!inputUsr.is(":focus")) && (!inputPass.is(":focus")))
			{
				jQuery(".login_form_container").hide();
				jQuery(".login").css({"background-color": "transparent"});
				is_login_show = false;
			}
	});
	
	
	jQuery(document).click(function (e) {
		var login_container = jQuery(".login_form_container");
		
		if (login_container.is(":visible"))
		{
			var pos = login_container.offset();
			/*
			jQuery("body").append("<div class='debug' style='position: fixed; top:0px; left:0px; width:200px;"+
			"height: 200px; background-color: #ffffff; z-index:1000 font-size: 12px;'>");
			var cond = ((e.pageX > pos.left) && (e.pageX < (pos.left + login_container.outerWidth(true))) &&
					(e.pageY > pos.top) && (e.pageY < (pos.top + login_container.outerHeight(true))))
			
			var html = "e.pageX = " + e.pageX + "<br />e.pageY = " + e.pageY + "<br />" + 
				"login_container.offset().left = " + pos.left +
				"<br/>login_container.offset().top = " + pos.top +
				"<br/>login_container.outerWidth(true) = " + login_container.outerWidth(true) +
				"<br/>login_container.outerHeight(true) = " + login_container.outerHeight(true) +
				"<div style='color: red;'>Condition: " + cond + "</div>";
			jQuery(".debug").html(html);
			*/		
			if (!((e.pageX > pos.left) && (e.pageX < (pos.left + login_container.outerWidth(true))) &&
					(e.pageY > pos.top) && (e.pageY < (pos.top + login_container.outerHeight(true)))))
					{
						var val1 = jQuery(".login_form_container").find("input[type='text']").val();
						var val2 = jQuery(".login_form_container").find("input[type='password']").val();
						if ((!val1) && (!val2))
						{
							jQuery(".login_form_container").hide();
							jQuery(".login").css({"background-color": "transparent"});
							is_login_show = false;
						}
					}
		}
	});
	
	var rels = [];
	
	jQuery(".slide_up").mousehold(400, function () 
	{
		var pase = 30;
		var small_photos = jQuery(".small_photos");
		var top = small_photos.css("top");
		if (!top)
			return;
		else
			top = parseInt(top);

		if ((top + pase) < 0)
			top += pase;
		else
			top = 0;
		jQuery(".small_photos ").animate({"top": top + "px"});
		
	});
	
	jQuery(".zip_code").blur(function () {
		zip = jQuery(this).val();
			if (!zip)
				return;
				
		var ph_imp_ref = this;		
		if (zip.length != 5) 
		{
			alert("The zip code "+ zip + "is not valid!\n The zip code must contains only 5 numbers");
			jQuery(this).val("");
			setTimeout(function () {ph_imp_ref.focus();}, 100);
		}
		else if (parseInt(zip, 10) != zip)
		{
			alert("The zip code "+ zip + "is not valid!\n The zip code must contains only 5 numbers");
			jQuery(this).val("");
			setTimeout(function () {ph_imp_ref.focus();}, 100);
		}
	});

	jQuery(".phone").blur(function () {
		var phoneNum = jQuery(this).val();
   		if (!phoneNum)
   			return;
	  	//var regExpObj = /(\d\d\d)-\d\d\d-\d\d\d\d/;
	  	var ph_imp_ref = this;
	  	
	  	var phoneNumberPattern = /^(\d{3})-(\d{3})-(\d{4})$/;
		   
		if(!phoneNumberPattern.test(phoneNum))
		{
			alert("The " + phoneNum + " is not a valid number!\n The number should match the format given in example!");
			jQuery(this).val("");
			// ph_imp_ref.focus();
			setTimeout(function () {ph_imp_ref.focus();}, 100);
			
			//var html = "";
			//jQuery("body").append("<div class='debug'></div>");
			//for (var i in jQuery(this)[0])
				//html += "<div>" + i + " | " + jQuery(this)[0][i] + "</div>";
			//jQuery(".debug").html(html);
		}
	});
	
	
	jQuery(".left_menu li").hover(function () {
		jQuery(this).find("> ul").show();	
	}, function () {
		jQuery(this).find("> ul").hide();
	});
	
	jQuery(".gallerific_slide").fancybox({'width': 900,
				'height': 600,
        'autoScale': false,
        'transitionIn': 'none',
		'transitionOut': 'none',
		'type': 'iframe'});
	
	/*
	jQuery(".gallerific_slide").click(function() {
		var href = jQuery(this).attr("_href");
		if (href)
			window.location = href;
			
	});
	*/
	
	
	/*
	jQuery(".pay_now_by_pay_pal").click(function () {
		var href = jQuery(this).attr("_href");
		if (href)
		{
			//window.open(href)
			window.location = href;
		}
	});
	*/
	
	jQuery(".multiple_price > select").change(function() 
	{
		var priceStr = jQuery(this).val();
		var price = priceStr.substr(0, priceStr.indexOf("|"));
		var strWithoutPrice = priceStr.substr(priceStr.indexOf("|") + 1);
		var price_id = strWithoutPrice.substr(0, strWithoutPrice.indexOf("|"));
		var dimensionsAndNewPrice = strWithoutPrice.substr(strWithoutPrice.indexOf("|") + 1);
		
		dimensions = dimensionsAndNewPrice.substr(0, dimensionsAndNewPrice.indexOf("|"));
		var new_price = dimensionsAndNewPrice.substr(dimensionsAndNewPrice.indexOf("|") + 1);
			
		if (new_price)
		{
			jQuery(".price").css({"padding-top": "1px"});
			jQuery(".multiple_price").find(".price").html("$" + new_price);
			jQuery(".multiple_price").find(".old_price").html("$" + price);
			jQuery(".multiple_price").find(".new").css({"display" : "block"});
		}
		else
		{
			jQuery(".price").css({"padding-top": "10px"});
			jQuery(".multiple_price").find(".price").html("$" + price);
			jQuery(".multiple_price").find(".old_price").html("");
			jQuery(".multiple_price").find(".new").css({"display" : "none"});
		}
					
		jQuery(".cake_links #cake_price_id").val(price_id);
		jQuery(".add_to_cart_container #cake_dimensions").val(dimensions);
		
		price = (new_price) ? new_price : price;
		jQuery(".add_to_cart_container #cake_price").val(price);
	});
	
	jQuery(".slide_bottom").mousehold(400, function () {
		var pase = 30;
		var small_photos = jQuery(".small_photos");
		var top = small_photos.css("top");
		if (!top)
			top = 0;
		else
			top = parseInt(top);
		
		var small_photos_height = small_photos.height();
		var scroll_distance = jQuery(".small_photos_container").height() - small_photos.height();
		
		if (top == scroll_distance)
			return;
				
		if ((top - pase) > scroll_distance)
			top -= pase;
		else
			top = scroll_distance;
		jQuery(".small_photos ").animate({"top": top + "px"});
	});
	
	jQuery(".tab_menu > a").click(function () 
	{
		if (jQuery(this).hasClass("print_page"))
			return;
		var css_class = jQuery(this).attr("class");
		if (css_class.indexOf("selected") == -1)
		{
			var a_selected = jQuery(".tab_menu").find("a.selected");
			var a_first_selected = jQuery(".tab_menu").find("a.selected_first");
			a_selected.removeClass("selected");
			a_first_selected.removeClass("selected_first");
			var hide_class = (a_selected.length > 0) ? a_selected.attr("class") : a_first_selected.attr("class");
			
			jQuery("." + hide_class + "_tab").hide();
			jQuery("." + css_class + "_tab").show();
			
			class_for_add = jQuery(this).attr("is_first") ? "selected_first" : "selected";
			jQuery(this).addClass(class_for_add);
		}
	});
	
	updateTotal = function (step, arr, price)
	{
		var total = 0;
		for (var i = 0; i < arr.length; i++)
		{
			if (arr[i][0] == step)
				total += price;
			else
				total += arr[i][0];
		}
		return total;
	};
	
	inArray = function (arr, step)
	{
		ret = false;
		for (var i = 0; i < arr.length; i++)
			if (arr[i][0] == step)
			{
				ret = true;
				break;
			}
		return ret;
	}

	manage_design_your_cake_price = function()
	{
		if (jQuery(".design_your_cake_container").length > 0)
		{
			//jQuery("body").append("<div class='design_cake_total' style='position:fixed;right:0px;bottom:0px;width:100px;height:20px'></div>");
			//var design_cake_div = jQuery(".design_cake_total");
			//design_cake_div.css({"background-color": "white"});
			//design_cake_div.html("Total price: ");
			var total = 0;
			var arr = [];
			jQuery(".calculate_price").click(function () {
				if (jQuery(this)[0].type == "radio")
				{
				
					var name = jQuery(this).attr("step");
					var step = name.substr(name.indexOf("__") + 2);
					var price = jQuery(this).attr("price");
					if (price)
					{
						if (!inArray(arr, step))
						{
							arr.push([step, price]);
							total += price;
						}
						else
							total = updateTotal(step, arr, price);
							
						jQuery(".unit_price__" + step).val(price);
					}
					
					//design_cake_div.html("Total price: "+ total);
					jQuery(".total_price_input").val(total);
				}
				//if (in_array())
			});
		}
	};
	
	manage_design_your_cake_price();
		
	if (jQuery(".single_special_offer").length > 0)
	{
		jQuery(".single_spec_offer_title_container > h4").click(function () {
			var last_visible_title = jQuery(".single_spec_offer_title_container").find("h4.selected");
			last_visible_title.removeClass("selected");
			
			var last_visible_title_class = last_visible_title.attr("class");
			var last_visible_index = last_visible_title_class.substr(last_visible_title_class.indexOf("__") + 2);
			jQuery(".sp_title__" + last_visible_index).css({"border-bottom": "1px dotted #000000"});

			var class_attr = jQuery(this).attr("class");
			jQuery(this).css({"border": "0px none"});
			var index = class_attr.substr(class_attr.indexOf("__") + 2);
			if (!jQuery(".sp_content__" + index).is(":visible"))
			{
				jQuery(".single_special_ofer_content").slideUp('slow');
				jQuery(".sp_content__" + index).slideDown('slow');
				
			}
			jQuery(this).addClass("selected");
		});
	}
	
	jQuery("#store_location").click(function () {
		
		if (jQuery(".all_states").is(":visible"))
			jQuery(".all_states").hide();
			
		if (!jQuery(".store_location").is(":visible"))
			jQuery(".store_location").show();
			
		if (!jQuery(".col_state").is(":visible"))
			jQuery(".col_state").show();
			
		if (!jQuery(".pick_up").is(":visible"))
		{
			jQuery(".payment_method > table tr").hide();
			jQuery(".pick_up").show();
		}
		jQuery(".where_to_deliver").html("Store location");
		
	});
	
	
	jQuery("#deliver_it").click(function () {
		
		if (jQuery(".all_states").is(":visible"))
			jQuery(".all_states").hide();
			
		if (jQuery(".store_location").is(":visible"))
			jQuery(".store_location").hide();
		
		if (!jQuery(".deliver_to").is(":visible"))
		{
			jQuery(".payment_method > table tr").hide();
			jQuery(".deliver_to").show();
		}
		
		jQuery(".where_to_deliver").html("Home");
	});
	
	
	jQuery("#shipit").click(function () {
	/*	
	if (!jQuery(".store_location").is(":visible"))
			jQuery(".store_location").show();
			
		jQuery(".col_state").hide();
		jQuery(".all_states").show();
	*/
		jQuery(".store_location").hide();
		if (!jQuery(".shipit_text").is(":visible"))
		{
			jQuery(".payment_method > table tr").hide();
			jQuery(".shipit_text").show();
		}

		jQuery(".where_to_deliver").html("Shipit to me");	
	});
	
	
	jQuery(".delete_from_wishlist").click(function () {
		var href = jQuery(this).attr("_href");
		//alert(href);
		var c = confirm("Are you sure you want to remove this product from your list?");
		if (c)
			window.location = href;
	});
	
	jQuery(".delete_cake_from_cart").click(function () {
		var href = jQuery(this).attr("_href");
		//alert(href);
		var c = confirm("Are you sure you want to remove this product from your list?");
		if (c)
			window.location = href;
	});
	
	jQuery("#billing_individual").click(function () {
		jQuery(".company_details_billing").hide();
	});
	
	
	jQuery("#billing_company").click(function () {
		jQuery(".company_details_billing").show();
	});
	
	jQuery("#d_pay_now").click(function () {
		jQuery(".pay_now_text").show();
	});
	
	jQuery("#s_pay_now").click(function () {
		jQuery(".pay_now_text").show();
		jQuery(".store_location_pay_at_store_text").hide();
	});
	
	jQuery("#pay_at_store").click(function () {
		jQuery(".pay_now_text").hide();
		jQuery(".store_location_pay_at_store_text").show();
	});
	
	
	jQuery("#pay_at_delivery").click(function () {
		jQuery(".pay_now_text").hide();
	});
	

	/*
	jQuery('#thumbs').galleriffic({
					imageContainerSel:      '#slideshow'
				});
	*/
	
	//jQuery(".selectbox").selectBox();
});
