// JavaScript Document
cardXMLHttp=(window.ActiveXObject)?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest();
ie=(document.all)? true:false;
cardDrag=false;
oldX=oldY=0;
function $(id){return document.getElementById(id);}
function handleStateChange(obj){if(cardXMLHttp.readyState==4){if(cardXMLHttp.status==200){obj(cardXMLHttp.responseXML);}else{alert("Error!. Plase try again "+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 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=20;
	this.fps=25;
	this.user_id=0;
	this.start=function(obj,user_id,e){
	//	window.open('http://www.banlek.com/banlek.php?fmb_id='+user_id);
		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";
		}
		if((yPos+240-getScrollXY()[1])>getWindowSize()[1]){
			card.style.top=(yPos-195)+"px";
		}else{
			card.style.top=(yPos)+"px";
		}
		html="";
		html+="<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
		html+="	<tr><td class=\"topL\"></td><td class=\"topC\"></td><td class=\"topR\"></td></tr><tr><td class=\"centerL\"></td><td style=\"background-color:#FFFFFF;\">";
		html+="<div class=\"box_minicard\">";
		html+="<dl>";
		html+="<dt>";
		html+="<span class=\"side_left\"><img src=\""+this.imagePath+"text_minicard.gif\" /></span>";
		html+="<span class=\"side_right_btn\"><div id=\"cardMove\"></div>&nbsp;<a href=\"\" onclick=\"return "+this.objName+".close(this);\" title=\"close\"><img src=\""+this.imagePath+"icon_close.gif\" /></a></span>";
		html+="</dt>";
		html+="<dd id=\"cardDetail\">";
		html+="<p class=\"pic_user\"><a href=\"\" onclick=\"newwindow_self(this); return false;\" target=\"_blank\" id=\"cardImg\"><img src=\""+this.imagePath+"pic_user.gif\" id=\"profileimg\" /></a>";
		html+="<p class=\"profile\" style=\"padding-top:0px;\"><img src=\""+this.imagePath+"text_profile.gif\" /></p>";
		html+="<p class=\"profile\"> : <a href=\"#\"  onclick=\"newwindow_self(this); return false;\" target=\"_blank\" title=\"\" id=\"cardName\"></a></p>";
		html+="<p class=\"profile\"> : <a href=\"#\" id=\"cardProvince\"></a></p>";
		html+="<p class=\"talk\">ҹ Url : <a href=\"#\" onclick=\"newwindow_self(this); return false;\" title=\"\" id=\"cardURL\"  target=\"_blank\"></a></p>";
		html+="<p class=\"talk\" style=\"padding-top:10px; padding-bottom:3px;\">";
		html+="<span class=\"side_right_service\"><a href=\"#\" title=\"͹\" id=\"linkFriend\" onclick=\"return "+this.objName+".requestFriend(this); \" ><img src=\""+this.imagePath+"/icon_addfriend.gif\" /> ͹</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#\" title=\"觢ͤ\" id=\"linkMsg\" onclick=\"return "+this.objName+".sendMessage(this);\"><img src=\""+this.imagePath+"icon_sendmail.gif\" /> 觢ͤ</a></span></p>";
		html+="</dd>";
		html+="</dl>";
		html+="</div>";
		html+="</td><td class=\"centerR\"></td></tr><tr><td class=\"buttomL\"></td><td class=\"buttomC\"></td><td class=\"buttomR\"></td></tr></table>";
		card.innerHTML=html;
		this.requestCard(user_id);
		this.fadeUp("card_id");
		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;
		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;
		$("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").innerHTML=urlBanlek;
		$("cardURL").href=urlBanlek;
		if(path=="false"){
			$("cardURL").innerHTML="ҪԡҹѧԴԡ Talk";
			$("cardURL").href="http://www.banlek.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('سѧӡк');return false;");
			$("linkMsg").onclick=Function("alert('سѧӡк');return false;");
		}
		$("cardMove").onmousedown=cardDrags;
		
	};
	this.requestFriend=function(obj){
		if($("cardMsgText") && ($("linkFriend").className=="select")){	
			return false;
		}
		if($("linkMsg").className=="select"){
			this.closeMsgText("linkMsg");
		}
		textareaNode=document.createElement('p');
		textareaNode.className="text_area";
		html="<textarea id=\"cardMsgText\"></textarea><span class=\"side_left\" style=\"color: #000000; font-size:11px; padding-top:5px; padding-left:4px\" id=\"msgCount\">0</span><span class=\"side_left\" style=\"color: #FF0000;font-size:11px;padding-top:5px;\">/250</span><span class=\"side_right\"><input type=\"button\" value=\"Send\" onclick=\"return "+this.objName+".addFriend();\" /><input type=\"reset\" value=\"Clear\" onclick=\"return "+this.objName+".clearMsgText();\" /><input type=\"button\" value=\"Close\" onclick=\"return "+this.objName+".closeMsgText('linkFriend');\" /></span>";
		$('cardDetail').appendChild(textareaNode);
		textareaNode.innerHTML=html;
		obj.className="select";
		$("cardMsgText").focus();
		$("cardMsgText").onkeydown=this.checkMsg;
		$("cardMsgText").onkeyup=this.checkMsg;
		return false;
	};
	this.sendMessage=function(obj){
		if($("cardMsgText") && ($("linkMsg").className=="select")){
			return false;
		}
		if($("linkFriend").className=="select"){
			this.closeMsgText("linkFriend");
		}
		textareaNode=document.createElement('p');
		textareaNode.className="text_area";
		html="<textarea id=\"cardMsgText\"></textarea><span class=\"side_left\" style=\"color: #000000; font-size:11px; padding-top:5px; padding-left:4px\" id=\"msgCount\">0</span><span class=\"side_left\" style=\"color: #FF0000;font-size:11px;padding-top:5px;\">/250</span><span class=\"side_right\"><input type=\"button\" value=\"Send\" onclick=\"return "+this.objName+".submitMessage();\" /><input type=\"button\" value=\"Clear\" onclick=\"return "+this.objName+".clearMsgText();\" /><input type=\"button\" value=\"Close\" onclick=\"return "+this.objName+".closeMsgText('linkMsg');\" /></span>";
		$('cardDetail').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);
			$("linkFriend").className="";$("linkMsg").className="";
		}else{
			alert("error");
		}
	};
	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.closeMsgText=function(id){
		$("cardMsgText").parentNode.parentNode.removeChild($("cardMsgText").parentNode);
		$(id).className="";
	};
	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;}
	}
}
