var d	= document;
var w	= window;
var siteurl	= "/";
var deal_id	= "";

w.onload	= function() {	
	tooltips_set_handlers();	
};


function get_nav()
{
	var nav	= {};
	nav.is_firefox	= false;
	nav.is_chrome	= false;
	nav.is_safari	= false;
	nav.is_opera	= false;
	nav.is_msie		= false;
	nav.ver	= 0;
	var ua	= navigator.userAgent.toLowerCase();
	var tmp;
	tmp	= ua.match(/firefox\/([0-9]+\.[0-9]+)/);
	if( tmp ) {
		nav.is_firefox	= true;
		nav.ver	= parseFloat(tmp[1]);
		return nav;
	}
	tmp	= ua.match(/chrome\/([0-9]+\.[0-9]+)/);
	if( tmp ) {
		nav.is_chrome	= true;
		nav.ver	= parseFloat(tmp[1]);
		return nav;
	}
	tmp	= ua.match(/msie\s([0-9]+\.[0-9]+)/);
	if( tmp ) {
		nav.is_msie	= true;
		nav.ver	= parseFloat(tmp[1]);
		return nav;
	}
	tmp	= ua.match(/opera/);
	if( tmp ) {
		nav.is_opera	= true;
		tmp	= ua.match(/version\/([0-9]+\.[0-9]+)/);
		if( tmp ) {
			nav.ver	= parseFloat(tmp[1]);
		}
		return nav;
	}
	tmp	= ua.match(/safari/);
	if( tmp ) {
		nav.is_safari	= true;
		tmp	= ua.match(/version\/([0-9]+\.[0-9]+)/);
		if( tmp ) {
			nav.ver	= parseFloat(tmp[1]);
		}
		return nav;
	}
	return nav;
}

function get_time()
{
	return parseInt( new Date().getTime().toString().substr(0, 10), 10 );
}

function get_time_ms()
{
	return new Date().getTime();
}

function confirmAndRedir(message, location)
{
	if( ! confirm(message) ) {
		return false;
	}
	w.location.href	= location;
}

function get_screen_scroll()
{
	var x=0, y=0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		x	= window.pageXOffset;
		y	= window.pageYOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		x	= document.body.scrollLeft;
		y	= document.body.scrollTop;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		y	= document.documentElement.scrollTop;
		x	= document.documentElement.scrollLeft;
	}
	return [x, y];
}

function get_screen_preview_size()
{
	var w=0, h=0;
	if( typeof( window.innerWidth ) == 'number' ) {
		w	= window.innerWidth;
		h	= window.innerHeight;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		w	= document.documentElement.clientWidth;
		h	= document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		w	= document.body.clientWidth;
		h	= document.body.clientHeight;
	}
	return [w, h];
}

function find_coordinates(obj)
{
	var X=0, Y=0;
	if( obj.offsetParent ) {
		X =	obj.offsetLeft;
		Y =	obj.offsetTop;
		if( obj.offsetParent ) {
			do {
				obj = obj.offsetParent;
				X +=	obj.offsetLeft;
				Y +=	obj.offsetTop;
			}
			while( obj.offsetParent );
		}
	}
	return [X,Y];
}

function trim(txt)
{
	if( typeof(txt) != "string" ) { return txt; }
	txt	= txt.replace(/^\s+/, "");
	txt	= txt.replace(/\s+$/, "");
	return txt;
}

function ltrim(txt)
{
	if( typeof(txt) != "string" ) { return txt; }
	txt	= txt.replace(/^\s+/, "");
	return txt;
}

function rtrim(txt)
{
	if( typeof(txt) != "string" ) { return txt; }
	txt	= txt.replace(/\s+$/, "");
	return txt;
}

function obj_class_add(obj, cl)
{
	if( !obj ) { return false; }
	if( !obj.className ) { obj.className = ""; }
	var tmp	= obj.className.split(" ");
	if(cl in tmp) { return true; }
	tmp[tmp.length]	= cl;
	obj.className	= tmp.join(" ");
}

function obj_class_del(obj, cl)
{
	if( !obj ) { return false; }
	if( !obj.className ) { obj.className = ""; }
	var tmp	= obj.className.split(" ");
	for(var i=0; i<tmp.length; i++) {
		if(tmp[i]==cl || tmp[i]==="") { delete tmp[i]; }
	}
	obj.className	= tmp.join(" ");
}

function ajax_init(is_xml)
{
	var req = false;
	if (w.XMLHttpRequest) {
		req = new XMLHttpRequest();
		if (req.overrideMimeType) { req.overrideMimeType( is_xml ? "application/xml" : "text/plain" ); }
	} else if (w.ActiveXObject) {
		try { req = new w.ActiveXObject("MSXML3.XMLHTTP"); } catch(e) {
		try { req = new w.ActiveXObject("MSXML2.XMLHTTP.3.0"); } catch(e) {
		try { req = new w.ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {
		try { req = new w.ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {
		}}}}
	}
	if ( ! req ) {
		return false;
	}
	return req;
}
	
function in_array(sth, arr)
{
	if( sth===undefined || arr.constructor!=Array ) {
		return false;
	}
	for(var i=0; i<arr.length; i++) {
		if( arr[i] == sth ) {
			return true;
		}
	}
	return false;
}

function array_unique(arr)
{
	var tmp	= [];
	for(var i=0; i<arr.length; i++) {
		if( in_array(arr[i], tmp) ) {
			continue;
		}
		tmp[tmp.length]	= arr[i];
	}
	return tmp;
}

function array_shuffle(o)
{
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
}



function tooltips_set_handlers() {
	var i, els = d.body.getElementsByTagName("*");
	var isonclick;
	if( !els ) { return; }
	for(i=0; i<els.length; i++) {
		if( ! els[i].getAttribute("tooltip") ) { continue; }
		if( els[i].getAttribute("tooltip_is_set") == 1 ) { continue; }
		isonclick	= els[i].getAttribute("tooltiponclick");
		isonclick	= isonclick=="true" || isonclick=="1" || isonclick=="tooltiponclick" ? true : false;
		if( isonclick ) {
			els[i].onclick		= function(event) { tooltip_show(this, event, true, this.getAttribute("tooltipdir"), this.getAttribute("tooltippos")); };
		}
		else {
			els[i].onmouseover	= function(event) { tooltip_show(this, event, false, this.getAttribute("tooltipdir"), this.getAttribute("tooltippos")); };
			els[i].onmouseout		= function() { tooltip_hide(this); };
		}
		els[i].onmousemove	= function() { tooltip_keep(this); var obj=this; setTimeout( function(){tooltip_keep(obj);}, 15 ); };
		els[i].onmouseup		= function() { tooltip_keep(this); var obj=this; setTimeout( function(){tooltip_keep(obj);}, 15 ); };
		els[i].setAttribute("tooltip_is_set", 1);
	}
	if( ! tooltips_set ) {
		var tmpf	= function() { setTimeout( function(){tooltip_hide(tooltip_last_el);}, 10 ); };
		if( d.addEventListener ) {
			d.addEventListener("click", tmpf, false);
			d.addEventListener("scroll", tmpf, false);
		}
		else if( d.attachEvent ) {
			d.attachEvent("onclick", tmpf);
			w.attachEvent("onscroll", tmpf, false);
		}
		tooltips_set	= true;
	}
}
setTimeout( tooltips_set_handlers, 200 );
var tooltips_set	= false;
var tooltip_last_el	= false;
var tooltip_last_tm	= false;
var tooltip_last_tmo	= false;
function tooltip_show(obj, event, isonclick, direction, position) {
	if( tooltip_last_el && tooltip_last_el.id != obj.id ) {
		tooltip_hide(tooltip_last_el, true);
	}
	if( tooltip_last_tmo ) {
		clearTimeout(tooltip_last_tmo);
	}
	var tt	= obj.getAttribute("tooltip");
	if( !event && w.event ) {
		event	= w.event;
	}
	if( !tt || !event ) { return; }
	if( !obj.id ) {
		obj.id	= "tooltip_"+Math.round(Math.random()*10000);
	}
	var ev_x	= event.clientX;
	var ev_y	= event.clientY;
	var pos_by		= "mouse";
	var pos_offx	= 0;
	var pos_offy	= 0;
	if( position ) {
		position	= position.split(",");
		pos_by		= position[0] && position[0]=="obj" ? "obj" : "mouse";
		pos_offx	= position[1] ? parseInt(position[1],10) : 0;
		pos_offy	= position[2] ? parseInt(position[2],10) : 0;
	}
	var nav	= get_nav();
	tooltip_last_tmo	= setTimeout( function() {
		if( tooltip_last_el && tooltip_last_el.id == obj.id ) {
			tooltip_keep(obj);
			return;
		}
		if( tooltip_last_el ) {
			tooltip_hide(tooltip_last_el, true);
		}
		tooltip_last_el	= obj;
		var dv	= d.createElement("DIV");
		dv.id	= "tooltipp_"+Math.round(Math.random()*10000);
		dv.className	= "tooltip";
		dv.setAttribute("obj_id", obj.id);
		dv.style.position		= "fixed";
		dv.style.display		= "block";
		dv.style.visibility	= "hidden";
		var scr	= get_screen_scroll();
		dv.onmouseover	= function() { tooltip_keep(); setTimeout( function(){tooltip_keep();}, 20 ); };
		dv.onmousemove	= function() { tooltip_keep(); setTimeout( function(){tooltip_keep();}, 20 ); };
		dv.onclick		= function() { tooltip_keep(); setTimeout( function(){tooltip_keep();}, 20 ); };
		if( ! isonclick ) {
			dv.onmouseout	= function() { setTimeout( function(){tooltip_hide(obj);}, 5 ); };
		}
		var dv2	= d.createElement("DIV");
		dv2.className	= "tooltip2";
		var dv3	= d.createElement("DIV");
		dv3.className	= "tooltip_top";
		dv2.appendChild(dv3);
		var dv4	= d.createElement("DIV");
		dv4.className	= "tooltip_body";
		if( isonclick ) {
			var lnk	= d.createElement("A");
			lnk.href	= "javascript:;";
			lnk.className	= "ttclose";
			lnk.onfocus	= function() { this.blur(); };
			lnk.onclick	= function() { tooltip_hide(obj, true); };
			dv4.appendChild(lnk);
		}
		var dv5	= d.createElement("DIV");
		dv5.innerHTML	= tt;
		dv5.style.overflow	= "visible";
		dv4.appendChild(dv5);
		dv2.appendChild(dv4);
		var dv5	= d.createElement("DIV");
		dv5.className	= "tooltip_bottom";
		dv2.appendChild(dv5);
		var dv6	= d.createElement("DIV");
		direction	= direction=="left_top" || direction=="bottom_left" || direction=="bottom_right" ? direction : "left_bottom";
		dv6.className	= "ttc_"+direction;
		dv2.appendChild(dv6);
		dv.appendChild(dv2)
		d.body.appendChild(dv);
		setTimeout( function() {
			var W	= parseInt(dv.clientWidth,10);
			var H	= parseInt(dv.clientHeight,10);
			if( pos_by == "obj" ) {
				var c	= find_coordinates(obj);
				var X	= c[0];
				var Y	= c[1];
				if( !X || !Y ) {
					pos_by	= "mouse";
				}
				else {
					if( direction == "left_top" ) { X += 15; Y -= 35; }
					else if( direction == "left_bottom" ) { X += 15; Y -= H-35; }
					else if( direction == "bottom_left" ) { X -= 33; Y -= H+3; }
					else if( direction == "bottom_right" ) { X -= W-38; Y -= H+3; }
					X	+= pos_offx;
					Y	+= pos_offy;
					dv.style.position	= "absolute";
				}
			}
			if( pos_by == "mouse" ) {
				var X	= parseInt(ev_x,10);
				var Y	= parseInt(ev_y,10);
				if( direction == "left_top" ) { X += 15; Y -= 35; }
				else if( direction == "left_bottom" ) { X += 15; Y -= H-35; }
				else if( direction == "bottom_left" ) { X -= 33; Y -= H+10; }
				else if( direction == "bottom_right" ) { X -= W-38; Y -= H+10; }
				Y	-= 10;
				if( nav.is_msie && nav.ver<=6 ) {
					dv.style.position	= "absolute";
					Y	+= get_screen_scroll()[1];
				}
				if( W && H ) {
					var sz = get_screen_preview_size();
					if( sz[0] && sz[1] ) {
						var mxw	= sz[0]-20;
						var mxh	= sz[1]-5;
						if( mxw < W + X ) {
							X	= mxw - W - 5;
							Y	+= 20;
						}
					}
				}
			}
			dv.style.left	= X + "px";
			dv.style.top	= Y + "px";
			if( isonclick ) {
				dv.style.visibility	= "visible";
			}
			else {
				var tm = obj.getAttribute("tooltiptm");
				tm	= parseInt(tm, 10);
				if( !tm || isNaN(tm) ) { tm = 100; }
				setTimeout( function(){ dv.style.visibility	= "visible"; }, tm );
			}
			obj.setAttribute("tt_tmp_id", dv.id);
			tooltip_keep(obj);
		}, 10);
	}, 50);
}
function tooltip_hide(obj, force) {
	if( ! obj ) { return; }
	if( force ) {
		clearTimeout( tooltip_last_tm );
		tooltip_last_tm	= false;
		tooltip_last_el	= false;
		var tmp	= obj.getAttribute("tt_tmp_id");
		if( tmp ) {
			tmp	= d.getElementById(tmp);
		}
		if( tmp ) {
			tmp.style.display	= "none";
			tmp.parentNode.removeChild(tmp);
		}
		obj.setAttribute("tt_tmp_id", false);
		return;
	}
	if( tooltip_last_tmo ) {
		clearTimeout(tooltip_last_tmo);
	}
	tooltip_last_tm	= setTimeout( function() { tooltip_hide(obj, true); }, 0 );
}
function tooltip_keep() {
	if( tooltip_last_tm ) { clearTimeout( tooltip_last_tm ); }
}
function tooltip_hide_bugged() {
	var i, bug = d.getElementsByTagName("DIV");
	for(i=0; i<bug.length; i++) {
		if( bug[i].className != "tooltip" ) { continue; }
		if( !bug[i].id.match(/^tooltipp_/) ) { continue; }
		if( bug[i].getAttribute("obj_id") == tooltip_last_el.id ) { continue; }
		bug[i].style.display = "none";
		bug[i].parentNode.removeChild(bug[i]);
	}
}
setInterval( tooltip_hide_bugged, 1000 );


