$(document).ready(function(){
	tabBookmarks();
	fixsize();
});

function tabBookmarks(){
	$("#menuGS").hide();
	$("#menuBS").hide();
	$("#menuG").hover(function(){ $("#menuGM").addClass("hctMenu_a"); $("#menuGS").show();
	},function(){ $("#menuGM").removeClass("hctMenu_a"); $("#menuGS").hide(); });
	$("#menuB").hover(function(){ $("#menuBM").addClass("hctMenu_a"); $("#menuBS").show();
	},function(){ $("#menuBM").removeClass("hctMenu_a"); $("#menuBS").hide(); });
	$("#menuGM").click(function(e){ e.preventDefault(); });
	$("#menuBM").click(function(e){ e.preventDefault(); });
	$(".tabsmenu").click(function(e){
		e.preventDefault();
		if ($("#mstart").attr('value') == "1"){
			LoadingTabs($(this));
		} else {
			setCookie("wearetabs",$(this).attr('value'));
			window.location = "http://wer.in.th/";
		}
	});
}

function fixsize(){
	if ($(".mctRoot").innerHeight() < $(".sctRoot").innerHeight()) {
		var h = $(".sctRoot").innerHeight() - ( $(".mctRoot").innerHeight() - $(".mctRoot").height() );
		$(".mctRoot").css('min-height', h);
	}
}

// Global functions
function replaceLink(){
	$(".mlink").each(function(){
		$(this).html(makeLink($(this).html())).removeClass("mlink");
	});
}

function makeLink(text) {
	var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
	return text.replace(exp,'<a href="$1" target="_blank">$1</a>'); 
}

var ld = false;
function Loading(){
	if (ld==false){
		ld = true;
		var s = $(".mctRoot:last");
		var h = s.css('height');
		s.before('<div class="mctRoot" id="loading" style="height:'+h+'"><div class="loading">กำลังโหลดข้อมูล...</div></div>').hide();
	}
}

function Loaded(){
	ld = false;
	$("#loading").remove();
	$(".mctRoot:last").show();
}

// Cookies
function setCookie(name, value, expires, path, domain, secure) {
	document.cookie= name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
	begin = dc.indexOf(prefix);
		if (begin != 0){ return null; }
	} else { begin += 2; }
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
	document.cookie = name + "=" +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}
