/** AJAX HOOD creator
 *
 * ©2008 Dennis Hilmar <dennis.hilmar@gmail.com>
 * This software is licenced under the GPL v.2 licence
 *

Objects are the boxes in HOOD
These all have a parent and an array of children...

 **/


var cssStyle = '#rootHoodDiv {margin: auto auto; width: 70px; padding: 10px; vertical-align: middle; text-align: center; text-weight: bold; background-color: #EEE; border-top: 3px solid #DDD; border-left: 3px solid #DDD; border-bottom: 3px solid #BBB; border-right: 3px solid #BBB; -moz-user-select:none;}';
cssStyle += 'div.Hoodlevel {marign: 0px auto; padding: 13px; margin: 10px; height: 35px; text-align: center; background-color: #EEE; border: 1px dashed red; }';
cssStyle += 'div.rootDivEditor { background: transparent url(images/icons/edit.png) no-repeat scroll 50% 50%; cursor: pointer; float: right; height: 20px; width: 20px; z-index: 1000;}';

//cssStyle += 'div.Hoodlevel {font-weight: bold; color: #629ED2; float: right;}';
//GM_addStyle(cssStyle);



function HOODdiagram(div) 
{
	this.levels = 0;

	this.layoutCss='hood.css';
	this.offsetX=0;
	this.offsetY=0;

	this.mainDiv = document.getElementById(div);
	this.Objects = new Array();
	this.activeItem;

	this.MSIE  = navigator.userAgent.indexOf('MSIE')>=0?true:false;
	this.opera = navigator.appVersion.indexOf('Opera')>=0?true:false;

		/* Creating the HOOD root */
	this.applyTheme(cssStyle);

	this._createHOODroot();


		try{
			ajaxObj = new sack('index.php');
		}catch(e){
			alert('You need to include the AJAX(SACK) js file');
			return;
		}

}


/* DragableElementBox Class */
HOODdiagram.prototype = {
	// {{{ _createHOODroot()
		/**
		 *
		 *  This function initiates the elements used.
		 * 
		 * @private
		 */
	_createHOODroot : function()
	{
		var root = this._createHOODlevel(this.mainDiv);


		var rootDiv = elem('div', {id: 'rootHoodDiv'}, {}, 'HOOD');

		rootDiv.onclick = this.chooseObj;

		root.appendChild(rootDiv);



		this._createHOODlevel(rootDiv);
	}
	// }}}
	,
	// {{{ _createHOODroot()
		/**
		 *
		 *  This function initiates the elements used.
		 * 
		 * @private
		 */
	_createHOODlevel : function(Obj)
	{
		var levelDiv = elem('div', {className: 'Hoodlevel'}, {}, '');
		levelDiv.parentObj = Obj;

		levelDiv.appendChild(elem('div', {className: 'levelDisplay'},{}, ''));

		this.mainDiv.appendChild(levelDiv);

		return levelDiv;
	}
	// }}}
	,
	// {{{ chooseObj()
		/**
		 *
		 *  This function initiates the elements used.
		 * 
		 * @private
		 */
	chooseObj : function(e)
	{
		if (!e) e = window.event;
		var selectedObj = e.target || e.srcElement;

		if (this.activeItem == selectedObj)
		{
			selectedObj.style.background = '#EEE';
			this.activeItem = false;
			return;
		}

		selectedObj.style.background = '#629ED2 url(images/icons/HOODedit.png) no-repeat scroll 100% 0%';

		this.activeItem = selectedObj;
	}
	// }}}
	,
	// {{{ applyTheme()
  /**
   *
   *  Adding the new theme into the stylesheet
   *
   *  @param String stylesheet = Declaration of styles
   * 
   * @public
   */
	applyTheme : function(newStyle) 
	{

		if (typeof PRO_addStyle != 'undefined')			PRO_addStyle(newStyle, document);
		else if (typeof GM_addStyle != 'undefined')	GM_addStyle(newStyle);
		else if (typeof document.createStyleSheet != 'undefined')
		{
				this.stylesheet = document.createStyleSheet();

				newStyle = newStyle.replace(/;/g, " !important;");
				var styles = newStyle.split(/}/);
				for (var i=0; i < styles.length; i++)
				{
					var rule = styles[i].split(/{/);
					this.stylesheet.addRule(rule[0], rule[1]);
				}

		} else
		{
			try {
				this.stylesheet = document.createElement('style');
				this.stylesheet.setAttribute('type', 'text/css');
				this.stylesheet.innerHTML = newStyle;
				document.getElementsByTagName('head')[0].appendChild(this.stylesheet);
			} catch(e) {
				var warning = elem('div', {className: 'warning'}, {}, 'This browser does not support appending CSS styles!\nThis page uses AJAX to append CSS to the items... Please use another browser or just enable javascript to see the page in its fullest.');
				this.mainDiv.appendChild(warning);
			}
		}

	}
	// }}}
}

