/**
* This class forms the core of the client side representation of our trees.
*/
function altbaslik( value, text, altdizin ) {
	this.value = value;
	this.text = text;
	this.children = altdizin;
}

// rtti for javascript...
altbaslik.prototype.isNode = true;

altbaslik.prototype.getText = function () {
	return this.text;
}

altbaslik.prototype.getValue = function () {
	return this.value;
}

altbaslik.prototype.getChildren = function () {
	return this.children;
}

function secilidegerial( kutusec ) {
   return kutusec.options[kutusec.selectedIndex].value;
}

function dinleyiciYukle( deger ) {
	if ( window.addEventListener )
	   window.addEventListener( "load", deger, false );
	else if ( window.attachEvent )
		window.attachEvent( "onload", deger );
}

function sonucdegistir( object, listener ) {

	if (object.addEventListener) {
		object.addEventListener("change", listener, false);
	}
	else if (object.attachEvent ) {
		object.attachEvent("onchange", listener);
	}
}

function kutusec( baglantikutusec ) {

	baglantikutusec.jsRepresentation = this;
	if (baglantikutusec.addEventListener) {
		baglantikutusec.addEventListener("change", this.changed, false);
	}
	else if ( baglantikutusec.attachEvent ) {
		baglantikutusec.attachEvent("onchange", this.changed);
	}


	this.box = baglantikutusec;
	this.subscribers = new Array();
	this.nodeOptions = new Array();
}


/**
* Returns a list of appropriate sub selections for the currently selected option.
*/
kutusec.prototype.getSubSelections =  function() {
	return this.nodeOptions[ this.box.value ];
}

// public:
kutusec.prototype.changed = function () {
	
	try {
		widget = event.srcElement;
	}
	catch(e) {
		widget = this;
	}

	widget.jsRepresentation.publishChangeEvent();
}

// public:
kutusec.prototype.addChangeEventSubscriber = function( changeEventSubscriber ) {
	this.subscribers.push( changeEventSubscriber );
}

// public:
kutusec.prototype.handleChangeEvent = function( htmlEventSource, jsEventSource ) {
	this.setOptions(jsEventSource.getSubSelections());
}

// public:
kutusec.prototype.setOptions = function( ArrayOfOptions ) {

	this.clearOptions();
	for ( var i = 0 ; i < ArrayOfOptions.length ; i++ )
		this.addOptionNode(ArrayOfOptions[i]);
		
	this.publishChangeEvent();
}

// public:
kutusec.prototype.setSelectedValue = function( value ) {
	for ( var i = 0, n = this.box.options.length ; i < n ; i++ ) {
		if ( this.box.options[i].value == value ) {
			this.box.options[i].selected = true;
			this.publishChangeEvent();
			break;
		}
	}
}

// private:
kutusec.prototype.publishChangeEvent = function() {
	for ( var i = 0 ; i <  this.subscribers.length ; i++ ) {
		subscriber = this.subscribers[i];
		subscriber.handleChangeEvent( this.box, this );
	}
}

// private:
kutusec.prototype.addOptionNode = function( NodeFromTree ) {
	this.box.options[ this.box.options.length ] = new Option( NodeFromTree.getText(), NodeFromTree.getValue() );
	this.nodeOptions[ NodeFromTree.getValue() ] = NodeFromTree.getChildren();
}

// private:
kutusec.prototype.clearOptions = function() {
	while ( this.box.options.length > 0 )
		this.box.options[ this.box.options.length - 1 ] = null;
}

/**
* This box will only display if there are options to select.
*/
function kaybolmakutusec(baglantikutusec) {
	return new kutusec(baglantikutusec);
}

kaybolmakutusec.prototype = kutusec.prototype;

kaybolmakutusec.prototype.setOptions = function( ArrayOfOptions ) {

	this.clearOptions();	
	if ( !ArrayOfOptions || ArrayOfOptions.length == 0 ) {
		this.box.style.display = "none";
	}
	else {
		this.box.style.display = "inline";
		for ( var i = 0 ; i < ArrayOfOptions.length ; i++ )
			this.addOptionNode(ArrayOfOptions[i]);		
	}
	
	this.publishChangeEvent();
}

	
