/*
	THAIMESS Javascript File
	Author: Theerasak Phuetthanyakij
	Website : http://www.thaimess.com
*/

var site = "http://" + window.location.hostname;
var tmp = "";

if(site=="http://localhost")
{
	site += "/thaimess.com";
	mainsite = site;
}
else if(site.indexOf("lookdd")>=0)
{
	mainsite = "http://www.lookdd.com";
}
else if(site.indexOf("msnja")>=0)
{
	mainsite = "http://www.msnja.com";
}
else
{
	mainsite = "http://www.thaimess.com";
}

window.onload = function() 
{	
	if($("#ads_area_display").length > 0 && $("#ads_area_load").length > 0)
	{
		document.getElementById('ads_area_display').appendChild(document.getElementById('ads_area_load'));	
		$("#ads_area_load").css("display","");
	}
	
	if($("#stat").length > 0 && $("#stat_load").length > 0)
	{
		document.getElementById('stat').appendChild(document.getElementById('stat_load'));	
		$("#stat_load").css("display","");
	}
}

function top_login()
{
	$("#whole_dark_bg")
		.css("width",document.body.clientWidth)
		.css("height",document.body.clientHeight)
		.fadeIn("slow");
}

function menu_pane_click(obj)
{
	$('#black_caption').css("display","none");
	$("#menu_pane_head_left").html("โปรดรอ ..");
	$("#menu_pane_data").html("<img src=\"" + mainsite + "/images/loader.gif\">");
	$("#menu_pane_bg")
		.css("top",($(obj).offset().top+25)+"px")
		.css("left",($(obj).offset().left-($("#menu_pane_bg").width()/2)+($(obj).width()/2))+"px")
		.css("display","block");
			
	$.ajax({
		url: site + '/servlet/?do=menuPaneGet&pane=' + $(obj).attr("menu") + "&jsoncallback=?",
		cache: false,
		//timeout: 4000,
		dataType: "json",
		contentType: 'application/json; charset=utf-8',
		success: function(json) {
			$("#menu_pane_head_left").html(json.header);
			$("#menu_pane_data").html(json.data);
			$("#menu_pane_bg")
				.css("top",($(obj).offset().top+25)+"px")
				.css("left",($(obj).offset().left-($("#menu_pane_bg").width()/2)+($(obj).width()/2))+"px")
				.css("display","block");
				
			if($("#menu_pane_form input[type=text]")[0])
			{
				$("#menu_pane_form input[type=text]")[0].focus();
			}
			
			$( 'html, body' ).animate( { scrollTop: $(obj).offset().top }, 'slow' );
		},
		error: function(){
			//window.location = mainsite + '/' + $(obj).attr("menu") + '/';
		}
	});
}

function openCenterPopup(obj)
{
	$('#black_caption').css("display","none");
	$("#menu_pane_head_left").html("โปรดรอ ..");
	$("#menu_pane_data").html("<img src=\"" + mainsite + "/images/loader.gif\">");
	$("#menu_pane_bg")
		.css("top",( $(window).height() - $("#menu_pane_bg").height() ) / 2+$(window).scrollTop() + "px")
		.css("left",( $(window).width() - $("#menu_pane_bg").width() ) / 2+$(window).scrollLeft() + "px")
		.css("display","block");
	
	$.ajax({
		url: site + '/servlet/?do=menuPaneGet&pane=' + $(obj).attr("menu") + (($(obj).attr("urloption")) ? $(obj).attr("urloption") : '') + "&jsoncallback=?",
		cache: false,
		dataType: "json",
		contentType: 'application/json; charset=utf-8',
		success: function(json) {	
	
			if(json.result)
			{
				$("#menu_pane_head_left").html(json.header);
				$("#menu_pane_data").html(json.data);
				$("#menu_pane_bg")
					.css("top","20px")
					.css("left",( $(window).width() - $("#menu_pane_bg").width() ) / 2+$(window).scrollLeft() + "px")
					.css("display","block");
				$( 'html, body' ).animate( { scrollTop: $("#menu_pane_bg").offset().top }, 'slow' );
				
				if($("#menu_pane_form input[type=text]")[0])
				{
					$("#menu_pane_form input[type=text]")[0].focus();
				}
			}
			else
			{
				gotoLogin();
			}
		}
	});
}

function menu_pane_post()
{
	$("#menu_pane_form :input").attr('readonly', true);
	$("#menu_pane_form :input[type=submit]").attr('disabled', true);
	
	$.ajax({
		url: site + '/servlet/?do=menuPanePost&pane=' + $("#menu_pane_form").attr("menu") + "&jsoncallback=?",
		type: 'POST',
		data: $("#menu_pane_form").serialize(),
		dataType: "json",
		cache: false,
		timeout: 10000,
		success: function(json) {
			
			if(json.result)
			{
				if(json.url=="refresh")
				{
					window.location.reload();
				}
				else
				{
					window.location=json.url;
				}
			}
			else
			{
				// error = 1(login incorrect), error = 2 (require field not complete)
				if(json.error == 1)
				{
					alert("ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง");
				}
				else if(json.error == 2)
				{
					alert("ข้อมูลที่กรอกมาไม่ครบถ้วน");
				}
				else if(json.error == 3)
				{
					alert("พบความผิดพลาด กรุณาเข้าสู่ระบบใหม่อีกครั้งก่อนการแก้ไข");
					gotoLogin();
				}
				
				$("#menu_pane_form :input").removeAttr("readonly");
				$("#menu_pane_form :input[type=submit]").removeAttr("disabled");
				
				if($("#menu_pane_form input")[0])
				{
					$("#menu_pane_form input")[0].focus();
				}
			}
		},
		error: function(){
			alert("กรุณารอสักครู่และดำเนินการใหม่อีกครั้ง");
		}
	});
}

function profile_edit()
{
	$.ajax({
		url: site + '/servlet/?do=profileEdit' + "&jsoncallback=?",
		type: 'POST',
		data: $("#profile_form").serialize(),
		dataType: "json",
		cache: false,
		timeout: 4000,
		success: function(json) {
			if(json.result)
			{
				jAlert("ข้อมูลได้ถูกแก้ไขเรียบร้อยแล้ว", 'ผลการดำเนินการ');
				window.location.reload();
			}
			else
			{
				if(json.error == 1)
				{
					jAlert("ไม่สามารถแก้ไขได้ในขณะนี้ กรุณาลองใหม่ภายหลัง", 'ผลการดำเนินการ');
				}
				else if(json.error == 2)
				{
					jAlert("ชื่อเล่นห้ามเป็นช่องว่าง และ เป็นภาษาไทยหรือภาษาอังกฤษเท่านั้น", 'ผลการดำเนินการ');
				}
			}
		}
	});
}

function comment()
{
	$("#comment_form :input").attr('readonly', true);
	$("#comment_form :input[type=submit]").attr('disabled', true);
	$("#comment_loading").html("<img src=\"" + mainsite + "/images/loader2.gif\">");
	
	$.ajax({
		url: site + '/servlet/?do=comment&s=' + $("#comment_form").attr("menu") + "&jsoncallback=?",
		type: 'POST',
		data: $("#comment_form").serialize(),
		dataType: "json",
		cache: false,
		timeout: 4000,
		success: function(json) {		
			if(json.result)
			{
				if(json.url=="refresh")
				{
					window.location.reload();
				}
				else
				{
					window.location=json.url;
				}
			}
			else
			{
				// error = 1(sesssion expire), error = 2 (cannot insert comment), error = 3 (not validate)
				if(json.error == 1 || json.error == 3)
				{
					alert("คุณยังไม่ได้เข้าสู่ระบบ ไม่สามารถแสดงความคิดเห็นได้");
					gotoLogin(1);
				}
				else if(json.error == 2)
				{
					jAlert("ไม่สามารถแสดงความคิดเห็นได้ในขณะนี้", 'ผลการดำเนินการ');
				}
				else if(json.error == 4)
				{
					jAlert("คุณยังไม่ได้กรอกข้อความใดๆ", 'ผลการดำเนินการ');
				}
				
				$("#comment_form :input").removeAttr("readonly");
				$("#comment_form :input[type=submit]").removeAttr("disabled");
				$("#comment_loading").html("");
			}
		}
	});
}

function confirmRefresh(obj)
{
	jConfirm($(obj).attr('quesdetail'), $(obj).attr('queshead'), function(r) {
		if(r)
		{
			$.ajax({
				url: site + '/servlet/?do=' + $(obj).attr('menu') + "&jsoncallback=?",
				cache: false,
				timeout: 4000,
				dataType: "json",
				contentType: 'application/json; charset=utf-8',
				success: function(json) {
					if(json.result)
					{
						if(json.url=="refresh")
						{
							window.location.reload();
						}
						else
						{
							window.location=json.url;
						}
					}
					else
					{
						jAlert("ไม่สามารถดำเนินการได้ในขณะนี้ กรุณาลองใหม่ภายหลัง", 'ผลการดำเนินการ');
					}
				},
				error: function(){
					jAlert("ไม่สามารถดำเนินการได้ในขณะนี้ กรุณาลองใหม่ภายหลัง", 'ผลการดำเนินการ');
				}
			});
		}
	});	
}

function gotoRegister(silence)
{
	if(!silence)
	{
		alert("สิทธืในการเข้าถึงส่วนนี้ถูกจำกัดไว้สำหรับสมาชิก กรุณาสมัครสมาชิกก่อน");
	}
	$( 'html, body' ).animate( { scrollTop: $('#menu_register').offset().top }, 'slow' );
	$('#menu_register').click();
}

function identify()
{
	$.get(site + '/servlet/?do=identify');
}

function gotoLogin(silence)
{
	if(!silence)
	{
		alert("สิทธืในการเข้าถึงส่วนนี้เฉพาะสมาชิกเท่านั้น กรุณาเข้าสู่ระบบก่อน");
	}
	$( 'html, body' ).animate( { scrollTop: $('#menu_login').offset().top }, 'slow' );
	$('#menu_login').click();
}

function menu_pane_move()
{
	$("#menu_pane_bg")
			.css("top",0)
			.css("left",0);
}

function menu_pane_close()
{
	$("#menu_pane_head span").html("");
	$("#menu_pane_data").html("");
	$("#menu_pane_bg").css("display","none");
	$('#black_caption').css("display","none");
}

function validateSetRed(obj_name)
{
	$("#menu_pane_form input[name=" + obj_name + "]").css("border","1px solid #d86969");
}

function validateSetGreen(obj_name)
{
	$("#menu_pane_form input[name=" + obj_name + "]").css("border","1px solid #81d869");
}

function registerValidate(obj)
{
	$.ajax({
		url: site + '/servlet/?do=validate' + "&jsoncallback=?",
		type: 'POST',
		data: $("#menu_pane_form").serialize(),
		dataType: "json",
		cache: false,
		timeout: 4000,
		success: function(json) {
			
			if($(obj).attr("name") == "user" || $(obj).attr("name") == "menu_pane_form")
			{
				if(!json.user.format || !json.user.zero || !json.user.exist)
				{
					if(!json.user.zero)
					{
						alert("คุณยังไม่ได้กรอกชื่อผู้ใช้");
						$("#menu_pane_form input[name=user]").focus();
					}
					else if(!json.user.exist)
					{
						alert("ชื่อผู้ใช้นี้ได้ถูกใช้แล้ว");
						$("#menu_pane_form input[name=user]").focus();
					}
					else if(!json.user.format)
					{
						alert("ชื่อต้องเป็นภาษาอังกฤษ หรือตัวเลข หรือ_ ไม่เกิน 15 ตัวอักษร");
						$("#menu_pane_form input[name=user]").focus();
					}
					
					
					validateSetRed("user");
				}
				else
				{
					validateSetGreen("user");
				}
			}
			
			if($(obj).attr("name") == "repass" || $(obj).attr("name") == "menu_pane_form")
			{
				if((!json.pass.match || !json.pass.zero))
				{
					alert("คุณยังไม่ได้กรอกรหัสผ่านหรือรหัสผ่านไม่ตรงกัน");
					validateSetRed("pass");
					validateSetRed("repass");
					$("#menu_pane_form input[name=pass]").focus();
				}
				else
				{
					validateSetGreen("pass");
					validateSetGreen("repass");
				}
			}
			
			if($(obj).attr("name") == "email" || $(obj).attr("name") == "menu_pane_form")
			{
				if(!json.email.zero)
				{
					alert("คุณยังไม่ได้กรอกอีเมล์หลัก");
					validateSetRed("email");
					$("#menu_pane_form input[name=email]").focus();
				}
				else if(!json.email.format)
				{
					alert("อีเมล์หลักของคุณไม่ถูกต้อง");
					validateSetRed("email");
					$("#menu_pane_form input[name=email]").focus();
				}
				else
				{
					validateSetGreen("email");
				}
			}
			
			if($(obj).attr("name") == "menu_pane_form" && json.user.format && json.user.zero && json.user.exist && json.pass.match && json.pass.zero && json.email.zero && json.email.format)
			{
				register();
			}
			else
			{
				$('#black_caption').css("display","none");
			}
		}
	});
}

function register()
{
	$.ajax({
		url: site + '/servlet/?do=register' + "&jsoncallback=?",
		type: 'POST',
		data: $("#menu_pane_form").serialize(),
		dataType: "json",
		cache: false,
		success: function(json) {
			
			if(json.result)
			{
				alert("การสมัครสมาชิกเสร็จสมบูรณ์");
				
				if(location.href.indexOf("forum")>=0)
				{
					window.location.reload();
				}
				else
				{
					window.location=json.url;
				}
			}
			else
			{
				alert("ไม่สามารถสมัครสมาชิกได้ในขณะนี้");
			}
		}
	});
}

function logout()
{
	$.get(site + '/servlet/?do=logout' + "&jsoncallback=?", function(json) {
		window.location.reload();
	}, "json");
}

function showRegisterCaption(obj)
{
	/*
	if($(obj).attr("title"))
	{
			$('#black_caption_text').html($(obj).attr("title"));
			$('#black_caption_text').css("width","100px");
			$('#black_caption')
				.css("top",($(obj).offset().top-30)+"px")
				.css("left",($(obj).offset().left+$(obj).width()-$('#black_caption').width())+"px")
				.fadeIn("def");
	}*/
}

function videoPost()
{
	$("#video_post :input").attr('readonly', true);
	$("#video_post :input[type=submit]").attr('disabled', true);
	
	$.ajax({
		url: site + "/servlet/?do=addVideo&jsoncallback=?",
		type: 'POST',
		data: $("#video_post").serialize(),
		dataType: "json",
		cache: false,
		timeout: 10000,
		success: function(json) {
			if(json.result)
			{
				if(json.url=="refresh")
				{
					window.location.reload();
				}
				else
				{
					window.location=json.url;
				}
			}
			else
			{
				
				// error = 1(login incorrect), error = 2 (require field not complete)
				if(json.error == 1)
				{
					alert("ข้อมูล URL ไม่ถูกต้อง");
				}
				else if(json.error == 2)
				{
					alert("ข้อมูลที่กรอกมาไม่ครบถ้วน");
				}
				
				$("#video_post :input").removeAttr("readonly");
				$("#video_post :input[type=submit]").removeAttr("disabled");
				
				if($("#video_post input")[0])
				{
					$("#video_post input")[0].focus();
				}
			}
		},
		error: function(){
			alert("กรุณารอสักครู่และดำเนินการใหม่อีกครั้ง");
		}
	});
}

$(document).ready(function() {

	$("#menu_pane_form :input[status=red]").hover(function(){
	
	});
	$('#member_data div').hover(function() {
		$("div",this)
			.css("top",($("img",this).offset().top+$("img",this).height()-17)+"px")
			.css("left",($("img",this).offset().left+1)+"px")	
			.fadeIn("fast")
			.html($("img",this).attr("nick"));
		$('#friend_message')
			.css("top",($("img",this).offset().top-46)+"px")
			.css("left",($("img",this).offset().left-40)+"px")
			.fadeIn("def");
		$('#friend_message_text marquee').html($("img",this).attr("msg"));
		$("img",this).css("opacity","1.0");
	},
	function(){
		$("#member_data div div").css("display","none");
		$('#friend_message').css("display","none");
	});
	
	$('.mid_display_border img').hover(function() {
		$(this).css("opacity","1.0");
	},
	function(){
		$(this).css("opacity","0.8");
	});
	
	$('#top_tools a').hover(function() {
	
		if($(this).attr("title"))
		{
			$('#black_caption_text').html($(this).attr("title"));
			tmp = $(this).attr("title");
			$(this).removeAttr('title');
			$('#black_caption')
				.css("top",($(this).offset().top-60)+"px")
				.css("left",($(this).offset().left+10)+"px")
				.fadeIn("def");
		}
	},
	function(){
		$('#black_caption').css("display","none");
		$(this).attr("title",tmp)
	});
	
	
	$('#quick_inbox').hover(function() {
		$(this).css("cursor","pointer");
		$(this).css("background-repeat","no-repeat");
	},
	function(){
		$(this).css("background-repeat","repeat-x");
	});
	
	
	
	$('.gototop').click(function(){
		$( 'html, body' ).animate( { scrollTop: 0 }, 'slow' );
	});
	//$("#google_ads_load").appendTo('#ads_google_left');
});


