/* Cookie Manager */
var Cookies = {
	init: function () {
		var allCookies = document.cookie.split('; ');
		for (var i=0;i<allCookies.length;i++) {
			var cookiePair = allCookies[i].split('=');
			this[cookiePair[0]] = cookiePair[1];
		}
	},
	create: function (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=/";
		this[name] = value;
	},
	erase: function (name) {
		this.create(name,'',-1);
		this[name] = undefined;
	}
};
Cookies.init();

/* Theme Manager: depends on Cookie Manager. */
var ThemeManager = {
	getTheme: function () {
         var theme = Cookies['ematheme'];
         if (theme != null && theme != "") {
             return theme;
         } else {
         /* If no cookie was found, create a new one.
            We set the default theme to 'manipur'. */
            Cookies.create('ematheme', 'kangleipak', 365);
            return 'kangleipak';
         }
    },
    loadTheme: function () {
        var themeName = ThemeManager.getTheme();
         
        /* In the HTML pages, we have one CSS link, with default
        theme value. We replace this as follows. */
        var links = document.getElementsByTagName('link');
        links[0].href = "../css/" + themeName + "/" + themeName + ".css";

        /* Set the correct theme in theme selector. */
        links = document.getElementsByTagName("select");
        for (i = 0; i < links.length; i++) {
            if (links[i].name == "theme") {
                links[i].value = themeName;
                links[i].selected="selected";
            }
        }
    },
    changeTheme: function (selection) {
        var newTheme = selection.options[selection.options.selectedIndex].value;
        Cookies.create('ematheme', newTheme, 365);
        ThemeManager.loadTheme();
    },
    /* This functions generates the theme selector and banner which will be
       displayed on the top of the pages. This will make inserting new
       themes and changing banner easier and less of a chore.
   
       To insert a new theme, just add the name of the theme to the
       themeElements array. The theme files must reside in css directory.
    */
    insertThemeSelector: function () {
	    /* Please change the following information. */
	    var elementSeparator = "|";
	    var themeElements = "gogreen|kangleipak|sangai|siroy";

	    /* Please leave the following untouched. */
        var themeArray = themeElements.split(elementSeparator);
        var banner = "<table class='banner' cellpadding='0' cellspacing='0'><tbody>" +
		"<tr><td class='theme-select'>Theme: " +
		"<select name='theme' onchange='ThemeManager.changeTheme(this);'>";
        for (i = 0; i < themeArray.length; i++) {
            banner += "<option value='" + themeArray[i] + "'>" +
            themeArray[i] + "</option>";
        }
        banner += "</select><br><br><br><br></td></tr>" +
        "<tr class='banner'><td class='banner'></td></tr></tbody></table>";
        document.write(banner);
    }
}


