﻿function getStyle(el, property)
{
    var value = null;
    var dv = document.defaultView;
    if (el.style[property])
    {
	    value = el.style[property];
    }
    else if (el.currentStyle && el.currentStyle[property])
    {
	    value = el.currentStyle[property];
    }
    else if ( dv && dv.getComputedStyle )
    {
	    var converted = "";
	    for(var i = 0, len = property.length;i < len; ++i)
	    {
	        converted = (property.charAt(i) == property.charAt(i).toUpperCase()) ? converted + '-' + property.charAt(i).toLowerCase() : converted + property.charAt(i);
        }
        if (dv.getComputedStyle(el, "") && dv.getComputedStyle(el, "").getPropertyValue(converted))
        {
	        value = dv.getComputedStyle(el, "").getPropertyValue(converted);
        }
    }    
    return value; 
};

function FindPos(el)
{
    if (el.parentNode === null)
    {
        return null;
    }
    var parent = null, box, pos = { x: 0, y: 0 };

    if (el.getBoundingClientRect)
    {
	    box = el.getBoundingClientRect();
	    var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
	    var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
	    pos.x = box.left + scrollLeft-2;
	    pos.y = box.top + scrollTop-2;
	    return pos;
    }
    else if (document.getBoxObjectFor)
    {
	    box = document.getBoxObjectFor(el);
	    var borderLeft = parseInt(getStyle(el, "borderLeftWidth"));
	    var borderTop = parseInt(getStyle(el, "borderTopWidth"));
	    pos.x = box.x - borderLeft;
	    pos.y = box.y - borderTop;
    }
    else
    {
	    pos.x = el.offsetLeft;
	    pos.y = el.offsetTop;
	    parent = el.offsetParent;
	    if (parent != el)
	    {
		    while (parent)
		    {
			    pos.x += parent.offsetLeft;
			    pos.y += parent.offsetTop;
			    parent = parent.offsetParent;
		    }
	    }
	    if (this.isOpera || this.isSafari && this.getStyle(el, "position") == "absolute" )
	    {
			    pos.x -= document.body.offsetLeft;
			    pos.y -= document.body.offsetTop;
	    }
    }
	parent = (el.parentNode) ? el.parentNode : null;
	while (parent && parent.tagName.toLowerCase() != "body" && parent.tagName.toLowerCase() != "html")
	{
	    pos.x -= parent.scrollLeft;
	    if(!this.isOpera)
	    {
    	    pos.y -= parent.scrollTop; 
	    }
	    parent = (parent.parentNode) ? parent.parentNode : null;
    }
    return pos;
};

function ShowCode(obj)
{
	var x = FindPos(obj);
	var f = document.getElementById('code');

	f.style.left = x.x + "px";
	f.style.top = x.y + "px";
	f.style.visibility = "visible";
	try
	{
	    obj.focus();
		f.getElementsByTagName("img").item(0).src = obj.src + "&wm=1&size=" + f.getAttribute("scale");
	}
	catch(e){}
	f.focus();
}

function Hide()
{
	document.getElementById('code').style.visibility = 'hidden';
}

function InitCode(scale)
{
	var main = document.createElement("div");
	main.setAttribute('id','code');
	main.setAttribute('scale', 6/*scale*/);
	main.style.visibility = 'hidden';
	main.style.position = 'absolute';
	main.style.left = '0px';
	main.style.top = '0px';
	main.style.border = '1px solid #ccc';
	main.style.width = '177px';
	main.style.padding = '3px';
	main.style.backgroundColor = '#eee';
	main.style.cursor = 'url(http://www.imatrix.lt/images/blank.cur), crosshair';
	main.onmouseout = function() { Hide(); return false; }
	main.onclick = function() { Hide(); return false; }
	
	var img = document.createElement("img");
	img.style.float = 'left';
	img.style.border = '1px solid #ccc';
	img.style.cursor = 'url(http://www.imatrix.lt/images/blank.cur), crosshair';
	img.onmouseout = function() { Hide(); return false; }
//	img.onclick = function() { Hide(); return false; }
	main.appendChild(img);

	var txt = document.createElement("div");
	txt.style.float = 'left';
	txt.style.textAlign = 'center';
	txt.style.fontSize = '14px';
	txt.style.width = '100%';
	txt.innerHTML = 'Let iMatrix open this article on your iPhone. Details on www.iMatrix.LT';
	main.appendChild(txt);

	document.getElementsByTagName("body").item(0).appendChild(main);
}
