// JavaScript Document

/* jsHandler.js */
function inc(filename){
	var body = document.getElementsByTagName('head').item(0);
	script = document.createElement('script');
	script.src = filename;
	script.type = 'text/javascript';
	body.appendChild(script);
}

function resizeScreen(){
	topHeight = 120;	
	bottomHeight = 62;
	screenHeight = getWindowSize()[1];
	contentDiv=document.getElementById('content-wrap');	if(screenHeight < 400) screenHeight = 1680;
	contentDiv.style.height= (screenHeight - (topHeight+bottomHeight)) + "px";
	if(getWindowSize()[0]<400)
	contentDiv.style.width= "1024px";
}
window.onresize=resizeScreen;

//inc('scripts/jquery.js');

function gebi(elemID){
	return document.getElementById(elemID);
}

function getById(elemID){
	//alert('I\'m here!')
	return document.getElementById(elemID);
}

function getWindowSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	//window.alert( 'Width = ' + myWidth );
	//window.alert( 'Height = ' + myHeight );
	return [myWidth, myHeight];
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getByName(elemName){
	elems = $("*[name='"+elemName+"']");
	//alert("found "+elems.length+" elements with name = "+elemName);
	return elems;
}

function trimIt(){
    result = this.valueOf();
    //strip spaces from the left
    while(result.charAt(0)==" ") result = result.substr(1);
    //strip spaces from the right
    while(result.charAt(result.length-1)==" "){
        result = result.substr(0,result.length-2);
    }
    return result;
}
//this function checks if a given string value is numeric and returna a boolean
//I use it to extend the functionality of Javascript's [String] object model
function isNumericValue(){
    str = this.valueOf();
    if (isNaN(str)) return false;		//check if it doesn't start with a number
    num = parseInt(result);
    if(str.length > num.toString().length) return false;	//check if it's a number followed by non-numeric characters
    return true;		//else, it's a valid number
}
String.prototype.isNumeric = isNumericValue;    //extend [String] object model to include an isNumeric() function
String.prototype.trim = trimIt;     //extend [String] object model to include a trim() function

function positionInfoBox(){
    infBox = getById('infoBox');
    staffInput = getById('staff');
    infBox.style.left = (document.body.clientWidth/3) + "px";
    infBox.style.top = "90px";
    alert("infBox.style.top=" + infBox.style.top);
}

function hideElement(elemID){
	//elem = document.getElementById(elemID);
	//elem.style.display = "none";	
	$("#"+elemID).fadeOut(1000);
}

function showElement(elemID,opacty){
	//elem = document.getElementById(elemID);
	//elem.style.display = "inline";
	if(opacty){
		$("#"+elemID).hide();
		$("#"+elemID).fadeTo(1000,opacty);
	}else{
		$("#"+elemID).fadeIn(1000);
	}
}

function centerElement(elemID){
	elem = document.getElementById(elemID);
	//elem.style.width = (window.innerWidth * 2 / 5) +"px";  //40% of screen width
	elemW = parseInt(elem.style.width);		//alert("elemW ==== "+elemW);
	elemH = parseInt(elem.style.height);	//alert("elemH ==== "+elemH);
	scrolls = getScrollXY();
	size = getWindowSize();
	elem.style.top = (scrolls[1] + (size[1] - elemH)/2) + "px";//the extra 120px is to compensate for IE toolbar
	elem.style.left = (size[0] - elemW)/2 + "px";
	//alert("dialog centered at " + elem.style.left + " x " + elem.style.top);
}

function makeReadOnly(elemID){
	elem = document.getElementById(elemID);
	elem.readOnly=true;
}

function makeReadWrite(elemID){
	elem = document.getElementById(elemID);
	elem.readOnly=false;
}

function pad(str, len, chr){
	theLength = len - str.length
	if(theLength>0){
		for(i=0; i<theLength; i++){
			str = chr + str;
		}
	}
	return str;
}

document.onmousemove = mouseMove;
document.onmouseup   = mouseUp;

function ShowPopup(popupid)	{
	
	scr = getById('transp_screen');
	//hp = document.getElementById(popupid);
	//center on screen
	centerElement(popupid);
	// Set popup to visible
	//hp.style.display = "inline";
	scr.style.display = "inline";	
	$("#transp_screen").fadeTo(1000, 0.7);
	popupid = "#" + popupid;
	$(popupid).fadeIn("slow");
	scr.style.height=getWindowSize()[1] + "px";
	//alert("transp screen ==>>" + scr.style.height + " x " + scr.style.width);
}

function HidePopup(popupid)	{
	//hp = document.getElementById(popupid);
	//scr = document.getElementById('transp_screen');
	popupid = "#" + popupid;
	$(popupid).fadeOut("slow");
	$("#transp_screen").fadeOut();
	//hp.style.display = "none";
	//scr.style.display = "none";
}

function showDialog(content){
	dialog = document.getElementById('dialogDiv');
	elem = 	document.getElementById('content_cell');
	elem.innerHTML = content;
	ShowPopup('dialogDiv');
	/*dialog.style.width = screen.availWidth/2 + "px";
	if(window.pageYOffset){
		//alert("window.pageYOffset = "+window.pageYOffset);
		dialog.style.top = window.pageYOffset + 150 + 'px';
	}else{
		//alert("document.body.scrollTop = "+document.body.scrollTop);
		dialog.style.top = document.body.scrollTop + 150 + 'px';
	}
	dialog.style.left = (screen.availWidth/2 - parseInt(dialog.style.width)/2) + "px";
	dialog.style.display = "inline";
	*/
}

function hideDialog(){
	dialog = document.getElementById('dialogDiv');
	dialog.style.display = "none";
}
chat_id=0;
function sendChat(){
	chat_elem = document.getElementById('chat_area');
	elem = document.getElementById('chatInput');
	if(chat_elem){
		chat_id++;
		talk_id = "talk" + chat_id
		chat_elem.innerHTML += "<span id='"+talk_id+"'><b>Guest:</b>&nbsp;" + elem.value + "</span><br/>";
		elem.value = "";
		elem.focus();
		document.getElementById('talk_id');
		talk.scrollIntoView(false);
	}
}
//drag-drop script begins here

document.onmousemove = mouseMove;
document.onmouseup   = mouseUp;

function mouseMove(ev){
	ev           = ev || window.event;
	var mousePos = mouseCoords(ev);
}

function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

function makeClickable(object){
	object.onmousedown = function(){
		dragObject = this;
	}
}

function mouseUp(ev){
	dragObject = null;
}

var dragObject  = null;
var mouseOffset = null;

function getMouseOffset(target, ev){
	ev = ev || window.event;

	var docPos    = getPosition(target);
	var mousePos  = mouseCoords(ev);
	return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}

function getPosition(e){
	var left = 0;
	var top  = 0;

	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}

	left += e.offsetLeft;
	top  += e.offsetTop;

	return {x:left, y:top};
}

function mouseMove(ev){
	ev           = ev || window.event;
	var mousePos = mouseCoords(ev);

	if(dragObject){
		dragObject.style.position = 'absolute';
		dragObject.style.top      = mousePos.y - mouseOffset.y;
		dragObject.style.left     = mousePos.x - mouseOffset.x;

		return false;
	}
}
function mouseUp(){
	dragObject = null;
}

function makeDraggable(item){
	if(!item) return;
	if(!document.all) return;
	item.onmousedown = function(ev){
		dragObject  = this;
		mouseOffset = getMouseOffset(this, ev);
		return false;
	}
}
//drag-drop script ends here
