var owidth=200, iwidth, ooffsetx=13, ooffsety=10, ox=0, oy=0, our=null, html='', delay=250, g_timeout, g_on=false, tr_style = 'table-row'

// see jquery and prototype how they do it.
var DOM2=document.getElementById
var AgentName = navigator.userAgent;

var ie4 = (document.all)?true:false
var ie5, ie6, ie7, ie8, ie9
if (ie4) {
  if (navigator.userAgent.indexOf('MSIE 5')>0) ie5=true
  if (navigator.userAgent.indexOf('MSIE 6')>0) ie6=true
  if (navigator.userAgent.indexOf('MSIE 7')>0) ie7=true
  if (navigator.userAgent.indexOf('MSIE 8')>0) ie8=true
  if (navigator.userAgent.indexOf('MSIE 9')>0) ie9=true
} else ie5 = ie6 = false

var ie=ie4||ie5||ie6||ie7||ie8||ie9
var opera = !!window.opera
var ns6 = ( navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1 )
        ||( navigator.userAgent.indexOf('AppleWebKit/') > -1)
//treat safari as ns6... too much legacy code refering to 'ns6' :)
ns6 = (ns6 && DOM2 && !ie && !opera)?true:false
if (/^Opera\/9\.5/.test(AgentName)) {
//treat opera9.5x as ns6...
	ns6 = true;
}
else if (/^Opera/.test(AgentName)) {
//... and all the other Opera versions should best match ie
	ie = true;
}

if((ns6)||(ie)){
	document.onmousemove=mouseMove
	if (ie && !opera) tr_style = 'block'
} else {
	ourlib=no_lib
	nd=no_lib
}

function no_lib () {
	return true
}

function lwr (what, where) {
// Commented line below is ins6erting parasite space character in places that it should'n be
// Example: /app/download.pl?posted=destination 
//			->> after bank fee when you select radiobutton

//	what+="\n";

    if (!what) what = '&nbsp;';		// insert space if no text to make sure that design will be ok

    if (DOM2) document.getElementById(where).innerHTML = what
		else if (ns6) {var l=document.layers[where].document; l.open(); l.write(what); l.close();}
			else document.all[where].innerHTML = what
}

function lwr_text (what, where) {
// Commented for the same reason like function "lwr"  !!!

//	what+="\n";

    if (!what) what = '&nbsp;';		// insert space if no text to make sure that design will be ok

	if (ns6) {
	  var HTMLElement = document.getElementById(where);
	  if(typeof HTMLElement!="undefined") {
		  var parsedText = document.createTextNode(what);
		  HTMLElement.innerHTML = "";
		  HTMLElement.appendChild( parsedText );
	  }
	} else if (DOM2) document.getElementById(where).innerText = what
	else if (ns6) {var l=document.layers[where].document; l.open(); l.write(what); l.close();}
	else document.all[where].innerText = what
}

//move div above to fit the page
function dontOverflow ( divID ) {
    var div_obj = getObject(divID);
    if (typeof(div_obj) == 'undefined') return;

    var divBottom = getBottom(div_obj);
    var clientHeight;
    if (document.body) {
        clientHeight = document.body.clientHeight
    } else if (document.documentElement && document.documentElement.clientHeight) {
        clientHeight = document.documentElement.clientHeight;
    }

    var divHeight = divBottom - getTop(div_obj);
    var newOy = oy;
    if ( clientHeight < oy + divHeight ) {
        newOy = clientHeight - divHeight
    }
    repositionTo(div_obj, null, newOy);//internet explorer keeps vertical distance and must be reset even if there is no overflow, because there could be overflow of the same div before
}

function ourlib_delayed () {
    if (typeof(g_timeout) != 'undefined' && g_timeout) clearTimeout(g_timeout)
    lwr(html, 'ourDiv')
    if (our) {
        showObject(our)
        dontOverflow('ourDiv');
    }
}

// this function should replace ourlib() one day, when we refactor the JS code
function showTextTip(p_html, ofs_x, ofs_y) {
    if (!$('#ourDiv').length) return;
    $('#ourDiv').html(p_html);

    if (!ofs_x) ofs_x = 13;
    if (!ofs_y) ofs_y = 10;

    $('#ourDiv').css({left: ox + ofs_x, top: oy + ofs_y});
    $('#ourDiv').show();

    $('#ourDiv').css("visibility", "");
}

// this function should replace nd() one day, when we refactor the JS code
function hideTextTip() {
    if (!$('#ourDiv').length) return;
    $('#ourDiv').css("visibility", "hidden");
    $('#ourDiv').hide();
}

function ourlib (p_html, ofs_x, ofs_y) {
    our = getObject("ourDiv");

    // yet if you don't have it - go away
    if (typeof(our) == 'undefined') return;

    if (ofs_x) ooffsetx = ofs_x; else ooffsetx=13;
    if (ofs_y) ooffsety = ofs_y; else ooffsety=10;

    repositionTo(our, ox, oy)
    dontOverflow("ourDiv");
    html=p_html
    if (typeof(g_timeout) != 'undefined' && g_timeout) clearTimeout(g_timeout)
    g_timeout=setTimeout('ourlib_delayed()', delay)
    g_on=true
}

function nd () {
	if (typeof(g_timeout) != 'undefined' && g_timeout) clearTimeout(g_timeout)
	if (our) {
		hideObject(our)
		delete our
	}
	g_on=false
	return true
}

function getObject(obj_id) {
    var obj;

    if(ie) {
        try {
        obj = eval(obj_id+'.style');
        } catch (e) { obj=document.getElementById(obj_id); };
    }
    if(ns6) obj=document.getElementById(obj_id);

    if(opera && typeof(obj)=="undefined" && $('#'+obj_id).length) {
        obj=$('#'+obj_id);
    }

    return obj;
}

function getObj(obj_id) {
    var obj;
    if(ie) {
        try {
        obj = eval('document.all.'+obj_id);
        } catch (e) { return getObject(obj_id); };
    }
    if(ns6) obj=document.getElementById(obj_id);
    return obj;
}

function hideObject(obj, no_display){
  if (typeof(obj) == 'undefined' || obj == null ) return false;
  if (ie) { obj.visibility="hidden"; if (!no_display) obj.display = 'none'; }
  else if (opera && obj.jquery) { obj.hide(); obj.css("visibility", "hidden"); }
  else if (ns6||DOM2) { obj.style.visibility="hidden"; if (!no_display) obj.style.display = 'none'; }
}

function showObject(obj, no_display, d_style){
  if (typeof(obj) == 'undefined' || obj == null ) return false;
  if (!d_style) d_style = 'block';
   if (ie) { obj.visibility="visible"; if (!no_display) obj.display = d_style; }
  else if (opera && obj.jquery) { obj.show(); obj.css("visibility", "visible"); }
  else if (ns6||DOM2) { obj.style.visibility="visible"; if (!no_display) obj.style.display = d_style; }
}

function repositionTo(obj,xL,yL){
    if (typeof(obj) == 'undefined') return;

    if(ns6){
        if ( xL != null )
            obj.style.left = xL + "px";
        if ( yL != null )
            obj.style.top = yL + "px";
    } else if (ie) {
        if ( xL != null )
		obj.left = xL + "px";
        if ( yL != null )
		obj.top = yL + "px";
    }
}

function placeLayer(){
	var placeX, placeY
	var winoffset = 0;
	if ( ie ) {
		if ( !document.body ) {
			return;
		}
		winoffset=document.body.scrollLeft;
	} else {
		winoffset=window.pageXOffset;
	}

	if (document.documentElement && document.documentElement.clientWidth) {
		iwidth=document.documentElement.clientWidth
	} else if (document.body && document.body.clientWidth) {
		iwidth=document.body.clientWidth
	}

	if (ns6) iwidth=outerWidth

	placeX=ox+ooffsetx
	if((eval(placeX)+ eval(owidth))>(winoffset + iwidth)){
		placeX=iwidth + winoffset - owidth
		if(placeX<0) placeX=0
	}

	var scrolloffset=(ie4)? document.body.scrollTop : window.pageYOffset
	placeY=oy + ooffsety
	if (g_on) {
		repositionTo(our, placeX, placeY)
		dontOverflow('ourDiv');
	}
}

function mouseMove(e){
	if (ns6){
		ox=e.pageX
		oy=e.pageY
	}

	if (ie4) {
		ox=event.x
		oy=event.y
	}

	if (ie && !ie8) {
		ox=event.x+document.documentElement.scrollLeft
		oy=event.y+document.documentElement.scrollTop
	}
	placeLayer()
}

function hide_row(id) {
	var obj;
	var i = 0;
  
	while ( typeof(obj = getObject(id+i)) != 'undefined' && obj ) {
		hideObject(getObject(id+i));
		i++;
	}
}

function show_row(id) {
	var obj;
	var i = 0;
  
	while ( typeof(obj = getObject(id+i)) != 'undefined' && obj ) {
		showObject(getObject(id+i), false, ie?'block':'table-row');
		i++;
	}
}

function getTop(element) {
    if (typeof(element) == 'undefined') return 0;

    var valueT = 0;
    do {
      valueT += element.offsetTop  || 0;
      element = element.offsetParent;
    } while (element);
	return valueT;
}


function getBottom(element) {
    if (typeof(element) == 'undefined') return 0;

    var valueB = getTop(element);
    return valueB + element.clientHeight;
}


function push_anchor_to_id (id, anchor) {
	if ( typeof(hint_anchors) != 'undefined' ) {
		for ( var i = 0; i < hint_anchors.length; i++ ) {
			if ( id == hint_anchors[i].id) {
				if ( typeof(hint_anchors[i].anchors) == 'undefined' ) {
					hint_anchors[i].anchors = new Array();
				}
			
				hint_anchors[i].anchors.push(anchor);
			}
		}
	}	
}

function q_visible (element) {	
	var max_loop_cnt = 100;
	var loop_cnt = 0;
    while ( element.parentNode != null) {
	  loop_cnt ++;
	  if ( element.style.visibility == 'hidden' || loop_cnt > max_loop_cnt) {
		return false;
	  }	 	
      element = element.parentNode;
	}

	return true;	
}

function repositionHints() {
	if ( typeof(hint_anchors) != 'undefined' ) {
		var maxBottom = 0;
		for ( var i = 0; i < hint_anchors.length; i++ ) {
		    var el = getObject(hint_anchors[i].id);
			
			if ( !el ) {//if element is not found, skip iteration
				continue;
			}
			
			var anchor = undefined;
			if ( typeof(hint_anchors[i].anchors) != 'undefined' ) { //if elements is attached to more than one anchors, position the element next to the virst visible anchor
				for ( var j = 0; j < hint_anchors[i].anchors.length; j ++) {
					var tmp_anchor = getObj((hint_anchors[i].anchors)[j]);
					if ( typeof(tmp_anchor) != 'undefined' && tmp_anchor != null )
					  if ( q_visible(tmp_anchor) ) {
						anchor = tmp_anchor;
						break;
  					  }
				}
			} else {
				anchor = getObj(hint_anchors[i].id + '_anchor');
			}

			var depend_offset = 0;
			if ( typeof(hint_anchors[i].depend_on) != 'undefined' ) {
				var depend_on_obj = getObj(hint_anchors[i].depend_on);
				if ( typeof(depend_on_obj) != 'undefined' && depend_on_obj != null ) {
					depend_offset = getBottom(depend_on_obj);				
				}
			} else 	if ( typeof(hint_anchors[i].depend_on_array) != 'undefined' ) {
				for ( var j = 0; j < hint_anchors[i].depend_on_array.length; j ++) {
					var tmp_depend = getObj((hint_anchors[i].depend_on_array)[j]);
					if ( typeof(tmp_depend) != 'undefined' && tmp_depend != null )
					  if ( q_visible(tmp_depend) ) {
						depend_offset = getBottom(tmp_depend);				
						break;
  					  }
				}				
			}

			var elObj = getObj(hint_anchors[i].id);
			
			if ( typeof(anchor) == 'undefined' ) {
				hideObject(el);
			} else {
				var pos = Math.max(getTop(anchor) + hint_anchors[i].offset, depend_offset);
				var offset = pos + hint_anchors[i].offset;
				if ( hint_anchors[i].show_on_reposition ) {
					showObject(el);
				}
				repositionTo(el, null, offset);
				if ( q_visible(elObj) ) {//maxbottom must be calculated only if element is visible
					maxBottom = Math.max(maxBottom, getBottom(elObj));
				}
			}	
		}

		var rightColumn = getObj('left_right_r');
		var centerColumn = getObj('left_right_l');		
		if ( rightColumn != null && centerColumn != null) {
			if ( getBottom(centerColumn) < maxBottom ) {
				rightColumn.style.height = (maxBottom - getTop(rightColumn)) + "px";
			}
		}
	}
}

