/*
 |™*******************************************************************************************™*
 | jQuery and jQuery Plugin
 |™*******************************************************************************************™*
*/

/*
 |™*******************************************************************************************™*
 | TinyMCE
 |™*******************************************************************************************™*
*/
if (jQuery("#TXAwebboard_detail, #TXAwebboard_reply_detail").length > 0) { // use jQuery("textarea") for all textarea to turn tinymce
	
	tinyMCE.init({
		// General options
		mode: "textareas",
		theme: "advanced",
		editor_selector : "mceEditor",
		editor_deselector : "mceNoEditor",
		skin: "o2k7",
		plugins: "tinyBrowser,advimage, safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
		// Theme options
		theme_advanced_buttons1: "newdocument,|,bold,italic,underline,strikethrough,emotions,|,justifyleft,justifycenter,justifyright,justifyfull,fontselect,fontsizeselect",
		theme_advanced_buttons2: "",
		theme_advanced_buttons3: "",
		//theme_advanced_buttons4: "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",

		theme_advanced_toolbar_location: "top",
		theme_advanced_toolbar_align: "left",
		//theme_advanced_statusbar_location: "bottom",
		//theme_advanced_resizing: true,
		file_browser_callback : "tinyBrowser"
	});
		
}	
/*
 |----------------------------------------------------------------------------------------------
 | Form plugin
 |----------------------------------------------------------------------------------------------
*/
$().ready(function() {
				   
//---------->> click for submit event form Subscribe
	$("#LIKsubscribe").click(function() { 

		var form_action = $("form#scribe").attr("action"); // get form action
		var link_type = $(this).attr("title"); // get link title
		var link_split = link_type.split(":"); // split link title

		var options = {
			//dataType: "html", // FOR DEBUG: when download file not return error
			dataType: "json",
			beforeSubmit: showRequest,
			success: showResponse,
			// !!! insert ?token=xxx in file add, edit form !!!
			url: form_action + "?module=" + link_split[0] + "&action=" + link_split[1] + "&operation=" + link_split[2],
			type: "post"       
		}

		// submit the form 
		$("form#scribe").ajaxSubmit(options); 
		
		// return false to prevent normal browser submit and page navigation 
		return false; 
		
	});

//---------->> click for submit event form Login
	$("#LIKlogin, #LIKlogin_top").click(function() { 

		var form_action = $("form#singin").attr("action"); // get form action
		var link_type = $(this).attr("title"); // get link title
		//var link_split = link_type.split(":"); // split link title

		var options = {
			//dataType: "html", // FOR DEBUG: when download file not return error
			dataType: "json",
			beforeSubmit: showRequest,
			success: showResponse,
			// !!! insert ?token=xxx in file add, edit form !!!
			url: form_action + "?auid=" + link_type,
			type: "post"        
		}

		// submit the form 
		$("form#singin").ajaxSubmit(options); 
		
		// return false to prevent normal browser submit and page navigation 
		return false; 
		
	});

//---------->> click for submit event form Logout
	$("#LIKlogout").click(function() { 

		var form_action = $("a#LIKlogout").attr("href"); // get form action

		var options = {
			//dataType: "html", // FOR DEBUG: when download file not return error
			dataType: "json",
			beforeSubmit: showRequest,
			success: showResponse,
			// !!! insert ?token=xxx in file add, edit form !!!
			url: form_action ,
			type: "post"        
		} 

		// submit the form 
		$("a#LIKlogout").ajaxSubmit(options); 
		
		// return false to prevent normal browser submit and page navigation 
		return false; 
		
	});
	
//---------->> attach handler to LINK TAG for submit event

	$("#LIKsave, #LIKsend, #LIKedit").click(function() { 

		var form_action = $("form").attr("action"); // get form action
		var link_type = $(this).attr("title"); // get link title
		var link_split = link_type.split(":"); // split link title

		var options = {
			//dataType: "html", // FOR DEBUG: when download file not return error
			dataType: "json",
			beforeSubmit: showRequest,
			success: showResponse,
			// !!! insert ?token=xxx in file add, edit form !!!
			url: form_action + "?module=" + link_split[0] + "&action=" + link_split[1] + "&auid=" + link_split[2],
			type: "post"        
		}

		// for tinymce & form plugin ajax submit
		if ($("#TXAwebboard_detail, #TXAwebboard_reply_detail").length > 0) {
			$("form").bind("form-pre-serialize", function(e) {
				tinyMCE.triggerSave();
			});
		}
		
		// submit the form 
		$("form").ajaxSubmit(options); 
		
		// return false to prevent normal browser submit and page navigation 
		return false; 
		
	});

//---------->> click for add cart

	$(".bt-cart").click(function() { 

		var form_action = "shopping-core.php"; // get form action
		var link_type = $(this).attr("title"); // get link title
		var link_split = link_type.split(":"); // split link title

		var options = {
			//dataType: "html", // FOR DEBUG: when download file not return error
			dataType: "json",
			beforeSubmit: showRequest,
			success: showResponse,
			// !!! insert ?token=xxx in file add, edit form !!!
			url: form_action + "?module=" + link_split[0] + "&action=" + link_split[1] + "&auid=" + link_split[2] + 
				 "&type=" + link_split[3] + "&id=" + link_split[4] + "&name=" + link_split[5] + "&size=" + link_split[6] + "&qty=" + link_split[7],
			type: "get"        
		}
		
		// submit the form 
		$().ajaxSubmit(options); 
		
		// return false to prevent normal browser submit and page navigation 
		return false; 
		
	});

//---------->> click for delete

	$(".LIKdelete").click(function() { 

		var form_action = "shopping-core.php"; // get form action
		var link_type = $(this).attr("title"); // get link title
		var link_split = link_type.split(":"); // split link title

		var options = {
			//dataType: "html", // FOR DEBUG: when download file not return error
			dataType: "json",
			beforeSubmit: showRequest,
			success: showResponse,
			// !!! insert ?token=xxx in file add, edit form !!!
			url: form_action + "?module=" + link_split[0] + "&action=" + link_split[1] + "&auid=" + link_split[2] + 
				 "&id=" + link_split[3],
			type: "get"        
		}
		
		// submit the form 
		$().ajaxSubmit(options); 
		
		// return false to prevent normal browser submit and page navigation 
		return false; 
		
	});
	
//---------->> attach handler to SUBMIT TAG for submit event

	$("form").submit(function() { 
							  
		var options = {
			dataType: "json",
			beforeSubmit: showRequest,
			success: showResponse
		}

		// for tinymce & form plugin ajax submit
		if ($("#TXAwebboard_detail, #TXAwebboard_reply_detail").length > 0) {
			$("form").bind("form-pre-serialize", function(e) {
				tinyMCE.triggerSave();
			});
		}
		
		// submit the form 
		$(this).ajaxSubmit(options); 
		
		// return false to prevent normal browser submit and page navigation 
		return false; 
		
	});
	

});

/*
 |™*******************************************************************************************™*
 | Function
 |™*******************************************************************************************™*
*/
/*
 |----------------------------------------------------------------------------------------------
 | beforeSubmit Function
 |----------------------------------------------------------------------------------------------
*/
function showRequest(formData, jqForm, options) {

	// blockUI plugin
	$.blockUI({
		message: "<div id='divLoading'>Please wait ...</div>",
		css: { 
			border: "none", 
			padding: "15px", 
			backgroundColor: "#000000", "-webkit-border-radius": "10px", "-moz-border-radius": "10px", 
			opacity: "0.5", 
			color: "#ffffff" 
		}
	});
	
}

/*
 |----------------------------------------------------------------------------------------------
 | Success Function
 |----------------------------------------------------------------------------------------------
*/
function showResponse(responseText, statusText) {

 
 	// for DEBUG ajax form plugin (when use upload file) change option dataType = html
	//alert("status: " + statusText + "\n\nresponseText: \n" + responseText); return false;

	$.each(responseText, function(key, value) { // json format
								  
		if (key == "SUCCESS") {
			$.prompt(value, { buttons: { }, top: "40%" });
		} else if (key == "UNSUCCESS") {
			$.prompt(value, { buttons: { OK: true }, top: "40%" });
		} else if (key == "REDIRECT") {
			$(this).delay(1000, function() {
				$("form").resetForm();
				window.location.href = decodeURIComponent(value);
			});
		} else {
			value = value + "<input type='hidden' id='focusfield' name='focusfield' value='" + key + "' />";
			$.prompt(value, { buttons: { OK: true }, top: "40%", callback: focusField });
		}
		
	});
	
	$.unblockUI();
	
}

/*
 |----------------------------------------------------------------------------------------------
 | Focus Field
 |----------------------------------------------------------------------------------------------
*/
function focusField(v, m, f) {
	
	$("#" + f.focusfield).focus().select(); // focusfield is a name of hidden field
	  
}

/*
 |----------------------------------------------------------------------------------------------
*/

/*
 |----------------------------------------------------------------------------------------------
 | [Check Change password]
 |----------------------------------------------------------------------------------------------
*/

$("#CHKchange").click(function () {							  
  if(this.checked){
	$("#change").css("display","inline-block");
  }else{
	$("#change").css("display","none");
  }
});


