//
// These functions help Hint Window construction
//

var mooHintWindowWidth = 200;		// px
var mooHintHideDelay = 2000;
var mooHintWndStyleClass = "hint_visible";				// style of the window
var mooHintWndTitleStyleClass = "hint_visible_header";	// style of the title/X
var mooHintContentStyleClass = "hint_content";
var mooHintId = 0;					// used to generate unitque hint window id's
var mooHintWindows = new Array();	// holds list of all hint window id's
var mooOnLayer = false;
var mooOnSource = false;

function mooShowLayer(lay) {
	if (ie4) {		
		document.all[lay].style.visibility = "visible";
		document.all[lay].style.display = "block";
	} 
	if (ns4) {
		document.layers[lay].visibility = "show";
	}
	if (ns6) {
		document.getElementById([lay]).style.visibility = "visible";
		document.getElementById([lay]).style.display = "block";			
	}
}

function mooHideLayer(lay) {
	if (ie4) {
		if(document.all[lay]){
		document.all[lay].style.visibility = "hidden";
		document.all[lay].style.display = "none";
		}
	}
	if (ns4) {
		if(document.layers[lay]){
		document.layers[lay].visibility = "hide";
		}
	}
	if (ns6) {
		if(document.getElementById([lay])){
		try{
			document.getElementById([lay]).style.visibility ;
			
		}catch(error){
			alert(lay);
			alert(document.getElementById([lay]));
			
		}
		document.getElementById([lay]).style.visibility = "hidden";
		document.getElementById([lay]).style.display = "none";	
		}
	}
}

function mooIsVisibleLayer(lay) {
	if (ie4) {			
		return document.all[lay].style.visibility == "visible";
	}
	if (ns4) {
		return document.layers[lay].visibility == "show";
	}
	if (ns6) {
		return document.getElementById([lay]).style.visibility == "visible";
	}
	
	return false;
}

function mooHideAllHintWindows(){
	for (var i=0; i < mooHintWindows.length; i++){
		mooHideLayer(mooHintWindows[i]);
	}
}

function mooShowHintWindow(id, sourceId, position){

	if (mooIsVisibleLayer(id)){
		return;
	}		
		
	mooHideAllHintWindows();

	var idElem = mooElementById(id);
	var sourceIdElem = mooElementById(sourceId);	    
   	var pos = mooFindObjectPosition(sourceIdElem);
 
	if(position ==1){
		idElem.style.top = pos.y + 12;
		idElem.style.left = pos.x ;
	}
	if(position ==4 ){
		idElem.style.top = pos.y ;
		idElem.style.left = pos.x + pos.width  ;
	}
	
	
	if(position ==2){
		idElem.style.top = pos.y + 12;
		idElem.style.left = pos.x + 12 - mooHintWindowWidth /2;
	}
	if(position ==3 ){
		idElem.style.top = pos.y + 12;
		idElem.style.left = pos.x + 12 - mooHintWindowWidth;
	}
 

	mooShowLayer(id);
}

function mooHideHintWindow(id){
	mooHideLayer(id);
}

function mooEmbedHintWindow(title, id, newid, position){
	frameStr = "<span id='" + newid + "'  class='" + mooHintWndStyleClass + "' style='z-index: 600'>"
	if(position==4){
		frameStr +="<table cellspacing=0 cellpadding=0 border =0 ><tr><td valign=top style='padding:0'><img src='"+triangleLeftSrc+"'></td><td style='padding:0'> "
		
	}
	
	frameStr += "<table style='padding:0' cellspacing=0 cellpadding=0 border =0 width=\"" + mooHintWindowWidth + "px\">"

	if(position==1){
		frameStr += "<tr><td align='left' valign=bottom><img src='"+triangleSrc+"'></td></tr>"
	}
	if(position==2){
		
		frameStr += "<tr><td align='center' valign=bottom><img src='"+triangleSrc+"'></td></tr>"
	}
	if(position==3){
		frameStr += "<tr><td align='right' valign=bottom><img src='"+triangleSrc+"'></td></tr>"
	}		



	frameStr += "<tr><td align=top style='padding:0'><table class='"+mooHintContentStyleClass+"' style='background-color:#cacbe7' width=100%>"
	frameStr += "<tr><td class='" + mooHintWndTitleStyleClass + "'>" + title + "</td>"
	frameStr += "<td align='right'><a href='' class='" + mooHintWndTitleStyleClass + "' onclick='javascript:mooHideHintWindow(\"" + newid + "\"); return false;'>[X]</a></td></tr>";
	frameStr += "<tr><td colspan='2' bgcolor='#efeefe' style='padding:5'>" + mooElementById(id).innerHTML + "</td></tr></table></td></tr></table>"; 
	
	if(position==4){
		frameStr +="</td></tr></table>"
	
	}
	
	
	
	frameStr += "</span>"; 
	if(document.getElementById(id+"_frame")){
	document.getElementById(id+"_frame").innerHTML = frameStr;
	}else{
		document.write(frameStr);
	}

	mooHintWindows[mooHintWindows.length] = newid;
}




//the imgPosition can be 1, 2 or 3. It shows the triangle left, center or right

function mooAttachHintWindowTo(title, id, sourceId, imgPosition, turnOff){ 

	if(turnOff){
		return ;
	}

	// init if needed
	if (mooHintWindows == null){
		mooHintWindows = new Array();
	}

	// generate unique id
	mooHintId++;
	
	var newid = "hintWnd" + mooHintId;	

	mooEmbedHintWindow(title, id, newid, imgPosition);	
	
	// wire up onmouseover of the source element
	var sourceIdElem = mooElementById(sourceId);

    sourceIdElem.onmouseover = function () {
    	mooOnSource = true;
    	mooShowHintWindow(newid, sourceId, imgPosition);  
    	return false;
    }

    sourceIdElem.onmouseout = function () {
    	mooOnSource = false;
    	window.setTimeout("shouldHide()", 1000 );
    	return false;
    }
    

    
  
    
    
    // wire up onmouseover and onmouseout for the hintwindow
    var hideIntervalId;
    var newIdElem = mooElementById(newid);			
    newIdElem.onmouseover = function () {
    	mooOnLayer = true;
	    clearInterval(hideIntervalId);
		mooShowHintWindow(newid, sourceId, imgPosition);  
    	return false;
    }
    
    

    
    
    newIdElem.onmouseout = function () {
    	/*
    	hideIntervalId = setInterval(
    		function(){ 
    			mooHideHintWindow(newid); 
    		}, 
			mooHintHideDelay
		);	    	*/
    	mooHideAllHintWindows();
    	mooOnLayer = false;
    	return false;
    }

   
    
}

function shouldHide(){
	if(mooOnLayer==false && mooOnSource==false){
		mooHideAllHintWindows();
	}else{
		window.setTimeout("shouldHide()", 1000 );
	}
	
}

