﻿var __PNGAllowed = true;

var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
if (browser=="Microsoft Internet Explorer"){
    __PNGAllowed = false;
}


// The Following function gets the Left of an object
function getModelLeft(obj){
    return obj.offsetLeft + (obj.offsetParent ? getModelLeft(obj.offsetParent) : obj.x ? obj.x : 0);
}
// The Following function gets the Top of an object
function getModelTop(obj){
    return (obj.offsetParent ? obj.offsetTop + getModelTop(obj.offsetParent) : obj.y ? obj.y : 0);    
}
// The Following function gets the Right and width of an object
function getModelRight(obj){
	var leftpos = getModelLeft(obj);
	return {right:obj.offsetWidth + leftpos, width:obj.offsetWidth };
}
// The Following function gets the Bottom and height of an object
function getModelBottom(obj){
	var bottompos = getModelTop(obj);
	return {bottom:obj.offsetHeight + bottompos, height:obj.offsetHeight };	
}

// The Following function gets the Bounds of an object
function getModelBounds(obj){    
	var LeftPos = getModelLeft(obj);
	var TopPos = getModelTop(obj);
	var RightPos = getModelRight(obj).right;
	var BottomPos = getModelBottom(obj).bottom;
	var Width = getModelRight(obj).width;
	var Height = getModelBottom(obj).height;	
	return{left: LeftPos, top: TopPos, right: RightPos, bottom: BottomPos, width: Width, height: Height};
}

var scrollModeltimeid = null;
var modalList = new Array();
function Modal(compid, focuscompid, parentid, bgcolor, bgopac){    
    var chcomp = this.GetElem(compid);
    var focuscomp = this.GetElem(focuscompid);
    this.parent = this.GetElem(parentid);
    this.focuscomp = focuscomp;
    this.comp = chcomp;
    this.comp.style.visibility = "hidden";
    this.comp.style.display = "none";
    this.comp.style.position = "absolute";    
    this.bgcolor = "#ffffff";
    this.bgopac = 35;
    this.shadowDistance = 0;
    this.shadowopac = 100;
    if(bgcolor!=null && bgcolor!=undefined){
        this.bgcolor = bgcolor;
    }
    if(bgopac!=null && bgopac!=undefined){
        this.bgopac = bgopac;
    }    
    this.currpos = 0;
    this.bgDiv = this.backgroundDiv();    
    this.shadowDiv = this.shadeDiv();    
    this.topper = this.Topper();
    this.allowposition = true;    
    modalList[modalList.length] = this;    
    MoveAllModals();    
    this.allowposition = false;    
    this.Hide();    
    return this;
}

Modal.prototype = {
    WindowInnerDimension: function(){
        var WinInnerWid = 630;
        var WinInnerHei = 460;
        if( typeof( window.innerWidth ) == 'number' ) {    
        WinInnerWid = window.innerWidth;    
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {    
        WinInnerWid = document.documentElement.clientWidth;    
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {    
        WinInnerWid = document.body.clientWidth;    
        }  
        if( typeof( window.innerWidth ) == 'number' ) {    
        WinInnerHei = window.innerHeight;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {    
        WinInnerHei = document.documentElement.clientHeight;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {    
        WinInnerHei = document.body.clientHeight;
        }  
        return {width:WinInnerWid, height:WinInnerHei};
    },
    WindowScroll: function(){
        var lft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
        var tp = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
        return {left:lft, top:tp}
    },
    Topper: function(){        
        var newcompid = this.comp.id + "_Topper";
        if (!this.GetElem(newcompid)){            
            //this.comp.style.position = "absolute";
            var newTopper = null;
            if(__PNGAllowed==false){                
                newTopper = document.createElement("iframe");
            }else{
                newTopper = document.createElement("div");
            }
            newTopper.id = newcompid;
            newTopper.frameborder = "0";
            newTopper.style.position = "absolute";
            newTopper.style.borderColor = "#ffffff";
            newTopper.style.borderWidth = "0px";
            newTopper.style.borderStyle = "none";
            newTopper.style.filter = "alpha(opacity=1)";
            newTopper.style.MozOpacity = "0.01";
            newTopper.style.KhtmlOpctiy = "0.01";
            newTopper.style.zIndex = "10100002";
            newTopper.style.left = "0px";
            newTopper.style.top = "0px";            
            newTopper.style.visibility = "hidden";
            var Bounds;
            if(this.parent){
                Bounds = getModelBounds(this.parent);
            }else{
                Bounds = getModelBounds(document.body);
            }
            newTopper.style.width = Bounds.width + "px";
            newTopper.style.height = Bounds.height + "px";
            this.comp.style.zIndex = "10100003";
            this.comp.parentNode.insertBefore(newTopper,this.comp);
        }
        return this.GetElem(newcompid);
    },
    backgroundDiv: function(){
        var newcompid = this.comp.id + "_backgroundDiv";
        if (!this.GetElem(newcompid)){
            var newTopper = document.createElement("div");
            newTopper.id = newcompid;
            newTopper.style.position = "absolute";
            if(!__PNGAllowed){
                newTopper.style.backgroundColor = this.bgcolor;
                newTopper.style.filter = "alpha(opacity=" + parseFloat(this.bgopac) + ")";
                newTopper.style.MozOpacity = parseFloat(this.bgopac)/100;
                newTopper.style.KhtmlOpctiy = parseFloat(this.bgopac)/100;
            }else{
                newTopper.style.backgroundImage = "url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAA1JREFUGFdjYGBgaAAAAIUAgbVRNccAAAAASUVORK5CYII=)"
            }
            newTopper.style.zIndex = "10100000";
            newTopper.style.left = "0px";
            newTopper.style.top = "0px";
            newTopper.style.visibility = "hidden";
            var Bounds;
            if(this.parent){
                Bounds = getModelBounds(this.parent);
            }else{
                Bounds = getModelBounds(document.body);
            }
            newTopper.style.width = Bounds.width + "px";
            newTopper.style.height = Bounds.height + "px";
            this.comp.parentNode.insertBefore(newTopper,this.comp);
        }
        return this.GetElem(newcompid);
    },
    shadeDiv: function(){
        var newcompid = this.comp.id + "_shadeDiv";
        if (!this.GetElem(newcompid)){
            var newTopper = document.createElement("div");
            newTopper.id = newcompid;
            newTopper.style.position = "absolute";            
            newTopper.style.backgroundColor = "#000000"
            newTopper.style.filter = "alpha(opacity=" + this.shadowopac + ")";
            newTopper.style.MozOpacity = parseFloat(this.shadowopac)/100;
            newTopper.style.KhtmlOpctiy = parseFloat(this.shadowopac)/100;
            newTopper.style.zIndex = "10100001";
            newTopper.style.visibility = "hidden";
            var Bounds;
            Bounds = getModelBounds(this.comp);
            newTopper.style.width = Bounds.width + "px";
            newTopper.style.height = Bounds.height + "px";
            newTopper.style.left = (Bounds.left + this.shadowDistance) + "px";
            newTopper.style.top = (Bounds.top + this.shadowDistance) + "px";
            this.comp.parentNode.insertBefore(newTopper,this.comp);
        }
        return this.GetElem(newcompid);
    },
    Show: function(){
        this.allowposition = true;
        this.Move();
        this.comp.style.visibility = "visible";
        this.comp.style.display = "block";
        this.topper.style.visibility = "visible";
        this.bgDiv.style.visibility = "visible";
        this.shadowDiv.style.visibility = "visible";        
        try{
            if(this.focuscomp){
                this.focuscomp.focus();
            }
        }catch(e){
        }
    },
    Hide: function(){
        this.comp.style.visibility = "hidden";
        this.comp.style.display = "none";
        this.topper.style.visibility = "hidden";
        this.bgDiv.style.visibility = "hidden";
        this.shadowDiv.style.visibility = "hidden";
        this.allowposition = false;
    },
    GetElem: function(ElementId){
        if(typeof(ElementId)=="string"){
            return document.getElementById(ElementId);
        }else{
            return ElementId;
        }
    },
    Move: function(){
        if(this.allowposition==true){try{
            this.comp.Bounds = getModelBounds(this.comp);
            var windowDim = this.WindowInnerDimension();
            var windowSc = this.WindowScroll();
            var Bounds;
            if(this.parent){
                Bounds = getModelBounds(this.parent);
            }else{
                Bounds = getModelBounds(document.body);
            }
            this.topper.style.width = Bounds.width + "px";
            this.topper.style.height = Bounds.height + "px";
            this.bgDiv.style.width = Bounds.width + "px";
            this.bgDiv.style.height = Bounds.height + "px";
            var CurrLeft = ((windowDim.width/2) - (this.comp.Bounds.width/2) + windowSc.left);
            var CurrTop = ((windowDim.height/2) - (this.comp.Bounds.height/2) + windowSc.top);
            this.comp.style.left = CurrLeft + "px";
            this.comp.style.top = CurrTop + "px";
            this.shadowDiv.style.width = this.comp.Bounds.width;
            this.shadowDiv.style.height = this.comp.Bounds.height;
            this.shadowDiv.style.left = (CurrLeft + this.shadowDistance) + "px";
            this.shadowDiv.style.top = (CurrTop + this.shadowDistance) + "px";
            }catch(e){alert(e);}
        }
    }
}

function MoveAllModals(){
    clearTimeout(scrollModeltimeid);
    for(i=0;i<modalList.length;i++){
        modalList[i].Move();
    }
    scrollModeltimeid = setTimeout("MoveAllModals();",10);
}