//extends
Object.extend(Element, {
	getParentForm: function(element){
		element = $(element);
		while (element.tagName != "FORM"){
			element = element.parentNode;
		}
		return element;
	},

	block: function(element){
		element = $(element);
		element.style.display = "block";
	},
	
	visible: function(element){
		element = $(element);
		element.style.visibility = "visible";
	},
	
	hidden: function(element){
		element = $(element);
		element.style.visibility = "hidden";
	},

	niceRemove: function(element){
		element = $(element);
		var fader = new fx.FadeSize(element, {onComplete: function(){Element.remove(element);}});
		fader.toggle('height');
	},

	add: function(container, element, options){
		if (options && options.onBottom == true) {
			return container.appendChild(document.createElement(element));
		}
		else {
			return container.insertBefore(document.createElement(element), container.childNodes[0]);
		}
	}
});

Object.extend(Form, {
	empty: function(form){
		var elements = Form.getElements(form);
		
		for (var i=1;el=elements[i];i++){
			if (el.tagName.toLowerCase() == "textarea" || el.type == "text" || el.type == "password")
				el.value = "";
			if (el.type == "checkbox") { 
				el.checked = false;
				Element.removeClassName(el.parentNode, "oneChoiceSel");
			}
		}
	}
});

Object.extend(String.prototype, {
	urlyfy: function(){
		noChars = /\$|,|@|#|~|`|\*|\^|\(|\)|\[|\/|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$/g;
		newVal = this.replace(noChars, "");
		s = newVal.replace(/\%/g,"-percent-");
		s = s.replace(/ /g,"-");
		s = s.replace(/\&/g,"-and-");
		s = s.replace(/\+/g,"-plus-");
		//s = s.replace(/?g,"a");
		//s = s.replace(/?g,"o");
		//s = s.replace(/?g,"e");
		//s = s.replace(/?g,"e");
		s = s.replace(/\=/g,"-equals-");
		s = s.toLowerCase();
		return s;
	}
});

//layout
var Layout = Class.create();
Layout.prototype = {
	initialize: function(changeSize, minSize, medSize, maxSize, cssStyle){
		this.medSize = medSize;
		this.changeSize = changeSize;
		this.minSize = minSize;
		this.maxSize = maxSize;
		this.cssStyle = cssStyle;
	},

	change: function(){
		var theWidth = this.getBrowserWidth();
		for (var i = 0; i < arguments.length; i++) {
			var element = $(arguments[i]);
			if (theWidth < this.changeSize) element.className = this.cssStyle;
			else element.className = "";
		}
	},

	constrain: function(el1, el2){
		el1 = $(el1);
		el2 = $(el2);
		var theWidth = this.getBrowserWidth();
		if (theWidth < this.minSize){
			el1.style.width = this.minSize -1 + "px";
			el2.style.width = this.minSize -12 + "px";
		}else if (theWidth > this.maxSize){
			el1.style.width = "100%";
			el2.style.width = this.maxSize - 18 + "px";
		}else {
			el1.style.width = "100%";
			el2.style.width = "98.5%";
		}
	},

	getBrowserWidth: function(){
		if (window.innerWidth) return window.innerWidth;
		else if (document.documentElement && document.documentElement.clientWidth != 0) return document.documentElement.clientWidth;
		else if (document.body) return document.body.clientWidth;
	}
};

//loaders
var Loader = Class.create();
Loader.prototype = {
	initialize: function(element, cssStyle, url, parameters, form, goodEnd){
		this.goodEnd = goodEnd || null;
		this.clearForm = form || null;
		this.url = url;
		this.parameters = parameters;
		this.loader = $(element);
		this.cssStyle = cssStyle;
		this.loader.fader = new fx.Opacity(this.loader, {duration: 300, onComplete: this.request.bind(this) });
		this.start();
	},
	
	start: function(){
		this.loader.className = "loader";
		this.loader.parentNode.className = this.cssStyle;
		this.loader.fader.custom(0.99, 0.2);
	},
	
	request: function(){
		this.loader.fader.options.onComplete = this.reset.bind(this);
		new Ajax.Request(this.url, {method: 'post', postBody: this.parameters, onComplete: this.check.bind(this) });
	},
	
	check: function(request){
		var repVal = $('loader').offsetHeight;
		var response = request.responseText;
		var res = response.split("-__-");
		if(res.length == 2){
			var err_type = res[0];
			var err_msg = res[1];
		//	alert(err_msg);
			moo.writeMsg(err_msg, "error");
		}
		else{
			this.loader.innerHTML = response.replace('tabwrap"', 'tabwrap" style="height:'+repVal+'px"');
			if (this.clearForm) Form.empty(this.clearForm);
			if (!this.goodEnd) moo.closeMsg();
			else if (this.goodEnd == true) moo.writeMsg('your item has been inserted successfully.', 'success');
			else if (this.goodEnd == 'deleted') moo.writeMsg('your item has been deleted successfully.', 'warning');
		}		
		this.end();
	},

	reset: function(){
		this.loader.className = "";
		this.loader.parentNode.className = "";
	},

	end: function(){
		Attitude.update(tableRules);
		this.loader.fader.custom(0.2, 0.99);
		if (!this.adjust) this.adjust = new fx.Height($('loader'), {duration: 300});
		//var pattern = /(<img[^>]*>)/ig; 
//		if (pattern.test($('loader').value)){
//		if(this.loader.img.readyState="complete"){ 
//			this.adjust.custom($('tabwrap').offsetHeight, $('tab').offsetHeight);
//		}
//		}
//		else{
			this.adjust.custom($('tabwrap').offsetHeight, $('tab').offsetHeight);
}
};

var Loader2 = Class.create();
Loader2.prototype = {
	initialize: function(element, cssStyle, url, parameters, form, goodEnd){
		this.goodEnd = goodEnd || null;
		this.clearForm = form || null;
		this.url = url;
		this.parameters = parameters;
		this.loader = $(element);
		this.cssStyle = cssStyle;
		this.loader.fader = new fx.Opacity(this.loader, {duration: 300, onComplete: this.request.bind(this) });
		this.start();
	},
	
	start: function(){
		this.loader.className = "loader2";
		this.loader.parentNode.className = this.cssStyle;
		this.loader.fader.custom(0.99, 0.2);
	},
	
	request: function(){
		this.loader.fader.options.onComplete = this.reset.bind(this);
		new Ajax.Request(this.url, {method: 'post', postBody: this.parameters, onComplete: this.check.bind(this) });
	},
	
	check: function(request){
		var repVal = $('loader').offsetHeight;
		//alert "dddd";
		var response = request.responseText;
		var res = response.split("-__-");
		if(res.length == 2){
			var err_type = res[0];
			var err_msg = res[1];
		//	alert(err_msg);
			moo.writeMsg(err_msg, "error");
		}
		else{
			this.loader.innerHTML = response.replace('tabwrap"', 'tabwrap" style="height:'+repVal+'px"');
			if (this.clearForm) Form.empty(this.clearForm);
			if (!this.goodEnd) moo.closeMsg();
			else if (this.goodEnd == true) moo.writeMsg('your item has been inserted successfully.', 'success');
			else if (this.goodEnd == 'deleted') moo.writeMsg('your item has been deleted successfully.', 'warning');
		}		
		this.end();
	},

	reset: function(){
		this.loader.className = "";
		this.loader.parentNode.className = "";
	},

	end: function(){
		//Attitude.update(tableRules);
		this.loader.fader.custom(0.2, 0.99);
		//if (!this.adjust) this.adjust = new fx.Height($('loader'), {duration: 300});
		//this.adjust.custom($('tabwrap').offsetHeight, $('tab').offsetHeight);
	}
};

var Addition = Class.create();
Addition.prototype = {
	setOptions: function(options) {
		this.options = {
			onComplete:	''
		},

		Object.extend(this.options, options || {});
	},

	initialize: function(element, html, container, options){
		container = $(container);
		this.setOptions(options);
		if (container.childNodes[0] && !this.options.onBottom)
			//var newElement = container.insertBefore(document.createElement(element), container.childNodes[0]);
			var newElement = Element.add(container, element, {onBottom : false});
		else
			//var newElement = container.appendChild(document.createElement(element));
			var newElement = Element.add(container, element, {onBottom : true});
		if (this.options.cssStyle) Element.addClassName(newElement, this.options.cssStyle);
		
		var fader = new fx.FadeSize(newElement, {duration: 400, onComplete: this.options.onComplete });
		fader.hide('height');
		newElement.innerHTML = html;
		fader.toggle('height');
	}
};

//livesearch
var LiveSearch = Class.create();
LiveSearch.prototype = {
	setOptions: function(options) {
	this.options = {
      onComplete:   ''
	},
	Object.extend(this.options, options || {});
	},

	initialize: function(container, url, parameters, options){
		this.setOptions(options);
		this.container = $(container);
		this.url = url;
		this.parameters = parameters;
		this.keys = new Array(2);
		this.timer = null;
	},

	chkKey: function(){
		this.keys.push(this.key);
		if (this.keys[1] != this.keys[2]) this.start();
		this.keys.pop();
	},

	start: function(key){
		if (key != null) this.key = key;
		if (this.key.length > 1)
			new Ajax.Request(this.url, {method: 'get', parameters: this.parameters+this.key, onComplete: this.display.bind(this) });
		else this.hide();
	},

	setTimer: function(key){
		this.key = key;
		if (this.timer != null){
			clearTimeout(this.timer);
		}
		this.timer = setTimeout(this.chkKey.bind(this), 500);
	},

	display: function(response){
		this.container.innerHTML = response.responseText;
		Element.block(this.container);
		Element.visible(this.container);
	},

	hide: function(){
		Element.hide(this.container);
		Element.hidden(this.container);
	}
};