﻿var iDoctors = 11;

function loadPage(type, code, target) {
    if (type != 'dindex' && type != 'companion') {
        var divlist = new Array("divCompIndexContainer","divCharIndexContainer","divSpeciesIndexContainer","divWorldsIndexContainer","divIntroContainer");
        for (i=0;i<divlist.length;i++) {
            if ($("#"+divlist[i])) {
                if (divlist[i].indexOf(target+"Container") != 0) {
                    $("#"+divlist[i]).slideUp("fast");
                }
            }    
        }
        if ($("#"+target+"Container")) {
            $("#"+target+"Container").slideToggle("fast", function () {
                    if (!($("#"+target+"Container").is(":visible"))) $("#divIntroContainer").slideDown("fast");
                }           
            );
        }
    }

    switch (type) {
        case 'dindex':
            if ($("#"+target+"Loading")) $("#"+target+"Loading").show();
            if ($("#"+target)) $("#"+target).hide(); 
            $.get("/AJAX.aspx", { type: type, doctor: code }, function(data) { loadPageHandler(data, type, code, target); } );
            break;

        case 'companion':
            if ($("#" + target + "Loading")) $("#" + target + "Loading").show();
            if ($("#" + target)) $("#" + target).hide();
            if ($("#tdTallAdvert")) $("#tdTallAdvert").hide();
            $.get("/companions.aspx", { name: code, ajax: 'true' }, function(data) { loadPageHandler(data, type, code, target); });
            break;

        case 'companions':
        case 'character':
        case 'species':
        case 'world':
            if ($("#"+target+"Loading")) $("#"+target+"Loading").show();
            if ($("#"+target)) $("#"+target).hide(); 
            $.get("/AJAX.aspx", { type: type }, function(data) { loadPageHandler(data, type, code, target); } );
            break;
    }
}

function loadPageHandler(data, type, code, target) {
    control = $("#"+target);
    
    if (control) {
        // Look for the start and end comments, and only take the contents between
        iStart = data.indexOf('<!-- Start -->') + 14;
        iEnd = data.lastIndexOf('<!-- End -->');
        data = data.substring(iStart, iEnd);
        control.html(data);
        control.show(); //slideDown("slow");
    }
    if ($("#"+target+"Loading")) $("#"+target+"Loading").hide(); 
    
    switch(type) {
        case "dindex":
            control = $("#doc"+code);
            if (control) {
                defaultdoc=$("#ctl00_MainContent_lnkDefaultDoctor");
                if (defaultdoc) {
                    defaultdoc.attr("href",control.attr("href"));
                    defaultdoc.attr("title", control.attr("title"));
                    defaultdoc.html(control.attr("title"));
                }
            }
    }
}

function titleSelectDoctor(iDoctor) {
    for (i=1;i<=iDoctors;i++) {
        image = document.getElementById('smdoc'+i);
        if (image) {
            if (i==iDoctor) {
                image.className='smdocsel';
            } else {
                image.className='smdoc';
            }            
        }
    }
}

function grayOut(vis, options) {
    var options = options || {};
    var zindex = options.zindex || 50;
    var opacity = options.opacity || 70;
    var opaque = (opacity / 100);
    var bgcolor = options.bgcolor || '#000000';
    var dark = document.getElementById('darkenScreenObject');
    if (!dark) {
        var tbody = document.getElementsByTagName("body")[0];
        var tnode = document.createElement('div');
        tnode.style.position = 'absolute';
        tnode.style.top = '0px';
        tnode.style.left = '0px';
        tnode.style.overflow = 'hidden';
        tnode.style.display = 'none';
        tnode.id = 'darkenScreenObject';
        tbody.appendChild(tnode);
        dark = document.getElementById('darkenScreenObject');
    }
    if (vis) {
        if (document.body && (document.body.scrollWidth || document.body.scrollHeight)) {
            var pageWidth = document.body.scrollWidth + 'px';
            var pageHeight = document.body.scrollHeight + 'px';
        } else if (document.body.offsetWidth) {
            var pageWidth = document.body.offsetWidth + 'px';
            var pageHeight = document.body.offsetHeight + 'px';
        } else {
            var pageWidth = '100%';
            var pageHeight = '100%';
        }
        pageHeight = '100%';
        dark.style.opacity = opaque;
        dark.style.MozOpacity = opaque;
        dark.style.filter = 'alpha(opacity=' + opacity + ')';
        dark.style.zIndex = zindex;
        dark.style.backgroundColor = bgcolor;
        dark.style.width = pageWidth;
        dark.style.height = pageHeight;
        dark.style.display = 'block';
    } else {
        dark.style.display = 'none';
    }
}