if( ! chkmc )
	var chkmc = 0;
if( ! chksc )
	var chksc = 0;
var wmpHTML = '';

var voiceArr=['index','photo','education','boy','girl','lifestyle','abroad']; //voice

function searchArray2011(inputArray,inputSearch){
	var countArr = inputArray.length;
	for(var i=0;i<countArr;i++)if(inputArray[i]==inputSearch) return true;
	return false;
}

function loadmenu( input , mc_i , sc_i )
{

	if( !mc_i && ! sc_i )
	{
		mc_i = 0;
		sc_i = 0;
	}
	var uri = urlencode( window.location );
	var userAgent = navigator.userAgent.toLowerCase(); if (userAgent){ var browserFirefox = /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent ); if (browserFirefox) $(document).ready(checkIframes); }
	
	if( searchArray2011(voiceArr,input)){ // load webmastertalk -> voice
		$.getJSON('/ajax/loadmenu.php',  {page : input , mc : mc_i , sc : sc_i , refer : uri , ajax : 1, json : 1 }, function(data){
			$.each(data, function(k, v){
				if( k == 'response' ) $('#toolbar').empty().append(  $.trim( v ) );
				else if ( k == 'wmpost' ){
					wmpHTML = $.trim( v );
					$('#webmaster-post').empty().append( wmpHTML );
				}
			});
		});
	}else{
		$.ajax({
			url : '/ajax/loadmenu.php' ,
			type : 'GET' ,
			data : { page : input , mc : mc_i , sc : sc_i , refer : uri , ajax : 1 } ,
			success : function( respond ){
				respond = $.trim( respond );
				$('#toolbar').empty().append( respond );
			}
		});
	}
	if( $.browser.msie == true && $.browser.version == 6.0 )
		topSkyMenu();
}

function doWmpLogin(){
	$('#username').focus();
	return false;
}
var tempWmpComment = 0;
var tempWmpCommentS = 0;
var tempWmpTID = 0;
function getWmp( limit,webpage ){
var tempShowOld = false;
	$.getJSON('/ajax/loadwmp.php',  { limit :limit, webpage : webpage}, function(data){
		$.each(data, function(k, v){
			if( k == 0){
				var tt_msg = '';
				if( v.t_id ==1363235)
					tt_msg = '<a href="http://ads.dek-d.com/adserver/adclick.php?log=yes&bannerid=3046" style="color:#4E3900; text-decoration: none;">' + $.trim( v.msg.replace(/\\/g, '') ) + '</a>';
				else if( v.t_id ==1488850)
					tt_msg = '<a href="http://ads.dek-d.com/adserver/adclick.php?log=yes&bannerid=3296" style="color:#4E3900; text-decoration: none;">' + $.trim( v.msg.replace(/\\/g, '') ) + '</a>';
				else if( v.t_id ==1501638)
					tt_msg = '<a href="http://ads.dek-d.com/adserver/adclick.php?log=yes&bannerid=3330" style="color:#4E3900; text-decoration: none;">' + $.trim( v.msg.replace(/\\/g, '') ) + '</a>';
				else if( v.t_id ==1534721)
					tt_msg = '<a href="http://ads.dek-d.com/adserver/adclick.php?log=yes&bannerid=3515" style="color:#4E3900; text-decoration: none;">' + $.trim( v.msg.replace(/\\/g, '') ) + '</a>';
				else if( v.t_id ==1549231)
					tt_msg = '<a href="http://ads.dek-d.com/adserver/adclick.php?log=yes&bannerid=3561" style="color:#4E3900; text-decoration: none;">' + $.trim( v.msg.replace(/\\/g, '') ) + '</a>';
				else
					tt_msg = $.trim( v.msg.replace(/\\/g, '') );
						
				$('#webmaster-show p').empty().append(  tt_msg + ' <a>(' + $.trim( v.comment ) + ')</a>' );
				if( $('#webmaster-show p a').length > 1 ) $('#webmaster-show p a:first-child').attr('title', $('#webmaster-show p a:first-child').html().replace(/\\/g, '') );
				$('#webmaster-show span').empty().append(  $.trim( v.time ) );
				tempWmpTID = v.t_id;
				if( v.comment == 0){
					tempShowOld = true;
					$('#webmaster-list').empty().append('<div id="wmp-none"><p>โพสต์ข้อความพูดคุยตอนนี้<br />แข่งกันเป็นความเห็นแรกๆ</p></div><h2>ข้อความก่อนๆของ Webmaster</h2><div id="wmp-list"><ul></ul></div>');
					$('#webmaster-more').empty().append('<a href="#" onclick="return getMoreWmp();">ดูต่อทั้งหมด</a>');
				}else{
					tempWmpCommentS = tempWmpComment = v.comment;
					$('#webmaster-list').empty().append('<div id="wmc-list"><ul></ul></div>');
					$('#webmaster-list').css('padding', '0');
					if( v.comment > limit) $('#webmaster-more').empty().append('<a href="#" onclick="return getMoreWmc(' + $.trim( v.t_id ) + ',\''+webpage+'\');">ดูต่อทั้งหมด</a>');
					
					//alert(v.t_id);
					getWmc( v.t_id, limit,webpage );
				}		
			}else{
				if(tempShowOld) $('#webmaster-list div ul').append('<li><a onclick="window.open(this.href, \'\',\'width=570,height=600,scrollbars=yes\'); return false;" href="http://my.dek-d.com/' + $.trim( v.username ) + '/voice/myVoice.php?id=' + $.trim( v.t_id ) + '">' + $.trim( v.msg ) + ' <span>(' + $.trim( v.comment ) + ')</span></a></li>');
			}
		});
	});
}
var wmpSending = false;
var wmcReceiving = false;
var lastReplyID = 0;
function sendWmp(webpage){
var tempSuccess = false;
	if(wmpSending) return false;
	var txt = $('#wmp-msg').val();
	if(txt.length < 4){ alert('กรุณาระบุข้อความอย่างน้อย 4 ตัวอักษร'); return false;}
	wmpSending = true;
	$.getJSON('/ajax/loadwmp.php',  { webpage:webpage, mode:'setwmc', msg:txt, id:tempWmpTID}, function(data){
		wmpSending = false;
		$.each(data, function(k, v){
			if(k == 0){
				if( v.status == 1) tempSuccess = true;
			}else
				if(tempSuccess){
					//tempWmpComment = parseInt(tempWmpComment)  + 1;
					tempWmpCommentS = parseInt(tempWmpCommentS)  + 1;
					$('#webmaster-show p a').html('('+ tempWmpComment +')');
					if( $('#webmaster-list #wmp-list').length > 0 )
					{
						$('#webmaster-list').css('padding','0');
						$('#webmaster-list').empty().append('<div id="wmc-list"><ul></ul></div>');	
					}
					var tClass = ( $('#webmaster-list div ul li:first-child').hasClass('r') ) ? '' : ' class="r"' ;
					if( $('#webmaster-list div ul li').length > 5 )
						$('#webmaster-list div ul li:last-child').remove();
					$('#webmaster-list div ul').prepend('<li' + tClass + '><a href="http://my.dek-d.com/' + $.trim( v.r_username ) + '"><img src="' + $.trim( v.r_display ) + '" /></a><span class="num">'+ ( tempWmpCommentS )+' <a href="http://my.dek-d.com/' + $.trim( v.r_username ) + '">' + $.trim( v.r_aliasname ) + '</a></span><p class="r_msg">' + $.trim( v.r_msg ) + '</p></li>');
					$('#wmp-msg').val('');			
				}
		});
	});
	return false;
}
function getWmc( id, limit, webpage ){
	$.getJSON('/ajax/loadwmp.php',  {webpage:webpage, mode:'wmc', id:id, limit :limit}, function(data){
		var i = 0;
		$.each(data, function(k, v){
			i++;
			tempClass =( k %2 == 0 )?'':' class="r"';
			 $('#webmaster-list div ul').append('<li' + tempClass + '><a href="http://my.dek-d.com/' + $.trim( v.r_username ) + '"><img src="' + $.trim( v.r_display ) + '" /></a><span class="num">'+ ( tempWmpComment - k)+' <a href="http://my.dek-d.com/' + $.trim( v.r_username ) + '">' + $.trim( v.r_aliasname ) + '</a></span><p class="r_msg">' + $.trim( v.r_msg ) + '</p></li>');
			 lastReplyID = v.r_id;
		});
		tempWmpComment = tempWmpComment - i;
	});
}
function getMoreWmp(){
		$.getJSON('/ajax/loadwmp.php',  { limit:100}, function(data){
			$.each(data, function(k, v){
				if(k > 3){ 
					$('#webmaster-list div ul').append('<li><a onclick="window.open(this.href, \'\',\'width=570,height=600,scrollbars=yes\'); return false;" href="http://my.dek-d.com/' + $.trim( v.username ) + '/voice/myVoice.php?id=' + $.trim( v.t_id ) + '">' + $.trim( v.msg ) + ' <span>(' + $.trim( v.comment ) + ')</span></a></li>');
				}
			});
			$('#webmaster-more').remove();
		});
	return false;
}

function getMoreWmc(id,webpage){
	if(wmcReceiving) return false;
	wmcReceiving = true;
		$.getJSON('/ajax/loadwmp.php',  { webpage:webpage, mode:'wmc', id:id, limit:500, r_id : lastReplyID }, function(data){
			var i =0;
			$.each(data, function(k, v){
					i++;
					var tempClass = ( $('#webmaster-list div ul li:last-child').hasClass('r') ) ? '' : ' class="r"' ;
					$('#webmaster-list div ul').append('<li' + tempClass + '><a href="http://my.dek-d.com/' + $.trim( v.r_username ) + '"><img src="' + $.trim( v.r_display ) + '" /></a><span class="num">'+ ( tempWmpComment - k )+' <a href="http://my.dek-d.com/' + $.trim( v.r_username ) + '">' + $.trim( v.r_aliasname ) + '</a></span><p class="r_msg">' + $.trim( v.r_msg ) + '</p></li>');
					lastReplyID = v.r_id;
			});
			tempWmpComment = tempWmpComment - i;
			wmcReceiving = false;
			if(tempWmpComment == 0 ) $('#webmaster-more').remove();
		});
	return false;
}
function urlencode(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}
function logout( input )
{
	$.ajax({
		type: "POST" ,
		url: "/ddtoolbar.php",
		data: ({ mode : 'dologout' }) ,
		success:function(response){
			$("body").trigger('isLogout');
			if( input == 'region' || input == 'board' || input == 'content_view' || input == 'abroad' || input == 'jamsai' ) window.location.href = window.location.href;
			loadmenu( input );
		}
	});
	return false;
}
function login( input )
{
	$.ajax({ 
		type: "POST" , 
		url: "/ddtoolbar.php",
		data: ({ mode:'dologin', user_name: $('#username').val(), password: $('#password').val(), remember_me:(($('#remember-pass').attr( 'checked' ))?'1':'0')}),
		success:function(response){
			response = $.trim( response );
			if( response == 'error1' )
				alert('กรุณาระบุ username หรือ password');
			else if( response == 'false' )
				alert('เกิดข้อผิดพลาดไม่สามารถ Login ได้');
			else if( response == 'not_activate' )
			{
				alert('คุณยังไม่ได้ยืนยันสมาชิก กรุณายืนยันสมาชิกก่อน');
				window.location.href = 'http://www.dek-d.com/mailclub/mymail.php';
			}
			else
			{
				$("body").trigger('isLogin');
				if(  input == 'region' || input == 'board' || input == 'content' ||input == 'abroad' || input == 'jamsai')  
					window.location.href = window.location.href;
				else 
					loadmenu( input );
			}
		}
	});
	return false;
}
function popup( url , width , height ){
	window.open(url,"","toolbar=0,scrollbars=1,location=0,status=0,resizable=1,width="+width+",height="+height);
	return false;
}
function setVoiceToolbar(){
	var ms = $.trim( $('#voice-input').val() );
	if( twt != ms )
	{
		$.ajax({ 
			type: "POST" , 
			url: "/myLib/voice.php" ,
			data: ({ mode: 'set' , msg : $('#voice-input').val() }) ,
			success: function(response){
				response = $.trim( response );
				if( response == '1' )
					alert('ส่งเรียบร้อยแล้ว !');
			}
		});
	}
	else
		alert('ไม่สามารถโพสซ้ำได้ !!');
	return false;
}
function topSkyMenu()
{
	$('#login-mainmenu .level-1').live( 'mouseover mouseleave' , function( event ){
		if( event.type == 'mouseover' )
			$(this).find('#login-submenu').show();
		else
			$(this).find('#login-submenu').hide();
	});
}


// ไหน ๆ ก็ต้องอยู่ทุกหน้าอยู่แล้ว ขอฝากไว้หน่อยนะ
var checkIframes =function() {
	for (i=0; i<window.frames.length; i++){
		try{
			if (
				window.frames[i].document.location.href != window.frames[i].frameElement.src && 
				!(window.frames[i].document.location.href == document.location.href && window.frames[i].frameElement.src == 'about:blank')
			){
				window.frames[i].frameElement.src = window.frames[i].frameElement.src;
			}
		}catch(e) {}
	}
};

	load= function() {
		if (typeof $ == "undefined") {
		  load.getScript("/resource/js/jquery-1.4.2.min.js");
		  load.tryReady(0);
		 }else  loadmenu(checkinPage, chkmc, chksc);
	}

	load.getScript = function(filename) {
		  var script = document.createElement('script');
		  script.setAttribute("type","text/javascript");
		  script.setAttribute("src", filename);
		  if (typeof script!="undefined") document.getElementsByTagName("head")[0].appendChild(script);
	}
	load.tryReady = function(time_elapsed) {
		if (typeof $ == "undefined") {
			if (time_elapsed <= 5000) {
				setTimeout("load.tryReady(" + (time_elapsed + 200) + ")", 200);
			} else {
				alert("Timed out while loading jQuery.")
			}
		} else {
			loadmenu(checkinPage);
		}
	}
	if( $('#toolbar').length == 0 )
	{
		document.write('<link rel="stylesheet" type="text/css" href="/2011toolbar/toolbar.min.css?ver=1.1" />');
		document.write('<div id="toolbar"></div>');
		load();
	}
	
var ie9Resource = new Array();
ie9Resource['index'] = new Array( "หน้าแรก Dek-D", "/", "/favicon.ico");
ie9Resource['board'] = new Array( "บอร์ดรวม", "/board/", "/favicon.ico");
ie9Resource['photo'] = new Array( "คอลัมน์ Take a Photo", "/photo/", "/favicon.ico");
function ie9_addFav(page){
	try {
		if(window.external.msIsSiteMode()){
			var temp = ie9Resource[page];
			window.external.msSiteModeCreateJumpList('Dek-D Faverite');
			window.external.msSiteModeAddJumpListItem( temp[0], temp[1], temp[2]);
			window.external.msSiteModeShowJumplist();
			alert('Add Faverite Done !!');
		}else{
			window.external.msAddSiteMode();
			alert('window.external.msIsSiteMode : ' + window.external.msIsSiteMode());
		}
	} catch (ex) {
		alert('error : ' + ex);
	}
	return false;
}
	
	

