/*
 |™*******************************************************************************************™*
 | TinyMCE
 |™*******************************************************************************************™*
*/
if (jQuery("#TXAboard_detail, #TXAboard_comm_detail, #TXAinfo_comm_detail, #TXAtrip_comm_detail, #TXAnews_comm_detail, #TXAdiary_comm_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"
	});
		
}	

jQuery().ready(function() {
	
	$('a[name^="CallDeleteComment"]').each(function(){
		$(this).click(function(){
			name = $(this).attr("name");
			id	 = name.split(':')[1];
			topic = name.split(':')[2];
			topicID	= name.split(':')[3];
			
			form_action = "webboard-call-del.php";
			
			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=Weboard&action=CalldelComment&auid=" + id + "&topic=" + topic + "&id=" + topicID,
				type: "post"        
			}
			
			jQuery(this).ajaxSubmit(options);   
			
			return false;
		});
	});	
			
});

/*
 |----------------------------------------------------------------------------------------------
 | Form plugin
 |----------------------------------------------------------------------------------------------
*/
jQuery().ready(function() {

	
//---------->> attach handler to LINK TAG for submit event

	jQuery("#LIKsave, #LIKsend, #LIKedit").click(function() { 
		
		var form_action = jQuery("form").attr("action"); // get form action
		var link_type = jQuery(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] + "&formNum=" + jQuery(this).attr("id"),
			type: "post"        
		}

		// for tinymce & form plugin ajax submit
		if (jQuery("#TXAwebboard_detail, #TXAwebboard_reply_detail").length > 0) {
			jQuery("form").bind("form-pre-serialize", function(e) {
				tinyMCE.triggerSave();
			});
		}
		
		// submit the form 
		jQuery("form").ajaxSubmit(options); 
		
		// return false to prevent normal browser submit and page navigation 
		return false; 
		
	});
	
	
	jQuery(".submit-bt").click(function() { 
		
		var form_action = jQuery("form").attr("action"); // get form action
		var link_type = jQuery(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] + "&formNum=" + jQuery(this).attr("id"),
			type: "post"        
		}

		// for tinymce & form plugin ajax submit
		if (jQuery("#TXAwebboard_detail, #TXAwebboard_reply_detail").length > 0) {
			jQuery("form").bind("form-pre-serialize", function(e) {
				tinyMCE.triggerSave();
			});
		}
		
		//submit by id
		jQuery("form#".jQuery(this).attr("id")).ajaxSubmit(options); 
		
		return false; 
		
	});
	
	


//---------->> click for rating

	$("a[name='rating']").click(function() { 
		
		var form_action = $(this).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: "get"        
		}
		
		// submit the form 
		$(this).ajaxSubmit(options); 
		
		// return false to prevent normal browser submit and page navigation 
		return false; 
		
	});
	
//---------->> attach handler to SUBMIT TAG for submit event

	jQuery("form").submit(function() { 

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

			
		var options = {
			//dataType: "html",
			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] + "&formNum=" + jQuery(this).attr("id")
			
		}

		// for tinymce & form plugin ajax submit
		if ($("textarea.mceEditor").length > 0) {
			$("form").bind("form-pre-serialize", function(e) {
				tinyMCE.triggerSave();
			});
		}
		
		// submit the form 
		jQuery(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
	jQuery.blockUI({
		message: "<div id='divLoading'>กรุณารอสักครู่ ...</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 if (key == "CLOSE") {
			$(this).delay(1000, function() {
				$("form").resetForm();
			});
			$.prompt(value, { buttons: { OK: true }, top: "40%", submit: function(){ window.close();} });
		} else {
			value = value + "<input type='hidden' id='focusfield' name='focusfield' value='" + key + "' />";
			$.prompt(value, { buttons: { OK: true }, top: "40%", callback: focusField, zIndex: "99999"});
		}
		
	});
	
	$.unblockUI();
	
}

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

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

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

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


/*
 |™*******************************************************************************************™*
 | jQuery comment box 
 |™*******************************************************************************************™*
*/
	$(function(){
		
		$("#BTTmore").click(function(){	
					
			//var loading = '<span class="post-img" id="loading"><img src="./images/ajax.gif" alt="loading"></span>';
			var Limit = 2; 
			$("span#loading").show();
			
			var gopage = parseInt($("#HIDpage").val()) + Limit;
			$("#HIDpage").val(gopage);

			var form_action = "./shop-core.php"; // get form action
			var link_type 	= $(this).next("#HIDtitle").val(); // get link title
			var link_split 	= link_type.split(":"); // split link title			  

			var moreHTML = $.ajax({
				url : form_action,
				data : "module=" + link_split[0] + "&action=" + link_split[1] + "&id=" + link_split[2] + "&gopage=" + gopage,
				async : false,
				success : function(moreHTML){
					//$("div#result_search").append(moreHTML);
				}
			}).responseText;

			setTimeout(function(){
				$("span#loading").hide();
				
				if(moreHTML != "-1"){
					$("ol#data_comments").append(moreHTML);
				}
				if($("#HIDpage").val() >= link_split[3] - Limit){
					$("div#older-post").remove();
				}
			},100);
							
		});
	});

