﻿
function pageSetup()
{
    externalLinks();
    window.onresize = function() { switcher.setStyleSheet() };
    switcher = new styleSwitcher();
    switcher.setStyleSheet();   
}

function styleSwitcher() 
{
	// id of the stylesheet element
	this.styleid = 'css760';
	// name of the global stylesheet
	this.smallstylesheetname = 'css/osmodus760.css';
	// trigger width of site - below this we use the small stylesheet
	this.triggerwidth = 984;
	// null stylesheet name - use this to prevent errors associated with a null link value
	this.nullstylesheet = 'osmodusnull.css';

	// ENTRY POINT: sets the stylesheet to the defaults (above) unless styleId and sheetname are specified
	this.setStyleSheet = function (styleId, sheetname)
	{
		if(!styleId)
			this.switchSheet(this.styleid, this.smallstylesheetname);
		else
			this.switchSheet(styleId, sheetname);
	};

	// set the specified stylesheet 
	this.switchSheet = function(styleId, sheetname)
	{
		var ssheet = $(styleId);
		var winWidth = this.getWidth();
		if (winWidth < this.triggerwidth)
			ssheet.setAttribute('href', sheetname);
		else
			ssheet.setAttribute('href', this.nullstylesheet);	
	};

	// return the width of the browser's window
	this.getWidth = function()
	{
		var ssheet;
		if (parseInt(navigator.appVersion)>3) {
			if (navigator.appName.indexOf("Microsoft")!=-1) {
				return document.body.offsetWidth;
			}
			else
			{
				return window.innerWidth;
			}
		}
	};

} // end of styleSwitcher

// ensures all links with attribute rel="external" open in a new browser window
// thanks to Gez Lemon :: http://juicystudio.com/
function externalLinks()
{
    var objCurrent, objReplacement;
    if (document.getElementsByTagName)
    {
        var objAnchors = document.getElementsByTagName('a');
       
        for (var iCounter=0; iCounter < objAnchors.length; iCounter++)
        {
            if (objAnchors[iCounter].getAttribute('href') &&objAnchors[iCounter].getAttribute('rel') == 'external')
            {
                objAnchors[iCounter].onclick = function(event){return launchWindow(this, event);}
                objAnchors[iCounter].onkeypress = function(event){return launchWindow(this, event);}
                if (document.replaceChild)
                {
                    objCurrent = objAnchors[iCounter].firstChild;
                    if (objCurrent.nodeType == 3) // Text node
                    {
                        objReplacement = document.createTextNode(objCurrent.data + ' (opens in a new window)');
                        objAnchors[iCounter].replaceChild(objReplacement, objCurrent);
                    }
                    else if (objCurrent.alt) // Current element is an image
                    {
                        objReplacement = objCurrent;
                        objReplacement.alt = objCurrent.alt + ' (opens in a new window)';
                        try
                        {
                            objAnchors[iCounter].replaceChild(objReplacement, objCurrent);
                        }
                        catch(e){}
                    }
                }
            }
        }
    }
}

function launchWindow(objAnchor, objEvent)
{
    var iKeyCode;
    if (objEvent && objEvent.type == 'keypress')
    {
        if (objEvent.keyCode)
            iKeyCode = objEvent.keyCode;
        else if (objEvent.which)
            iKeyCode = objEvent.which;
        if (iKeyCode != 13 && iKeyCode != 32)
            return true;
    }
    return !window.open(objAnchor);
}

var switcher;
