/**
* 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 showFlash();
//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;
        });

     /*   $('link[rel*=style][title]').each(function(i) {
            if (this.getAttribute('title') == 'base') this.disabled = true;
        });*/
        

        createCookie('style', styleName, 365);

	showFlash();
    }

    function showFlash()
    {
      var target = getCurrentTarget();
        var styleSheet = getCurrentStyleSheet();
        var firstMod = getCurrentMod();
        var flashvars = { contentTarget: target,
        activeColor: styleSheet,
            firstMod: firstMod,
            music: getMusicSwitch()
        };
        var params = { wmode: "opaque" };
        var attributes = {};
        swfobject.embedSWF("/Content/@@CompanyName@@/Html/Flash/homes.swf?labelHome1='Start to decorate'&labelHome2='Explore Combine'&labelHome3='Inspiration'&labelHome4='Find the right product'&labelBuss1='Professional Painters'&labelBuss2='Residential'&labelBuss3='Commercial'&labelBuss4='Institutional'&labelBuss5='Architecs & Designers'&labelBuss6='Comex Industrial Coatings'", "flashContainer", "970", "330", "9.0.45.0", "", flashvars, params, attributes);
    }


    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 SetCurrentMod(mod) {
    createCookie('mod', mod, 365)
}

function getCurrentMod() {
    var c = readCookie('mod');
    if (c) return c; else return '';

    return null;
}

function SetMusicSwitch(value) {
    createCookie('music', value, 365)
}

function getMusicSwitch() {
    var c = readCookie('music');
    if (c) return c; else return 'on';
}

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

