var chatBox, chatBoxOwner, chatBoxFlag;

var posX = 0, posY = 0, shiftX = 0, shiftY = 0, mouseX = 0, mouseY = 0; 

var IE = document.all ? true : false;

if (!IE) { document.captureEvents(Event.MOUSEMOVE); }

// detect mouse movement
document.onmousemove = getMousePosition;

/* creat chatBox */
function getChatBox(div_id, img, arr) {

	if (chatBox == null) {

		makeChatBox(img, arr);
		chatBoxOwner = div_id;
		
	} else if (chatBoxOwner != div_id) {

		hideChatBox(div_id);
		getChatBox(div_id, img, arr);
	}
}

/* creat chatBox */
function makeChatBox(img, arr) {

	setPosition(img);

	chatBox = document.createElement("div");
	chatBox.className = "chatBox";
		
	chatBox.innerHTML = getContent(arr);
		
	document.body.appendChild(chatBox);	
		
	chatBox.onmouseover = function() { setFlag(); }
	chatBox.onmouseout = function() { hideChatBox('onChatBox', null); }

	/* check if mouse pointer is on top of chatBox location before chatBox is created */
	if (checkMousePosition(img)) { setFlag(); }

	chatBox.style.position = "absolute";
	chatBox.style.top = parseInt(posY) - shiftY + "px";
	chatBox.style.left = parseInt(posX) - shiftX + "px";

	chatBox.zIndex = 1000;	
}

/*function getName(s) {

	var d = s.lastIndexOf('.');
	alert(s.substring(s.lastIndexOf('/') + 1, d < 0 ? s.length : d));
	return s.substring(s.lastIndexOf('/') + 1, d < 0 ? s.length : d);
}*/

/* check if chatBox is created and hide it if so */
function checkChatBox() { if (chatBox != null) { hideChatBox(chatBoxOwner, null); } }

/* check for mouse pointer location before chatBox is created */
function checkMousePosition(img) {

	setPosition(img);
	if (mouseX >= posX && mouseX <= posX+23 && mouseY >= posY && mouseY <= posY + 126) { return true; } else { return false; }
}

/* set image position */
function setPosition(img) {

	// IE 6 and 7
	if (isIE() == 6 || isIE() == 7) {
		
		// posX = ie_findX(img);
		// posY = ie_findY(img);
		
		// posX = ie_findX(document.getElementById(getName(img.src)));
		// posY = ie_findY(document.getElementById(getName(img.src)));
		
		posX = ie_findX(document.getElementById(img.name));
		posY = ie_findY(document.getElementById(img.name));				
		
	} else {

		// posX = img.offsetLeft;
		// posY = img.offsetTop;
		
		// posX = document.getElementById(getName(img.src)).offsetLeft;
		// posY = document.getElementById(getName(img.src)).offsetTop;
		
		posX = document.getElementById(img.name).offsetLeft;
		posY = document.getElementById(img.name).offsetTop;
	}
	
	shiftX = 238;
	shiftY = 85;			
}

/* Get current mouse Position */
function getMousePosition(e) {
  
  // IE
  if (IE) { 
  	
    mouseX = event.clientX + document.documentElement.scrollLeft;
    mouseY = event.clientY + document.documentElement.scrollTop;	
	
  // other browsers	
  } else {  
  	
    mouseX = e.pageX;
    mouseY = e.pageY;
  } 

  // catch possible negative values if browser is not IE
  if (mouseX < 0) { mouseX = 0; }
  if (mouseY < 0) { mouseY = 0; }
}

/* set flag to distinguish whether mouse pointer is on top of chatBox */
function setFlag() { chatBoxFlag = true; }

/* is browser IE? */
function isIE() { return (navigator.appName=='Microsoft Internet Explorer') ? parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]):-1; }

/* delete photo upload by user */
function hide(id) { document.getElementById(id).style.display = 'none'; }

/* Clear all data entered by user */
function clear() {

	//document.getElementById('select_bar').value = '1';
	document.getElementById('distId').value = '';
	document.getElementById('comment_tarea').value = '';
	document.getElementById('comment').checked = '';
	document.getElementById('sms_box_word_count').innerHTML = '可輸入90字';
	hide('photo');
}

/* check whether user has input all required data 
function checkInput() {

	//document.getElementById('select_bar').value = '1';
	document.getElementById('distId').value = '';
	document.getElementById('comment_tarea').value = '';
	document.getElementById('comment').checked = '';
	
	document.getElementById('share_msg').style.background = '#FFDFDF';
	document.getElementById('share_msg').innerHTML = "<font color='#FF0000'>請填寫所需資料</font>";
}
*/
/* used for IE 6, 7 to find mouse position x y */
function ie_findX(obj) {
	
	var curleft = 0;
	
	if(obj.offsetParent) {
	
		while(1) {
			
			curleft += obj.offsetLeft;
			if(!obj.offsetParent) { break; }
          	obj = obj.offsetParent;
        }
		
	} else if(obj.x) { curleft += obj.x; }
	
    return curleft;
}

function ie_findY(obj) {
	
    var curtop = 0;
    if(obj.offsetParent) {
		
        while(1) {
			
          curtop += obj.offsetTop;
          
		  if(!obj.offsetParent) { break; }
          obj = obj.offsetParent;
        }
	} else if(obj.y) { curtop += obj.y; }
	
    return curtop;
}

/* function used for retrieving data */
function getContent(arr) {
	
	var buf = "<div class='chatBox_user'>"+
				"<a href='"+arr['userName_link']+"'>"+arr['userName']+"</a>&nbsp;(<a href='"+arr['score_link']+"'>"+arr['score']+"</a>)&nbsp;"+getStar(arr['star'])+
			  "</div>"+
			  "<div class='chatBox_date'>"+arr['date']+"&nbsp;<a href='javascript: hideChatBox(\"odd_case\", null);'><img src='/image/sms/index/delete_on.gif' /></a></div>"+
			  "<div class='chatBox_title'>"+getReview(arr['review'])+"&nbsp;<a href='"+arr['food_link']+"'>"+arr['food']+"</a>&nbsp;(<a href='"+arr['location_link']+"'>"+arr['location']+"</a>)</div>"+
			  "<div class='chatBox_content' style='height:46px;#height:40px;_height:40px;'><a href='"+ arr['reply_link'] +"'>"+arr['content']+"</a></div>"+
			  "<div class='chatBox_reply'><a href='"+arr['reply_link']+"'>"+arr['reply']+"</a></div>"+
			  "<div class='chatBox_icon'>"+
			  	arr['facebook_link']+"<img src='/image/sms/index/chatbox_facebook_icon.gif' /></a>&nbsp;"+
				"<!--a href='"+arr['twitter_link']+"'><img src='/image/sms/index/chatbox_twitter_icon.gif' /></a-->"+
			  "</div>";
	
	return buf;
}

function getStar(n) {

	var str='';
	for (var i=0;i<parseInt(n);i++) { str = str + "<img src='/image/sms/index/chatbox_star.gif' />";	}
	return str;
}

function getReview(r) {

	var str='';

	switch(parseInt(r)) {
		case 1:
			str="<img src='/image/sms/index/chatbox_good_icon.gif' />";
			break;
		case 2:
			str="<img src='/image/sms/index/chatbox_ok_icon.gif' />";		
			break;
		case 3:
			str="<img src='/image/sms/index/chatbox_bad_icon.gif' />";		
			break;		
	}
	return str;
}

/* hdie chatbox and destroy */
function hideChatBox(div_id, img) {

	if (div_id == 'onChatBox' && chatBox != null && chatBoxFlag != false) {

		setTimeout(function() {
							
			if(chatBox != null) {
				
				var max_x = chatBox.offsetLeft + 263;
				var max_y = chatBox.offsetTop + 126;
					
				var max_x2 = posX + 70;
				var max_y2 = posY + 70;		
					
				var cur_x = mouseX;
				var cur_y = mouseY;
					
				var chatBoxPosX;
				var chatBoxPosY;
			
				// IE 6 and 7
				if (isIE() == 6 || isIE() == 7) {
						
					chatBoxPosX = ie_findX(chatBox);
					chatBoxPosY = ie_findY(chatBox);
						
					if (isIE() == 7) { max_x = max_x + 5; }
						
				} else {
				
					chatBoxPosX = chatBox.offsetLeft;
					chatBoxPosY = chatBox.offsetTop;
				}		
			
				// alert((cur_x >= max_x && cur_x >= max_x2)+', '+(cur_x <= chatBoxPosX+5 && cur_x <= posX)+', '+(cur_y >= max_y-5 && cur_y >= max_y2)+', '+(cur_y <= chatBoxPosY+5 && cur_y <= posY));
				// alert('curY: '+cur_y+', posY: '+posY);
				// if (cur_x >= max_x || cur_x <= chatBoxPosX+5 || cur_y >= max_y-5 || cur_y <= chatBoxPosY+5) { hideChatBox('odd_case', null); }	
				if ((cur_x >= max_x && cur_x >= max_x2) || (cur_x <= chatBoxPosX+5 && cur_x <= posX) || (cur_y >= max_y-5 && cur_y >= max_y2) || (cur_y <= chatBoxPosY+5 && cur_y <= posY)) { hideChatBox('odd_case', null); }	
			}
			
		}, 300);

	} else if ((div_id == 'odd_case' && chatBox != null) || (chatBox != null && chatBoxOwner != div_id)) {

		chatBox.style.display = "none";
		document.body.removeChild(chatBox);
		chatBox = null;
		chatBoxOwner = null;
		chatBoxFlag = null;

	} else if ((chatBox != null && chatBoxOwner == div_id && chatBoxFlag == true) || (chatBox != null && chatBoxOwner == div_id && chatBoxFlag != true) || (chatBox != null && chatBoxOwner == div_id && chatBoxFlag == false)) {
		
		setTimeout(function() {
				   
			var max_x = posX + 70;
			var max_y = posY + 70;
			var cur_x = mouseX;
			var cur_y = mouseY;	
		
			if (isIE() == 6 || isIE() == 8) {
			
				max_x = max_x - 8;
				max_y = max_y - 8;
						
				posX = posX + 5;
				posY = posY + 8;
			
			} else if (isIE() == 7) {
						
				max_x = max_x - 4;
				max_y = max_y - 7;
						
				posX = posX + 4;
				posY = posY + 5;
						
			} else { 
			
				max_x = max_x - 5;
				max_y = max_y - 5;
						
				posX = posX + 5;
				posY = posY + 5;
			}

			if (chatBoxFlag == false || chatBoxFlag == null) {
			
				if (cur_x > max_x || cur_x <= posX || cur_y >= max_y || cur_y <= posY) { hideChatBox('odd_case', null); }
				
			} else { hideChatBox('onChatBox', null); }
		
		}, 300);
					
	}
}
