﻿var detect = navigator.userAgent.toLowerCase();
var OS, browser, version, total, thestring;

function checkIt(string) {
    place = detect.indexOf(string) + 1;
    thestring = string;
    if (place) return true;
    else return false;
}


var ie = document.all;
var ns6 = document.getElementById && !document.all;
var opera = checkIt('opera');
var crossobj = false;


if (ns6) {
    function emulateEventHandlers(eventNames) {
        for (var i = 0; i < eventNames.length; i++) {
            document.addEventListener(eventNames[i], function(e) { window.event = e; }, true); // using capture
        }
    }

    emulateEventHandlers(["click", "mousemove"]);
}


function ietruebody() {
    return (document.compatMode != "BackCompat") ? document.documentElement : document.body
}

function enlarge(which, e, position, imgwidth) {
    
    imgwidth = imgwidth ? imgwidth : getFloatinWidth();
    imgheight = 0;

    which = '<img src="' + which + '" />';

    crossobj = document.getElementById ? document.getElementById("showimage") : document.all.showimage

    var pos_X = 0;
    var pos_Y = 0;

    if (!e) e = window.event;
    if (e) {
        if (typeof (e.pageX) == 'number') {
            pos_X = e.pageX;
            pos_Y = e.pageY;
        }
        else if (typeof (e.clientX) == 'number') {
            pos_X = e.clientX;
            pos_Y = e.clientY;
            if (document.body && (document.body.scrollTop || document.body.scrollLeft) && !(window.opera || window.debug || navigator.vendor == 'KDE')) {
                pos_X += document.body.scrollLeft;
                pos_Y += document.body.scrollTop;
            }
            else if (document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft) && !(window.opera || window.debug || navigator.vendor == 'KDE')) {
                pos_X += document.documentElement.scrollLeft;
                pos_Y += document.documentElement.scrollTop;
            }
        }
    }

    var horzpos = ns6 ? pageXOffset + e.clientX : ietruebody().scrollLeft + event.clientX
    var vertpos = ns6 ? pageYOffset + e.clientY : ietruebody().scrollTop + event.clientY

    var scroll_X = 0;
    var scroll_Y = 0;

    if (document.body && (document.body.scrollTop || document.body.scrollLeft) && !(window.debug || navigator.vendor == 'KDE')) {
        scroll_X = document.body.scrollLeft;
        scroll_Y = document.body.scrollTop;
    }
    else if (document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft) && !(window.debug || navigator.vendor == 'KDE')) {
        scroll_X = document.documentElement.scrollLeft;
        scroll_Y = document.documentElement.scrollTop;
    }

    //		var pos_X = horzpos + 15;
    //		var pos_Y = vertpos + 15;
    var win_size_X = 0;
    var win_size_Y = 0;

    var win_size_X = 0;
    var win_size_Y = 0;

    if (window.innerWidth && window.innerHeight) {
        win_size_X = window.innerWidth;
        win_size_Y = window.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientWidth && document.documentElement.clientHeight) {
        win_size_X = document.documentElement.clientWidth;
        win_size_Y = document.documentElement.clientHeight;
    }
    else if (document.body && document.body.clientWidth && document.body.clientHeight) {
        win_size_X = document.body.clientWidth;
        win_size_Y = document.body.clientHeight;
    }

    if (!imgheight) imgheight = 0;
    if (!imgwidth) imgwidth = 0;

    pos_X += 15;
    pos_Y += 15;

    if (crossobj.offsetWidth && crossobj.offsetHeight) {
        if (pos_X - scroll_X + crossobj.offsetWidth + 5 > win_size_X) pos_X -= (crossobj.offsetWidth + 15);
        if (pos_Y - scroll_Y + crossobj.offsetHeight + 5 > win_size_Y) pos_Y -= (crossobj.offsetHeight + 15);
    }

    //		window.status = 'x:'+horzpos+' y:'+vertpos+' pos_x:'+pos_X+' pos_y:'+pos_Y+' height:'+crossobj.offsetHeight+' width:'+crossobj.offsetWidth+' offset_x:'+scroll_X+' offset_y:'+scroll_Y;

    crossobj.style.left = pos_X + "px";
    crossobj.style.top = pos_Y + "px";
    //		crossobj.style.width=imgwidth+"px";

    crossobj.innerHTML = '<div class=floatin>' + which + '</div>';
    crossobj.style.visibility = "visible";
    return false;
}

function closepreview() {
    if (!crossobj) crossobj = document.getElementById ? document.getElementById("showimage") : document.all.showimage;
    crossobj.style.visibility = "hidden";
}


function getFloatinWidth() {
    // find width from css

    if (opera) return 300;
    ww = false;

    for (ss = 0; ss < document.styleSheets.length; ss++) {
        a = document.styleSheets[ss];

        if (ie) css = a.rules;
        else css = a.cssRules;

        for (k = 0; k < css.length; k++) {
            if (css[k].selectorText == ".floatin") ww = css[k].style.width.replace(/px/, "");
        }
    }

    if (ww) return ww;
    else return 300;
}
