$(document).ready(function(){
	SexyLightbox.initialize({
		color: 'black', 
		dir: 'images/lightbox'
	});
    before_input("#txtSearch", "Search Product...");
    before_input("#txtEmail", "Enter your email...");
	$("#logo").click(function(){
		window.location = "index.php";						  
	});
	$('#category > li').bind('mouseover', jsddm_open);
	$('#category > li').bind('mouseout',  jsddm_timer);
	
	$("#slideShow").cycle({
		fx: 'fade',
		speed:    2500, 
   		timeout:  1000 
	});
	
	$("#txtTelephone, #txtFax").mask("(99) (9) 999 9999");
	$("#txtZip").mask("99999");
});

var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;
document.onclick = jsddm_close;

function jsddm_open()
{  
	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('ul').css('visibility', 'visible');
}

function jsddm_close()
{  
	if(ddmenuitem) 
	{
		ddmenuitem.css('visibility', 'hidden');
	}
}

function jsddm_timer()
{  
	closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer()
{  
	if(closetimer)
   {  
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

function before_input(id, text)
{
    var objSet = id;  
    var objSetText = text;  
    if($(objSet).val() == "")
    {  
        $(objSet).val(objSetText);  
    }     
    $(objSet).focus(function()
    {  
        if($(objSet).val() == objSetText)
        {  
            $(objSet).val("");  
        }     
    }).blur(function()
    {  
        if($(objSet).val() == "")
        {  
            $(objSet).val(objSetText);  
        }     
    }); 
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function chk_frm_sub_scribe()
{
	var email = $("#txtEmail");
	
	if((email.val() == "") || (email.val() == "Enter your email..."))
	{
		$("span.error_message").hide("fast", function(){
			$(this).html("Please, Enter email address.").fadeIn("fast", function(){
				$(this).fadeOut(3000);
			});
		});
		email.focus();
		return false;
	}
	else if(!isValidEmailAddress(email.val())) 
	{
		$("span.error_message").hide("fast", function(){
			$(this).html("Please, Enter valid email address.").fadeIn("fast", function(){
				$(this).fadeOut(3000);
			});
		});
		email.val("");
		email.focus();
		return false;
	}
	else
	{
		$("#btnSubscribe").val("Loading...");
		$("#btnSubscribe").attr("disabled", "disabled");
		var options = { 
			success: function(data)
			{
				if(data == "Success!!")
				{
					email.val("Enter your email...");
					$("span.error_message").hide("fast", function(){
						$(this).html("Thank you for using our service.").fadeIn("fast", function(){
							$(this).fadeOut(3000);
							$("#btnSubscribe").removeAttr("disabled");
							$("#btnSubscribe").val("Subscribe");
						});
					});
				}
				else
				{
					email.val("Enter your email...");
					$("span.error_message").hide("fast", function(){
						$(this).html(data).fadeIn("fast", function(){
							$(this).fadeOut(3000);
							$("#btnSubscribe").removeAttr("disabled");
							$("#btnSubscribe").val("Subscribe");
						});
					});
				}
			}
		}; 
		$("#frmSubscribe").ajaxSubmit(options); 
		return false;
	}
}

function chk_frm_contact()
{
	var name = $("#txtName");
	var email = $("#txtCEmail");
	var subject = $("#txtSubject");
	var message = $("#txtMessage");
	
	if(name.val() == "")
	{
		$("#cf_error").hide("fast", function(){
			$(this).html("Please, Enter your name.").fadeIn("fast", function(){
				$(this).fadeOut(3000);
			});
		});
		name.focus();
		return false;
	}
	else if(email.val() == "")
	{
		$("#cf_error").hide("fast", function(){
			$(this).html("Please, Enter email address.").fadeIn("fast", function(){
				$(this).fadeOut(3000);
			});
		});
		email.focus();
		return false;
	}
	else if(!isValidEmailAddress(email.val())) 
	{
		$("#cf_error").hide("fast", function(){
			$(this).html("Please, Enter valid email address.").fadeIn("fast", function(){
				$(this).fadeOut(3000);
			});
		});
		email.val("");
		email.focus();
		return false;
	}
	else if(subject.val() == "")
	{
		$("#cf_error").hide("fast", function(){
			$(this).html("Please, Enter subject.").fadeIn("fast", function(){
				$(this).fadeOut(3000);
			});
		});
		subject.focus();
		return false;
	}
	else if(message.val() == "")
	{
		$("#cf_error").hide("fast", function(){
			$(this).html("Please, Enter message.").fadeIn("fast", function(){
				$(this).fadeOut(3000);
			});
		});
		message.focus();
		return false;
	}
	else
	{
		$("#btnSend").val("Sending...");
		$("#btnSend").attr("disabled", "disabled");
		var options = { 
			success: function(data)
			{
				if(data == "Success!!")
				{
					name.val("");
					email.val("");
					subject.val("");
					message.val("");
					$("#cf_error").hide("fast", function(){
						$(this).html("Thank you for using our service.").fadeIn("fast", function(){
							$(this).fadeOut(3000);
							$("#btnSend").removeAttr("disabled");
							$("#btnSend").val("Send");
						});
					});
				}
				else
				{
					name.val("");
					email.val("");
					subject.val("");
					message.val("");
					$("#cf_error").hide("fast", function(){
						$(this).html(data).fadeIn("fast", function(){
							$(this).fadeOut(3000);
							$("#btnSend").removeAttr("disabled");
							$("#btnSend").val("Send");
						});
					});
				}
			}
		}; 
		$("#frmContact").ajaxSubmit(options); 
		return false;
	}
}

function chk_frm_order()
{
    var id = $("#txtID");
    var color = $("#txtColor");
    var qty = $("#txtQty");
	var size = $("#txtSize");
    
    if(id.val() == "")
    {
        SexyLightbox.close();
		return false;
    }
    else if(color.val() == "")
    {
        $("#cf_error").hide("fast", function(){
			$(this).html("Please, Select color.").fadeIn("fast", function(){
				$(this).fadeOut(5000);
			});
		});
		back_to_select_color();
		return false;
    }
    else if((qty.val() == "") || (qty.val() <= "0"))
    {
        $("#cf_error").hide("fast", function(){
			$(this).html("Please, Enter quantity do you want?").fadeIn("fast", function(){
				$(this).fadeOut(5000);
			});
		});
		qty.focus();
        qty.select();
		return false;
    }
	else if(size.val() == "")
    {
        $("#cf_error").hide("fast", function(){
			$(this).html("Please, Select size do you want?").fadeIn("fast", function(){
				$(this).fadeOut(5000);
			});
		});
		size.focus();
        back_to_select_size();
		return false;
    }
    else
    {
		$("#btnAdd").attr("disabled", "disabled");
		var options = { 
			success: function(data)
			{
				if(data == "Added")
				{
					$("#cf_error").hide("fast", function(){
						$(this).html("Added to Shopping Bag.").fadeIn("fast", function(){
							$(this).fadeOut(1000, function(){
								$("#btnAdd").removeAttr("disabled");
								var total_item = parseInt($("#shopping_bag_item").text());
								var add_qty = parseInt(qty.val());
								total_item = Math.round(total_item + add_qty);
								$("#shopping_bag_item").text(total_item);
								color.val("");
								size.val("");
								qty.val("1");
								$("#size_selection").html("");  
								back_to_select_color();
							});
						});
					});
                    //window.location = 'index.php?menu=bag';
				}
				else if(data == "Already in Shopping Bag"){
					$("#cf_error").hide("fast", function(){
						$(this).html(data).fadeIn("fast", function(){
							$(this).fadeOut(3000);
							$("#btnAdd").removeAttr("disabled");
						});
					});
				}
				else
				{
					id.val("");
					color.val("");
					size.val("");
					qty.val("1");
					$("#cf_error").hide("fast", function(){
						$(this).html(data).fadeIn("fast", function(){
							$(this).fadeOut(3000);
							$("#btnAdd").removeAttr("disabled");
						});
					});
				}
			}
		}; 
		$("#frmOrder").ajaxSubmit(options); 
		return false;  
    }
}

function check_num(e)
{
    var keyPressed;
    
    if(window.event)
    {
        keyPressed = window.event.keyCode; // IE
        if ((keyPressed < 48) || (keyPressed > 57)) 
        {
            window.event.returnValue = false;
        }
    }
    else
    {
        keyPressed = e.which; // Firefox      
        if ((keyPressed < 48) || (keyPressed > 57)) 
        {
            keyPressed = e.preventDefault();
        }
    }
}

function select_color(color)
{
    $("#txtColor").val(color);
    $("#order_color_question").fadeOut(250, function(){
        $("#order_size_question").fadeIn(250);
    });
    $("#order_color_answer").hide();
    $("#order_size_answer").fadeIn(500);
    if(color.length > 7)
    {
        var back_color = color.substr(0, 7);
        var fore_color = color.substr(8, 7);
        
        $("#color_selection").html("<div style='float:left; margin-right:5px; font-weight:bold;'>Color : </div><div class='product_2color_container'><div class='product_2color_back' style='background-color:" + back_color + "'>&nbsp;</div><div class='product_2color_fore' style='background-color:" + fore_color + "'>&nbsp;</div></div>"); 
    }
    else
    {
        $("#color_selection").html("<div style='float:left; margin-right:5px; font-weight:bold;'>Color : </div><div class='product_color' style='background-color:" + color + "'>&nbsp;</div>");   
    }
}

function select_size(size)
{
    $("#txtSize").val(size);
    $("#order_size_question").fadeOut(250, function(){
        $("#order_qty_question").fadeIn(250);
    });
    $("#order_size_answer").hide();
    $("#order_qty_answer").fadeIn(500, function(){
        $("#txtQty").focus();
        $("#txtQty").select();
    });
        $("#size_selection").html("<div style='font-weight:bold;'>Size : " + size + "</div>");   
}

function back_to_select_color()
{
    $("#txtColor").val("");
	$("#txtQty").val("1");
    $("#order_qty_question, #order_size_question").fadeOut(250, function(){
        $("#order_color_question").fadeIn(250);
    });
    $("#order_qty_answer").hide();
	$("#order_size_answer").hide();
    $("#order_color_answer").fadeIn(2000);
    $("#color_selection").html("");
}

function back_to_select_size()
{
    $("#txtSize").val("");
	$("#txtQty").val("1");
    $("#order_qty_question, #order_color_question").fadeOut(250, function(){
        $("#order_size_question").fadeIn(250);
    });
    $("#order_qty_answer").hide();
	$("#order_color_answer").hide();
    $("#order_size_answer").fadeIn(2000);
    $("#size_selection").html("");
}

function remove_item(id)
{
	var qty = parseInt($("#spl_" + id + " .qty").text());
	var total_item = parseInt($(".total_item").text());
	total_item = Math.round(total_item - qty);
	$.ajax({
		type: "POST",
		url: "remove_item.php",
		data: "remove=" + id,
		success: function(data)
		{
			if(data != "Removed")
			{
				alert(data);
			}
			else
			{
				if(total_item >= 12)
				{
					$("#spl_" + id).fadeOut("fast");
					$(".total_item").text(total_item);
					$("#whole_price").show();
					$("#shopping_bag_price_w").html('<a href="index.php?menu=bag" title="Update Price" class="add_to_order" style="font-weight:normal;">Update Total Price</a>');
				}
				else
				{
					$("#spl_" + id).fadeOut("fast");
					$(".total_item").text(total_item);
					$("#whole_price").hide();
					$("#shopping_bag_price").html('<a href="index.php?menu=bag" title="Update Price" class="add_to_order" style="font-weight:normal;">Update Total Price</a>');
				}
				
			}
		}
	});
}

function edit_qty(id)
{
	$("#btn_edit_" + id).fadeOut(250, function(){
		$("#btn_update_" + id).fadeIn(250);
	});	
	$("#spl_" + id + " .qty span").fadeOut(250, function(){
		$("#spl_" + id + " .qty div").fadeIn(250);
		$("#txtQty_" + id).focus();
		$("#txtQty_" + id).select();
	});	
}

function update_quantity(id)
{
	var qty = parseInt($("#txtQty_" + id).val());
	var qty_old = parseInt($("#spl_" + id + " .qty span").text());
	var total_item = parseInt($("#shopping_bag_item").text());
	total_item = Math.round(total_item - qty_old);
	$.ajax({
		type: "POST",
		url: "update_quantity.php",
		data: "id=" + id + "&qty=" + qty,
		success: function(data)
		{
			if(data != "Updated")
			{
				alert(data);
			}
			else
			{
				total_item = Math.round(total_item + qty);
				if(total_item >= 12)
				{
						$("#btn_update_" + id).fadeOut(250, function(){
						$(".total_item").text(total_item);
						$("#btn_edit_" + id).fadeIn(250);
					});	
					$("#spl_" + id + " .qty div").fadeOut(250, function(){
						update_items(total_item);
						$("#spl_" + id + " .qty span").text(qty).fadeIn(250);
						$("#shopping_bag_price_w").html('<a href="index.php?menu=bag" title="Update Price" class="add_to_order" style="font-weight:normal;">Update Total Price</a>');
						$("#whole_price").show();
					});
				}
				else
				{
					$("#btn_update_" + id).fadeOut(250, function(){
						$(".total_item").text(total_item);
						$("#btn_edit_" + id).fadeIn(250);
					});	
					$("#spl_" + id + " .qty div").fadeOut(250, function(){
						update_items(total_item);
						$("#spl_" + id + " .qty span").text(qty).fadeIn(250);
						$("#shopping_bag_price").html('<a href="index.php?menu=bag" title="Update Price" class="add_to_order" style="font-weight:normal;">Update Total Price</a>');
						$("#whole_price").hide();
					});
				}
			}
		}
	});
}

function update_items(total)
{
	$("#shopping_bag_item").text(total);
}

function check_out()
{
	$("#frmCheckOut").slideDown("slow");
	$("#linkCheckOut").hide();
}

function chk_frm_checkout()
{
    var name = $("#txtName");
    var email = $("#txtCOEmail");
    var tel = $("#txtTelephone");
	var fax = $("#txtFax");
	var address = $("#txtAddress");
	var city = $("#txtCity");
	var state = $("#txtState");
	var zip = $("#txtZip");
    
    if(name.val() == "")
    {
        $("#cf_error").hide("fast", function(){
			$(this).html("Please, Enter Your name.").fadeIn("fast", function(){
				$(this).fadeOut(5000);
			});
		});
		name.focus();
		return false;
    }
    else if(email.val() == "")
    {
        $("#cf_error").hide("fast", function(){
			$(this).html("Please, Enter Your email.").fadeIn("fast", function(){
				$(this).fadeOut(5000);
			});
		});
		email.focus();
		return false;
    }
	else if(tel.val() == "")
    {
        $("#cf_error").hide("fast", function(){
			$(this).html("Please, Enter Your telephone number.").fadeIn("fast", function(){
				$(this).fadeOut(5000);
			});
		});
		tel.focus();
		return false;
    }
	else if(address.val() == "")
    {
        $("#cf_error").hide("fast", function(){
			$(this).html("Please, Enter Shipping address.").fadeIn("fast", function(){
				$(this).fadeOut(5000);
			});
		});
		address.focus();
		return false;
    }
	else if(city.val() == "")
    {
        $("#cf_error").hide("fast", function(){
			$(this).html("Please, Enter City.").fadeIn("fast", function(){
				$(this).fadeOut(5000);
			});
		});
		city.focus();
		return false;
    }
	else if(state.val() == "")
    {
        $("#cf_error").hide("fast", function(){
			$(this).html("Please, Enter State.").fadeIn("fast", function(){
				$(this).fadeOut(5000);
			});
		});
		state.focus();
		return false;
    }
	else if(zip.val() == "")
    {
        $("#cf_error").hide("fast", function(){
			$(this).html("Please, Enter Zip code.").fadeIn("fast", function(){
				$(this).fadeOut(5000);
			});
		});
		zip.focus();
		return false;
    }
    else
    {
		$("#btnCheckOut").attr("disabled", "disabled");
		var options = { 
			success: function(data)
			{
				if(data == "Success!!")
				{
					alert("Thank you for using our service.");
					window.location = "index.php";
				}
				else
				{
					$("#cf_error").hide("fast", function(){
						$(this).html(data).fadeIn("fast", function(){
							$(this).fadeOut(3000);
							$("#btnCheckOut").removeAttr("disabled");
						});
					});
				}
			}
		}; 
		$("#frmCheckOut").ajaxSubmit(options); 
		return false;  
    }
}
