var Zoomer = new Zoomer();

function Zoomer() {

    $(document).ready(function() {
        $('body').append('<div id="tipDiv" style="position:absolute; visibility:hidden; z-index:100"></div>');
        Zoomer.initTip();
        Zoomer.tooltip.css('left', '0px');
        Zoomer.tooltip.css('top', '0px');
    });

    this.dom = (document.getElementById) ? true : false;
    this.ns5 = (!document.all && this.dom || window.opera) ? true: false;
    this.ie5 = ((navigator.userAgent.indexOf("MSIE")>-1) && this.dom) ? true : false;
    this.ie4 = (document.all && !this.dom) ? true : false;
    this.nodyn = (!this.ns5 && !this.ie4 && !this.ie5 && !this.dom) ? true : false;

    this.tipFollowMouse = true;
    this.offX = 20;
    this.offY = -50;

    this.tipBorderColor = "#d9d9d9";
    this.tipBorderWidth = 0;
    this.tipPadding = 5;
    this.startStr = '<img src="';
    this.midStr = '" border="0">';

    this.tooltip = null;
    this.initTip = function() {
        if(this.nodyn) { return; }
        this.tooltip = $('#tipDiv');
        this.tooltip.css({
            'font-family' : this.tipFontFamily,
            'font-size' : this.tipFontSize,
            'color' : this.tipFontColor,
            'background-color' : this.tipBgColor,
            'border-color' : this.tipBorderColor,
            'border-width' : this.tipBorderWidth + "px",
            'padding' : this.tipPadding + "px",
            'border-style' : this.tipBorderStyle,
            'border-radius' : '5px',
            'box-shadow' : '0px 0px 5px #999999'
        });

        if(this.tooltip && this.tipFollowMouse) {
            document.onmousemove = this.trackMouse;
        }
    };


    this.t1 = 0;
    this.t2 = 0;
    this.tipOn = false;
    this.showImage = function(evt, img) {
        if(!this.tooltip) { return; }
        if(this.t1) { clearTimeout(this.t1); }
        if(this.t2) { clearTimeout(this.t2); }
        this.tipOn = true;
        this.tooltip.html(this.startStr + img + this.midStr);
        if(!this.tipFollowMouse) {
            this.positionTip(evt);
        }
        else {
            this.t1 = setTimeout("Zoomer.tooltip.css('visibility', 'visible')", 100);
        }
    };

    this.mouseX = 0;
    this.mouseY = 0;
    this.trackMouse = function(evt) {
        standardbody = (document.compatMode == "CSS1Compat") ? document.documentElement : document.body;
        Zoomer.mouseX = (Zoomer.ns5) ? evt.pageX : window.event.clientX + standardbody.scrollLeft;
        Zoomer.mouseY = (Zoomer.ns5) ? evt.pageY : window.event.clientY + standardbody.scrollTop;
        if(Zoomer.tipOn) {
            Zoomer.positionTip(evt);
        }
        delete standardbody;
    };

    this.positionTip = function(evt) {
        if(!this.tipFollowMouse) {
            standardbody = (document.compatMode == "CSS1Compat") ? document.documentElement : document.body
            this.mouseX = (this.ns5) ? evt.pageX : window.event.clientX + standardbody.scrollLeft;
            this.mouseY = (this.ns5) ? evt.pageY : window.event.clientY + standardbody.scrollTop;
        }

        var tpWd = (this.ie4 || this.ie5) ? this.tooltip.clientWidth : this.tooltip.offsetWidth;
        var tpHt = (this.ie4 || this.ie5) ? this.tooltip.clientHeight : this.tooltip.offsetHeight;

        var winWd = (this.ns5) ? (window.innerWidth-20) + window.pageXOffset : standardbody.clientWidth + standardbody.scrollLeft;
        var winHt = (this.ns5) ? (window.innerHeight-20) + window.pageYOffset : standardbody.clientHeight + standardbody.scrollTop;

        if((this.mouseX + this.offX + tpWd) > winWd) {
            this.tooltip.css('left', ((this.mouseX - (tpWd + this.offX)) + "px"));
        }
        else {
            this.tooltip.css('left', ((this.mouseX + this.offX) + "px"));
        }

        if((this.mouseY + this.offY + tpHt) > winHt) {
            this.tooltip.css('top', ((winHt - (tpHt + this.offY)) + "px"));
        }
        else {
            this.tooltip.css('top', ((this.mouseY + this.offY) + "px"));
        }
        if(!this.tipFollowMouse) {
            this.t1 = setTimeout("Zoomer.tooltip.css('visibility', 'visible')", 100);
        }
        delete standardbody;
    };

    this.hidenImage = function() {
        if(!this.tooltip) { return }
        this.t2 = setTimeout("Zoomer.tooltip.css('visibility', 'hidden')", 100);
        this.tipOn = false;
    };

}


