/**
* Styleswitch stylesheet switcher built on jQuery
* Under an Attribution, Share Alike License
* By Kelvin Luck ( http://www.kelvinluck.com/ )
**/

(function($) {
    $(document).ready(function() {
        $('.styleswitch').click(function() {
            switchStylestyle(this.getAttribute("rel"));
            return false;
        });
        var c = readCookie('style');
        if (c) switchStylestyle(c); //else switchStylestyle('base');

        $('link[rel*=style][title]').each(function(i) {
            if (this.getAttribute('href') == '/Content/Common/Html/Style/comex.css') {
		this.setAttribute('title', 'comex');
		this.disabled = false;
	    }
        });

    });

    function switchStylestyle(styleName) {
	var colour = '/Content/Common/Html/Style/' + styleName + '.css';

        if (checkCssExist(colour) == false)
            addCss(colour, styleName);

        $('link[rel*=style][title]').each(function(i) {
            if (this.getAttribute('title') != 'default' && this.getAttribute('title') != "base") this.disabled = true;
            if (this.getAttribute('title') == "base") this.disabled = false;
            if (this.getAttribute('title') == styleName) this.disabled = false;
        });

        createCookie('style', styleName, 365);
		
   }

    function addCss(file, styleName) {
        var fileref = document.createElement("link");
        fileref.setAttribute("rel", "stylesheet");
        fileref.setAttribute("type", "text/css");
        fileref.setAttribute("title", styleName);
        fileref.setAttribute("href", file);
        if (typeof fileref != "undefined")
            document.getElementsByTagName("head")[0].appendChild(fileref);
    }

    function checkCssExist(filename) {
        var exists = 0;
        var targetelement = "link"; //determine element type to create nodelist from
        var targetattr = "href"; //determine corresponding attribute to test for
        var allsuspects = document.getElementsByTagName(targetelement)
        if (allsuspects.length > 26) {
            for (var i = allsuspects.length; i >= 0; i--) { //search backwards within nodelist for matching elements to remove
                if (allsuspects[i] && allsuspects[i].getAttribute(targetattr) != null && allsuspects[i].getAttribute('href') != filename && allsuspects[i].getAttribute('title') != "default" && allsuspects[i].getAttribute('title') != "base")
                    allsuspects[i].parentNode.removeChild(allsuspects[i]); //remove element by calling parentNode.removeChild()
            }
        }

        for (var i = allsuspects.length; i >= 0; i--) { //search backwards within nodelist for matching elements to remove
            if (allsuspects[i] && allsuspects[i].getAttribute(targetattr) != null && allsuspects[i].getAttribute('href') == filename)
                exists = 1;
        }

        if (exists == 1)
            return true;
        else
            return false;
    }
})(jQuery);
// cookie functions http://www.quirksmode.org/js/cookies.html

    function getCurrentStyleSheet()
    {
		var c = readCookie('style');
        if (c) return c; else return 'base';

		return null;
    }

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}
// /cookie functions
