function clear_notes() {
	noteCont = document.getElementById( "note" );
	if ( noteCont )
		noteCont.innerHTML = "";
}

function add_note( note ) {
	noteCont = document.getElementById( "note" );
	if ( noteCont )
		noteCont.innerHTML = noteCont.innerHTML + note + "<br />";
}

function post_load() {
	for ( var i in postLoadFunctions ) {
		postLoadFunctions[ i ]();
	}
}
postLoadFunctions = [];

function register_post_load_function( func ) {
	postLoadFunctions.push( func );
}

confUpdateFunctions = {}
function register_conf_updater( section, conf_updater ) {
	confUpdateFunctions[section] = conf_updater;
}

function get_cookie( name_ ) {
	try {
		var value = "";
		if ( document.cookie.length > 0 ) {
			var search = name_ + "=";
			var offset = document.cookie.indexOf( search );
			if ( offset != -1 ) { /* if cookie exists */
				offset += search.length; /* set index of beginning of value */
				var end = document.cookie.indexOf( ";", offset ); /* set index of end of cookie value */
				if ( end == -1 ) {
					end = document.cookie.length;
				}
				value = unescape( document.cookie.substring( offset, end ) );
			}
		}
		return value;
	} catch ( e ) {
		add_note( "Exception: " + e );
		return ( "" );
	}
}

function set_cookie( name_, value_, expireDays_ ) {
	try {
		var expDate = new Date();
		expDate.setDate( expDate.getDate() + expireDays_ );
		document.cookie = name_ + "=" + escape( value_ ) /*+ "; domain=.codestation.org; path=/"*/ + ( ( typeof ( expireDays_ ) != 'undefined' ) ? "" : "; expires=" + expDate.toUTCString() );
	} catch ( e ) {
		add_note( "Exception: " + e );
	}
}

function transform( element, functor ) {
	if ( ( element !== null ) && ( element.id != "note" ) ) {
		functor( element );
		if ( element.hasChildNodes() ) {
			for ( var i in element.childNodes ) {
				if ( element.childNodes[i].tagName !== undefined ) {
					transform( element.childNodes[i], functor );
				}
			}
		}
	}
}

function get_state_window( id ) {
	try {
		var state = "";
		if ( document.getElementById( id ).parentNode.style.display == "none" )
			state = "0";
		else
			state = "1";
		return ( state );
	} catch ( e ) {
		add_note( "Exception: " + e );
		return ( "" );
	}
}

function get_state_rollup( id ) {
	try {
		var state = "";
		if ( document.getElementById( id ).style.display == "none" )
			state = "0";
		else
			state = "1";
		return ( state );
	} catch ( e ) {
		add_note( "Exception: " + e );
		return ( "" );
	}
}

function get_state( type, id ) {
	try {
		var state = "0";
		if ( type == "window" )
			state = get_state_window( id );
		else if ( type == "rollup" )
			state = get_state_rollup( id );
		return ( state );
	} catch ( e ) {
		add_note( "Exception: " + e );
		return ( "" );
	}
}

function rollup( widget, tag, section, compressParent ) {
	try {
		var element = document.getElementById( tag );
		var label = widget.innerHTML.substring( 1 );
		if ( element.style.display == "none" ) { /* expand */
			widget.innerHTML = "-" + label;
			element.style.display = "block";
			if ( typeof ( compressParent ) != 'undefined' ) {
				element.parentNode.style.paddingTop = element.parentNode.style.paddingTopOrig;
				element.parentNode.style.paddingBottom = element.parentNode.style.paddingBottomOrig;
			}
		} else { /* rollup */
			widget.innerHTML = "+" + label;
			element.style.display = "none";
			if ( typeof ( compressParent ) != 'undefined' ) {
				element.parentNode.style.paddingTopOrig = element.parentNode.style.paddingTop;
				element.parentNode.style.paddingTop = "2px";
				element.parentNode.style.paddingBottomOrig = element.parentNode.style.paddingBottom;
				element.parentNode.style.paddingBottom = "2px";
			}
		}
		if ( typeof ( section ) != 'undefined' )
			update_conf( section );
	} catch ( e ) {
		add_note( "Exception: " + e );
	}
}

function minimize( tag, section ) {
	try {
		document.getElementById( tag ).parentNode.style.display = "none";
		document.getElementById( "min_" + tag ).style.display = "inline";
		if ( typeof ( section ) != 'undefined' )
			update_conf( section );
	} catch ( e ) {
		add_note( "Exception: " + e );
	}
}

function restore( tag, section ) {
	try {
		document.getElementById( "min_" + tag ).style.display = "none";
		document.getElementById( tag ).parentNode.style.display = "block";
		if ( typeof ( section ) != 'undefined' )
			update_conf( section );
	} catch ( e ) {
		add_note( "Exception: " + e );
	}
}

function handle_icon( element ) {
	try {
		var attrClass = element.getAttribute( "class" );
		if ( ( attrClass !== null ) && ( attrClass.indexOf( "close-button" ) >= 0 ) ) {
			element.style.display = "block";
		}
	} catch ( e ) {
		add_note( "Exception: " + e + " " + typeof ( element ) );
	}
}	

function apply_conf() {
	try {
		var conf = get_cookie( "codestation-client" );
		auth = document.getElementById( "auth" );
		if ( ( conf.length == 0 ) || ( conf[0] == '0' ) )
			rollup( auth.parentNode.childNodes[1], "auth" );
	} catch ( e ) {
		add_note( "Exception: " + e );
	}
}

function xml_http_post( url, action, subject ) {
	var xmlHttpReq = false;
	var self = this;
	if ( window.XMLHttpRequest ) // Mozilla/Safari
		self.xmlHttpReq = new XMLHttpRequest();
	else if ( window.ActiveXObject ) // IE
		self.xmlHttpReq = new ActiveXObject( "MicroSuck.XMLHTTP" );
	else {
		add_note( "Your browser does not support AJAX." );
		return;
	}
	self.xmlHttpReq.open( 'POST', url, true );
	self.xmlHttpReq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
	self.xmlHttpReq.onreadystatechange = function() {
		try {
			if ( self.xmlHttpReq.readyState == 4 ) {
				if ( self.xmlHttpReq.status != 200 ) {
					alert( "Your request encountered some problems." );
				} else {
//					alert( "ALERT: " + self.xmlHttpReq.statusText );
						update_page( action, self.xmlHttpReq.responseText );
				}
			}
		} catch ( e ) {
			add_note( "Exception: " + e );
		}
	}
//	alert( "ok: " + get_query_string() );
	if ( action != "clear_note" ) {
		self.xmlHttpReq.send( get_query_string( action, subject ) );
	} else {
		document.getElementById( "note" ).innerHTML = "";
	}
}

function get_query_string( action, subject ) {
	switch ( action ) {
		case "filter" : {
			var form = document.forms['query'];
			var word = form.query.value;
			qstr = 'application=codestation&action=ajax&type=query&query=' + escape( word );  // NOTE: no '?' before querystring
			break;
		}
		case "note" : {
			qstr = 'application=codestation&action=ajax&type=note&note=' + subject;
			break;
		}
	}
	return qstr;
}

function update_page( action, str ) {
	switch ( action ) {
		case "filter" : {
			document.getElementById( "result" ).innerHTML = str;
			document.getElementById( "note" ).innerHTML = "";
			break;
		}
		case "note" : {
			document.getElementById( "note" ).innerHTML = str;
			break;
		}
	}
}

function main_update_conf() {
	try {
		var conf = get_state( "rollup", "auth" );
		set_cookie( "codestation-client", conf );
	} catch ( e ) {
		add_note( "Exception: " + e );
	}
}

function update_conf( section ) {
	confUpdateFunctions[section]();
}

(function() {
	register_post_load_function( apply_conf );
	register_conf_updater( "", main_update_conf );
})(); 


