jQuery(window).load(function () {
    var $ = jQuery;

    var TEH_EVENT = new Array();
    TEH_EVENT['closetimer'] = 0;
    TEH_EVENT['contentSet'] = 0;

    var mywidth = 220;
    var myheight = 271;

    function TEH_EVENT_CreateIframe() {
        var httpOrSSL = (("https:" == document.location.protocol) ? "https" : "http");
        var ifr = $('<iframe src="' + httpOrSSL + '://www.tryggehandel.se/verifybutik.php?id=265" />');
        ifr.css({
            display: 'none',
            position: 'absolute',
            height: myheight + 'px',
            width: mywidth + 'px',
            border: 'none',
            zIndex: '9999'
        })
	    .attr('frameBorder', '0')
	    .attr('scrolling', 'no')
	    .attr('allowTransparency', 'true')
	    .mouseover(TEH_EVENT_Show)
	    .mouseout(TEH_EVENT_HideTime);

        return ifr[0];
    }
    function findPosX(obj) {
        var curleft = 0;
        if (obj.offsetParent) {
            while (obj.offsetParent) {
                curleft += obj.offsetLeft;
                obj = obj.offsetParent;
            }
        }
        else if (obj.x)
            curleft += obj.x;

        return curleft;
    }
    function findPosY(obj) {
        var curtop = 0;
        if (obj.offsetParent) {
            while (obj.offsetParent) {
                curtop += obj.offsetTop;
                obj = obj.offsetParent;
            }
        }
        else if (obj.y)
            curtop += obj.y;

        return curtop;
    }
    var llogo = $('#right_column .icon')[0];
    function TEH_EVENT_Show(event) {
        TEH_EVENT_CancelClose();
        if (!TEH_EVENT['iframe']) {
            TEH_EVENT['iframe'] = TEH_EVENT_CreateIframe();
            document.body.appendChild(TEH_EVENT['iframe']);
        }
        if (TEH_EVENT['iframe'].style.display == 'none') {
            var yy = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
            var px = GetWndCliSiz("Width");
            var py = GetWndCliSiz("Height");
            var tx = llogo.width;
            var ty = llogo.height;
            var lx = findPosX(llogo);
            var ly = findPosY(llogo);
            var totalwidth = tx + lx + mywidth;
            var totalheight = ly + ty + myheight + yy;
            if (totalwidth > px) {
                var newposx = lx - mywidth + 20;
                TEH_EVENT['iframe'].style.left = newposx + 'px';
            }
            else {
                TEH_EVENT['iframe'].style.left = lx + tx + 'px';
            }

            if (document.addEventListener) {
                yDiff = ly - window.scrollY;
                TEH_EVENT['iframe'].style.top = self.pageYOffset + yDiff + 'px';
            }
            else {
                yDiff = ly - document.body.parentNode.scrollTop
                TEH_EVENT['iframe'].style.top = document.documentElement.scrollTop + yDiff + 'px';
            }
        }
        TEH_EVENT['iframe'].style.display = 'inline';
    }
    function TEH_EVENT_Hide() {
        if (TEH_EVENT['iframe']) TEH_EVENT['iframe'].style.display = 'none';
    }
    function TEH_EVENT_ShowTime() {
        TEH_EVENT['opentimer'] = window.setTimeout(TEH_EVENT_Show, 300);
    }
    function TEH_EVENT_HideTime() {
        if (TEH_EVENT['iframe']) {
            TEH_EVENT['closetimer'] = window.setTimeout(TEH_EVENT_Hide, 500);
        }
        else {
            window.clearTimeout(TEH_EVENT['opentimer']);
        }
    }
    function TEH_EVENT_CancelClose() {
        if (TEH_EVENT['closetimer']) {
            window.clearTimeout(TEH_EVENT['closetimer']);
            TEH_EVENT['closetimer'] = null;
        }
    }
    function GetWndCliSiz(s) {
        var db, y = window["inner" + s], sC = "client" + s, sN = "number";
        if (typeof y == sN) {
            var y2;
            return (
			((db = document.body) && typeof (y2 = db[sC]) == sN && y2 && y2 <= y) ? y2
			: ((db = document.documentElement) && typeof (y2 = db[sC]) == sN && y2 && y2 <= y) ? y2
			: y
		);
        }
        return (
		((db = document.documentElement) && (y = db[sC])) ? y
		: document.body[sC]
	);
    }
    $(llogo)
        .mouseover(TEH_EVENT_Show)
        .mouseout(TEH_EVENT_HideTime);
});
