// Preload Images
img1 = new Image(40, 40);  
img1.src="images/loading_circle.gif";

img2 = new Image(40, 40);  
img2.src="images/loading_circle.gif";
			
// When DOM is ready
$(document).ready(function(){start_modal();start_like_btn();});

function start_like_btn(){
	$(".like_mini").parent().mouseover(function() {$(this).find(".like_mini").removeClass("hide");});
	$(".like_mini").parent().mouseleave(function() {$(this).find(".like_mini").addClass("hide");});
}

function start_modal(){

	$(".show_dialog").css("cursor", "hand");	$(".show_dialog").css("cursor", "pointer");
	$(".show_form").css("cursor", "hand");$(".show_form").css("cursor", "pointer");
	$(".wait_cursor").css("cursor", "hand");$(".wait_cursor").css("cursor", "pointer");
	$(".show_img").css("cursor", "hand");$(".show_img").css("cursor", "pointer");
	
	$('a[class= show_img]').click(function(e) {
		e.preventDefault();
		var _id = $(this).attr("name");
		$.modal("<img id='img_show' src='thumbs/mem_thumbs/445_256_"+_id+".jpg' />",{
		onClose: function (dialog) {dialog.container.fadeOut('slow', function () {$.modal.close();});},
		overlayClose:true,containerCss:{
		backgroundColor:"#fff",
		borderColor:"#fff",
		paddingTop:15,paddingBottom:15,textAlign:"center",
		width:475
	}});
	});
	
	$('a[class= show_dialog]').click(function(e) {
		e.preventDefault();
		
		var _name = $(this).attr("name");
		var _arr = _name.split(",");
		var _how = _arr[0];
		var _id = _arr[1];
		var _file = "";

		switch (_how)
		{
		case "a_point":
			_file = "a_point.php?a_id=" + _id;
		  break;
		case "a_ban":
			_file = "a_ban.php?a_id=" + _id;
		  break;
		case "a_ban_sub":
			_file = "a_ban.php?from=a_sub&a_id=" + _id;
		  break;
		case "a_yellow":
			_file = "a_yellow.php?a_id=" + _id;
		  break;
		case "q_point":
			_file = "q_point.php?q_id=" + _id;
		  break;
		case "q_ban":
			_file = "q_ban.php?q_id=" + _id;
		  break;
		case "q_pin":
			_file = "q_pin.php?q_id=" + _id;
		  break;
		case "q_yellow":
			_file = "q_yellow.php?q_id=" + _id;
		  break;
		case "q_bookmark":
			_file = "q_bookmark.php?q_id=" + _id;
		  break;
		case "q_rec":
			_file = "q_recommend.php?q_id=" + _id;
		  break;
		case "logout":
			_file = "logout.php";
		  break;
		default:

		}

		$("body").css("cursor", "wait");
		$.get(_file, function(data){
			// create a modal dialog with the data
		var _modal = $(data).modal({
			onClose: function (dialog) {dialog.container.fadeOut('slow', function () {	$.modal.close();});},
			overlayClose:true,onShow: function (dialog) {
			$("body").css("cursor", "auto");
			// When the form is submitted
			$("#status a[class= send_data]").click(function(e){  
				e.preventDefault();
				// Show Gif Spinning Rotator
				$("#status").find('#ajax_loading').show();
				
				// 'this' refers to the current submitted form  
				var str = $(this).attr("name");
				
				// -- Start AJAX Call --
			
				$.ajax({  
					type: "GET",
					url: _file,  // Send the login info to this page
					data: str,  
					success: function(msg){  
				   
					$("#status").ajaxComplete(function(event, request, settings){  
						
						// Hide Gif Spinning Rotator
						$("#status").find('#ajax_loading').hide();
					
						if(msg == 'OK') // LOGIN OK?
						{  
							var _response = '<p align="center"><br />ดำเนินการเรียบร้อยแล้ว<br /><br />(แต่การปรับปรุงอาจยังไม่เห็นผลทันที)<br /><br /><br /></p>'; 
							 
							 $(this).html(_response); // Refers to 'status'
								setTimeout(function(){_modal.close();}, 1500);
						 } 
						 else // ERROR?
						 {  
							 var _response = msg;
								$('#_response').html(_response);
								setTimeout(function(){_modal.close();}, 1500);
							 
						 }  
					 });  
				  }  
			  });  
		  
			// -- End AJAX Call --
		
			return false;
		
			}); // end submit event
			
		}});

		});
	});
	
	$('a[class= show_form]').click(function(e) {
		e.preventDefault();
		var _name = $(this).attr("name");
		var _arr = _name.split(",");
		var _how = _arr[0];
		var _id = _arr[1];
		var _file = "";
		var _form_type = "";

		switch (_how)
		{
		case "a_reply":
			if (_id.indexOf(":") == -1){
				_file = "a_reply.php?a_id=" + _id;
				var _target = $(this).parent().parent().parent().find(".detail");
				_form_type = "add_reply";
			}else{
				_file = "a_reply.php?q_id="+q_id+"&q_topic="+q_topic+"&a_id=" + _id;
			}
			if (_id.indexOf(":") != -1){
				if (_id.match(/:/g).length <= 1){
					var _target = $(this).parent().parent().parent().find(".detail");
					_form_type = "add_reply";
				}else{
					_target = $(this).parent().parent().parent().parent().parent().parent().parent().find(".detail");
					_form_type = "add_sub_reply";
				}
			} else if (_id.indexOf("^") != -1){
				_target = $(this).parent().parent();
				_form_type = "edit_reply";
			}
		    break;
		case "a_reply_2":
			var _arr_ = _id.split("|");
			var _q_id = _arr_[1];
			var _q_topic = _arr_[2];
			var _q_permission = _arr_[3];
			var _a_permission = _arr_[4];
			_id = _arr_[0];
			_file = "a_reply.php?q_id="+_q_id+"&q_topic="+_q_topic+"&a_id=" + _id+"&q_permission="+_q_permission+"&a_permission="+_a_permission;
			_form_type = "a_reply_2";
		    break;
		case "short":
			_target = $(this).parent().parent().parent().parent().parent().parent().parent().find("#short_area");
			_file = "a_quick.php?mode=short&q_id=" + _id;
			_form_type = "short";
		    break;
		default:
			return;
		}
		
		$("body").css("cursor", "wait");
		$.get(_file, function(data){
			// create a modal dialog with the data
		var _modal = $(data).modal({
			onClose: function (dialog) {dialog.container.fadeOut('slow', function () {	$.modal.close();});},
			overlayClose:true,onShow: function (dialog) {			
			$("body").css("cursor", "auto");
			setTimeout(function(){$("textarea").focus();},50);
			$("textarea").keydown(function(e){
				// Enter was pressed without shift key
				if (e.keyCode == 13 && !e.shiftKey)
				{
					e.preventDefault();
					$('form#a_reply').submit();
				}
			});
			// When the form is submitted
			$("#status form").submit(function(e){  
				e.preventDefault();
				// Hide 'Submit' Button
				$('#submit').attr("disabled", "true");
				
				// Show Gif Spinning Rotator
				$("#status").find('#ajax_loading').show();
				
				// 'this' refers to the current submitted form  
				var str = $(this).serialize();  
				
				// -- Start AJAX Call --
			
				$.ajax({  
					type: "POST",
					url: _file.split('?')[0], 
					data: str,  
					success: function(msg){  
				   
					$("#status").ajaxComplete(function(event, request, settings){  
					 
					 // Show 'Submit' Button
						$('#submit').attr("disabled", "");
						
						// Hide Gif Spinning Rotator
						$("#status").find('#ajax_loading').hide();
					
						if(msg == 'OK' || msg =="")
						{  
							switch (_form_type) {
								case "add_reply":
									var _reply_text = '<table class="a_reply_area" cellspacing="0" cellpadding="0"><tr><td width="60" valign="top"><img class="50img" title="'+$(this).find("#a_name").val()+'" src="thumbs/mem_thumbs/50_50_'+$(this).find("#m_id").val()+'.jpg" class="mini_thumb" /></td><td valign="top"><div class="a_reply_button" style="color:#AAAAAA">(ปุ่มจะแสดงเมื่อรีเฟรชหน้า)</div><div style="font-weight:bold" >'+$(this).find("#a_name").val()+'<span class="replyfont"></span></div>'+$(this).find("#a_reply_detail").val()+'</td></tr></table>';
									_target.append(_reply_text);
									var _response = '<p align="center"><br /><br />ดำเนินการเรียบร้อยแล้ว<br /><br /><br /><br /></p>'; 
									break;
								case "add_sub_reply":
									var _reply_text = '<table class="a_reply_area" cellspacing="0" cellpadding="0"><tr><td width="60" valign="top"><img class="50img" title="'+$(this).find("#a_name").val()+'" src="thumbs/mem_thumbs/50_50_'+$(this).find("#m_id").val()+'.jpg" class="mini_thumb" /></td><td valign="top"><div class="a_reply_button" style="color:#AAAAAA">(ปุ่มจะแสดงเมื่อรีเฟรชหน้า)</div><div style="font-weight:bold" >'+$(this).find("#a_name").val()+'<span class="replyfont"></span></div>'+$(this).find("#a_reply_detail").val()+'</td></tr></table>';
									_target.append(_reply_text);
									var _response = '<p align="center"><br /><br />ดำเนินการเรียบร้อยแล้ว<br /><br /><br /><br /></p>'; 
									break;
								case "edit_reply":
									_target.contents()
									.filter(function() { return this.nodeType == 3; })
									.replaceWith($(this).find("#a_reply_detail").val());
									var _response = '<p align="center"><br /><br />ดำเนินการเรียบร้อยแล้ว<br /><br /><br /><br /></p>'; 
									break;
								case "short":
									if ($(this).find("#a_name").val()){
									var short_text = '<table class="a_hilight_area" cellpadding="0" cellspacing="0"><tbody><tr><td width="55"><img title="'+$(this).find("#a_name").val()+'" src="thumbs/mem_thumbs/50_50_'+$(this).find("#from_m_id").val()+'.jpg"></td><td valign="top">'+$(this).find("#a_detail").val()+'</td></tr></tbody></table>';
									_target.prepend(short_text);
									var _response = '<p align="center"><br /><br />ดำเนินการเรียบร้อยแล้ว<br /><br /><br /><br /></p>'; 
									}
									break;
								default:
									var _response = '<p align="center"><br />ดำเนินการเรียบร้อยแล้ว<br /><br />(แต่การปรับปรุงอาจยังไม่เห็นผลทันที)<br /><br /><br /></p>'; 
							}

							$(this).html(_response); // Refers to 'status'
							setTimeout(function(){_modal.close();}, 1500);
							
							//setTimeout('go_to_private_page("")', 2000); 
						 }  
						 else // ERROR?
						 {  
							 var _response = msg;
							$('#_response').html(_response);
							//setTimeout(function(){_modal.close();}, 2000);
						 }  
					 });  
				  }  
			  });  
		  
			// -- End AJAX Call --
		
			return false;
		
			}); // end submit event
			
		}});

		});
	});
				
	
	
	$("#login_link").click(function(e){
		e.preventDefault();
		$("body").css("cursor", "wait");
		$.get("login.php", function(data){
			// create a modal dialog with the data
		$(data).modal({
			onClose: function (dialog) {dialog.container.fadeOut('slow', function () {	$.modal.close();});},
			overlayClose:true,onShow: function (dialog) {
			$("body").css("cursor", "auto");
			// When the form is submitted
			$("#status form").submit(function(e){  
				e.preventDefault();
				// Hide 'Submit' Button
				$('#submit').attr("disabled", "true");
				// Show Gif Spinning Rotator
				$("#status").find('#ajax_loading').show();
				
				// 'this' refers to the current submitted form  
				var str = $(this).serialize();  
				
				// -- Start AJAX Call --
			
				$.ajax({  
					type: "POST",
					url: "login.php",  // Send the login info to this page
					data: str,  
					success: function(msg){  
				   
					$("#status").ajaxComplete(function(event, request, settings){  
					 
					 // Show 'Submit' Button
						$('#submit').attr("disabled", "");
						
						// Hide Gif Spinning Rotator
						$("#status").find('#ajax_loading').hide();  
					
						if(msg == 'OK') // LOGIN OK?
						{  
							var login_response = '<div id="login_form" style="width:100%; height:130px; text-align:center ; vertical-align:middle"><br /><img src="images/loading_circle.gif"></div>';  
							
							$('a.modalCloseImg').hide();  
							 
							 $(this).html(login_response); // Refers to 'status'
							
							// After 2 seconds redirect the 
							setTimeout('go_to_private_page("")', 1500); 
						 }  
						 else if(msg.indexOf(".php") != -1)
						 {  
							 var login_response = '<div id="login_form" style="width:100%; height:130px; text-align:center ; vertical-align:middle"><br /><img src="images/loading_circle.gif"></div>'; 
							$('a.modalCloseImg').hide();  
							 
							 $(this).html(login_response); // Refers to 'status'
							// After 2 seconds redirect the 
							setTimeout('go_to_private_page("' + msg + '")', 1500); 
						 }  
						 else // ERROR?
						 {  
							 var login_response = msg;
							 $('#login_response').html(login_response);
						 }  
					 });  
				  }  
			  });  
		  
			// -- End AJAX Call --
		
			return false;
		
			}); // end submit event
			
		}});

		});
	});

}

function go_to_private_page(where)
{
	if(where != ""){
			
		window.location = where;
	}else{
		window.location.reload(); //window.location = 'private.php'; // Members Area
	}
}

function auto_close(_modal)
{
	_modal.close();
}
