/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 **************************************************/

var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};


// Tooltip (fuer Vorschau in Navigation)

/***********************************************
* Cool DHTML tooltip script- ฉ Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(thetext, thecolor, thewidth){
if (ns6||ie){
if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
tipobj.innerHTML=thetext
enabletip=true
return false
}
}

function positiontip(e){
if (enabletip){
var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
//Find out how close the mouse is to the corner of the window
var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20

var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<tipobj.offsetWidth)
//move the horizontal position of the menu to the left by it's width
tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
else if (curX<leftedge)
tipobj.style.left="10px"
else
//position the horizontal position of the menu where the mouse is positioned
tipobj.style.left=curX+offsetxpoint+"px"

//same concept with the vertical position
if (bottomedge<tipobj.offsetHeight)
tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
else
tipobj.style.top=curY+offsetypoint+"px"
tipobj.style.visibility="visible"
}
}

function hideddrivetip(){
if (ns6||ie){
enabletip=false
tipobj.style.visibility="hidden"
tipobj.style.left="-1000px"
tipobj.style.backgroundColor=''
tipobj.style.width=''
}
}

function disableselect(e){
	return false
}
function reEnable(){
	return true
}

function myScrollbar(status)
{
	if(status=='disable')
	{
		document.body.style.overflow='hidden';
		document.documentElement.style.overflow='hidden';
		myHideObject();
	}
	else{
		document.body.style.overflow='';
		document.documentElement.style.overflow='';
		myShowObject();
	}
}

function myHideObject(){
		var elements = $A(document.getElementsByTagName('object'));
		elements.extend(document.getElementsByTagName(window.ActiveXObject ? 'select' : 'embed'));
		elements.each(function(el){ el.style.visibility = 'hidden'; });
}

function myShowObject(){
		var elements = $A(document.getElementsByTagName('object'));
		elements.extend(document.getElementsByTagName(window.ActiveXObject ? 'select' : 'embed'));
		elements.each(function(el){ el.style.visibility = ''; });
}

function contextmenu(e)
{
	if (navigator.appName == 'Netscape' && e.which == 3) {
      showContextMenu(e);
      return false;
	}
   else if (navigator.appName == 'Microsoft Internet Explorer' && event.button==2)
	{
         showContextMenu(e);
         return false;
	}
   return true;

}

function showContextMenu(e) 
{
	var tempContext_Time;
	clearTimeout(tempContext_Time);
	if(popupContext){
		$('divContext').style.display='block'; 
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
		$('divContext').style.left=(ie)? ietruebody().scrollLeft+event.clientX-$('divContext').offsetWidth+"px" : window.pageXOffset+e.clientX-($('divContext').offsetWidth/2)+"px";
		$('divContext').style.top=(ie)? ietruebody().scrollTop+event.clientY-($('divContext').offsetHeight-5)+"px" : window.pageYOffset+e.clientY-($('divContext').offsetHeight)+"px";

		tempContext_Time=setTimeout( function() { closeContextMenu(); }, 5000);  
	}
}

function closeContextMenu(){$('divContext').style.display='none';}

function hideContext()
{
	$('divContext').style.display='none'; 
	popupContext=true;
}

var temp_Clock;
function tick(divClock){
	clearTimeout(temp_Clock);
	dt_now = new Date()
	dt_now.setTime(dt_now.getTime()+dt_diff) 

	montharrayz = new Array("มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
	daysarrayz = new Array("อาทิตย์","จันทร์","อังคาร","พุธ","พฤหัสบดี","ศุกร์","เสาร์");

	intHours		= dt_now.getHours();
	intMinutes		= dt_now.getMinutes();
	intSeconds		= dt_now.getSeconds();
	
	dateString = "<div class=txtTahomaSize10> "
	dateString+= dt_now.getDate() + " "
	dateString+= montharrayz[dt_now.getMonth() ]+ " "
	dateString+= dt_now.getFullYear()+543 + "</div>" 
	
	timeString = "<div class=txtTahomaSize20 style='font-weight:bold;'>"
	timeString += intHours;      
	timeString +=((intMinutes < 10)?":0":":")+intMinutes; 
	timeString +=((intSeconds < 10)?":0":":")+intSeconds; 
	timeString = dateString + timeString + "</div> "

	$(divClock).innerHTML=timeString;	
	temp_Clock=setTimeout(function(){ tick(divClock); },1000);
} 

function setPositionTutor(h1,w2,h2,l3,h4)
{
	var H1=h1;
	var W2=w2;
	var H2=h2;
	var L3=l3;
	var H4=h4;
	var H3=H2;
	var T2=H1;
	var T3=H1;
	var T4=parseInt(T2)+parseInt(H2);
	var W3=1000-parseInt(L3);

	$('tutor-frame1').setStyle('height',H1+'px');
	$('tutor-frame2').setStyle('height',H2+'px');
	$('tutor-frame2').setStyle('width',W2+'px');
	$('tutor-frame2').setStyle('top',T2+'px');
	$('tutor-frame3').setStyle('height',H3+'px');
	$('tutor-frame3').setStyle('left',L3+'px');
	$('tutor-frame3').setStyle('top',T3+'px');
	$('tutor-frame3').setStyle('width',W3+'px');
	$('tutor-frame4').setStyle('top',T4+'px');
	$('tutor-frame4').setStyle('height',H4+'%');
}

function showTip(divID, status)
{
	if($(divID)){$(divID).setStyle('display',status);}
}

function myAlert(borderArea,text,color,option,iH)
{
	//if($('iAlert')) return false;
	var iColor=(color)?color:'txtColorRed';
	var W=parseInt($(borderArea).getStyle('width'));
	var H=parseInt($(borderArea).getStyle('height'));
	var iH=(iH)?iH:0;

	var newAlert=document.createElement("div");
	newAlert.id="iAlert";
	newAlert.style.position='absolute';
	newAlert.style.left='0px';
	newAlert.style.top='0px';		
	newAlert.style.width=W+'px';
	newAlert.style.height=H+'px';		
	newAlert.style.zIndex=20001; 

		var borderFB=document.createElement("div");
		borderFB.id="borderAlert";
		borderFB.className='radius';
		borderFB.style.position='absolute';
		borderFB.style.width='355px';
		borderFB.style.height='98px';		
		borderFB.style.left=((W-355)/2)+'px';
		borderFB.style.top=(((H-98)/2)+iH)+'px';		

		var newBox=document.createElement("div");
		newBox.id="iBoxAlert";
		newBox.className='alert-box';
		newBox.style.left='8px';
		newBox.style.top='8px';		

		var newText=document.createElement("div");
		newText.className='disp-text';
		newText.innerHTML='<center><STRONG class="txtTahomaSize12 '+iColor+'">'+text+'<STRONG></center>';

		var newEXIT=document.createElement("div");
		newEXIT.className='new-button-exit';
		newEXIT.style.position='absolute';
		newEXIT.style.left='147px';
		newEXIT.style.bottom='8px';		
		newEXIT.onclick=function(){(option)?eval(option):deleteMyAlert(borderArea);}		

	$(borderArea).appendChild(newAlert);	
	$('iAlert').appendChild(borderFB);		
	$('borderAlert').appendChild(newBox);		
	$('iBoxAlert').appendChild(newText);	
	$('iBoxAlert').appendChild(newEXIT);	
}
function deleteMyAlert(borderArea)
{
	var oChild=$(borderArea).lastChild;
	$(borderArea).removeChild(oChild);
}

function myConfirm(borderArea,text,ifunction,color)
{
	var iColor=(color)?color:'txtColorBlue';
	var W=parseInt($(borderArea).getStyle('width'));
	var H=parseInt($(borderArea).getStyle('height'));

	var newConfirm=document.createElement("div");
	newConfirm.id="iConfirm";
	newConfirm.style.position='absolute';
	newConfirm.style.left='0px';
	newConfirm.style.top='0px';		
	newConfirm.style.width=W+'px';
	newConfirm.style.height=H+'px';		
	newConfirm.style.zIndex=2; 

		var borderFB=document.createElement("div");
		borderFB.id="borderAlert";
		borderFB.className='radius';
		borderFB.style.position='absolute';
		borderFB.style.width='355px';
		borderFB.style.height='98px';		
		borderFB.style.left=((W-355)/2)+'px';
		borderFB.style.top=((H-98)/2)+'px';		

		var newBox=document.createElement("div");
		newBox.id="iBoxConfirm";
		newBox.className='alert-box';
		newBox.style.left='8px';
		newBox.style.top='8px';		
		//newBox.style.left=((W-355)/2)+'px';
		//newBox.style.top=((H-98)/2)+'px';		

		var newText=document.createElement("div");
		newText.className='disp-text';
		newText.innerHTML='<center><STRONG class="txtTahomaSize12 '+iColor+'">'+text+'<STRONG></center>';

		var newOK=document.createElement("div");
		newOK.className='new-button-yes';
		newOK.style.position='absolute';
		newOK.style.left='105px';
		newOK.style.bottom='8px';		
		newOK.onclick=function(){ eval(ifunction); deleteMyConfirmBox(borderArea);}		

		var newEXIT=document.createElement("div");
		newEXIT.className='new-button-exit';
		newEXIT.style.position='absolute';
		newEXIT.style.left='190px';
		newEXIT.style.bottom='8px';		
		newEXIT.onclick=function(){deleteMyConfirmBox(borderArea);}		

	$(borderArea).appendChild(newConfirm);	
	$('iConfirm').appendChild(borderFB);		
	$('borderAlert').appendChild(newBox);		
	$('iBoxConfirm').appendChild(newText);	
	$('iBoxConfirm').appendChild(newOK);	
	$('iBoxConfirm').appendChild(newEXIT);	
}
function deleteMyConfirmBox(borderArea)
{
	var oChild=$(borderArea).lastChild;
	$(borderArea).removeChild(oChild);
}

function tipShowItemWear(e, JSONdata)
{
		var vX=(ie)?event.clientX+ietruebody().scrollLeft:e.pageX;	
		var vY=(ie)?event.clientY+ietruebody().scrollTop:e.pageY;	

		vX=((vX+338)>1000)?(vX-350):vX;
		
		$('tooltip_prompbox').setStyle('left',(vX+10)+'px');
		$('tooltip_prompbox').setStyle('top',(vY+5)+'px');
		$('tooltip_prompbox').setStyle('display','');		

		var itemList = eval('(' + JSONdata + ')');

		$('tooltip_picture').innerHTML=itemList.item_picture;
		$('tooltip_name1').innerHTML=itemList.name;
		$('tooltip_name2').innerHTML=(itemList.name2.length>7)?itemList.name2:'';
		$('tooltip_status').innerHTML=itemList.status;
		$('tooltip_price').innerHTML=itemList.price;
		$('tooltip_tribe').innerHTML=itemList.tribe;
		
		itemList.description=itemList.description.replace(/{br}/ig, "<br>");
		$('tooltip_description').innerHTML=itemList.description;

		if(itemList.special.length > 5){
			$('Gem').setStyle('display','');
			$('tooltip_special').innerHTML=itemList.special;
		}else{ $('Gem').setStyle('display','none');}
		
		$('tooltip_plus').innerHTML=(itemList.plus>0)?' +'+itemList.plus:'';
		$('tooltip_element').innerHTML=itemList.element_picture;
		$('tooltip_card').innerHTML=itemList.card_picture;						
		
		if(itemList.card_status1.length>30){
			$('tooltip_card1').innerHTML=itemList.card_status1;
		}else{ $('tooltip_card1').innerHTML=''; }
		if(itemList.card_status2.length>30){
			$('tooltip_card2').innerHTML=itemList.card_status2;
		}else{ $('tooltip_card2').innerHTML=''; }
		if(itemList.card_status3.length>30){
			$('tooltip_card3').innerHTML=itemList.card_status3;
		}else{ $('tooltip_card3').innerHTML=''; }			
}

function tipShowCard(e, cardID, name, description, status, price, wear, cardtype)
{
		var vX=(ie)?event.clientX+ietruebody().scrollLeft:e.pageX;	
		var vY=(ie)?event.clientY+ietruebody().scrollTop:e.pageY;	

		vX=((vX+338)>1000)?(vX-350):vX;
		
		$('tooltip2_prompbox').setStyle('left',(vX+10)+'px');
		$('tooltip2_prompbox').setStyle('top',(vY+5)+'px');
		$('tooltip2_prompbox').setStyle('display','');		
		
		$('tooltip2_picture').innerHTML='<img src="../../upload/card/'+cardID+'" border=0>';
		$('tooltip2_name').innerHTML=name;

		description=description.replace(/{br}/ig, "<br>");
		$('tooltip2_description').innerHTML=description;

		$('tooltip2_status').innerHTML=status;
		$('tooltip2_price').innerHTML=price;
		$('tooltip2_wear').innerHTML=wear;				
		$('tooltip2_cardtype').innerHTML='<img src="'+cardtype+'" border=0>';
						
}

itemTypeName=new Array('','<span class=txtColorGray>ทั่วไป</span>','<span class=txtColorBlue>เควส</span>','<span class=txtColorRed>หายาก</span>','<span class=txtColorGreen>ปริศนา</span>'); 
function tipShowItem(e, JSONdata)
{
		var vX=(ie)?event.clientX+ietruebody().scrollLeft:e.pageX;	
		var vY=(ie)?event.clientY+ietruebody().scrollTop:e.pageY;	

		vX=((vX+338)>1000)?(vX-350):vX;		
		
		$('tooltip1_prompbox').setStyle('left',(vX+10)+'px');
		$('tooltip1_prompbox').setStyle('top',(vY+5)+'px');
		$('tooltip1_prompbox').setStyle('display','');		

		var itemList = eval('(' + JSONdata + ')');
		//alert(itemList.name);

		$('tooltip1_picture').innerHTML=itemList.item_picture;
		$('tooltip1_name1').innerHTML=itemList.name;

		itemList.description=itemList.description.replace(/{br}/ig, "<br>");
		$('tooltip1_description').innerHTML=itemList.description;

		$('tooltip1_price').innerHTML=itemList.price;		
		if(itemList.level>1)
			$('tooltip1_level').innerHTML='	เลเวลที่ใช้ได้ : <span class="txtColorHeadTools">Lv '+itemList.level+'</span>';
		else
			$('tooltip1_level').innerHTML='';

		if(itemList.itemtype>1)
			$('tooltip1_type').innerHTML='	 ( '+itemTypeName[itemList.itemtype]+' )';
		else
			$('tooltip1_type').innerHTML='';

}


function hideItem(){
	$('tooltip_prompbox').setStyle('display','none');
	$('tooltip1_prompbox').setStyle('display','none');
	$('tooltip2_prompbox').setStyle('display','none');
}

