﻿// -- MSIE 6.0 & 7.0

var ie7 = /MSIE 7.0/g.test(navigator.userAgent),
ie6 = /MSIE 6.0/g.test(navigator.userAgent),
docMode = document.documentMode;
var classAttr = (ie7 || ie6 || (docMode && docMode == 7)) ? "className":"class",
popupTarget = $("#resultsPopup");

var Popup = function(source,target){
    this.findIdFromClass = function(str){
        str = str.match(/[P+]+[0-9]{1,}/)[0];
        return str.split("P")[1];
    };
    this.show = function(x,y){
        document.body.appendChild(this.targetDiv);
        this.targetDiv.innerHTML = this.html;
        this.targetDiv.style.display = "block";
        this.targetDiv.style.top = (this.offsetY + y) + "px";
        this.targetDiv.style.left = (this.offsetX + x) + "px";
    };
    this.innerShow = function(){
        this.holderDiv.appendChild(this.targetDiv);
        this.targetDiv.innerHTML = this.html;
        this.targetDiv.style.display = "block";
        this.targetDiv.style.top = "0px";
        this.targetDiv.style.left = "0px";
    };
    this.hide = function(){
        this.targetDiv.style.display = "none";
    };
    this.getHtmlElem = function(elm){
        if(typeof elm.jquery == "string") {
            return elm[0];
        } else {
            return elm;
        };
    };
    this.offsetX = 0;
    this.offsetY = 0;
    this.targetDiv = this.getHtmlElem(target);
    this.sourceDiv = this.getHtmlElem(source);
    this.id = this.findIdFromClass(this.sourceDiv.getAttribute(classAttr));
    this.htmlDiv = document.getElementById("P" + this.id);
    this.html = this.htmlDiv.innerHTML;
    this.holderDiv = null;
};

window.jQuery.fn.popup = function(x,y){
    return this.each(function() {  
        this.popup = new Popup(this,popupTarget);
        this.popup.offsetX = x ? x : 0;
        this.popup.offsetY = y ? y : 0;
        $(this).hover(function(e){
            this.popup.show(e.pageX,e.pageY)
        },function(){
            this.popup.hide()
        });
    });
};

window.jQuery.fn.innerPopup = function(o){
    return this.each(function() {
        this.popup = new Popup(this,popupTarget);
        targetElem = typeof o == "string" ? $(o) : $(this).parent().parent();
        this.popup.holderDiv = this.popup.getHtmlElem(targetElem);
    	
        $(this).hover(function(e){
            this.popup.innerShow();
        },function(){
           this.popup.hide()
        });
    });
};

window.jQuery.fn.clearPopup = function(){
    return this.each(function(i){
        $(this).unbind('hover');
        if(this.popup) {
            this.popup = '';
        };
    });
};

