/*
* @version : 4.4 (Patch 7)
* @update : 2010-04-09
*/

$(function(){

	/****************************************
	 * Initial
	 ****************************************/
	$("div.Footer").flash({
		src: "flash/footer.swf", width: 900, height: 75,
		wmode: "transparent", menu: "false", flashvars: { callback: 'popupTerm()' }
	});


	$("div.flash_title, div.flash_price, div.flash_crust").each(function() {
		var meta = $(this).metadata();
		wmode = ( $(this).metadata().flashvars.wmode != undefined ? $(this).metadata().flashvars.wmode :  "opaque" ); // opaque, transparent;
		$( this ).flash({
				src: meta.flash,
				width: meta.width,
				height: meta.height,
				wmode: wmode, menu: "false",
				flashvars: { title: (meta.flashvars.title || ""), title_th: (meta.flashvars.title_th || ""), title_en: (meta.flashvars.title_en || ""), id: $(this).attr("id") }
			}, { version: 8 }
		);
	});

	/*if ($("div.flash_title").length > 0 ) {
		$("div.flash_title").each(function() {
			wmode = ( $(this).metadata().flashvars.wmode != undefined ? $(this).metadata().flashvars.wmode :  "opaque" ); // opaque, transparent;
			$( this ).flash({
					src: $(this).metadata().flash,
					width: $(this).metadata().width,
					height: $(this).metadata().height,
					wmode: wmode, menu: "false",
					flashvars: { title_th: $(this).metadata().flashvars.title_th , title_en: $(this).metadata().flashvars.title_en, id: $(this).attr("id") }
				}, { version: 8 }
			);
		});
	}
	if ($("div.flash_price").length > 0 ) {
		$("div.flash_price").each(function() {
			wmode = ( $(this).metadata().flashvars.wmode != undefined ? $(this).metadata().flashvars.wmode :  "opaque" ); // opaque, transparent;
			$( this ).flash({
					src: $(this).metadata().flash,
					width: $(this).metadata().width,
					height: $(this).metadata().height,
					wmode: wmode, menu: "false",
					flashvars: { title: $(this).metadata().flashvars.title }
				}, { version: 8 }
			);
		});
	}

	if ($("div.flash_crust").length > 0 ) {
		$("div.flash_crust").each(function() {
			wmode = ( $(this).metadata().flashvars.wmode != undefined ? $(this).metadata().flashvars.wmode :  "opaque" ); // opaque, transparent;
			$( this ).flash({
					src: $(this).metadata().flash,
					width: $(this).metadata().width,
					height: $(this).metadata().height,
					wmode: wmode, menu: "false",
					flashvars: { title_th: $(this).metadata().flashvars.title_th , title_en: $(this).metadata().flashvars.title_en }
				}, { version: 8 }
			);
		});
	}*/

	if ($("div.flash_tooltip").length > 0 ) {
		$("div.flash_tooltip").each(function() {
			$( this ).flash({
					src: $(this).metadata().flash,
					width: $(this).metadata().width,
					height: $(this).metadata().height,
					wmode: "transparent", menu: "false",
					flashvars: { title_en: ($(this).metadata().flashvars.title || ""), id: $(this).attr("id"), swf_width: $(this).metadata().width }
				}, { version: 8 }
			);
		});
	}

	/*if ($("div.flash_banner").length > 0 ) {
		$("div.flash_banner").each(function() {
			var meta = $(this).metadata();
			wmode = "opaque"; // opaque, transparent;
			if ( meta.flashvars != undefined && meta.flashvars.wmode != undefined ) {
				wmode = meta.flashvars.wmode;
			}
			$( this ).flash({
					src: meta.flash,
					width: meta.width,
					height: meta.height,
					wmode: wmode, menu: "false",
					flashvars: { code: (meta.flashvars.code || ""), id: (meta.flashvars.id || ""), type: (meta.flashvars.type || "") }
				}, { version: 8 }
			);
		});
	}*/


	// Global Element
	$("a").each( function() {
		if ($(this).attr("href") == undefined || $(this).attr("href") == "#" || $(this).attr("href") == "") {
			if ( $(this).parent().parent().parent().attr("id") != "menu" ) {
				$(this).attr({href:"#"});
				//$(this).css("cursor", "default");
				$(this).click( function() { return false; });
			}
		}
		$(this).focus( function() { $(this).blur(); });
	});
	$("input[type=submit], input[type=reset], input[type=button], input[type=select], input[type=image], input[type=radio], input[type=checkbox]" ).each( function() {
		$(this).focus( function() { $(this).blur(); });
	});

	// handle select text
	$("input[type=text], textarea").each( function() {
			$(this).focus( function() {
				$(this).select();
				if ( !$(this).hasClass("focus") ) $(this).addClass("focus");
			});

			if( $(this).hasClass("username") ) {
				$(this).blur( function() {
					$(this).val( clean_whitespace($(this).val()) );
					$(this).removeClass("focus");
				});
			} else {
				$(this).blur( function() {
					$(this).val( jQuery.trim( $(this).val() ) );
					$(this).removeClass("focus");
				});
			}
	});

	$("input[type=password]", this).each( function() {
			$(this).focus( function() {
				$(this).select();
				if ( !$(this).hasClass("focus") ) $(this).addClass("focus");
			});
			$(this).blur( function() {
				$(this).removeClass("focus");
			});
	});

	/*if ( $("div#swfBalloon").length > 0 ) {
		var meta = $("div#swfBalloon").metadata();
		if ( meta.flashvars.close_hour == undefined ) meta.flashvars.close_hour = "0";
		if ( meta.flashvars.close_min == undefined ) meta.flashvars.close_min = "0";
		if ( meta.flashvars.close_ms == undefined ) meta.flashvars.close_ms = "0";

		$("div#swfBalloon").flash({
				src: "flash/balloon.swf",
				width: 220, height: 115,
				wmode: "transparent", menu: "false",
				flashvars: { close_hour: meta.flashvars.close_hour, close_min: meta.flashvars.close_min, close_ms: meta.flashvars.close_ms }
		});
	}*/

	$("div.Shortcuts a#mnLogin").click(function(e){ e.preventDefault(); });
	$("div.Shortcuts a#mnLogout2").click(function(e){ e.preventDefault(); });

	if ( $("div#Pizza_ContentRight, div.cRight div.Cart").length > 0 ) {
		// Remove
		$("div#Pizza_ContentRight a.Pizza_btCancel, div.cRight div.Cart a.btCancel").each( function() {
			$(this).click(function() {
				var meta = $.metadata.get(this);
				if (meta.option == undefined) meta.option = '';

				$.ajax({
					url: "api/cart.php",
					data: "action=remove&code="+meta.code+"&option="+meta.option,
					type: "get",
					cache: false,
					dataType: "html",
					complete: function (result) {
						//document.write(result.responseText);
						document.location.reload();
					},
					error: function(result) {
						document.location.reload();
						//alert(result.statusText);
					}
				});
			});
		});

		// btIncrease
		$("div#Pizza_ContentRight a.Pizza_btIncrease, div.cRight a.btIncrease").each( function() {
			$(this).click(function() {
				var meta = $.metadata.get(this);
				if (meta.option == undefined) meta.option = '';

				$.ajax({
					url: "api/cart.php",
					data: "action=increase&code="+meta.code+"&option="+meta.option,
					type: "get",
					cache: false,
					dataType: "html",
					complete: function (result) {
						//document.write(result.responseText);
						//alert('increase');
						document.location.reload();
					},
					error: function(result) {

						document.location.reload();
						//alert(result.statusText);
					}
				});
			});
		});

		// Decrease
		$("div#Pizza_ContentRight a.Pizza_btDecrease, div.cRight div.Cart a.btDecrease").each( function() {
			$(this).click(function() {
				var meta = $.metadata.get(this);
				if (meta.option == undefined) meta.option = '';
				$.ajax({
					url: "api/cart.php",
					data: "action=decrease&code="+meta.code+"&option="+meta.option,
					type: "get",
					cache: false,
					dataType: "html",
					complete: function (result) {

						document.location.reload();
					},
					error: function(result) {
						//document.write(result.statusText);
						document.location.reload();
						//alert(result.statusText);
					}
				});
			});
		});

		// Decrease
		$("div#Pizza_ContentRight input.btCheckOut, div.cRight div.Cart input.btCheckout_").each( function() {
			$(this).click(function(e) {
				e.preventDefault();
				//คุณสั่งอาหารจากร้าน KFC ไม่ถึง 120 บาท (ไม่รวมค่าจัดส่ง)
				//Your KFC orders price less than 120 baht. (Don't include delivery charge)

				$.ajax({
					url: "api/cart.php",
					data: 'action=get_price',
					type: "get",
					cache: false,
					dataType: "html",
					complete: function (result) {
						if ( parseInt(result.responseText) >=120 ) {

							// Check Recommend Menu
							$.ajax({
								url: "api/popup.php",
								data: "action=chk_checkout",
								type: "get",
								cache: false,
								dataType: "html",
								complete: function (result) {
									if ( parseInt(result.responseText) == 1 ) {
										alert('Please check for function get_product_list_cart');
										bindMenuCheckout();
									} else {
										location.href = 'ordering.php';
										// action=add_cart_option&id=117
									}
								},
								error: function(result) {
									//alert(result.statusText);
								}
							});

						} else {
							if ( $("body").hasClass("en") ) {
								bindErrorMessage("Notice", "Your KFC orders price less than 120 baht. (Don't include delivery charge)");
							} else {
								bindErrorMessage("Notice", "คุณสั่งอาหารจากร้าน KFC ไม่ถึง 120 บาท (ไม่รวมค่าจัดส่ง)");
							}
						}

					},
					error: function(result) {
						//alert(result.statusText);
					}
				});

			});
		});
	}

	// Login
	if($("form#frmLogin").length > 0) {
		$("form#frmLogin").each( function(){
			$(this).submit(function(e){
				e.preventDefault();
				var validationError = false;
				$("input[type=text], input[type=password]", this).each( function() {
					if ($(this).attr("class")) {
						if (!validateField(this)) validationError = true;
					}
				});


				if ( validationError==true && $("form#frmLogin input#username").hasClass("focus") ) {
					alert("Please enter your username.");
					$("form#frmLogin input#username").focus();
				} else if ( validationError==true && $("form#frmLogin input#password").hasClass("focus") ) {
					alert("Please enter your password.");
					$("form#frmLogin input#password").focus();
				}
				if ( !validationError ) {
					$.ajax({
						url: "api/login.php",
						data: $("form#frmLogin").serialize(),
						type: "post",
						cache: false,
						dataType: "html",
						complete: function (result) {
							switch( result.responseText ) {
								case "1":
									document.location.reload();
									//$("div#cTopLogin").animate( { top: -350 }, { queue: false, duration: 300, complete: function() { destroyOverlay(); $(document).unbind("mousemove"); bindLocation(); } } );
									break;
								case "100": alert("Please try again."); break;
								case "101":
								case "102": alert("Invalid username or password. Please try again."); break;
								case "103": alert("Your registration not complete. Please activate your account."); break;
							}
							$("form#frmTopLogin input#topUsername").focus();

						},
						error: function(result) {
							//alert(result.statusText);
						}
					});

				}
				return false;
				//return !validationError;
			});

		});
	}



	// =============check min price ================

	$("div#Pizza_ContentRight input.btpayment, div.cRight div.Cart input.btCheckout").each( function() {
		$(this).click(function(e) {
			e.preventDefault();

			if($("form#frmLogin").length == 0) {
				$.ajax({
					url: "api/cart.php",
					data: 'action=chk_min_price',
					type: "get",
					cache: false,
					dataType: "text",
					complete: function (result) {
						if ( result.responseText == "1" ) {

							//location.href = 'ordering_step1.php?step=1';
							$.ajax({
								url: "api/cart.php",
								data: 'action=chk_tradeup',
								type: "get",
								cache: false,
								dataType: "text",
								complete: function (result) {
									//alert(result.responseText);
									if ( result.responseText == "1" ) {
										bindTradeup();
									}else{
										location.href = 'ordering_step1.php';
									}

								},
								error: function(result) {
									//alert(result.statusText);
								}
							});


						} else {
							bindDisplayMessage("คุณสั่งอาหารจากร้าน Pizzahut ไม่ครบ "+result.responseText+"บาท (ไม่รวมค่าจัดส่ง)", "");

						}

					},
					error: function(result) {
						//alert(result.statusText);
					}
				});

			}else{
				bindDisplayMessage("ท่านยังไม่ได้ทำการล็อกอินเข้าใช้งานระบบ", "");
			}

		});
	});

	/*---------end check min price */

	/* Term and Condition */
	$("div#Pizza_navFooter a#Pizza_Term").each(function(){
		$(this).click(function(e){
			e.preventDefault();
			popupTerm();
		});
	});

});
