//define List Object
function ListObject( id, name, parentId, isPopulated ) {
	this.id = id;
	this.name = name;
	this.parentId = ( parentId ) ? parentId : 0;
	this.childs = [];
	this.hasEntries = isPopulated;
}

var o = ListObject.prototype;

o.getName = function() { return this.name; }

o.getId = function() { return this.id; }

o.getParentId = function() { return this.parentId; }

o.addChild = function( object ) {
	 this.childs[ this.childs.length ] = object;
}

o.hasChilds = function() { return this.childs.length > 0 }

o.getChilds = function() { return this.childs; }

o.isPopulated = function() { return this.hasEntries; }

delete o;
// end define

// define AbstracLister
function AbstractLister() {
	this.lookUpTable = {};
	this.parentList = [];
}

var o = AbstractLister.prototype;

o.add = function( object ) {
	this.lookUpTable[ object.getId() ] = object;
	if( !object.getParentId() ) {
		this.parentList[ this.parentList.length ] = object;
	}
}

o._build = function() {
	for( var id in this.lookUpTable ) {
		var object = this.lookUpTable[ id ];
		var parentId = object.getParentId()
		if( parentId ) {
			this.lookUpTable[ parentId ].addChild( object );
		}
	}
}

delete o;
//end define

//define OSXStyleListView
function OSXStyleListView() {
	this.builder = null;
	this.activeSelect = null;
}

var o = OSXStyleListView.prototype = new AbstractLister();

o.init = function( divName ) {
	this.builder = new SelectBuilder( document.getElementById( divName ), this );
	this._build();
	this.activeSelect = this.builder.add();
	this._fillSelect( this.parentList, this.activeSelect );
}

o.handleOnChange = function () {
	//original scope = calling Select Object
	var scope = this[ 'scope' ];
	if( this != scope.activeSelect ) {
		scope.activeSelect = this;
	}
	scope.builder.remove( scope._getActiveSelectNumber() );
	if( scope.activeSelect.selectedIndex == - 1 ) { return true; }
	var object = scope.lookUpTable[ scope._getActiveId() ];
	if( object.hasChilds() ) {
		var select  = scope.builder.add( scope._getActiveSelectNumber() );
		scope._fillSelect( object.getChilds(), select );
	} 
}

o._checkSelection = function()  {
	if( this.activeSelect.selectedIndex == - 1 ) {
		alert( 'Keine Kategorie ausgew�hlt' );
		return false;
	 }
	 return true;
}

o._checkSubCategories = function() {
	if( this.lookUpTable[ this._getActiveId() ].hasChilds() ) {
		alert( 'Die gewaehlte Kategorie enthaelt Unterkategorien. Es koennen keine Zeichnungen hinzugefuegt werden!' );
		return false;
	}
	return true;
}

o._getActiveId = function() { 
	return this.activeSelect.options[ this.activeSelect.selectedIndex ].value;
}

o._getActiveSelectNumber = function() {
	var id = this.activeSelect.id;
	return parseInt( id.split( '_' )[ 1 ] );
}

o._fillSelect = function ( list, select ) {
	for ( var i = 0; i < list.length; i++ ) {
		var o = new Option( list[ i ].getName(), list[ i ].getId() );
		if( list[ i ].isPopulated() ) {
			o.style.color = '#ee4444';
		}
		select.options[ i ] = o;
	}
	select.selectedIndex = -1;
}
delete o;
//end define

//define CategoryLister 
function CategoryLister() { ; }

var o = CategoryLister.prototype = new OSXStyleListView()

o.submitEdit = function() {
	if( !this._checkSelection() ) { return false; }
	document.location = 'category.php?id=' + this._getActiveId() + this.getPageMode();
	return false;
}

o.submitAdd = function() {
	if( !this._checkSelection() ) { return false; }
	if( this.lookUpTable[ this._getActiveId() ].isPopulated() ) {
		alert( 'Die gewaehlte Rubrik enthaelt Zeichnungen! Es koennen keine Unterkategorien mehr angelegt werden' );
		return false;
	}
	document.location = 'category.php?parentid=' + this._getActiveId() + this.getPageMode();
	return false;
}

o.submitDelete = function() {
	if( !this._checkSelection() ) { return false; }
	if( window.confirm( 'Soll die Kategorie ' + this.lookUpTable[ this._getActiveId() ].getName() + ' geloescht werden?' ) ) {
		document.location = 'categoryProcess.php?to_delete=' + this._getActiveId() + this.getPageMode();
	}
	return false;
}

o.submitNewDrawing = function() {
	if( !this._checkSelection() || !this._checkSubCategories() ) { return false; }
	document.location = 'drawing.php?cid=' + this._getActiveId() + this.getPageMode();
	return false;
}

o.getPageMode = function() {
	return '&mode=' + document.forms[ 0 ].mode.value;
}

delete o;
//end define

//define SelectBuilder
function SelectBuilder( target, callerRef ) { 
	this.selectCount = 0;
	this.target = target;
	this.caller = callerRef;
 }

var o = SelectBuilder.prototype;

o.add = function( number ) {
	var identifier = 'select_' + ( number  + 1 );
	if( document.getElementById( identifier ) ){
		return document.getElementById( identifier );
	}
	this.selectCount++;
	var _select = document.createElement( 'SELECT' );
	_select.id = 'select_' + this.selectCount;
	_select.size = 10;
	_select[ 'scope' ] = this.caller;
	_select.onchange = this.caller.handleOnChange;
	//not nice, doubles execution
	_select.onclick = this.caller.handleOnChange;
	this.target.appendChild( _select );
	return _select;
}

o.remove = function( number ) {
	while( number < this.selectCount ) {
		if( this.selectCount ) {
			var toRemove = document.getElementById( 'select_' + this.selectCount );
			this.target.removeChild( toRemove );
			this.selectCount--;
		}
	}
}	
delete o;
//end define

//define TreeView
function TreeView( targetDiv ) { ; }

var o = TreeView.prototype = new AbstractLister();

o.init = function( targetDiv ) {
	this.openNodes = null;
	this.level = 0;
	this.treeHeight = 0;
	this.nodeCount = 0;
	this.container = document.getElementById( targetDiv );
	this.imgBasePath = '/drawings/';
	Preloader.preload( this.imgBasePath + 'img/pixel.gif' );
	Preloader.preload( this.imgBasePath + 'img/treeLane.gif' );
	Preloader.preload( this.imgBasePath + 'img/treeTCrossClosed.gif' );
	Preloader.preload( this.imgBasePath + 'img/treeTCrossOpen.gif' );
	Preloader.preload( this.imgBasePath + 'img/treeEnd.gif' );
	this._build();
	this._buildTree( this.parentList, this.container );
}
	
o._buildTree = function( list, target, skipLanes ) {
	
	for( var i = 0; i < list.length; i++) {
		var _table = HtmlHelp.getTable();
		var _tBody = HtmlHelp.getTBody();
		var _tr = HtmlHelp.getRow();
		var object = list[ i ];
		var _div = HtmlHelp.getDiv();
		var isEndNode = ( i == list.length - 1 );
		if( this.level ) {
			this._insertLaneImages( _tr, skipLanes );
			_div.style.display = 'none';
		} else {
			_div.style.display = 'block';
		}
		var _contentTD = document.createElement( 'TD' );
		if( object.hasChilds() ) {
			this._createTriggerDiv( object, _contentTD,_div, isEndNode );
		} else {
			this._createDiv( object, _contentTD, isEndNode );
		}
		_tr.appendChild( _contentTD );
		_tBody.appendChild( _tr );
		_table.appendChild( _tBody );
		_div.appendChild( _table );
		target.appendChild( _div );
		if( _div.style.display == 'block' ) {
			this.treeHeight += _div.offsetHeight;
		}
		if( object.hasChilds() ) {
			this.level++;
			this._buildTree( object.getChilds(), _div, isEndNode  );
			this.level--;
		}
	}
}

o._insertLaneImages = function ( _tr, skipLanes ) {
	for( var j = 0; j < this.level; j++  ) {
		var _td = document.createElement( 'TD' );
		if( skipLanes  && j == ( this.level - 1 ) ) {
			_td.setAttribute( 'background', this.imgBasePath + 'img/pixel.gif' );
		} else {
			_td.setAttribute( 'background', this.imgBasePath + 'img/treeLane.gif' );
		}
		_td.appendChild( HtmlHelp.getImage( 'img/pixel.gif'  ) );
		_tr.appendChild( _td );
	}
}

o._handleOnClick = function(){
	var nodes = this[ '__parentNode__' ].childNodes;
	var scope = this[ '__scope__' ];
	if( scope.openNodes && this[ '__parentNode__' ].parentNode.childNodes.item( 0 ) != scope.openNodes.item( 0 ) ) {
		if( scope.openNodes.item( 0 )!= nodes.item( 0 )  ) {
			scope._hide( scope.openNodes );
			scope.openNodes = nodes;
		}
	}
	if( scope.openNodes == null ) {  scope.openNodes = nodes; }
	scope._updateDisplay( nodes );
}

o._showDrawings = function() {
	var _div = document.getElementById( 'iholder'  );
	var _frame = _div.childNodes.item( 0 );
	_frame.src ='index.php?id=97&category_id=' + this[ '__categoryId__' ] + '&languageid=' + languageid; 
	return false;
}

o._updateDisplay = function( nodes ) {
	for ( var i = 0; i <  nodes.length; i++ ) {
		var item = nodes.item( i );
		this._checkImage( item );
		if( item.nodeType != 1 || item.tagName != 'DIV' ) {
			continue;
		}
		if( item.style.display == 'block' ) {
			this.treeHeight -= item.offsetHeight;
			item.style.display = 'none';
			if( item.hasChildNodes() ) {
				this._hide( item.childNodes );
			}
		} else {
			item.style.display = 'block';
			this.treeHeight += item.offsetHeight;
		}
	}
	this._updateFiller();
}

o._hide = function( nodes ) {
	for ( var i = 0; i <  nodes.length; i++ ) {
		var item = nodes.item( i )
		this._checkImage( item, true );
		if( item.nodeType != 1 || item.tagName != 'DIV' ) {
			continue;
		}
		this.treeHeight -= item.offsetHeight;
		item.style.display = 'none'; 
		if( item.hasChildNodes() ) {
			this._hide( item.childNodes );
		}
	}
}

o._swapSource = function( node, forceClose ) {
	var src = node.src;
	if( src.indexOf( 'img/treeTCrossClosed.gif' ) != -1 || src.indexOf( 'img/treeTCrossOpen.gif' ) != -1 ) {
		node.src = this.imgBasePath + ( ( src.indexOf( 'img/treeTCrossClosed.gif' ) != -1  &&  !forceClose ) ?  'img/treeTCrossOpen.gif' : 'img/treeTCrossClosed.gif' );
	}
}

o._checkTdNodes = function( nodes, forceClose ) {
	for( var i = 0; i < nodes.length; i++ ) {
		var node = nodes.item( i ).childNodes.item( 0 );
		if( node.tagName == 'TABLE' || node.tagName == 'A' ) {
			this._checkImage( node, forceClose  );
		}
	}
}

o._checkImage = function( node, forceClose ) {
	if( node.tagName == 'A' ) { 
		if(  node.childNodes.item( 0 ).tagName == 'IMG' ) {
			this._swapSource(  node.childNodes.item( 0 ), forceClose );
		}
		return true;
	}
	if( node.tagName == 'TABLE' ) {
		this._checkTdNodes( node.childNodes.item( 0 ).childNodes.item( 0 ).childNodes, forceClose );
	}
}

o._setTriggerAnchorAttributes  = function( _anchor, _div ) {
	_anchor.onclick = this._handleOnClick;
	_anchor[ '__parentNode__' ] = _div;
	_anchor[ '__scope__' ] = this;
}

o._createTriggerDiv = function( object, _td,_div, isEndNode ) {
	var _table = HtmlHelp.getTable();
	var _tBody = HtmlHelp.getTBody();
	var _tr = HtmlHelp.getRow();
	var _textNode = document.createTextNode( object.getName() );
	var src = ( isEndNode ) ? 'img/treeTCrossClosed.gif' : 'img/treeTCrossClosed.gif';
	var _tdLink = HtmlHelp.getTextCell();
	var _imgAnchor = HtmlHelp.getAnchor();
	this._setTriggerAnchorAttributes( _imgAnchor, _div )
	var _anchor = HtmlHelp.getAnchor();
	this._setTriggerAnchorAttributes( _anchor, _div )
	_anchor.appendChild( _textNode );
	_tdLink.appendChild( _anchor );
	var _tdImage = HtmlHelp.getImageCell( src, isEndNode, _imgAnchor );
	_tr.appendChild( _tdImage );
	_tr.appendChild( _tdLink );
	_tBody.appendChild( _tr );
	_table.appendChild( _tBody );
	_td.appendChild( _table );
}

o._createDiv = function( object, _td, isEndNode ) {
	var _table = HtmlHelp.getTable();
	var _tBody = HtmlHelp.getTBody();
	var _tr = HtmlHelp.getRow();
	var src = ( isEndNode ) ? 'img/treeEnd.gif' : 'img/treeTCross.gif';
	var _tdImage = HtmlHelp.getImageCell( src, isEndNode );
	var _tdLink = HtmlHelp.getTextCell();
	var _textNode = document.createTextNode( object.getName() );
	if( object.isPopulated() ) {
		var _anchor =  HtmlHelp.getAnchor();
		_anchor.onclick = this._showDrawings;
		_anchor[ '__categoryId__' ] = object.getId();
		_anchor.appendChild( _textNode )
		_tdLink.appendChild( _anchor );
	} else {
		_tdLink.appendChild( _textNode );
	}
	_tr.appendChild( _tdImage );
	_tr.appendChild( _tdLink );
	_tBody.appendChild( _tr );
	_table.appendChild( _tBody );
	_td.appendChild( _table );
}

o._updateFiller = function() {
	var _filler = document.getElementById( 'filler' );
	if( !_filler ) { return true; }
	if( this.treeHeight >= 360 ) {
		_filler.style.height = this.treeHeight - 360;
	} else {
		_filler.style.height = 0;
	}
}
	
delete o;
//end define

function HtmlHelper() { 
	this.imgBasePath = '/drawings/'; 
}

o = HtmlHelper.prototype;

o.getTable = function() {
	var table = document.createElement( 'TABLE' );
	table.setAttribute( 'cellPadding', 0 );
	table.setAttribute( 'cellSpacing', 0 );
	table.setAttribute( 'border', 0 );
	return table;
}

o.getTBody = function() {
	var tBody = document.createElement( 'TBODY' );
	tBody.setAttribute( 'vAlign' , 'top' )
	return tBody;
}
o.getRow = function() {
	var tr = document.createElement( 'TR' );
	tr.setAttribute( 'vAlign', 'top' );
	return tr;
}

o.getTextCell = function() {
	var tdLink = document.createElement( 'TD' );
	tdLink.style.paddingTop = '9px';
	tdLink.style.fontSize = '11px';
	return tdLink;
}

o.getImage = function( src ) {
	var img = document.createElement( 'IMG' );
	src = this.imgBasePath + src;
	img.setAttribute( 'src',  src );
	img.setAttribute( 'width', 18 );
	img.setAttribute( 'height', 20 );
	img.setAttribute( 'border', 0 );
	img.setAttribute( 'hspace', 0 );
	img.setAttribute( 'vspace', 0 );
	return img;
}

o.getDiv = function() {
 	var div = document.createElement( 'DIV' );
	div.style.fontSize = '11px';
	div.style.fontFamily = 'verdana';
	div.style.vAlign = 'top';
	return div;
}

o.getImageCell = function( imgSrc, isEndNode, _anchor ) {
	var tdImage = document.createElement( 'TD' );
	var img = this.getImage( imgSrc );
	if( isEndNode ) {
		tdImage.setAttribute( 'background', this.imgBasePath + 'img/pixel.gif' );
	} else {
		tdImage.setAttribute( 'background', this.imgBasePath + 'img/treeLane.gif' );
	}
	if( _anchor ) {
		_anchor.appendChild( img );
		tdImage.appendChild( _anchor );
	} else {
		tdImage.appendChild( img );
	}
	return tdImage;
}

o.getAnchor = function() {
	var _anchor = document.createElement( 'A' );
	_anchor.setAttribute( 'href', 'javascript:void( 0 )' );
	_anchor.style.fontSize = '11px';
	return _anchor;
}

delete o;

function ImagePreloader() { 
	this.images = [];
 }

o = ImagePreloader.prototype;

o.preload = function( src ) {
	var img = new Image();
	img.src = src;
	this.images[ this.images.length ] = img;
}

delete o;
//pseudo static
var HtmlHelp = new HtmlHelper();
var Preloader = new ImagePreloader();
