var HtmlObjectsVersion = 1;

function TextMenu(menuOptions)
{
	function optionDiv(funct)
	{
		var opt=new Element('td',{ 'class':"htmlOption_td",
			events:
			{
			click: funct,
			mouseover: function()
			{
			this.setStyle('textDecoration','underline');
			//(new Fx.Tween(this)).start('opacity', 0, 1);//works with mootools 1.2 not joomla's
			},
			mouseout: function()
			{
				this.setStyle('textDecoration','none');
			}
			},
			styles:
			{
				//position: 'relative',
				cursor: 'pointer',
				color: 'blue'//,
					//	display: 'inline-block'

			}

		});
		return opt;
	}
	//TODO: rename this method to spacerTD since it is misleading
	function spacerDiv()
	{
		var space=new Element('td',{'class':"htmlOptionSpace_td"});
		//space.innerHTML=txt;
		//space.setStyle('position','relative');
		//space.setStyle('display', 'inline-block');
		//space.setStyle('textAlign', 'center');
		//if($defined(width))
		//	space.setStyle('width',width);
		return space;
	}
	function namedOptionDiv(funct, name)
	{
		var div=optionDiv(funct);
		div.innerHTML=name;
		return div;
	}
	var container=new Element('table',{'class':"htmlOptionsTable",styles:{height:"35px"}});
	this.options={};
	container.appendChild(new Element('td',{'class':"htmlOptionsLeft"}));
	var opts=this.options;
	var spacer=null; // null so the first iteration doesnt have a spacer
	$each(menuOptions,function(value,key)
			{
		var funct=function(){
			this.fireEvent('mouseout');
			this.fireEvent('mouseleave');
			value();

		};
		if($defined(spacer))
			container.appendChild(spacer);
		spacer=spacerDiv(); 
		opts[key]={};
		opts[key].element=namedOptionDiv(funct,key);
		opts[key].funct=funct;
		container.appendChild(opts[key].element);
			});
	container.appendChild(new Element('td',{'class':"htmlOptionsRight"}));
	this.container=container;
}
TextMenu.prototype.enable=function(key)
{
	//this.options[key].element.setStyle('position', 'relative');
	this.options[key].element.setStyle('cursor', 'pointer');
	this.options[key].element.setStyle('color', 'blue');
	//this.options[key].element.setStyle('display', 'inline-block');
	this.options[key].element.removeEvents();
	this.options[key].element.addEvent('click',this.options[key].funct);
	this.options[key].element.addEvent('mouseover',function(){
		this.setStyle('textDecoration','underline');
	});
	this.options[key].element.addEvent('mouseout',function(){
		this.setStyle('textDecoration','none');
	});

};
TextMenu.prototype.setTip=function(key,string)
{
	this.options[key].element.setAttribute('title',string);
	new mediaTips(this.options[key].element);

};
TextMenu.prototype.disable=function(key)
{
	this.options[key].element.setStyle('color', 'grey');
//	this.options[key].element.setStyle('display', 'inline-block');
	this.options[key].element.setStyle('cursor', '');

	this.options[key].element.removeEvents();
	this.options[key].element.addEvent('click',function(){});
	this.options[key].element.addEvent('mouseover',function(){});
	this.options[key].element.addEvent('mouseout',function(){});

};
TextMenu.prototype.rename=function(key,newName)
{
	this.options[key].element.innerHTML=newName;
};
/**
 * valueofDOM grabs the first nodeValue of the first node with matching tag name
 * or null if node exists.
 * @param domElement a DOMElement 
 * @param tagName the name of the desired leaf node contained in domElement
 * @return the nodeValue of the leaf node or null if it does not exits
 */
function valueofDOM(domElement, tagName)
{
	var item = domElement.getElementsByTagName(tagName).item(0);	
	return $defined(item)?item.firstChild.nodeValue:null;
}



function ripMedia(content,wrapper)
{
	var removeList=[];
	//alert(content.getElementsByTagName('object').length);
	var container=null;


	function rip(tag)
	{
		$each(content.getElementsByTagName(tag),function(value){
			if(container==null)
				container=new Element('div',{'class':'shockwaveMediaContent'});
			//value.parentNode.removeChild(value);
			removeList.push(value);

			var url=value.getAttribute("src");
			var type=value.getAttribute("type");
			var data=value.getAttribute("data");
			var i=0;
			if(type=="application/x-shockwave-flash")
			{
				if(url&&url.toLowerCase().indexOf("youtube")>=0)
				{	
					container.appendChild((new YouTubeContent(url)).youTubeImage(wrapper));
				}
				else if(data&&data.toLowerCase().indexOf("justin")>=0)
				{
					container.appendChild((new JustinContent(data,value.getAttribute("flashvars"))).createImage(wrapper));
				}
				else if(url&&url.toLowerCase().indexOf("ustream")>=0)
				{
					container.appendChild((new ShockwaveContent(url,value.getAttribute("flashvars"))).createImage(wrapper));
				}
			}
			//			else

//			container.appendChild((new ShockwaveContent(url)).createImage(marker));


		});
		while(removeList.length>0)
		{
			var top=removeList.pop();//removes elements from list
			if(top.parentNode.nodeName=='object')
				top=top.parentNode;

			//(new Element('tr')).parentNode.nodeName

			top.parentNode.removeChild(top);
		}
	}

	rip("embed");
	rip("object");

	return container;
}
function labeledContent(title,content)
{
	var container=new Element('tr',{id:'labeledRow'});
	var label=new Element('tr',{id:'labeledRowTitle'});
	label.innerHTML=title;
	var contentContainer=new Element('tr',{id:'labeledRowContent'});
	container.appendChild(label);
	container.appendChild(contentContainer);
	contentContainer.appendChild(content);

	return container;
}

function isUrl(s) {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
	return regexp.test(s);
}

function setInnerHTML(container, txt, clss)
{
	var d=new Element('div',{'class':clss});
	d.innerHTML=txt;
	container.appendChild(d);
}

/*
Script: dropMenu.js
Drop menu going Nth levels

License:
	MIT-style license.

	Author:
		Copyright (c) 2008 Chris Esler, <http://www.chrisesler.com/mootools>

 */
//Also known as IE fix
var TridentFix = new Class({
	tridentFix: function(item){
	item.addEvents({
		'mouseover':function(){
		this.addClass('iehover');
	},
	'mouseout':function(){
		this.removeClass('iehover');
	}
	});
}
});


var DropMenu = new Class({
	Implements: [Options,TridentFix],
	/* 
	don't know about options yet
		but set it up anyways just in case 
	 */
	options: 
	{
	mode: 'horizontal'
	},
	menu: null,
	initialize: function(menu,options){
		if(options) this.setOptions(options);

		this.menu = $(menu);

		// grab all of the menus children - LI's in this case
		var children = this.menu.getChildren();

		// loop through children
		children.each(function(item,index){
			// declare some variables 
			var fChild, list;

			/* 
				fChild = first child - which should be an A tag
				list = submenu UL
			 */
			fChild = item.getFirst();
			list = fChild.getNext('ul');

			// check if IE, if so apply fix
			if(Browser.Engine.trident) this.tridentFix(item);

			// if there is a sub menu UL
			if(list){
				item.mel = list; // pel = parent element
				list.pel = item; // mel = menu element
				new SubMenu(list); // hook up the subMenu
			}
		},this); // binding loop to this object for trident fix

	}	
});



var SubMenu = new Class({
	Implements: [Options,TridentFix],
	/* 
	don't know about options yet
		but set it up anyways just in case 
	 */
	options: 
	{
	mode: 'vertical'
	},
	menu: null, // storage for menu object
	depth: 0, // storage for current menu depth
	initialize: function(el,depth,options){
		if(options) this.setOptions(options); // set options
		if(depth) this.depth = depth;// set depth

		this.menu = el; //attach menu to object

		if(this.depth == 0)	this.menu.addClass('submenu'); // class for first level
		if(this.depth >= 1)	this.menu.addClass('sub_submenu'); // class for deeper levels - in case :P

		this.menu.fade('hide'); // set menu to hid

		/*
			hook up menu's parent with event
			to trigger menu
		 */
		this.menu.pel.addEvents(this.parentEvents); 

		// get menu's child elements
		var children = this.menu.getChildren();

		// loop through children
		children.each(function(item,index){
			// declare some variables 
			var fChild, list;

			/* 
				fChild = first child - which should be an A tag
				list = submenu UL
			 */
			fChild = item.getFirst();
			list = fChild.getNext('ul');

			// check if IE, if so apply fix
			if(Browser.Engine.trident) this.tridentFix(item);

			// if the menu item has a sub_submenu
			if(list){
				/*
					create marker for menu item
					that has a sub_submenu
					this is to show persistence and 
					where you are in the menu tree
				 */
				var count = new Element('span').set('html','\&raquo;').addClass('counter');

				item.adopt(count); // stuff it inside li
				count.fade('hide'); // hide it

				item.mel = list; // mel = menu element
				item.count = count; // attach count accessor to menu item
				list.pel = item; // pel = parent element

				// create new subMenu with depth incremented
				new SubMenu(list,this.depth+1);
			}
		},this); //bound to this for trident fix
	},
//	menu parent mouse events
	parentEvents: {
		'mouseover': function(){
		/*
				if it has a count accesor
				then fade it in 
		 */
		if(this.count) this.count.fade('in');

		// fade in menu
		this.mel.fade('in');		
	},
	'mouseout': function(){
		/*
				if it has a count accesor
				then fade it out 
		 */
		if(this.count) this.count.fade('out');

		// fade out menu
		this.mel.fade('out');
	}
	}
});




var GeoliveDockable = Drag.Move.extend({
	options: 
	{
	proximity: 20
	},
	initialize: function(element, options, location) {
		$(element).setStyle('position','fixed');
		this.setOptions(options);
		this.parent(element, this.options);
		this.dock(location || 'top');	
		this.state={left:false,right:false,top:false,bottom:false};

	},
	drag: function(event) {
		this.parent(event);
		this.checkBounds();
	},
	checkBounds:function(){
		var cntnr=this.options.innerContainer;	

		var windowHeight = cntnr.clientHeight;
		var windowWidth = cntnr.clientWidth;
		var browserHeight = document.documentElement.scrollHeight||document.documentElement.clientHeight||window.outerHeight;
		var browserWidth = window.innerWidth || document.documentElement.clientWidth;

		//mm_debug(this.element);
		//mm_debug(document.documentElement);

		var top_out=((this.element.offsetTop+cntnr.offsetTop) < this.options.proximity?true:false);
		var bottom_out=((browserHeight - (cntnr.offsetTop+this.element.offsetTop + this.element.offsetHeight)) <  this.options.proximity?true:false);
		var left_out=((this.element.offsetLeft+cntnr.offsetLeft) < this.options.proximity?true:false);
		var right_out=((browserWidth - (cntnr.offsetLeft+this.element.offsetLeft + this.element.offsetWidth)) <  this.options.proximity?true:false);

		var top=(Math.abs(this.element.offsetTop) < this.options.proximity?true:false);
		var bottom=(Math.abs(windowHeight - (this.element.offsetTop + this.element.offsetHeight)) <  this.options.proximity?true:false);
		var left=(Math.abs(this.element.offsetLeft) < this.options.proximity?true:false);
		var right=(Math.abs(windowWidth - (this.element.offsetLeft + this.element.offsetWidth)) <  this.options.proximity?true:false);

		//	var inside=(this.element.offsetTop>-this.options.proximity)&&


		if(top) {
			this.dock('top');
			this.state.top=true;
			this.onDock();

		}
		else if(top_out){
			this.dock('top_out');
			this.state.top=true;
			this.onDock();
		}
		else if(this.state.top){
			this.state.top=false;
			this.onUndock();
		}		
		if(bottom) {
			this.dock('bottom');
			this.state.bottom=true;
			this.onDock();
		}
		else if(bottom_out){
			//mm_debug("bottom_out");
			this.dock('bottom_out');
			this.state.bottom=true;
			this.onDock();			
		}
		else if(this.state.bottom){
			this.state.bottom=false;
			this.onUndock();
		}
		if(left) {
			this.dock('left');
			this.state.left=true;
			this.onDock();
		}
		else if(left_out) {
			this.dock('left_out');
			this.state.left=true;
			this.onDock();
		}
		else if(this.state.left){
			this.state.left=false;
			this.onUndock();
		}
		if(right) {
			this.dock('right');
			this.state.right=true;
			this.onDock();
		}
		else if(right_out) {
			this.dock('right_out');
			this.state.right=true;
			this.onDock();
		}
		else if(this.state.right){
			this.state.right=false;
			this.onUndock();
		}
	},

	onDock: function(){
		this.fireEvent('onDock', this.state);
	},
	onUndock: function(){
		this.fireEvent('onUndock', this.state);
	},
	dock: function(location) {

		//mm_debug("options.innerContainer");
		//mm_debug(this.options.innerContainer);
		var cntnr=this.options.innerContainer||{};

		var windowHeight = cntnr.clientHeight;
		var windowWidth = cntnr.clientWidth;
		var browserHeight = document.documentElement.scrollHeight||document.documentElement.clientHeight||window.outerHeight;
		var browserWidth = window.innerWidth || document.documentElement.clientWidth;

		switch(location) {
		case 'top':
			this.element.setStyle('top',-1);
			break;
		case 'top_out':
			this.element.setStyle('top',(0-cntnr.offsetTop));
			break;
		case 'bottom':
			this.element.setStyle('top',windowHeight - this.element.offsetHeight);
			break;
		case 'bottom_out':
			this.element.setStyle('top',browserHeight - this.element.offsetHeight -cntnr.offsetTop);
			break;
		case 'left':
			this.element.setStyle('left',0);
			break;
		case 'left_out':
			this.element.setStyle('left',0-cntnr.offsetLeft);
			break;
		case 'right':
			this.element.setStyle('left',windowWidth - this.element.offsetWidth);
			break;
		case 'right_out':
			this.element.setStyle('left',browserWidth - cntnr.offsetLeft-this.element.offsetWidth);
			break;
		default:
			break;
		}
	}
});

GeoliveDockable.implement(new Options(), new Events());


/**
 * the following causes errors and it not used
 */
Element.implement({
	makeDockable: function(options,location) {
	return new Drag.Dock(this,options,location);
}
});	









function Compatability(){}
Compatability.prototype={};


Compatability.SetOpacity=function(el,tryLevel,backupLevel)
{
	if(tryLevel==1||BrowserDetect.browser!="Explorer")
		$(el).setStyle('opacity',tryLevel);
	else if(backupLevel)
		$(el).setStyle('opacity',backupLevel);
};



