function getInsideFrameURL(sidk) {
    var sidk = sidk ? 'sidk=' + sidk : '';
    var search = '';
    var level = 1;
    var obj = self;
    var end = 0;

    for (level = 1; level < 5; level++){
        if (obj == obj.parent) break;
        obj = obj.parent;
    }

    obj = self;

    for (i = 1; i < 5; i++){
        if (obj.location.search.indexOf('?if=' + level) != -1 ||
            obj.location.search.indexOf('&if=' + level) != -1){
            level ++;
        }
        if (obj == obj.parent) break;
        obj = obj.parent;
    }

    search = location.search;
    if (search && search != '?'){
        if (sidk && search.indexOf(sidk) != -1)
            sidk = '';
        if (search.indexOf('?if=') != -1){
            if (search.length > 5){
                search = '?' + search.substring(6, search.length);
            } else {
                search = '';
            }
        }
        while (search){
            var pos = search.indexOf('&if=');
            if (pos == -1) break;
            var left = search.substring(0, pos);
            var right = search.substring(pos + 5, search.length);
            search = left + right;
        }
        search += (search ? '&if=' : '?if=') + level + (sidk ? '&' + sidk : '');
    } else {
        search = '?if=' + level + (sidk ? '&' + sidk : '');
    }
    return location.protocol + '//' + location.host + location.pathname + search + location.hash;
}

function SelfFrameCommon(sidk) {
    var query = sidk ? '?sidk=' + sidk : '';

    try {
        var _ = parent.location.protocol;
    } catch(e) {
        parent.location = self.location;
        return;
    }

    if (parent.smallSideBar || (self.location.pathname == '/logon.html'
        && self.location.pathname != top.location.pathname)) {
        top.location = self.location;

    } else if (!parent.SideBar){
        document.open();
        document.write(
            '<frameset cols="155,*">'
            + '<frame name="SideBar" src="/sidebar.html' + query + '" noresize scrolling="auto" frameborder="1" marginwidth="0" marginheight="0">'
            + '<frame name="Content" src="' + getInsideFrameURL(sidk) + '" noresize scrolling="auto" frameborder="0" marginwidth="0" marginheight="0">'
            + '</frameset></html>');
        document.close();

    } else if (parent.SideBar.contentPage) {
        if (location.pathname != '/logon.html') {
            parent.SideBar.contentPage = location.href;
        } else {
            parent.SideBar.contentPage = '';
        }
    }
}

function SelfFrameChatHost(sidk) {
    var query = sidk ? '?sidk=' + sidk : '';

    if (!parent.SideBar) {
        document.open();
        document.write(
            '<frameset cols="155,*">'
            + '<frame name="SideBar" src="/chathost/sidebar.html' + query + '" noresize scrolling="auto" frameborder="1" marginwidth="0" marginheight="0">'
            + '<frame name="Content" src="' + getInsideFrameURL(sidk) + '" noresize scrolling="auto" frameborder="0" marginwidth="0" marginheight="0">'
            + '</frameset>');
        document.close();
    }
}

function SelfFrameStudio(sidk) {
    var query = sidk ? '?sidk=' + sidk : '';

    if (!parent.SideBar) {
        document.open();
        document.write(
            '<frameset cols="155,*">'
            + '<frame name="SideBar" src="/studio/sidebar.html' + query + '" noresize scrolling="auto" frameborder="1" marginwidth="0" marginheight="0">'
            + '<frame name="Content" src="' + getInsideFrameURL(sidk) + '" noresize scrolling="auto" frameborder="0" marginwidth="0" marginheight="0">'
            + '</frameset>');
        document.close();
    }
}

function SelfFrameChatHostPictures(sidk) {
    var query = sidk ? '?sidk=' + sidk : '';

    if (!top.SideBar) {
        top.document.open();
        top.document.write(
            '<frameset cols="155,*">'
            + '<frame name="SideBar" src="/chathost/sidebar.html' + query + '" noresize scrolling="auto" frameborder="1" marginwidth="0" marginheight="0">'
            + '<frame name="Content" src="' + getInsideFrameURL(sidk) + '" noresize scrolling="auto" frameborder="0" marginwidth="0" marginheight="0">'
            + '</frameset>');
        top.document.close();

    } else if (!top.Content.Days) {
        top.Content.document.open();
        top.Content.document.write(
            '<frameset cols="155,*,0" framespacing="0">'
            + '<frame name="Days" src="/chathost/photoarchiveslist.html' + query + '" noresize scrolling="auto" frameborder="0">'
            + '<frame name="pics" src="' + getInsideFrameURL(sidk) + '" noresize scrolling="auto" frameborder="0" marginwidth="0" marginheight="0">'
            + '<frame name="dataForm" src="/archivepictureattrchange.html' + query + '" noresize scrolling="no" frameborder="0" marginwidth="0" marginheight="0">'
            + '</frameset>');
        top.Content.document.close();
    }
}

function SelfFrameHidden(framename, framepagename) {
    if (!top.SideBar || !top.Content) {
        top.document.open();
        top.document.write(
            '<frameset cols="155,*">'
            + '<frame name="SideBar" src="/chathost/sidebar.html" noresize scrolling="auto" frameborder="1" marginwidth="0" marginheight="0">'
            + '<frame name="Content" src="' + getInsideFrameURL() + '" noresize scrolling="auto" frameborder="0" marginwidth="0" marginheight="0">'
            + '</frameset>');
        top.document.close();

    } else if (!top.Content.frames[framename]) {
        top.Content.document.open();
        top.Content.document.write(
            '<frameset cols="*,0">'
            + '<frame name="Content" src="' + getInsideFrameURL() + '" noresize scrolling="auto" frameborder="0" marginwidth="0" marginheight="0">'
            + '<frame name="' + framename + '" src="' + framepagename + '" noresize scrolling="auto" frameborder="0" marginwidth="0" marginheight="0">'
            + '</frameset>');
        top.Content.document.close();
    }
}

function sidebarCommon(sidk, relURL){
    var url;
    if (!relURL){
        url = '/sidebar.html';
    } else if (relURL === true){
        url = 'sidebar.html';
    } else {
        url = relURL + 'sidebar.html';
    }
    var query = sidk ? '?sidk=' + sidk : '';

    try {
        var _ = parent.location.protocol;
    } catch(e) {
        parent.location = self.location;
        return;
    }

    if (parent.smallSideBar || (self.location.pathname == '/logon.html'
        && self.location.pathname != top.location.pathname)) {
        top.location = self.location;

    } else if (!parent.SideBar){
        document.open();
        document.write(
            '<frameset cols="155,*">'
            + '<frame name="SideBar" src="' + url + query + '" noresize scrolling="auto" frameborder="1" marginwidth="0" marginheight="0">'
            + '<frame name="Content" src="' + getInsideFrameURL(sidk) + '" noresize scrolling="auto" frameborder="0" marginwidth="0" marginheight="0">'
            + '</frameset>');
        document.close();

    } else if (parent.SideBar.contentPage) {
        if (location.pathname != '/logon.html') {
            parent.SideBar.contentPage = location.href;
        } else {
            parent.SideBar.contentPage = '';
        }
    }
}
