// JavaScript Document
cardXMLHttp=(window.ActiveXObject)?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest();
ie=(document.all)? true:false;
cardDrag=false;
oldX=oldY=0;
var box="";

function getWindowSizeAll(a){var myWidth=0,myHeight=0;if(typeof(window.innerWidth)=='number'){scrollSize=16;myWidth=(a)?window.innerWidth:window.screen.width;myHeight=(a)?window.innerHeight:document.body.parentNode.scrollHeight;}else if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)){myWidth=(a)?document.documentElement.clientWidth:document.body.offsetWidth;myHeight=(a)?document.documentElement.clientHeight:document.body.parentNode.scrollHeight;scrollSize=2;}else if(document.body&&(document.body.clientWidth||document.body.clientHeight)){myWidth=(a)?document.body.clientWidth:document.body.offsetWidth;myHeight=(a)?document.body.clientHeight:document.body.offsetHeight;scrollSize=2;}return[myWidth-scrollSize,myHeight];}
function $(id){return document.getElementById(id);}
function handleStateChange(obj){if(cardXMLHttp.readyState==4){if(cardXMLHttp.status==200){obj(cardXMLHttp.responseXML);}else{alert(minicardError+cardXMLHttp.statusText);}}}
function getWindowSize(){var myWidth=0,myHeight=0;if(typeof(window.innerWidth)=='number'){myWidth=window.innerWidth;myHeight=window.innerHeight;scrollSize=16;}else if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)){myWidth=document.documentElement.clientWidth;myHeight=document.documentElement.clientHeight;scrollSize=2;}else if(document.body&&(document.body.clientWidth||document.body.clientHeight)){myWidth=document.body.clientWidth;myHeight=document.body.clientHeight;scrollSize=2;}return[myWidth-scrollSize,myHeight];}
function getScrollXY(){var scrOfX=0,scrOfY=0;if(typeof(window.pageYOffset)=='number'){scrOfY=window.pageYOffset;scrOfX= window.pageXOffset;}else if(document.body&&(document.body.scrollLeft||document.body.scrollTop)){scrOfY=document.body.scrollTop;scrOfX=document.body.scrollLeft;}else if(document.documentElement&&(document.documentElement.scrollLeft||document.documentElement.scrollTop)){scrOfY=document.documentElement.scrollTop;scrOfX=document.documentElement.scrollLeft;}return[scrOfX,scrOfY];}

function drawMask2(fn){
	html="";
	mask=document.createElement('div');
	mask.setAttribute('id','mask');
	mask.style.left="0px";
	mask.style.top="0px";
	mask.style.bottom="0px";
	mask.style.position="absolute";
	//mask.style.backgroundColor="#000000";
	
	document.body.appendChild(mask);
	mask.style.width=getWindowSizeAll(true)[0]+"px";
	mask.style.height=getWindowSizeAll(false)[1]+"px";
	//if(ie){mask.style.filter="alpha(opacity=0)";}
	//else{mask.style.opacity=0;}
	if(fn){mask.onclick=fn;mask.style.cursor="pointer";}
}
function loadding2(fnCancel){
	drawMask2(fnCancel);
}
function cancelLoad2(){
	document.body.removeChild($('mask'));
}
function closeManage2(){
	$('minicard').parentNode.removeChild($('minicard'));
	$('topMinicard').parentNode.removeChild($('topMinicard'));
	box="";
	cancelLoad2();
}
function openTalk(){
	window.open('/talkDetail.php','','height=480,width=730,left='+Math.round((getWindowSize()[0]-730)/2)+',top='+Math.round((getWindowSize()[1]-480)/2));
	}
function cardDrags(e){
	thisObj=(ie)?event.srcElement:e.target;
	if(thisObj.id=="cardMove"){
		cardDrag=true;
		document.onmouseup=cardDrops;
	}
	oldX=(ie)?event.clientX+getScrollXY()[0]:e.pageX;
	oldY=(ie)?event.clientY+getScrollXY()[1]:e.pageY;
	document.onmousemove=cardMove;
}
function cardDrops(e){new Function("cardDrag=false");
	cardDrag=false;
}
function cardMove(e){
	if(cardDrag){
		xPos=(ie)?event.clientX+getScrollXY()[0]:e.pageX;
		yPos=(ie)?event.clientY+getScrollXY()[1]:e.pageY;
		thisObj=(ie)?event.srcElement:e.target;
		$("card_id").style.left=($("card_id").offsetLeft+(xPos-oldX))+"px";
		$("card_id").style.top=($("card_id").offsetTop+(yPos-oldY))+"px";
		oldX=xPos;
		oldY=yPos;
	}
}
function miniCard(name){
	this.objName=name;
	this.imagePath="../../mini_card/default/";
	this.step=1;
	this.fps=1;
	this.user_id=0;
	this.start=function(obj,user_id,e){
		this.user_id=user_id;
		if($("card_id")){
			$("card_id").parentNode.removeChild($("card_id"));
		}
		xPos=(ie)?e.clientX+getScrollXY()[0]:e.pageX;
		yPos=(ie)?e.clientY+getScrollXY()[1]:e.pageY;
		card=document.createElement('div');
		document.body.appendChild(card);
		card.setAttribute("id","card_id")
		card.style.zIndex=15000;
		//if(ie){card.style.filter="alpha(opacity="+0+")";}
		//else{card.style.MozOpacity=0;}
		card.style.position="absolute";
		if((xPos+329)>getWindowSize()[0]){
			card.style.left=(xPos-329)+"px";
		}else{
			card.style.left=(xPos)+"px";
		}//195
		if((yPos+90-getScrollXY()[1])>getWindowSize()[1]){
			card.style.top=(yPos-50)+"px";
		}else{
			card.style.top=(yPos)+"px";
		}
		loadding2(closeManage2); 
		html="";
		html+="<div id=\"topMinicard\" style=\"text-align:right; width:100px;\"><a href=\"javascript:void(0)\" onclick=\"closeManage2();\"><img src=\"images/icon/icon_close.gif\" style=\" float:right; border:0px;\"></a>";
		html+="</div>";
		html+="<div class=\"minicard\" id=\"minicard\">";
		html+="<li class=\"boxmini\">";
		html+="<p class=\"linemini\" onmouseover=\"this.className='lineminiOn'\" onmouseout=\"this.className='linemini'\"><a href=\"javascript:void(0)\" title=\"\" id=\"cardURL\"  target=\"_blank\" onclick=\"closeManage2();\">BanLek</a></p>";
		html+="<p class=\"linemini\" onmouseover=\"this.className='lineminiOn'\" onmouseout=\"this.className='linemini'\"><a href=\"javascript:void(0)\" id=\"cardAllblogURL\"  target=\"_blank\" onclick=\"closeManage2();\">AllBlogThai</a></p>";
		html+="<p class=\"linemini\" onmouseover=\"this.className='lineminiOn'\" onmouseout=\"this.className='linemini'\"><a href=\"javascript:void(0)\" id=\"linkMsg\" onclick=\"return "+this.objName+".sendMessage();\">Send Massage</a></p>";
		html+="<p class=\"linemini\" onmouseover=\"this.className='lineminiOn'\" onmouseout=\"this.className='linemini'\"><a href=\"javascript:void(0)\" id=\"linkFriend\" onclick=\"return "+this.objName+".requestFriend(); \">Add Friend</a></p>";
		html+="</li>";
		html+="<div id=\"boxarea\">";
		html+="</div>";
		html+="</div>";
		
		card.innerHTML=html;
		//alert('pp');
		this.requestCard(user_id);
		this.fadeUp("card_id");
		//fadeUpToo('card_id',10,10);
		return false;
	};
	this.close=function(obj){
		card=$("card_id");
		this.fadeDown("card_id");
		setTimeout("card.parentNode.removeChild(card)",this.step*this.fps)
		return false;
	};
	this.requestCard=function(fmb_id){
		//if(ie){cardXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");}
		cardXMLHttp=(window.ActiveXObject)?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest();
		toFunc=this.getProfile;
		cardXMLHttp.onreadystatechange=function(){handleStateChange(toFunc);};
		cardXMLHttp.open("GET","miniCardDetail.php?fn=getDetailCard&fmb_id="+fmb_id,true);
		cardXMLHttp.send(null);
	};
	this.getProfile=function(result){
		login=result.getElementsByTagName('login')[0].firstChild.nodeValue;
		istalk=result.getElementsByTagName('istalk')[0].firstChild.nodeValue;
		profile=result.getElementsByTagName('profile')[0];
		proId=profile.getAttribute('id');
		displayName=profile.getElementsByTagName('display')[0].firstChild.nodeValue;
		displayImage=profile.getElementsByTagName('image')[0].firstChild.nodeValue;
		path=profile.getElementsByTagName('path')[0].firstChild.nodeValue;
		blogpath=result.getElementsByTagName('blogpath')[0].firstChild.nodeValue;
		try{	
			province=profile.getElementsByTagName('province')[0].firstChild.nodeValue;
			provinceID=profile.getElementsByTagName('province')[0].getAttribute('id');
		}catch(e){province='';provinceID=0;}
		//urlTalk="http://www.banlek.com/talk.php?fmb_id="+proId;
		urlBanlek="http://www.banlek.com/"+path;
		urlAllblog="http://"+blogpath+".allblogthai.com";
		//$("profileimg").src="http://member.mediathai.net/memberFile/images/"+displayImage;
		//$("cardName").innerHTML=displayName;
		//$("cardImg").href=(path=="false")?"http://www.banlek.com/":urlBanlek;
		//$("cardName").href=(path=="false")?"http://www.banlek.com/":urlTalk;
		if(path=="false"){
			$("cardName").onclick=Function("return false;");
		}
		//$("cardProvince").innerHTML=province;
		//$("cardProvince").href="http://www.banlek.com/search.php?fmb_id="+proId+"&rProvince="+provinceID;
		$("cardURL").href=urlBanlek;
		$("cardAllblogURL").href=urlAllblog;
		//$("cardURL").href=urlBanlek;
		if(path=="false"){
			$("cardURL").innerHTML="สมาชิกท่านนี้ยังไม่เปิดใช้บริการ Talk";
			$("cardURL").href="http://www.banlek.com/";
		}
		if(blogpath=="false"){
			$("cardAllblogURL").onclick=Function("alert('"+displayName+" ยังไม่ได้เปิดใช้งาน Allblogthai/This member is not open blog service.');return true;");
			$("cardAllblogURL").href="http://www.allblogthai.com/";
		}		
		if(eval(login)){
			if(!eval(istalk)){
				$("linkFriend").onclick=Function("openTalk();return false;");
				$("linkMsg").onclick=Function("openTalk();return false;");
			}else{
				if(path=="false"){
					$("linkFriend").onclick=Function("alert('"+displayName+" ยังไม่ได้เปิดใช้งาน Talk');return false;");
					$("linkMsg").onclick=Function("alert('"+displayName+" ยังไม่ได้เปิดใช้งาน Talk');return false;");
				}
			}
		}else{
			$("linkFriend").onclick=Function("alert('คุณยังไม่ได้ทำการเข้าสู่ระบบ/You are not log in.'); closeManage2(); return false;");
			$("linkMsg").onclick=Function("alert('คุณยังไม่ได้ทำการเข้าสู่ระบบ/You are not log in.'); closeManage2(); return false;");
		}
		//$("cardMove").onmousedown=cardDrags;
		
	};
	this.requestFriend=function(){
		if($("cardMsgText") && (box=="friend")){	
			return false;
		}
		if(box=="Msg"){
			this.closeMsgText("imassage");
		}
		box="friend";
		textareaNode=document.createElement('li');
		textareaNode.className="friend";
		textareaNode.id="ifriend";
		html="";
		html+="<p class=\"linefriend\">";
		html+="<textarea id=\"cardMsgText\"></textarea>";
		html+="</p>";
		html+="<p class=\"linefriend\">";
		html+="<span><input type=\"button\" value=\"Send\" onclick=\"return "+this.objName+".addFriend();\" id=\"btSend\" />";
		html+="<input type=\"button\" value=\"Close\" onclick=\"return "+this.objName+".closeAllBox('ifriend');\" id=\"btClose\" /></span>";
		html+="</p>";
		
		$('boxarea').appendChild(textareaNode);
		textareaNode.innerHTML=html;
		/*obj.className="select";*/
		$("cardMsgText").focus();
		$("cardMsgText").onkeydown=this.checkMsg;
		$("cardMsgText").onkeyup=this.checkMsg;
		return false;
	};
	this.sendMessage=function(){
		if($("cardMsgText") && (box=="Msg")){
			return false;
		}
		if(box=="friend"){
			this.closeMsgText("ifriend");
		}
		box="Msg";
		textareaNode=document.createElement('li');
		textareaNode.className="massage";
		textareaNode.id="imassage";
		html="";
		html+="<p class=\"linemassage\">";
		html+="<textarea id=\"cardMsgText\"></textarea>";
		html+="</p>";
		html+="<p class=\"linemassage\">";
		html+="<span><input type=\"button\" value=\"Send\" onclick=\"return "+this.objName+".submitMessage();\" id=\"btSend\" />";
		html+="<input type=\"button\" value=\"Close\" onclick=\"return "+this.objName+".closeAllBox('imassage');\" id=\"btClose\" /></span>";
		html+="</p>";
		$('boxarea').appendChild(textareaNode);
		textareaNode.innerHTML=html;
		/*obj.className="select";*/
		$("cardMsgText").focus();
		$("cardMsgText").onkeyup=this.checkMsg;
		$("cardMsgText").onkeydown=this.checkMsg;
		return false;
	};
	this.submitMessage=function(){
		//if($("cardMsgText").value.length>250){alert("ขนาดตัวอักษรต้องไม่เกิน 250 อักษร");return false;}
		//if(ie){cardXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");}
		cardXMLHttp=(window.ActiveXObject)?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest();
		toFunc=this.resSubmitMessage;
		cardXMLHttp.onreadystatechange=function(){handleStateChange(toFunc);};
		cardXMLHttp.open("post","/miniCardDetail.php",true);
		cardXMLHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		cardXMLHttp.send("msg="+encodeURIComponent($("cardMsgText").value)+"&fn=sendMessage&toId="+this.user_id);
	};
	this.resSubmitMessage=function(result){
		res=result.getElementsByTagName("result")[0].firstChild.nodeValue;
		if(res!="false"){
			alert(res);
			$("cardMsgText").parentNode.parentNode.removeChild($("cardMsgText").parentNode);
			$("btSend").parentNode.parentNode.removeChild($("btSend").parentNode);
			//$("btClose").parentNode.parentNode.removeChild($("btClose").parentNode);
			$("linkFriend").className="";$("linkMsg").className="";
			closeManage2();
		}else{
			alert("error");
			closeManage2();
		}
	};
	this.addFriend=function(){
		//if($("cardMsgText").value.length>250){alert("ขนาดตัวอักษรต้องไม่เกิน 250 อักษร");return false;}
		//if(ie){cardXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");}
		cardXMLHttp=(window.ActiveXObject)?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest();
		toFunc=this.resSubmitMessage;
		cardXMLHttp.onreadystatechange=function(){handleStateChange(toFunc);};
		cardXMLHttp.open("post","/miniCardDetail.php",true);
		cardXMLHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		cardXMLHttp.send("msg="+encodeURIComponent($("cardMsgText").value)+"&fn=addFriend&toId="+this.user_id);
	}
	this.gotoAllblog=function(){
	};
	this.closeMsgText=function(id){
		$(id).parentNode.removeChild($(id));
		box="";
	};
	this.closeAllBox=function(id){
		closeManage2();
	};
	this.clearMsgText=function(){
		$("cardMsgText").value="";
		//$("msgCount").innerHTML="0";
		//$("msgCount").style.color="#000000";
		$("cardMsgText").focus();
	};
	this.checkMsg=function(e){
		msgLen=$("cardMsgText").value.length;
		//$("msgCount").innerHTML=msgLen+"";
		//$("msgCount").style.color=(msgLen>250)?"#FF0000":"#000000";
		
	};
	this.fadeUp=function(Id){
		for(var i=0;i<=this.step;i++){
			setTimeout(this.objName+".fadeRuning('"+Id+"',"+(Math.round(100/this.step)*i)+")",i*this.fps);
		}
	};
	this.fadeDown=function(Id){
		for(var i=0;i<=this.step;i++){
			setTimeout(this.objName+".fadeRuning('"+Id+"',"+(Math.round(100/this.step)*(this.step-i))+")",i*this.fps);
		}
	}
	this.fadeRuning=function(Id,Op){
		obj=$(Id);
		//if(ie){obj.style.filter="alpha(opacity="+Op+")";}
		//else{obj.style.MozOpacity=Op/100;}
	}
	return true;
}