window.onresize = do_select_layout;

function clear_search(field) {
	field.value = "";
	search_selected = true;
}

function set_active_style(id) {
	var link, style
	
	// Test all the links in the document
	var links = document.getElementsByTagName("link");
	for ( i = 0; (link = links[i]); i++ ) {
		// Process only style links
		if ( link.getAttribute("rel").indexOf("style") != -1 ) {
			link.disabled = true;
		}
	}
	// Test all the styles in the document
	var styles = document.getElementsByTagName("style");
	for ( i = 0; (style = styles[i]); i++ ) {
		style.disabled = true;
	}
	// Enable the base style and the one specified by the caller
	base = document.getElementById("base_style");
	selected = document.getElementById(id);
	base.disabled = false;
	selected.disabled = false;
}

function do_select_layout() {
	// Don't do anything further if in print layout
	prn = document.getElementById("print_style");
	if ( !prn.disabled ) return;

	// select a stylesheet based on window size
	if ( parseInt(navigator.appVersion)>3 ) {
		if (navigator.appName.indexOf("Microsoft") != -1) {
			width = document.documentElement.clientWidth;
			if ( width == 0 ) {
				width = document.body.clientWidth;
			}
		}
		else { // Firefox, Opera, Safari, etc
			width = window.innerWidth;
		}
		select_layout(width);
	};
}

function select_layout(size) {
	if ( size >= 1240 )
		set_layout_style('size_large');
	else if ( size >= 980 )
		set_layout_style('size_medium');
	else
		set_layout_style('size_small');
}

function set_layout_style(style_id) {
	var links = document.getElementsByTagName("link");
	for ( i = 0; (link = links[i]); i++ ) {
		// Disable all style links related to page layout size
		if ( link.getAttribute("rel").indexOf("style") != -1 ) {
			id = link.getAttribute("id");
			if ( id.indexOf("size") == 0 ) {
				if ( id == style_id )
					link.disabled = false;
				else
					link.disabled = true;
			}
		}
	}
}	

function normal_view() {
	window.location.reload();
}
