var cookies = new Object();

function get_filename(href) {
	var end = href.lastIndexOf(".");
	var name = href.substr(0, end);
	end = name.lastIndexOf("/")+1;
	name = name.substr(end);
	return name;
}

function extractCookies() {
	var name, value;
	var beginning, middle, end;
	for (name in cookies) {
		cookies = new Object();
		break;
	}
	beginning = 0;
	while (beginning < document.cookie.length) {
		middle = document.cookie.indexOf('=', beginning);
		end = document.cookie.indexOf(';', beginning);
		if (end == -1) {
			end = document.cookie.length;
		}
		if ((middle > end) || (middle == -1)) {
			name = document.cookie.substring(beginning, end);
			value = "";
		} else {
			name = document.cookie.substring(beginning, middle);
			value = document.cookie.substring(middle + 1, end);
		}
		cookies[name] = unescape(value);
		beginning = end + 2
	}
}

function get_cookie_style () {
	extractCookies();
	if (cookies['dalbrook_active_style']) {
		return cookies['dalbrook_active_style'];
	} else {
		return '';
	}
}

function get_active_style () {

	var style_name = '';
	var style_handle;
	var i;
		
	for (i=0; i<document.styleSheets.length; i++) {
		style_handle = document.styleSheets[i];
		if (! style_handle.disabled) {
			style_name = get_filename(style_handle.href);
		}
	}
	return style_name;
}

function is_possible_style (possible_style_name) {

	var style_handle;
	var style_name;
	var i;
		
	for (i=0; i<document.styleSheets.length; i++) {
		style_handle = document.styleSheets[i];
		style_name = get_filename(style_handle.href);
		if (style_name == possible_style_name) {
			return true;
		}
	}
	return false;
}

function set_style_cookie (which_style) {
	expiry_date = new Date(2010,1,1);
	document.cookie = "dalbrook_active_style=" + which_style + "; expires=" + expiry_date.toUTCString();
	return true;
}

function set_active_style (which_style) {

	var this_style;
	var this_name;
	var i;
	var success = false;

	for (i=0; i<document.styleSheets.length; i++) {
		this_style = document.styleSheets[i];
		this_name = get_filename(this_style.href);
		if (this_name == which_style) {
			this_style.disabled = false;
			set_style_cookie(this_name);
			success = true;
		} else {
			this_style.disabled = true;
		}
	}
	return success;
}

function set_up_style_buttons () {
        var buttons_element = document.getElementById("buttons");
        if (buttons_element) {
                var buttons_text = "Select style:&nbsp;&nbsp;";
                for (var i=0; i<document.styleSheets.length; i++) {
                        var this_style = document.styleSheets[i];
                        var this_name = get_filename(this_style.href);
                        buttons_text = buttons_text +
                                "<button" +
                                " id='set_style_" + this_name + "' " +
                                " name='set_style_" + this_name + "' " +
                                " onclick=set_style('" + this_name + "')>" +
                                this_name + "<\/button>&nbsp;&nbsp;";
                }
                buttons_element.innerHTML = buttons_text
        }
}

function set_style(which_style) {
        set_active_style (which_style);
        set_style_cookie(which_style);
        disable_style_button (which_style);
}

function disable_style_button (which_style) {
        var buttons = document.body.getElementsByTagName('button');
        var tt;
        for (var i=0; i<buttons.length; i++) {
                this_button = buttons[i];
                if ('set_style_' == this_button.id.substring(0, 10)) {
                        if (this_button.id == 'set_style_' + which_style) {
                                this_button.disabled = true;
                        } else {
                                this_button.disabled = false;
                        }
                }
        }
}

function style_widget() {

        set_up_style_buttons();

        var which_style = get_cookie_style();

        if (which_style && is_possible_style(which_style)) {
                set_active_style(which_style);
        } else {
                which_style = get_active_style();
                set_style_cookie(which_style);
        }
        disable_style_button(which_style);
}

