self.name = 'RoyalMainWindow';

if (top != self)
    top.location.href = "/";

window.onerror = setDefaults;
if (opener != null)
	opener = null;
var isSmall = false;
var isPreferences = false;
var isHidden = false;
var CheckTimeout = null;
var RemoteVar = 0;
var StartOpen = false;
var DesignElements = { LEFT: '', MEN: '', TOP: '', IBR: '', RIGHT: '' };
var wins = {};
function doLogout() {}
function setDefaults(){return true;}

function getUI() {
	var dim = top.getWinDims(top)
	var sHeight = screen.height;
	var sWidth = screen.width;
	var fWidth = dim.w > 1250 ? size.cfWidth2 : size.cfWidth1;
	sHeight = sHeight < dim.h ? dim.h : sHeight;
	sWidth = sWidth < dim.w ? dim.w : sWidth;
	var d$ = 'xGAYROYALx'
	return URL = 'CheckUI=' + fWidth + d$ + dim.w + d$ + dim.h + d$ + sHeight + d$ + sWidth;
}

function $(ID) {return document.getElementById(ID);}

function buildIFrame(Name, id, Left, Top, Width, Height, SRC) {
    if ($(id) != null) {
        oIFrame = $(id);
        oIFrame.frameBorder = '0';
	    if (!isNaN(Left)) oIFrame.style.left = Left + 'px';
	    if (!isNaN(Top)) oIFrame.style.top = Top + 'px';
	    if (!isNaN(Width)) oIFrame.style.width = Width + 'px';
	    if (!isNaN(Height)) oIFrame.style.height = Height + 'px';
	    oIFrame.style.display = 'block';
	    oIFrame.allowTransparency = 'true';
	    oIFrame.src = SRC;
	} 
}

function getWinDims(winObj) {
    var locW = 0;
    var locH = 0;
    if (typeof winObj.innerHeight == 'number') {
        locW = winObj.innerWidth;
        locH = winObj.innerHeight;
    } else { 
        if (winObj.document.documentElement && winObj.document.documentElement.clientHeight) {
            locW = winObj.document.documentElement.clientWidth;
            locH = winObj.document.documentElement.clientHeight;
        } else {
            if (winObj.document.body && winObj.document.body.clientHeight) {
               locW = winObj.document.body.clientWidth;
               locH = winObj.document.body.clientHeight;
           }
       }
    }
    return { w: locW < 1 ? 800 : locW, h: locH < 1 ? 600 : locH }
}

function getCounter(){return (new Date()).getTime();}

function getFrameQuery(val) {
    fQuery = val;
    loadFrames();
}

size = new function() {
    this.lfWidth = 203;
    this.rfWidth = 183;
    this.cfWidth1 = 608;
    this.cfWidth2 = 864;
    this.tHeight1 = 64;
    this.tHeight2 = 144;
    this.minWidth = 400;
    this.minHeight = 300;
    this.getScrollBars = function() {
        if (arguments.length < 2)
            return { x: 0, y: 0 };
        var x = arguments[0] < 992;
        var y = arguments[1] < 380;
        var xWidth = yWidth = 0;
        if (x) {
            if ((typeof (top.innerHeight) != 'undefined' && (top.document.documentElement.clientHeight < top.innerHeight)))
                yWidth = (top.innerHeight - top.document.documentElement.clientHeight);
        }
        if (y) {
            if ((typeof (top.innerWidth) != 'undefined' && (top.document.documentElement.clientWidth < top.innerWidth)))
                xWidth = (top.innerWidth - top.document.documentElement.clientWidth);
        }
        top.document.body.setAttribute('scroll', (x || y) ? 'yes' : 'no');
        top.document.body.style.overflow = (x || y) ? 'auto' : 'hidden';
        if (!window.opera) {
            top.document.body.style.overflowX = x ? 'auto' : 'hidden';
            top.document.body.style.overflowY = y ? 'auto' : 'hidden';
        }
        return { x: xWidth, y: yWidth };

    }
}

function loadFrames() {
    var w = getWinDims(top).w;
    var h = getWinDims(top).h;
    $('waitImg').style.display = 'block';    
    winWidth = w;
    winHeight = h;
    var grnavh = isSmall?64:144;
    var cfw = w < (size.lfWidth + size.cfWidth2 + size.rfWidth) ? size.cfWidth1 : size.cfWidth2;
    if (cfw != aktWidth){
        var cpr = cfw==size.cfWidth1?3:4;
        sendRequest("iframes.asp?CPR=" + cpr + "&CFW=" + cfw + "&v=" + getCounter(),getFrameQuery);
        aktWidth = cfw;
        return void(null);
    }
    removeEvent(window, "load", loadFrames);
    var cw = size.lfWidth + cfw + size.rfWidth;
    var spacew = parseInt((w - size.lfWidth - size.rfWidth - cfw) / 2);
    spacew= (spacew < 0)?0:spacew;
    document.body.setAttribute('scroll','no');
    if (!isChecked && contUrl != null) {
        if (screen.height < 768) {
            contUrl += contUrl.indexOf('?') > -1 ? '&' : '?';
            contUrl += getUI();
        }
    }
    isChecked = true;
    buildIFrame("chat","chat", 0, 0, 100, 0, fSrc[0]);
    buildIFrame("dbaction", "dbaction", 0, 0, 100, 0, fSrc[1]);
    buildIFrame("leftspace", "leftspace", 0, 0, spacew, h, "/framed/left.asp?" + fQuery + "LEFT");
    buildIFrame("menu", "menu", spacew, 0, size.lfWidth, h, "/framed/menu.asp");
    buildIFrame("grnav", "grnav",spacew + size.lfWidth, 0, cfw + size.rfWidth, grnavh + 200, "/framed/top.asp?" + fQuery + "TOP");
    buildIFrame("content", "contentid", spacew + size.lfWidth, grnavh, cfw, h - grnavh, contUrl);
    buildIFrame("infobar","infobar"    ,spacew + size.lfWidth + cfw,0, size.rfWidth, h, "/ibr/default.asp");
    buildIFrame("rightspace", "rightspace", cw + spacew, 0, spacew, h, "/framed/left.asp?" + fQuery + "RIGHT");
    for (var i = 0; i < top.frames.length; i++) {
        if (top.frames[i].name == '')
            top.frames[i].name = top.frames[i].frameElement.name;
    }
    setTimeout('removeScrollBars(0,0)', 1);
    resizeEventActive = false;
    setInterval(resizeEvent, 1000);
}

var resizeEventActive = true;
var aktDesign = null;

function resizeEvent() {
    if (resizeEventActive) return;
    resizeEventActive = true;
    var cpr = 0;
    var w = getWinDims(top).w;
    var h = getWinDims(top).h;
    w = w - size.getScrollBars(w, h).x;
    h = (h<380?380:h) - size.getScrollBars(w,h).y;
    if (h != winHeight) {
        winHeight = h;
        if (h < 381) {
            try {
                if (parseInt(top.grnav.DesignInfo.split('|')[1], 10) != 25) {
                    aktDesign = parseInt(top.grnav.DesignInfo.split('|')[1], 10);
                    top.grnav.ChangeDesign(25);
                    isSmall = true;
                }
            } catch (e) { }
        } else {
            if (aktDesign != null && aktDesign != parseInt(top.grnav.DesignInfo.split('|')[1],10)) {
                try{
                    top.grnav.ChangeDesign(aktDesign);
                    aktDesign = null;
                    isSmall = false;
                }
                catch (e){}
            }
        }
        var grnavh = isSmall ? 64 : 144;
        $("grnav").style.height = grnavh + "px";
        $("leftspace").style.height = h + "px";
        $("contentid").style.top = grnavh + "px";
        $("contentid").style.height = h - grnavh + "px";
        $("rightspace").style.height = h + "px";
        $("menu").style.height = h + "px";
        $("infobar").style.height = h + "px";
        resizeEventActive = false;
        setTimeout('resizeEvent()', 1);
        return;
    }
    if (top.content && top.content.document && top.content.document.body) {
        if (top.content.document.styleSheets.length > 0) {
            if (top.content.document.styleSheets[0].href != null &&
                top.content.document.styleSheets[0].href.indexOf('style.css.asp') > -1 &&
                top.content.document.styleSheets[0].href.indexOf(aktWidth) == -1) {
                aktWidth = (aktWidth == size.cfWidth1) ? size.cfWidth2 : size.cfWidth1;
                winWidth -= 1;
            }
        }
    }
    if (w != winWidth) {
        winWidth = w;
        cfw = w < (size.lfWidth + size.cfWidth2 + size.rfWidth) ? size.cfWidth1 : size.cfWidth2;
        var cw = size.lfWidth + cfw + size.rfWidth;
        var spacew = parseInt((w - cw) / 2);
        spacew= (spacew < 0)?0:spacew;
        $("contentid").style.width = cfw + "px";
        $("contentid").style.left = spacew + size.lfWidth + "px";
        $("rightspace").style.width = spacew + "px";
        top.rightspace.scrollTo(0, 0);
        $("rightspace").style.left = cw + spacew + "px";
        $("leftspace").style.width = spacew + "px";
        $("grnav").style.width = cfw + size.rfWidth + "px";
        $("grnav").style.left = spacew + size.lfWidth + "px";
        $("menu").style.left = spacew + "px";
        $("infobar").style.left = spacew + size.lfWidth + cfw + "px";
        if (w < (size.cfWidth1+size.lfWidth + size.rfWidth))
            document.body.style.width = cw + 'px';
        top.grnav.wWidth = cfw;
        top.grnav.getResize('b');
        if (cfw != aktWidth) {
            cpr = cfw == size.cfWidth1 ? 3 : 4;
            aktWidth = cfw;
            sendRequest("/User/Settings/cellsPerRowAuto.asp?CPR=" + cpr + "&CFW=" + cfw + "&v=" + getCounter(), getCFW);
            return;
        }
        closeInnerWindow();
    }
    resizeEventActive = false;
}

function getCFW(txt) {
    aktWidth = isNaN(txt)?608:parseInt(txt);
    top.content.location.href = top.content.location.href;
    winWidth -= 1;
    setTimeout('resizeEventActive = false;', 2000);
}

var loginTimeout = null;

function forceLogin(e) {
    e = e || window.event;
    var show;
    if (loginTimeout != null) {
        if (e) {
            clearTimeout(loginTimeout)
            loginTimeout = null;
        } else {
            return;
        }
    }
    if ($('loginframe') != null) {
        var lF = $('loginframe');
        show = (lF.style.display!='block'); 
        lF.style.top = (isSmall ? 10 : 90) + 'px';
        lF.style.left = (parseInt($('infobar').style.left) - 33) + 'px';
        lF.style.display = show ? 'block' : 'none';
        if (show) {
            addEvent(lF, 'mousedown', forceLogin);
            loginTimeout = setTimeout('forceLogin(true)', 5000);
        } else {
            removeEvent(lF, 'mousedown', forceLogin);
        }
    }   
}
    
function removeScrollBars(frameNumber,tries){
    tries++;
    win = top.frames[frameNumber];
    if (win && win.document && win.document.body){         
        var id = top.document.getElementsByTagName('IFRAME')[frameNumber].id;
        if (id != 'contentid'){
            win.frameElement.style.overflow = 'hidden';
            win.document.body.setAttribute('scroll','no');
        } 
        frameNumber++;
        if (frameNumber<top.frames.length){
            setTimeout('removeScrollBars('+frameNumber +',0)',1);
        } else {
            addEvent(window, 'resize', resizeEvent);
            $('waitImg').style.display = 'none';
            resizeEvent();
        }
    } else  {      
        if (tries > 10)
            {return;}
        else {
            setTimeout('removeScrollBars('+frameNumber +',' + tries +')',500);
         }
    }
}

function frameLoaded(val) { return void (null); }

addEvent(window, "load", loadFrames);

function debug(val, append) {
    var deb = $('debug');
    if (deb == null) {
        deb = top.document.createElement('DIV');
        deb.style.cssText = 'position:absolute;top:5px;left:5px;background-color:red;color:white;z-index:2000;'
        top.document.body.appendChild(deb);
    }
    if (append) {
        deb.innerHTML += val;
    } else {
        deb.innerHTML = val;
    }
}

function showInnerWindow(type, showClose, codeObj) {
    closeInnerWindow();
    var cB, tA;
    var winID = type == 0 ? 'innerWin0' : 'innerWin1';
    var iW = $(winID);
    if (iW == null) {
        iW = document.createElement('DIV');
        iW.id = winID;
        cB = document.createElement('A');
        cB.href = '#';
        cB.className = 'innerWindowClose';
        addEvent(cB, 'click', closeInnerWindow);
        iW.appendChild(cB);
        tA = document.createElement('DIV');
        tA.id = type == 0 ? 'innerTxt0' : 'innerTxt1';
        iW.appendChild(tA);
        document.body.appendChild(iW);
    } else {
        cB = iW.firstChild;
        tA = iW.lastChild;
    }
    cB.style.display = showClose ? 'block' : 'none';
    if (typeof codeObj == 'string')
        tA.innerHTML = codeObj;
    else
        tA.appendChild(codeObj);
    iW.style.left = (parseInt(getWinDims(top).w / 2, 10) - 285) + 'px';
    iW.style.top = isSmall?'120px':'200px';
    iW.style.display = 'block';
}

function closeInnerWindow() {
    var wins = new Array('innerWin0','innerWin1');
    for (i=0;i<wins.length;i++){
        if ($(wins[i]) != null){
            $(wins[i]).style.display = 'none';
        }
    }
}

