// Copyright QT Luong 2005 - All rights reserved
// code by Web-dev professional from Chernivtsy, Chernivetskaya, Ukraine


ISIE = document.all ? true : false;
ISDOM = document.getElementById ? true : false;
ISNS4 = document.layers ? true : false;

// ID of element, which would be displayed as tooltip
var tooltipId = "tooltip";

// tooltip element
var tooltip = null;

// array that contains key-values for text
var tooltipTextMap = [];

// element, for each tooltip is locked
var currentlyLockedElement = null;

var currentlyUnderMouse = null;

// element, for each tooltip is showing now
currentElement = null;

function escapeText(text){
    return text.replace(/\n/g, '').replace(/[\"]/g, "\\\"");
}

function getElement(wnd, styleId) {
    var element;

    if (ISDOM) {
        element = wnd.document.getElementById(styleId);
    } else if (ISIE) {
        element = wnd.document.all[styleId];
    } else if (ISNS4) {
        element = wnd[styleId];
    }
    return element;
}

function isDef(){
	for(var i=0; i<arguments.length; ++i){
		if(typeof(arguments[i])=='undefined')
			return false;
	}
	return true;
}

// calculates elements offset from the left
function elOffsetX(e){
	var x = 0;
	
	while (e) {
		if (isDef(e.offsetLeft))
			x += e.offsetLeft;
		e = isDef(e.offsetParent) ? e.offsetParent : null;
	}
	
	return x;
}

// calculates elements offset from the top
function elOffsetY(e){
	var y = 0;
	
	while (e) {
		if (isDef(e.offsetTop))
			y += e.offsetTop;
			
		e = isDef(e.offsetParent) ? e.offsetParent : null;
	}
	
	return y;
}

// shows tooltip near 'el' element
function showTooltip(el, text){
    // do nothing if tolltip is locked to this element
	if(el == currentlyLockedElement)
		return;

	tooltip = tooltip || getElement(window, tooltipId);

	// form tooltips text
	text = getTooltipText(el, text);

	if (text == null || text == "")
		return;

	unlockTooltip();

    currentElement = el;
    currentlyUnderMouse = el;
	// set events
	el.onmouseout = function(){currentlyUnderMouse = null; window.setTimeout('unlockTooltip()', 1000);};
	lockTooltip(el);

	tooltip.innerHTML = text;

	positeTooltip(el);
}

function getTooltipText(el, text){
    // if text was sent to function - return it
	if(text != null && text != "")
		return text;

	if(el.getAttribute("tooltipType") == 'inline')
		return el.getAttribute("tooltipText");

	if(el.getAttribute("tooltipType") == 'array')
		return tooltipTextMap[el.getAttribute("tooltipKey")];

	// if no text was sent to function - try to fetch it from 'title' attribute
	if(el.title != null && el.title != "")
		return el.title;

	// if no text was sent to function and title attribute is now specified and this is image - try to get alt attribute value
	if(el.tagName == "IMG" && el.alt != null && el.alt != "")
		return el.alt;

	return null;
}

function pageWidth(){
	return window.innerWidth != null ? window.innerWidth : document.body != null ? document.body.clientWidth : 800;
}

function pageHeight(){
	return window.innerHeight != null ? window.innerHeight : document.body != null ? document.body.clientHeight : 500;
}

// position tooltip to the text
function positeTooltip(el){
	if(tooltip.clientHeight > pageHeight())
		tooltip.height = pageHeight() / 2;
		
	var w = el.clientWidth;
	var h = el.clientHeight;

	if (w == null || w == '' || w == 0)
		w = el.scrollWidth;
	
	if (h == null || h == '' || h == 0)
		h = el.scrollHeight;

	var left = elOffsetX(el) + w - 5;
	var top = elOffsetY(el) + h - 2;

	if(pageWidth() < left + tooltip.clientWidth)
		left = elOffsetX(el) - tooltip.clientWidth + 5;

    if(left < 0)
        left = 0;

    if(document.body.scrollTop + pageHeight() < top + tooltip.clientHeight){
		top = elOffsetY(el) - tooltip.clientHeight;
	}

    if(top < 0){
        top = 0;
        left += 5;
    }

    tooltip.style.left = left + "px";
	tooltip.style.top = top + "px";
}

// lock tooltip on screen
function lockTooltip(el){
	el.onclick = function(){unlockTooltip()};
	tooltip.className = "tooltipLocked";
	currentlyLockedElement = el;
}

// remove lock from tooltip and hide it
function unlockTooltip(){
	if(currentlyUnderMouse != null){
		window.setTimeout('unlockTooltip()', 1000);
		return;
	}

	hideTooltip();
	currentlyLockedElement = null;
	tooltip.className = "tooltipFloating";
}

// remove tooltip from screen
function hideTooltip(){
	tooltip.style.left = '-1000px';
	tooltip.style.top = '-1000px';
    currentElement = null;
}

// get all elements with specified classname
function getElementsByAttribute() {
	var children = document.getElementsByTagName('*') || document.all;
	var elements = new Array();

	for (var i = 0; i < children.length; i++) {
		var child = children[i];

		if(child.hasAttribute("tooltipType"))
			elements.push(child);
	}

	return elements;
}

// attach tooltips to elements
function attachTooltips(){
	var els = getElementsByAttribute();
	
	for(var i = 0; i < els.length; i++){
		var el = els[i];

	            var text = getTooltipText(el);

        	    if(text != null && text != "")
                	eval("el.onmouseover = function(){showTooltip(this, \"" + escapeText(text) + "\")}");
    }
}

function favorites_submit ()
{
  document.favorites.submit() ;
}

function license_submit ()
{
  document.license.submit() ;
}

function order_submit ()
{
  document.order.submit() ;
}


function wallpaper_submit ()
{
  document.wallpaper.submit() ;
}

function buy_submit ()
{
  document.buy.submit() ;
}


function similar_submit ()
{
  document.similar.submit() ;
}

function simplicity_submit ()
{
  document.simplicity.submit() ;
}




