// JavaScript Document

var helpDetailsGot, help_main_div, help_title_div, help_text_div, helpBodyElement,helpMoveWindow,helpXmoveStart,helpYmoveStart,help_container_div, helpDivHeight,helpDivWidth, helpDivXoffset, helpDivYoffset, helpMouseX, helpMouseY;

if(typeof(ajax)!='object'){
	var ajax = new sack();
}

helpMoveWindow = false;
helpXmoveStart = false;
helpYmoveStart = false;
helpDivXoffset = 20;
helpDivYoffset = 20;

var helpBarHeight = 20;

var ns4=document.layers
var ns6=document.getElementById&&!document.all
var ie4=document.all

var onloadPrev=window.onload;
window.onload=function(){ if(onloadPrev)onloadPrev(); helpInit(); }

function helpInit(){
	helpDetailsGot= new Array();
	
	help_main_div = document.createElement('div');
	help_main_div.style.position = 'absolute';
	help_main_div.style.width = '300px';
	help_main_div.style.fontSize = '12px';
	help_main_div.style.fontFamily = 'Arial';
//	help_main_div.style.zIndex = '999';
	help_main_div.style.backgroundColor = '#FFFFFF';
	help_main_div.style.border = '#CCCCCC 1px solid';
	help_main_div.align = 'left';
	helpHideElement(help_main_div);
	
	
	var help_bar_div = document.createElement('div');
	help_bar_div.style.backgroundColor = '#0000AA';
	help_bar_div.align = 'center';
	help_bar_div.style.height = helpBarHeight+'px';
	help_bar_div.style.color = '#FFFFFF';
	help_bar_div.style.fontWeight = 'bold';
	help_bar_div.style.cursor = 'move';
	help_bar_div.onmousedown = function(){
		helpMoveWindow=true;
		if(ie4)help_bar_div.setCapture();
	};
	help_bar_div.onmouseup = function(){
		helpMoveWindow=false;
		helpXmoveStart=false;
		helpYmoveStart=false;
		if(ie4)help_bar_div.releaseCapture();
	};
	onloadPrev=document.onmouseup;
	document.onmouseup=function(){ if(onloadPrev)onloadPrev(); help_bar_div.onmouseup(); }
	
	var imgElem = document.createElement('img');
	imgElem.src = SITE_URL+'/images/close_btn.gif';
	imgElem.border = 0;
	
	var aClose = document.createElement('a');
	aClose.href = "javascript: helpHideElement(help_main_div)";
	aClose.appendChild(imgElem);
	
	var barTable = document.createElement('table');
	var barTbody = document.createElement('tbody');
	barTable.width = '90%';
	barTable.cellspacing = '0';
	barTable.cellpadding = '0';
	var barTR = document.createElement('tr');
	var titleTD = document.createElement('td');
	var btnTD = document.createElement('td');
	barTR.appendChild(titleTD);
	barTR.appendChild(btnTD);
	barTbody.appendChild(barTR);
	barTable.appendChild(barTbody);
	
	titleTD.align='left'
	titleTD.innerHTML+='Help';
	
	btnTD.align='right'
	btnTD.appendChild(aClose);
	
	help_bar_div.appendChild(barTable);
	
	help_title_div = document.createElement('div');
	help_title_div.style.fontSize = '1.3em';
	help_title_div.style.fontWeight = 'bold';
	help_title_div.style.lineHeight = '1.6em';
	help_title_div.align = 'center';
	
	help_text_div = document.createElement('div');
	help_text_div.style.margin = '10px';
	
	
	help_container_div = document.createElement('div');
	help_container_div.style.height = '200px';
	help_container_div.style.width = '100%';
	help_container_div.style.overflow = 'auto';
	
	help_main_div.appendChild(help_bar_div);
	help_container_div.appendChild(help_title_div);
	help_container_div.appendChild(help_text_div);
	help_main_div.appendChild(help_container_div);
	
	helpBodyElement = document.getElementsByTagName('body');
	helpBodyElement = helpBodyElement[0];
	
	if(ns4)document.captureEvents(Event.MOUSEMOVE);
	
	onloadPrev=document.onmousemove;
	document.onmousemove=function(e){ if(onloadPrev)onloadPrev(e); helpGgetMouse(e); }
}

function helpSaveDetails(url){
	eval(ajax.response);
	
	//save the details
	helpDetailsGot[url]=Array();
	helpDetailsGot[url]['title']=details.title;
	helpDetailsGot[url]['text']=details.text;
	helpDetailsGot[url]['width']=details.width;
	helpDetailsGot[url]['height']=details.height;
	
	helpShowDetails(helpDetailsGot[url]);
}


function helpShowDetails(details){
	help_title_div.innerHTML=details['title'];
	help_text_div.innerHTML=details['text'];
	help_main_div.style.width = details['width']+'px';
	help_container_div.style.height = details['height']+'px';
	help_main_div.style.zIndex = helpGetMaxZIndex()+1;
	helpDivWidth = parseInt(details['width']);
	helpDivHeight = parseInt(details['height']) + parseInt(helpBarHeight);
	
	helpInitWindowPos();
}

function getHelpPage(url){
	
	//chech if the details are already available
	if(helpDetailsGot[url]){
		helpShowDetails(helpDetailsGot[url]);
		return
	}
	
	var details_page=SITE_URL+'/admin/index.php?op=help';
	ajax.requestFile = details_page+"&page_url="+escape(url);
	ajax.onCompletion = function(){ helpSaveDetails(url); };	// Specify function that will be executed after file has been found
	ajax.runAJAX();
}


function helpShowElement(element){
	element.style.display = 'block';
	element.style.visibility='visible';
	
	helpBodyElement.appendChild(element);
}

function helpHideElement(element){
	element.style.display = 'none';
	element.style.visibility='hidden';
}

function helpInitWindowPos(){
	var helpScroll = helpGetScrollXY();
	x = helpMouseX;
	y = helpMouseY;
	
	if(y+helpDivHeight>document.documentElement.clientHeight+helpScroll[1]){
		y = document.documentElement.clientHeight+helpScroll[1] - helpDivHeight - 2*helpDivYoffset;
	}
	
	if(x+helpDivWidth>document.documentElement.clientWidth+helpScroll[0]){
		x = document.documentElement.clientWidth+helpScroll[0] - helpDivWidth - 2*helpDivXoffset;
	}
	
	help_main_div.style.left=(x+helpDivXoffset)+'px';
	help_main_div.style.top=(y+helpDivYoffset)+'px';

	helpShowElement(help_main_div);
}

function helpGgetMouse(e){
	var x= (ns4||ns6)?e.pageX:event.x+(document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft);
	var y=(ns4||ns6)?e.pageY:event.y+(document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop);
	helpMouseX = x;
	helpMouseY = y;
	
	if(helpMoveWindow){
		if(!helpXmoveStart || !helpYmoveStart){
			helpFindPos(help_main_div);
			helpXmoveStart -= x;
			helpYmoveStart -= y;
		}
		
		 help_main_div.style.left = (((ns4||ns6)?e.pageX:event.x+(document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft)) + helpXmoveStart)+'px';
		help_main_div.style.top = (((ns4||ns6)?e.pageY:event.y+(document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop)) + helpYmoveStart)+'px';
	}
}

function helpFindPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	helpXmoveStart = curleft;
	helpYmoveStart = curtop;
}

function helpGetMaxZIndex(){
	var allElems = document.getElementsByTagName?
	document.getElementsByTagName("*"):
	document.all; // or test for that too
	var maxZIndex = 0;
	for(var i=0;i<allElems.length;i++) {
		var elem = allElems[i];
		var cStyle = null;
		if (elem.currentStyle) {cStyle = elem.currentStyle;}
		else if (document.defaultView && document.defaultView.getComputedStyle) {
			cStyle = document.defaultView.getComputedStyle(elem,"");
		}
		var sNum;
		if (cStyle) {
			sNum = Number(cStyle.zIndex);
		} else {
			sNum = Number(elem.style.zIndex);
		}
		if (!isNaN(sNum)) {
			maxZIndex = Math.max(maxZIndex,sNum);
		}
	}
	return maxZIndex;
}

function helpGetScrollXY() {
  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 ];
}
