/*
* @version : 2.4.1 (Patch 3)
* @update : 2011-11-28
* @author : บริษัท อีมีเดีย จำกัด
* @comment : Support CSC, 1150 VIP
*/

$(function(){

	// 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(e) { e.preventDefault(); });
			}
		}*/
		if ($(this).attr("href") == undefined || $(this).attr("href") == "#" || $(this).attr("href") == "") {
			$(this).attr({href:"#"});
			$(this).click( function(e) { e.preventDefault(); });
		}
		$(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() {
			if ( $(this).hasClass("username") || $(this).hasClass("number") || $(this).hasClass("amount") ) {
				$(this).attr("autoComplete", "Off");
			}
			$(this).focus(function() {
				$(this).select();
				if ( !$(this).hasClass("focus") ) $(this).addClass("focus");
			});

			$(this).blur(function() {
				if ( $(this).hasClass("username") ) { $(this).val( clean_whitespace($(this).val()) ); }
				if ( $(this).attr("type") != "password" ) { $(this).val( jQuery.trim( $(this).val() ) ); }
				if ( $(this).hasClass("uppercase") ) { $(this).val( $(this).val().toUpperCase() ); }

				$(this).removeClass("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");
		});
	});


	$("input.phone,input.phone_ext,input.numeric").each(function() {
		$(this).keypress(function(e) {
			return key_digit(e);
		});
	});


	// Login Top
	$("div.Header div.plMember a.btLogin").click(function(e){
		e.preventDefault();
		dialogLogin( "api/dialog_"+$(this).attr("href") );
	});

	// My Order
	if ( $("div.Header div.plMenu a.btMyOrder").length > 0 ) {
		$("div.Header div.plMenu a.btMyOrder").click(function(e){
			if ( $("div#plFoodTray").length == 0 && ($(this).attr("href")=="payment.php") || $("div#plFoodTray table.tbList tr.item").length > 0 ) {
				e.preventDefault();
				blockPage();
				location.href = "payment.php";
			} else if ( $("div.Header div.plMember a.btLogin").length > 0 ) {
				e.preventDefault();
				dialogLogin( "api/dialog_"+$("div.Header div.plMember a.btLogin").attr("href"), function(){ blockPage(); location.href="order_history.php"; } );
			}
		});
	}

	// Logout
	$("div.Header div.plMember a.btLogout").click(function(e){
		e.preventDefault();
		Logout("api/"+$(this).attr("href"));
	});

	// VIP Member
	$("div#pageVIP div.plUpgrade a.btLogin").click(function(e){
		e.preventDefault();
		dialogLogin( "api/dialog_login.php");
	});

	/* Drag & Drop */
	handle_DragDrop();

	// Add to cart
	handle_btOrderNow();

	// btInfo
	//handle_btInfo();

	// btCustomize
	handle_btCustomize();

	// btBack
	handle_btBack();

	// Food Tray
	handle_Foodtray();

	// Last Order
	handle_LastOrder();

	// Favorite Menu
	handle_FavoriteMenu();

	// Customize Chicken
	handle_ChickenPart();

	// Customize Menu
	handle_Customize();

	// Date/Time
	handle_Datetime();

	// Popup
	/*if ($.fn.prettyPhoto!=undefined) {
		// Initial prettyPhoto
		$("a[rel^=\'prettyPhoto\']").prettyPhoto({
			slideshow: false,
			show_title: false,
			overlay_gallery: false,
			modal: true,
			deeplinking: false,
			default_width: 700,
			default_height: 400,
			allow_resize: false,
			keyboard_shortcuts: false,
			image_markup: '<a href="#" class="pp_bt_close"><img id="fullResImage" src="{path}" /></a>'
		});

		$.prettyPhoto.open('images/popup/2011-10-26.jpg');
		$("a.pp_bt_close").live("click", function(e){
			e.preventDefault();
			$.prettyPhoto.close();
		});
	}*/

});

/*$("div#Body").resize(function(e){
	$("div#Footer").css("position", "absolute").css("bottom", "0px");
});*/

//bindDisplayMessage("", (get_lang()=="en" ? "Sorry for inconvenience! We are upgrading our system<h5>Online Ordering is out of service from now on untill midnight.<br><br>To place food order, please call 1150</h5>" : "ขณะนี้กำลังทำการอัพเกรดระบบใหม่<br /><br />หากต้องการสั่งอาหาร<br />กรุณาใช้บริการผ่าน Call Center ที่หมายเลข 1150 ค่ะ"));

var hash_firstload = true;
$(window).bind('hashchange', function(e) {

	if ( typeof loadContent == "function" ) {
		var url = $.bbq.getState("url");
		var baseUrl = $.url.attr("file")+(($.url.attr("query")||"")!="" ? "?"+$.url.attr("query") : "");
		//var url = $.bbq.getState("url");
		//console.log(url + ":"+baseUrl)
		if ( hash_firstload==true && url==baseUrl) url='';
		if ( url != undefined && url != '' && url != '#') loadContent( url );
		hash_firstload = false;
	}
});

// Since the event is only triggered when the hash changes, we need to trigger
// the event now, to handle the hash the page may have loaded with.
$(document).ready(function(){
	$(window).trigger('hashchange');
});
