﻿var usercontents = document.getElementById("usercontents");
var _init = false;
var pageLoaded = false;
var _scrolltimeid = null;
var _modalArr = new Array();
function LoadingModal(compid){    
    var chcomp = this.GetElem(compid);    
    this.comp = chcomp;
    this.currpos = 0;    
    this.bgDiv = null;
    this.topper = this.Topper();
    this.Move();
    _modalArr[_modalArr.length] = this;
    if(!_init){
        _init = true;
        _positionAllModals();
    } 
    return this;
}
LoadingModal.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 newcomp = document.createElement("div");
            newcomp.id = this.comp.id + "_BackGroundDiv";            
            newcomp.style.borderStyle = "none";
            newcomp.style.position = "absolute";
            var newTopper = document.createElement("iframe");
            newTopper.id = newcompid;
            newTopper.frameborder = 0;
            newTopper.scrolling = "no";
            newTopper.style.filter = "alpha(opacity=0)";
            newTopper.style.position = "absolute";
            var contBounds = _getBounds(usercontents);
            newTopper.style.left = contBounds.left + "px";
            newTopper.style.top = contBounds.top + "px";
            newTopper.style.width = contBounds.width + "px";
            newTopper.style.height = contBounds.height + "px";
            newcomp.style.height = contBounds.height + "px";
            newcomp.style.width = contBounds.width + "px";
            newTopper.style.left = contBounds.left + "px";
            newTopper.style.top = contBounds.top + "px";
            newcomp.style.left = contBounds.left + "px";
            newcomp.style.top = contBounds.top + "px";
            newTopper.style.zIndex = "1";
            newcomp.style.zIndex = "2";
            this.comp.style.zIndex = "3";
            this.comp.parentNode.insertBefore(newTopper,this.comp);
            this.comp.parentNode.insertBefore(newcomp,this.comp);
        }
        this.bgDiv = this.GetElem(this.comp.id + "_BackGroundDiv");
        return this.GetElem(newcompid);
    },
    Show: function(){
        this.comp.style.display = "block";        
        this.topper.style.display = "block";        
        this.bgDiv.style.display = "block";
    },
    Hide: function(){        
        this.comp.style.display = "none";
        this.topper.style.display = "none";
        this.bgDiv.style.display = "none";
    },
    GetElem: function(ElementId){
        return document.getElementById(ElementId);
    },
    Move: function(){
        this.comp.Bounds = _getBounds(this.comp);
        var windowSc = this.WindowScroll();
        var contBounds = _getBounds(usercontents);
        this.topper.style.width = contBounds.width + "px";
        this.topper.style.height = contBounds.height + "px";
        this.bgDiv.style.width = contBounds.width;
        this.bgDiv.style.height = contBounds.height;
        this.topper.style.left = contBounds.left + "px";
        this.topper.style.top = contBounds.top + "px";
        this.bgDiv.style.left = contBounds.left + "px";
        this.bgDiv.style.top = contBounds.top + "px";
        var CurrLeft = ((contBounds.width/2) - (this.comp.Bounds.width/2) + windowSc.left + contBounds.left);
        var CurrTop = ((contBounds.height/2) - (this.comp.Bounds.height/2) + windowSc.top + contBounds.top);
        this.comp.style.left = CurrLeft + "px";
        this.comp.style.top = CurrTop + "px";
    }
}

function _positionAllModals(){
    for(i=0;i<_modalArr.length;i++){
        _modalArr[i].Move();
    }
    clearTimeout(_scrolltimeid);
    _scrolltimeid = setTimeout("_positionAllModals();",10);
}

var oldDefaultonload = window.onload;
var afterLoading = new Array();
var afterHiding = new Array();
var loadingdiv = document.getElementById("divPageLoading");
if(!loadingdiv){
    var newcomp = document.createElement("div");
    newcomp.id = "divPageLoading";
    newcomp.style.position = "absolute";
    newcomp.style.left = "0px";
    newcomp.style.top = "0px";
    if(typeof(loadingMessage)=="string"){
        newcomp.innerHTML = '<table class="defaultfontb" style="background-color:White; color:Black; border:solid 1px #CCCCCC;"><tr><td align="center">' + loadingMessage + '</td></tr></table>';
    }else{
        newcomp.innerHTML = '<table style="background-color:whitesmoke; border:solid 1px #CCCCCC;"><tr><td align="center" class="defaultfontb">Please wait while we load Innovativestone.com</td></tr></table>';
    }    
    newcomp.style.visibility = "visible";
    usercontents.parentNode.insertBefore(newcomp,usercontents);
    loadingdiv = document.getElementById("divPageLoading");
}

var loadingcomp = new LoadingModal("divPageLoading");
if(typeof(displayloading)!="undefined" && displayloading!=null){    
    if(displayloading!=true){
        loadingcomp.Hide();
        usercontents.className = "showusercontents";        
    }else{
        loadingcomp.Show();
        usercontents.className = "hideusercontents";
    }
}else{
    loadingcomp.Show();
    usercontents.className = "hideusercontents";
}

var runned = false;
window.onload = function(){    
    if(usercontents){
        usercontents.style.visibility = "visible";
        usercontents.style.display = "block";
    }
    for(i=0;i<afterLoading.length;i++){
        var currfn = afterLoading[i];
        currfn();
    }
    _LoadingPageOnLoad();    
}

function _LoadingPageOnLoad(){
    if(runned!=true){
        runned = true;
        if(usercontents){
            usercontents.style.visibility = "visible";
            usercontents.style.display = "block";
        }    
        if(oldDefaultonload){
            oldDefaultonload();
        }
        loadingcomp.Hide();
        for(i=0;i<afterHiding.length;i++){        
            var currfn = afterHiding[i];
            currfn();
        }
    }
}

// The Following function gets the Left of an object
function _getLeft(obj){
    return obj.offsetLeft + (obj.offsetParent ? _getLeft(obj.offsetParent) : obj.x ? obj.x : 0);
}
// The Following function gets the Top of an object
function _getTop(obj){
    return (obj.offsetParent ? obj.offsetTop + _getTop(obj.offsetParent) : obj.y ? obj.y : 0);    
}
// The Following function gets the Right and width of an object
function _getRight(obj){
	var leftpos = _getLeft(obj);
	return {right:obj.offsetWidth + leftpos, width:obj.offsetWidth };
}
// The Following function gets the Bottom and height of an object
function _getBottom(obj){
	var bottompos = _getTop(obj);
	return {bottom:obj.offsetHeight + bottompos, height:obj.offsetHeight };	
}

// The Following function gets the Bounds of an object
function _getBounds(obj){    
	var LeftPos = _getLeft(obj);
	var TopPos = _getTop(obj);
	var RightPos = _getRight(obj).right;
	var BottomPos = _getBottom(obj).bottom;
	var Width = _getRight(obj).width;
	var Height = _getBottom(obj).height;	
	return{left: LeftPos, top: TopPos, right: RightPos, bottom: BottomPos, width: Width, height: Height};
}
