var pop_border = 1;
var pop_pad_top = 0;//3
var pop_pad_left = 0;//4
var pop_bordercolor = "#003300";
var pop_bgcolor = '#FFFFCC';
var pop_font = "Arial,Verdana,Helvetica,sans-serif";
var pop_textsize = 2;
var pop_fontsize = '8pt';
var pop_textcolor = "#003300";

var pop_offset_x = 12;
var pop_offset_y = 10;

var pop = null;
var initialized = false;
var pop_width = 2000;
var mouse_x=0, mouse_y=0;

var ie4 = document.all && !document.getElementById;
var ie5 = document.all && document.getElementById;
var ns6 = !document.all && document.getElementById;
var ns4 = document.layers;

function hintInit(){
	if(ie4 || ie5 || ns4 || ns6){
		if (ns4) pop = document.HintDiv;
		if (ie4 || ie5) pop = HintDiv;
		if (ns6) pop = document.getElementById("HintDiv");

		if (ns4) document.captureEvents(Event.MOUSEMOVE);
//		document.onmousemove = mouseMoved;
		appendEventHandlingCode('document', 'onmousemove', 'mouseMoved(e)');
	} else {
		hintpopup = do_nothing;
		hintpopdn = do_nothing;
	}
	initialized = true;
}

function hintpopup(id, customText) {
	
	/* Marto: The id param is deprecated - leave empty when calling the function */
	
	if (!initialized) return false;	
	
	var text, html_width, html_height;
	
	// Marto:
	if (customText == undefined || customText == "") {
		return false;
	} else {
		text = customText;
	}
	
	if (ie4 || ie5) { var layerhtml = "<DIV id=\"hint_div_element\" width=\"100%\" STYLE=\"background:"+pop_bgcolor+"; border:"+pop_border+"px solid "+pop_bordercolor+"; padding:"+pop_pad_top+"px "+pop_pad_left+"px; font:"+pop_fontsize+" "+pop_font+"; color:"+pop_textcolor+"; position: absolute; top: 0; left: 0; z-index: 1;\">"+text+"</DIV>"; }
	if (ns4) { var layerhtml = "<TABLE "+html_width+html_height+" BORDER=0 CELLPADDING="+pop_border+" CELLSPACING=0 BGCOLOR="+pop_bordercolor+"><TR><TD><TABLE WIDTH=\"100%\" "+html_height+" BORDER=0 CELLPADDING="+pop_pad_left+" CELLSPACING=0 BGCOLOR="+pop_bgcolor+"><TR><TD><FONT FACE=\""+pop_font+"\" COLOR=\""+pop_textcolor+"\" SIZE=\""+pop_textsize+"\">"+text+"</FONT></TD></TR></TABLE></TD></TR></TABLE>"; }
	if (ns6) { var layerhtml = "<TABLE "+html_width+html_height+" CELLSPACING=0 STYLE=\"background:"+pop_bgcolor+"; border:"+pop_border+"px solid "+pop_bordercolor+"\"><TR><TD STYLE=\"padding:"+pop_pad_top+"px "+pop_pad_left+"px; font:"+pop_fontsize+" "+pop_font+"; color: "+pop_textcolor+";\">"+text+"</TD></TR></TABLE>"; }

	/* 
		Svilen:
		
		A workaround for the IE form overlapping problem.
		We have to get the div's width and height and pass them to the IFRAME which will cover the form elements.
		So first we create the div and get it's width and height. Then we destroy the div and create another one with an iframe.	
	*/
	
	layerWrite(layerhtml);
	var hintObject = getElement('hint_div_element');
	if (ie4 || ie5) { layerhtml = "<iframe frameborder=\"0\" scrolling=\"no\" src=\"javascript: false;\" width=\""+(hintObject.clientWidth+2)+"\" height=\""+(hintObject.clientHeight+2)+"\" style=\"z-index: 0; filter: Beta(Style=0,Opacity=0);\"></iframe>"+layerhtml; }
	/* End: A workaround for the IE form overlapping problem. */
	
	layerWrite(layerhtml);
	
	setPopupPos(mouse_x, mouse_y);
	
	showObject(pop);
	
	return true;
}

function hintpopdn() {
	if ( initialized && (ie4 || ie5 || ns4 || ns6) && (pop != null) ) {
		hideObject(pop);
	}
	return true;
}


function mouseMoved(e) {
	if ( ns4 || ns6 ) {
		mouse_x=e.pageX;
		mouse_y=e.pageY;}
	if (ie4) {
		mouse_x=event.x;
		mouse_y=event.y;}
	if (ie5) {
		mouse_x=event.clientX+self.document.body.scrollLeft;
		mouse_y=event.clientY+self.document.body.scrollTop;}

	if (document.getElementById("testMouse"))
		document.getElementById("testMouse").innerHTML = mouse_x;

	setPopupPos(mouse_x, mouse_y);
}

function setPopupPos(mouse_x, mouse_y) {
	if (!initialized) return false;

	if (ie4 || ie5) { var win_width = self.document.body.clientWidth;  var win_height = self.document.body.clientHeight; }
	if (ns4) { var win_width = self.innerWidth;  var win_height = self.innerHeight; }
	if (ns6) { var win_width = self.outerWidth;  var win_height = self.outerHeight; }

	var win_scroll_x = (ie4 || ie5) ? self.document.body.scrollLeft : self.pageXOffset;
	var win_scroll_y = (ie4 || ie5) ? self.document.body.scrollTop : self.pageYOffset;

	var pop_width_current = (ie4 || ie5)? pop.clientWidth:pop.offsetWidth;
	var pop_height_current = (ie4 || ie5)? pop.clientHeight:pop.offsetHeight;
	
	var scrollbar_size = (ie4 || ie5)? 0:16;

	var pos_x = mouse_x + win_scroll_x + pop_offset_x;
	if (pos_x + pop_width_current > win_width + win_scroll_x - scrollbar_size)
		pos_x = win_width + win_scroll_x - pop_width_current - scrollbar_size;
	if (pos_x < win_scroll_x)
		pos_x = win_scroll_x;

	var pos_y = mouse_y + pop_offset_y;

	if (pos_y + pop_height_current > win_height + win_scroll_y - scrollbar_size)
		pos_y = win_height + win_scroll_y - pop_height_current - scrollbar_size;

	if (pos_y + pop_height_current > win_height + win_scroll_y - scrollbar_size)
		pos_y = mouse_y - pop_height_current - pop_offset_y;

	//self.status = "msx:" +mouse_x+ " msy:" +mouse_y+ " | posx:" +pos_x+ " posy:" +pos_y+ " | scrx:" +win_scroll_x+ " scry:" +win_scroll_y+ " | offx:" +pop_offset_x+ " offy:" +pop_offset_y+ " | cW:" + pop_width_current + " cH:" + pop_height_current;

	moveLayerTo(pop, pos_x, pos_y);
}

function moveLayerTo(obj,x,y) {
	if (ns4) {
		obj.left = x;
		obj.top = y;
	} else if (ie4 || ie5) {
		obj.style.left = x;
		obj.style.top = y;
	} else if (ns6) {
		obj.style.left = x + "px";
		obj.style.top = y + "px";
	}
}

function showObject(obj) {
	if (ns4) obj.visibility = "show";
	else if (ie4 || ie5) obj.style.visibility = "visible";
	else if (ns6) obj.style.visibility = "visible";
}

function hideObject(obj) {
	if (ns4) obj.visibility = "hide";
	else if (ie4 || ie5) obj.style.visibility = "hidden";
	else if (ns6) obj.style.visibility = "hidden";
}

function layerWrite(txt) {
	if (ns4) {
		var lyr = self.document.HintDiv.document;
		lyr.write(txt);
		lyr.close();
	} else if (ie4 || ie5) {
		document.all.HintDiv.innerHTML=txt;
	} else if (ns6) {
		var range = self.document.createRange();
		range.setStartBefore(pop);
		domfrag = range.createContextualFragment(txt);
		while (pop.hasChildNodes()) {
			pop.removeChild(pop.lastChild);
		}
		pop.appendChild(domfrag);
	}
}


function do_nothing() {
	return 1;
}
