JHElementExtensions = {
  getElementByClassName: function(element,name) {
    element = $(element);
  	return document.getElementByClassName(name,element);
  },
  findElementPos: function(obj) {
      if (!(obj = $(obj))) return;
	  var curleft = 0;
	  var curtop = 0;
	  if(obj.x) curleft +=obj.x;
	  if(obj.y) curtop +=obj.y;
	    curleft = obj.offsetLeft
	    curtop = obj.offsetTop
	    while (obj = obj.offsetParent) {
//	    alert(obj.id+":"+obj.offsetLeft+":"+obj.offsetTop);
	      curleft += obj.offsetLeft
	      curtop += obj.offsetTop
	    }
	  return [curleft,curtop];

  },
  removeAllChildren: function(l) {
  	  l = $(l);
      while(l && l.hasChildNodes()) {
      	var n = l.childNodes[0];
      	if(n) {
	      	l.removeChild(n);
      	}
      }
  },
  findParentByClass: function(node,className) {
        if (!(node = $(node))) return;
		if(!node) return null;
//		if(node.getAttribute("class") && node.getAttribute("class").indexOf(className) >= 0) {

		if(node.className && Element.hasClassName(node,className)) {
			return node;
		}
		if(node.parentNode) {
			return Element.findParentByClass(node.parentNode,className);
		} else {
			return null;
		}
	},
  setClassName: function (el, className) {
        if (!(el = $(el))) return;
		Element.removeClassName(el,"*");
		Element.addClassName(el,className);
	},
  findElementInNode: function (node,n) {
        if (!(node = $(node))) return;
		var i;
		for(i=0;i<node.childNodes.length;i++) {
			if(node.childNodes[i].id == n) {
				return node.childNodes[i];
			} else {
				var r = Element.findElementInNode(node.childNodes[i],n);
				if(r) {
					return r;
				}
			}
		}
		return null;
	},

	getElementPosition : function (node) {
		node = $(node);
		if(!node) return -1;
		par = node.parentNode;
		for(i=0;i<par.childNodes.length;i++) {
			if(par.childNodes[i] == node) {
				return i;
			}
		}
		return -1;
	},
	isAFormElement : function (el) {
		el = $(el);
	    if (['INPUT', 'TEXTAREA', 'SELECT'].include(el.tagName))
	    return true; else return false;
	},
	getClassNameLike : function(node,cn) {
        if (!(node = $(node))) return;
//        alert(node.id+":"+Element.classNames(node));
		var r = null;
		Element.classNames(node).each(function(item) {
//        alert(item);
			if(item.indexOf(cn) == 0) {
				r =  item;
			}
		});
		return r;

	},
	removeClassNameLike : function (node,cn) {
        if (!(node = $(node))) return;
//        alert(node.id+":"+Element.classNames(node));
		Element.classNames(node).each(function(item) {
//        alert(item);
			if(item.indexOf(cn) == 0) {
				Element.removeClassName(node,item);
			}
		});
	}

}
Object.extend(Element, JHElementExtensions);
Element.addMethods(JHElementExtensions);

JHFormExtensions = {
 	postTo: function(f,el,url,opt) {
//  		Element.update(el,JHPExtensions.constant('saving'));
	    var myopts = {
	    	asynchronous: true,
	    	evalScripts: true,
	    	method: 'post',
	    	postBody: Form.serialize(f)
	    }
//	    alert(Form.serialize(f));
	    myopts = Object.extend(myopts, opt || {});
//	    alert(url);
		new Ajax.Updater(el,url, myopts);

    }
}

JHFormElementExtensions =  {
	setValue : function(field,v) {
		field = $(field);
		if(field) {
			if(field.options) {
					var i;
					for(i=0;i<field.options.length;i++) {
						if(field.options[i].value == v) {
							field.selectedIndex = i;
							field.options[i].selected=true;
							break;
						}
					}
			} else {
				field.value = v;
			}
		}

	}

}


Object.extend(Form, JHFormExtensions);
Object.extend(Form.Element, JHFormElementExtensions);

//Form.postTo = JHFormExtensions.postTo;
//Form.addMethods(JHFormExtensions);
//Object.extend(Form.Methods, JHFormExtensions || {})


document.getElementByClassName = function (name,el) {
	var els = document.getElementsByClassName(name,el);
	if(els && els[0]) return els[0];
	return null;
}

window.getInnerWindowSize = function () {
		if(Browser.isExplorer) {
			if(document.body.clientWidth == 0 || document.body.clientHeight == 0) {
				return [document.documentElement.clientWidth,document.documentElement.clientHeight];
			} else {
				return [document.body.clientWidth,document.body.clientHeight];
			}
		} else {
			return [window.innerWidth,window.innerHeight];
		}
}

var JHPExtensions = Class.create();
JHPExtensions.prototype = {

	message : function(n) {
//		alert(this.constant(n));
		this.showMessageBox(this.constant(n));
	},
	constant : function (n) {
		return isdefined('JHP_'+n) ? window['JHP_'+n] : n;
	},
	confirm : function (c,opts) {
		return this.confirm(this.constant(c),opts);
	},
	confirmText : function (c,opts) {
		var myopts = {
              	windowParameters: {
              		width:300,
	                className: 'alphacube'
              	},
                okLabel: this.constant('ok'),cancelLabel: this.constant('cancel'),
                buttonClass: "button",
                id: 'confirmDialog',
                cancel:function() {return true;},
                ok:function() {return true;}
        };
       var newOpts= Object.extend(myopts,opts);
       Dialog.confirm(c,newOpts);
       return false;
	},
	_handleAjaxMessageBoxOnError : function(transport) {
		var t = transport.responseText;
		var d= $('ajax_message_box');
		if(!d) {
		  	d = Builder.node('div',{id:'ajax_message_box',style:'display:none;position:absolute;'});
		  	document.body.appendChild(d);
		}
		Element.removeAllChildren(d);
		new Insertion.Top(d,transport.responseText);
	    var errors = Element.getElementsByClassName(d,'error');
//	    alert("HU"+errors);


		if(errors && errors.length > 0) {
			JHPExtensions._handleMessageBoxContent(d.innerHTML);
			if(this.onMBFailure) {
				this.onMBFailure(transport);
			}
		} else {
			if(this.onMBSuccess) {
				this.onMBSuccess(transport);
			}
		}
		this.onMBSuccess = null;
		this.onMBFailure = null;
	},
	_handleAjaxMessageBoxError : function() {
		JHPExtensions._handleMessageBoxContent(this.constant('internal_server_error'));
		if(this.onMBFailure) {
			this.onMBFailure(transport);
		}
		this.onMBFailure = null;
	},
	_handleMessageBoxContent : function(content) {
		this.showMessageBox(content)
	},
	showMessageBox : function(t) {
		Dialog.alert(t,
            {windowParameters: {}, okLabel: this.constant('close') });

	},
	showURLMessageBoxOnError : function(u,opts) {
		/* TODO PARSE ERROR and dont forget to pass the onsuccess handler
	    */
	    if(opts) {
			this.onMBSuccess = opts.onSuccess;
			this.onMBFailure = opts.onInternalFailure;
			opts.onSuccess = null;
			opts.onInternalFailure = null;
	    }
	    var ajaxOptions = {
	    	evalScripts: true,
	    	asynchronous: true,
	    	method:'get',
	    	onSuccess:this._handleAjaxMessageBoxOnError.bind(this),
	    	onFailure:this._handleAjaxMessageBoxError.bind(this)
	    };
		var options=Object.extend(opts || {},ajaxOptions);
	    var r = new Ajax.Request(u,options);
		//return this.showURLMessageBox(u,opts);
	},
	showURLMessageBox : function(u,opts) {
	    var ajaxOptions = {
	    	evalScripts: true,
	    	asynchronous: true,
	    	method:'get'
	    };
	    ajaxOptions=Object.extend(ajaxOptions , opts || {});

		Dialog.alert({url: u, options: ajaxOptions},
            {windowParameters: {}, okLabel: this.constant('close') });

	},
	checkTinyMCEContent : function(fieldname) {
		if(tinyMCE && $(fieldname)) {
			/// for firefox
	//		tinyMCE.execCommand('mceRemoveControl',false,fieldname);
			//tinyMCE.getEditorId(fieldname)
			var c = tinyMCE.getContent();
			if(c) {
				$(fieldname).value = c;
				return c;
			}
		}
		if($(fieldname)) return $(fieldname).getValue();
	},
	openContent : function(where,url,opt) {
	    var myopts = {
	    	asynchronous: true,
	    	evalScripts: true
	    }
	    myopts = Object.extend(myopts, opt || {});
		new Ajax.Updater(where,url, myopts);
	}




}
Object.extend(JHPExtensions, JHPExtensions.prototype);

function isdefined( variable) {
    return (typeof(window[variable]) == "undefined")?  false: true;
}


function over(o) {
	Element.classNames(o).add('over');
	}
function out(o) {
	Element.classNames(o).remove('over');
	}

Object.extend(String.prototype, {
  toBool: function() {
  	var s = this.toLowerCase();
  	if(s[0]=='y' ||s[0]=='1'||s[0]=='j') return true; else return false;
  	}});


