
	function lkOver(e) {
		if (e.title) {
			self.status = e.title;
		}
		
		//e.style.cursor = 'pointer';
		//e.style.textDecoration = 'underline';
		e.className = e.getAttribute('defaultClass') + ' lkOver';
	}
	
	function lkOut(e) {
		self.status = '';
		e.className = e.getAttribute('defaultClass');
		//e.style.cursor = 'default';
		//e.style.textDecoration = 'none';
	}
	
	function lkDown(e) {
		e.className = e.getAttribute('defaultClass') + ' lkDown';
	}
	
	function lkClick(e) {
		if (e.confirm == true && !confirm(e.confirmMsg)) {
			return false;
		}
		
		if (e.type == 'window') {
			e.target = e.getAttribute('target');
			var features = 'width=' + e.getAttribute('width') + ',height=' + e.getAttribute('height');

			if (e.getAttribute('features'))
				features += ',' + e.getAttribute('features');
			
			window.open(e.url, e.target, features);			
		} else if (e.type == 'popup') {
			
		} else if (e.type == 'submit') {
			e.href = e.url;
			smart_submit(e);
		} else {
			//location.href = e.href;
			if (e.url) {
				location.href = e.url;
			}
		}
		
		return false;
	}
	
	function lkInit(e) {		
		e.setAttribute('defaultClass', e.className);
		
		if (e.getAttribute('href')) {
			e.url = e.getAttribute('href');
		}
		
		e.title = e.getAttribute('title');
		e.type = e.getAttribute('type');
		e.target = e.getAttribute('target');
		e.confirmMsg = e.getAttribute('confirm');
		if (e.confirmMsg != undefined)
			e.confirm = true;
		else
			e.confirm = false;
			
		e.onmouseover = function() { lkOver(this); }
		e.onmouseout = function() { lkOut(this); }
		e.onmousedown = function() { lkDown(this); }
		e.onclick = function() { return lkClick(this); }
		
	}
	
	// Form Functions
	function submit_form(frm_id, action, method) {
		var frm = $(frm_id);
		
		if (method == 'GET' || method == 'POST')
			frm.method = method;
		
		if (action) {
			frm.action = action;
		}

		frm.submit();
		return false;
	}
	
	function smart_submit(e) {
		var frm = e.getAttribute('formid');
		if (!frm && e.form.id) {			
			frm = e.form.getAttribute('id');
		}
		
		if (!frm) {
			return false;
		}
		
		var action = null;
		/*
		if (e.tagName == 'A' || e.className == 'lk') {
			action = e.href;
		} else {
			action = e.getAttribute('action');
		}*/
		
		e.action = e.getAttribute('action');
		if (e.action != undefined) {
			action = e.action;
		} else {
			action = e.href;
		}
		
		submit_form(frm, action, e.getAttribute('method'));
		return false;
	}


	// Effect functions
	function effectLk(e) {
		//if (e.hasAttribute('effect')) {
			e.effect = e.getAttribute('effect');
		//}
		
		var effect = e.effect.split(',');
		var lastEffectIndex = e.getAttribute('lastEffectIndex');
		
		if (e.element == null)
			e.element = e.id;				
		
		//alert(e.lastEffectIndex + ': ' +e.effect[e.lastEffectIndex]);
		new Effect[effect[lastEffectIndex]](e.getAttribute('element'));
		
		lastEffectIndex++;

		if (lastEffectIndex == effect.length)
			lastEffectIndex = 0;
		
		e.setAttribute('lastEffectIndex', lastEffectIndex);
		
		return lkClick(e);
	}

	function $Q(frmId) {	
		frm = frmId;
		if (frm instanceof Object) {
		} else {
			var frm = $(frm);	
		}
		
		var uri = [];
		for (var i = 0; i < frm.elements.length; i++) {			
			var value = '';
			
			if (frm.elements[i].type == 'button') {				
				value = encodeURIComponent(frm.elements[i].textContent);
			} else {
				value = $F(frm.elements[i]);
			}
			
			var name = frm.elements[i].name;
			if (value && name)
				uri[i] =  name + '=' + value;
		}

		return uri.join('&');
	}

	function $att(element, att) {
		return  Element.readAttribute(element, att);
	}

	var dex = {
		include : function (src) {
			document.write('<script type="text/javascript" src="' + src +'"></script>');
		}
	}
	

	var DexRules = {
		".msg" : function (e) {
			new Effect.Pulsate(e);
		},
		'.lk' : function(e) {
			lkInit(e);
			e.onmouseover = function() {			
				lkOver(e);				
			},
			e.onmouseout = function() {
				lkOut(e);
			},
			e.onclick = function() {
				return lkClick(e);
			}
		},
		'.sortable' : function(e) {
			Sortable.create(e);
		},
		'.popup' : function(e) {
			e.onclick = function() {
				var atts = new Array();
				var width = this.getAttribute('width');
				var height = this.getAttribute('height');
				var features = this.getAttribute('features')
				
				if (width)
					atts.push('width=' + width);

				if (height)
					atts.push('height=' + height);
				
				if (features)
					atts.push(features);
				
				window.open(this.href,this.target,atts.join(','));
				return false;
			}
		},
		'.delete' : function(e) {
			e.onclick = function() {
				return confirm(this.getAttribute('msg'));
			}
		},
		'.del_rows' : function(e) {
			e.onclick = function() {
				if (confirm(this.getAttribute('msg'))) {
					smart_submit(e);
				}
				return false;
			}
		},
		'.add_image' : function(e) {
			e.onclick = function() {
				window.open(this.href,this.target,'width=' + this.getAttribute('width') + ',height=' + this.getAttribute('height') + ',top=35,left=35,screenX=35,screenY=35');
				return false;
			}
		},
		'.preview' : function(e) {
			e.onclick = function() {
				///this.href = function() { return false; };
				window.open(this.href,'','width=' + e.getAttribute('width') + ',height=' + e.getAttribute('height') + 'scrollbar=yes,top=35,left=35,screenX=35,screenY=35');
				return false;
			}
		},
		'table td' : function(e) {			
			e.onclick = function() {				
				var selectable = this.getAttribute('selectable');
				if (selectable == 'true') {
					var itemId = this.parentNode.getAttribute('itemid');
					if (itemId) {
						var chk = $('item' + itemId);
						if (chk) {
							chk.checked = !chk.checked;
						}
					}
				}
			}
		},
		'table tr' : function(e) {
			e.overClass = e.getAttribute('overclass');
			if (e.overClass != null) {
				e.defaultClass = e.className;				
				e.onmouseover = function() {
					if (this.overClass) {
						this.className = this.overClass;
					}
				};
				e.onmouseout = function() {				
					this.className = this.defaultClass;
				}
			}
		},
		'.enableitem' : function(e) {
			if (e.getAttribute('href')) {		
				new Form.Element.EventObserver(e, function(element, value) {
					new Ajax.Request(element.getAttribute('href'), {
						onLoading: function() { element.disabled = true },
						onComplete:function(request) { element.disabled = false; new Effect.Highlight(element.parentNode) },
						asynchronous:true,
						evalScripts:true,
						parameters:Form.Element.serialize(element)
					});
				});
			}
		},
		'select.submit' : function(e) {
			e.onchange = function() {
				return smart_submit(e);
			}
		},
		'input.submit' : function(e) {
			e.onclick = function() {
				return smart_submit(e);
			}
		},
		'.save_logos_order' : function(e) {
			e.onclick = function() {
				var itemId = e.parentNode.parentNode.getAttribute('itemId');
				var items = document.getElementsByClassName(e.getAttribute('items'));

				var str = '';
				var params = new Array();
				for (var i in items) {
					if (items[i].type != 'hidden')
						continue;
					
					params.push(encodeURIComponent(items[i].name) + '=' + encodeURIComponent(items[i].value));
				}
				str = params.join('&');

				new Ajax.Request(e.href, {
					onComplete:function(request){ new Effect.Highlight(e.parentNode); },
					asynchronous:true,
					evalScripts:true,
					parameters:str
				});

				return false;
			}
		},
		'.submit_sortables' : function(e) {
			e.onclick = function() {
				var items = document.getElementsByClassName(e.getAttribute('items'));

				var str = '';
				var params = new Array();
				for (var i in items) {
					if (!items[i].form)
						continue;
					
					params.push(encodeURIComponent(items[i].name) + '=' + encodeURIComponent(items[i].value));
				}
				str = params.join('&');				

				new Ajax.Updater(e.getAttribute('update'), e.href, {
					onComplete:function(request){ new Effect.Highlight(e.getAttribute('update')); Behaviour.apply() },
					asynchronous:true,
					evalScripts:true,
					parameters:str
				});

				return false;
			}
		},
		'.view_images' : function(e) {
///			e.isOpen = false;
			e.onclick = function() {
				var update = this.getAttribute('update');
				var url = this.getAttribute('url');
				
				if (this.isOpen) {
					new Effect.BlindUp(update);
					this.isOpen = false;
				} else {
					this.isOpen = true;
					new Ajax.Updater(update, url, {
						onComplete:function(request){ new Effect.BlindDown(update); Behaviour.apply() },
						asynchronous:true,
						evalScripts:true
					});

					//new Effect.BlindDown(this.getAttribute('update'));
					
				}
				
				return false;
			}
		},
		'.goto' : function(e) {
			e.onclick = function () {			
				if (e.getAttribute('href')) {
					location.href = e.getAttribute('href');
				}
			}
		},
		'.submitto' : function (e) {
			e.onchange = function () {
				if (this.getAttribute('href')) {
					location.href = this.getAttribute('href') + '/' + this.value;
				}
			}
		},
		'.smartform' : function(e) {
			e.onsubmit = function() {
				return false;
			}
		},
		'.smartform input[type=submit]' : function(e) {
			e.onclick = function() {
				return smart_submit(e);
			}
		},
		'.close_window' : function(e) {
			e.onclick = function() {
				window.close();
			}
		},		
		'.debug' : function(e) {
			e.onclick = function() {
				alert(this.getAttribute('width'));
				return false;
			}
		},
		'.effect' : function(e) {			
			e.setAttribute('lastEffectIndex',0);
			e.onclick = function() {
				effectLk(this);
			}
		},
		'.smart_list .effect' : function(e) {
			document.getElementById(e.getAttribute('element')).style.display = 'none';
			e.setAttribute('lastEffectIndex',0);
			e.onclick = function() {				
				this.setAttribute('effect', 'BlindDown,BlindUp');
				new Effect.ScrollTo(this.id, {offset: -10})
				effectLk(this);
			}		
		},
		'.gallery td' : function(e) {
			e.onmouseover = function() {				
				this.className = 'over';
			}
			e.onmouseout = function() {				
				this.className = 'up';
			}
		},
		
		// Webcontrols
		'.fckeditor' : function(e) {
			
		},
		'.win' : function(e) {
			e.onclick = function() {
				var win = new Window(this.att('winid','win'), {
								title: this.att('title',''), 
								width: this.att('width',350), 
								height: this.att('height',350),
								className: 'darkX'
							});
				win.setURL(this.att('href','#'));
				win.setDestroyOnClose();
				win.showCenter();
				
				return false;
			}
		},
		'#albumMenu' : function(e) {
			e.onchange = function() {
				var value = $F(this);
				if (value != '') {
					location.href = this.form.action + value;
				}
			}
		},
		'.selectLink' : function(e) {
			e.onclick = function() {
				this.href += $F( $att(this, 'bind') );
			}
		},
		'.ajaxRequestLk' : function(e) {
			e.onclick = function () {
				
			}
		},
		'.ajaxUpdateLk' : function(e) {
			e.onclick = function() {
				new Ajax.Updater(this.att('update'), this.att('href',''), {
						parameters: this.att('params',''),
						asynchronous: true,
						evalScripts: true,
						method: this.att('method','post'),
						onComplete:function(){},
						onSuccess:function(request){
							Behaviour.apply();
						}
					});
				
				this.startIndicator();				
				return false;
			},
			e.startIndicator = function() {
				if (this.att('indicator')) {
					Element.show( this.att('indicator') );
				}
			},
			e.stopIndicator = function() {
				alert(this.att('indicator'));
				if ( this.att('indicator')) {
					Element.hide( this.att('indicator') );
				}
			}

		},
		'.tboptions' : function(e) {
			e.onclick = function() {
				var indicator = this.att('indicator');
				new Ajax.Updater('tbcontent', this.att('href',''), {
						//parameters: this.att('params',null),
						asynchronous: true,
						evalScripts: true,
						method: 'post',
						onComplete:function() {							
							if ( indicator) {
								Element.hide( indicator );
							};
							new Effect.BlindDown('tbcontent', {queue: 'end'});
							Behaviour.apply();
						}
					});
				this.startIndicator();				
				return false;
			},
			e.startIndicator = function() {
				if (this.att('indicator')) {
					Element.show( this.att('indicator') );
				}
			}
		},

		'.show_edit' : function(e) {
			e.onclick = function() {			
				var item = this.att('item');
				var album = $('album');							
	
				if (Element.visible(item)) {					
					e.close();
				} else {
					e.open();
				}
				
				return false;
			},
			e.close = function() {
				var item = this.att('item');
				var thumb = $( this.att('thumb') );	
				Element.hide(item);
				this.down().src = this.att('defaultIcon');
				Position.relativize( thumb );
			},
			e.open = function() {
				var item = this.att('item');
				var album = $('album');
				var img = this.down();
				var thumb = $( this.att('thumb') );
				
				if (!this.att('defaultIcon'))
					this.setAttribute('defaultIcon', img.src);

				img.src = this.att('cancelsrc');				
				
				if (album.att('editing')) {
					$( album.att('editing') ).close();
				}
				
				console.log(album.att('editing'));
				console.log(this.id);				
				
				Element.show(item);
				album.setAttribute('editing', this.id);
			}
		},
		'.hideElement' : function(e) {
			e.onclick = function() {
				alert(this.att('update'));
				Element.hide( this.att('update') );
			}
		},
		'.hideForm' : function(e) {
			e.onclick = function() {
				
			}
		},
		'.sortable2' : function(e) {
			var o = new Object();
			o.tag = e.att('tag','li');
			o.overlap = e.att('overlap','vertical');
			o.constraint = e.att('constraint',false);
			Sortable.create(e.id, o);
		},
		'.editable' : function(e) {
			new Ajax.InPlaceEditor(e.id, e.att('action'));
		},
		'.submit_sortables2' : function(e) {
			e.onclick = function() {
				var items = document.getElementsByClassName(e.getAttribute('items'));
				var str = '';
				var params = new Array();
				for (var i in items) {
					if (!items[i].form)
						continue;
					
					params.push(encodeURIComponent(items[i].name) + '=' + encodeURIComponent(items[i].value));
				}				
				str = params.join('&');
				
				var indicator = $('indicator2');

				new Ajax.Updater(e.getAttribute('update'), e.href, {
					onComplete:function(request){ new Effect.Highlight(e.getAttribute('update')); Behaviour.apply(); indicator.hide(); },
					asynchronous:true,
					evalScripts:true,
					parameters:str
				});
				
				indicator.show();

				return false;
			}
		}		
	}

Behaviour.register(DexRules);