﻿function $(id)
{
	if (document.getElementById) return document.getElementById(id);
	else if (document.all) return document.all[id];
	else return null; // browser is too retarded
}

function load()
{
	resize();
}

function disable()
{
	var links = document.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++)
		links[i].onclick = function() { return false; }
}


function resize() 
    {

    var BrowserDetect = {
        init: function() {
            this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
            this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
            this.OS = this.searchString(this.dataOS) || "an unknown OS";
        },
        searchString: function(data) {
            for (var i = 0; i < data.length; i++) {
                var dataString = data[i].string;
                var dataProp = data[i].prop;
                this.versionSearchString = data[i].versionSearch || data[i].identity;
                if (dataString) {
                    if (dataString.indexOf(data[i].subString) != -1)
                        return data[i].identity;
                }
                else if (dataProp)
                    return data[i].identity;
            }
        },
        searchVersion: function(dataString) {
            var index = dataString.indexOf(this.versionSearchString);
            if (index == -1) return;
            return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
        },
        dataBrowser: [
		{
		    string: navigator.userAgent,
		    subString: "Chrome",
		    identity: "Chrome"
		},
		{ string: navigator.userAgent,
		    subString: "OmniWeb",
		    versionSearch: "OmniWeb/",
		    identity: "OmniWeb"
		},
		{
		    string: navigator.vendor,
		    subString: "Apple",
		    identity: "Safari",
		    versionSearch: "Version"
		},
		{
		    prop: window.opera,
		    identity: "Opera"
		},
		{
		    string: navigator.vendor,
		    subString: "iCab",
		    identity: "iCab"
		},
		{
		    string: navigator.vendor,
		    subString: "KDE",
		    identity: "Konqueror"
		},
		{
		    string: navigator.userAgent,
		    subString: "Firefox",
		    identity: "Firefox"
		},
		{
		    string: navigator.vendor,
		    subString: "Camino",
		    identity: "Camino"
		},
		{		// for newer Netscapes (6+)
		    string: navigator.userAgent,
		    subString: "Netscape",
		    identity: "Netscape"
		},
		{
		    string: navigator.userAgent,
		    subString: "MSIE",
		    identity: "Explorer",
		    versionSearch: "MSIE"
		},
		{
		    string: navigator.userAgent,
		    subString: "Gecko",
		    identity: "Mozilla",
		    versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
		    string: navigator.userAgent,
		    subString: "Mozilla",
		    identity: "Netscape",
		    versionSearch: "Mozilla"
		}
	],
        dataOS: [
		{
		    string: navigator.platform,
		    subString: "Win",
		    identity: "Windows"
		},
		{
		    string: navigator.platform,
		    subString: "Mac",
		    identity: "Mac"
		},
		{
		    string: navigator.userAgent,
		    subString: "iPhone",
		    identity: "iPhone/iPod"
		},
		{
		    string: navigator.platform,
		    subString: "Linux",
		    identity: "Linux"
		}
	]

    };
    BrowserDetect.init();

    var DoTheResize = false;

    // version logic
    if (BrowserDetect.browser == 'Explorer' && parseInt(BrowserDetect.version) < 7) { DoTheResize = true; }
    if (BrowserDetect.browser == 'Firefox' && parseInt(BrowserDetect.version) < 3) { DoTheResize = true; }

    if ( DoTheResize == true ) {
        var junk = resize();
    }
}

function resize2()
{

    
    
    
	resize.setBrowserSize();
		
	height = resize.browserHeight;
	width = resize.browserWidth;
	if (height < resize.minHeight) height = resize.minHeight;
		
	var offsetBottom = Math.min(0, resize.browserHeight - resize.minHeight);	
	for (id in resize.items)
	{	
		var element = $(id);
		if (!element) continue;
		
		var item = resize.items[id];
		
		if (item.bottom || item.bottom == 0) element.style.bottom = (item.bottom + offsetBottom) + "px";		
		if (item.top && item.bottom) element.style.height = Math.max(0, height - item.top - item.bottom) + "px";
		if (item.left && item.right) element.style.width = Math.max(0, width - item.left - item.right) + "px";
	}
}

resize.minHeight = 730;
resize.minWidth = 960;

resize.browserHeight = 0;
resize.browserWidth = 0;

resize.setBrowserSize = function()
{
	if (typeof(window.innerHeight) == "number" && typeof(window.innerWidth) == "number")
	{
		resize.browserHeight = window.innerHeight;
		resize.browserWidth = window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth)
	{
		resize.browserHeight = document.documentElement.clientHeight;
		resize.browserWidth = document.documentElement.clientWidth;
	}
	else if (document.body && document.body.clientHeight && document.body.clientWidth)
	{
		resize.browserHeight = document.body.clientHeight;
		resize.browserWidth = document.body.clientWidth;
	}
}

resize.items = new Array();
resize.add = function(id, top, right, bottom, left)
{
	var item = new Object();
	item.top = top;
	item.right = right;
	item.bottom = bottom;
	item.left = left;
	resize.items[id] = item;	
}


function openItem() { }

openItem.article = function(id)
{
	var url = resolveUrl("Article.aspx?" + id);
	openItem.navigate(url);
}

openItem.category = function(id)
{
	var url = resolveUrl("Category.aspx?" + id);
	openItem.navigate(url);
}

openItem.section = function(id)
{
	var url = resolveUrl("Section.aspx?" + id);
	openItem.navigate(url);
}

openItem.file = function(id) {
    var url = resolveUrl("ResourceFile.ashx?" + id);
    //openItem.navigate(url);
    // var cWin = open('', 'FileWin', 'scrollbars');
    // void(cWin.location = url);
    var newwin = window.open(url,'_blank','scrollbars');
}

openItem.navigate = function(url)
{
	void(window.location = url);
}

jQuery(document).ready(function()
{
	jQuery(".sidebar > .toc > .section > img").click(function()
	{
		var next = jQuery(this).parent(".section").next(".categoryContainer");
		if (next.css("display") != "none") return;
		jQuery(".sidebar > .toc > .categoryContainer").not(next).slideUp();
		next.slideDown();
	});
});

function initializeToc(sectionId)
{
	var open = jQuery(".sidebar > .toc > #toc_section_" + sectionId);
	if (sectionId != -1 && open) open.next(".categoryContainer").show();
	else jQuery(".sidebar > .toc > .categoryContainer:first").show();
}