﻿var usercontents = document.getElementById("usercontents");
var _init = false;
var pageLoaded = false;
var _scrolltimeid = null;
var _modalArr = new Array();
function LoadingModal(compid, focuscompid){    
    var chcomp = this.GetElem(compid);
    var focuscomp = this.GetElem(focuscompid);
    this.focuscomp = focuscomp;
    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.style.position = "absolute";
            setOpacity(newTopper,0);            
            newTopper.style.left = _getLeft(document.body) + "px";
            newTopper.style.top = _getTop(document.body) + "px";
            newTopper.style.width = this.WindowInnerDimension().width + "px";
            newTopper.style.height = this.WindowInnerDimension().height + "px";
            newcomp.style.height = newTopper.style.height;
            newcomp.style.width = newTopper.style.width;
            newTopper.style.zIndex = "10000";
            newcomp.style.zIndex = "10001";
            this.comp.style.zIndex = "10002";
            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";
        setOpacity(this.comp,100);
        this.topper.style.display = "block";
        setOpacity(this.topper,0);
        this.bgDiv.style.display = "block";        
        setOpacity(this.bgDiv,0);
        try{
            if(this.focuscomp){
                this.focuscomp.focus();
            }
        }catch(e){
        }
    },
    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 windowDim = this.WindowInnerDimension();
        var windowSc = this.WindowScroll();
        this.topper.style.width = _getRight(document.body).width + "px";
        this.topper.style.height = _getBottom(document.body).height + "px";
        this.bgDiv.style.width = this.topper.style.width;
        this.bgDiv.style.height = this.topper.style.height;
        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";
    }
}

function setOpacity(comp,percent){
    if(comp){
        comp.style.filter = "alpha(opacity=" + percent + ")";
        comp.style.MozOpacity = (percent/100);
        comp.style.KhtmlOpacity = (percent/100);
        comp.currOp = percent;
    }
}

function getOpacity(comp){
    if(comp){
        if(comp.style.MozOpacity!=null){
            comp.currOp = comp.style.MozOpacity * 100;
        }
        if(comp.currOp!=null){
            return comp.currOp;
        }else{
            return 100;
        }
    }
}

function FadeOut(comp,speed,step){
    if(comp){
        clearTimeout(comp.fadetimeid);
        if(getOpacity(comp)>0){
            setOpacity(comp, getOpacity(comp)-step);            
            comp.fadetimeid = setTimeout("FadeOut(document.getElementById('" + comp.id + "')," + speed + "," + step + ");",speed);
        }else{
            setOpacity(comp, 0);
            comp.style.display = "none";            
        }        
    }
}

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";
    var exte = "";    
    if(typeof(pathext)!="undefined" && pathext!=null){
        exte = pathext;
    }
    /*if(getFlashPlayerVersion().major >= 8){
        var swftext = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ' ;
        swftext += 'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" ';
        swftext += 'width="550" height="250" id="test" align="middle">';
        swftext += '<param name="allowScriptAccess" value="sameDomain" />';
        swftext += '<param name="movie" value="'+ exte +'images/flash/butterflyflying.swf" />';
        swftext += '<param name="quality" value="high" />';
        swftext += '<param name="wmode" value="transparent" />';
        swftext += '<param name="bgcolor" value="#ffffff" />';
        swftext += '<embed src="'+ exte +'images/flash/butterflyflying.swf" wmode="transparent" quality="high" bgcolor="#ffffff"';
        swftext += ' width="550" height="250" name="test" align="middle" allowScriptAccess="sameDomain"';
        swftext += ' type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
        newcomp.innerHTML = '<table><tr><td>'+ swftext +'</td></tr><tr><td align="center" class="defaultfontbig">Please wait while we load Everlifestone.com</td></tr></table>';        
    }else{
        newcomp.style.backgroundColor = "White";
        newcomp.style.borderColor = "#666666";
        newcomp.style.borderWidth = "1px";
        newcomp.style.borderStyle = "solid";        
        newcomp.innerHTML = "<table class='defaultfont' cellpadding='3'><tr><td valign='middle' align='center'><img src='" + exte + "images/preloader3.gif' alt=''/></td></tr><tr><td align='center'>Loading please wait...</td></tr></table>";
    }*/    
    newcomp.innerHTML = "<table class='defaultfont' cellpadding='3'><tr><td align='center'>Please wait while we load Everlifestone.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";
        //usercontents.style.visibility = "visible";
        //usercontents.style.display = "block";        
    }else{
        loadingcomp.Show();
        usercontents.className = "hideusercontents";
    }
}else{
    loadingcomp.Show();
    usercontents.className = "hideusercontents";
}

var runned = false;
window.onload = function(){
    /*var pageforms = document.getElementsByTagName("form");    
    for(i=0;i<pageforms.length;i++){
        if(pageforms[i]){
            pageforms[i].style.display = "block";
        }
    }*/
    if(usercontents){
        usercontents.className = "showusercontents";
        //usercontents.style.visibility = "visible";
        //usercontents.style.display = "block";
    }
    for(i=0;i<afterLoading.length;i++){
        var currfn = afterLoading[i];
        currfn();
    }
    _LoadingPageOnLoad();
    //loadingdiv.style.display = "none";
}

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){
    /*if(obj.style.display){
        obj.oldDisplay = obj.style.display;
    }
    obj.style.display = "block";*/
	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;
	/*if(obj.oldDisplay){
	    obj.style.display = obj.oldDisplay;
	}*/
	return{left: LeftPos, top: TopPos, right: RightPos, bottom: BottomPos, width: Width, height: Height};
}

function getFlashPlayerVersion(){
	var PlayerVersion = new FlashPlayerVersion([0,0,0]);
	if(navigator.plugins && navigator.mimeTypes.length){
		var x = navigator.plugins["Shockwave Flash"];
		if(x && x.description) {
			PlayerVersion = new FlashPlayerVersion(x.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split("."));
		}
	}else if (navigator.userAgent && navigator.userAgent.indexOf("Windows CE") >= 0){ // if Windows CE
		var axo = 1;
		var counter = 3;
		while(axo) {
			try {
				counter++;
				axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+ counter);
				PlayerVersion = new FlashPlayerVersion([counter,0,0]);
			} catch (e) {
				axo = null;
			}
		}
	} else { // Win IE (non mobile)		
		try{
			var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		}catch(e){
			try{
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
				PlayerVersion = new FlashPlayerVersion([6,0,21]);
				axo.AllowScriptAccess = "always"; // error if player version < 6.0.47
			}catch(e){
				if (PlayerVersion.major == 6) {
					return PlayerVersion;
				}
			}
			try{
				axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			}catch(e){}
		}
		if (axo != null) {
			PlayerVersion = new FlashPlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));
		}
	}
	return PlayerVersion;
}
function FlashPlayerVersion(arrVersion){
	this.major = arrVersion[0] != null ? parseInt(arrVersion[0]) : 0;
	this.minor = arrVersion[1] != null ? parseInt(arrVersion[1]) : 0;
	this.rev = arrVersion[2] != null ? parseInt(arrVersion[2]) : 0;
}