//drjs.core.js
//Metodos utilizados pelas outras bibliotecas do framework

//if(__debugger__addMsg==null) {
	function __debugger__addMsg() {
	}
//}
//if(__debugger__addBP==null) {
	function __debugger__addBP() {
	}
//}

var Browser = {
	IE:  !!(window.attachEvent && navigator.userAgent.indexOf('Opera') === -1),
	FF:  navigator.userAgent.indexOf('Firefox') > -1,
	Firefox: navigator.userAgent.indexOf('Firefox') > -1,
    Opera: window.opera,
    WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
    Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
	Safari: navigator.userAgent.match(/*Safari*/),
    MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
}

function isArray(arr) {//alert(arr.constructor);
	//alert(typeof(arr.reverse));
	if(arr[0] && typeof(arr.sort)=="function") {// !arr.tagName) {
		return true;
	} else {
		return false;
	}
}
Array.prototype.indexOf = function(obj) {
	for(var i=0; i<this.length; i++) {
		if(this[i]==obj) {
			return i;
		}
	}
	return -1;
}
Array.prototype.remove = function(obj) {
	var pos = this.indexOf(obj);
	var newArray = Array();
	return newArray.concat(this.slice(0, pos), this.slice(pos+1, this.length));
}
Array.prototype.removePos = function(pos) {
	if(pos!=0 && this.length!=1) {
		if(pos==this.length+1) {
			return [].concat(this.slice(0, pos), []);
		} else {
			return [].concat(this.slice(0, pos), this.slice(pos+1, this.length));
		}
	} else {
		if(pos==0 && this.length>1) {
			return [].concat(this.slice(1, this.length), []);
		} else {
			return [];
		}
	}
}
Object.prototype.find = function(property, value) {
	var newarr = [];
	for(var i=0; i<this.length; i++) {
		//alert(this[i].tagName);
		if(this[i].getAttribute && this[i].getAttribute(property) == value) {
			newarr.push(this[i]);
		}
	}
	return newarr;
}
Array.ToArray = function(object) {
	var arr = [];
	for(var i=0; i<object.length; i++) {
		arr.push(object[i]);
	}
	return arr;
}

Date.clone = function(date) {
	return new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
}
Date.getDayDiff = function(date1, date2) {
	return Number((date1-date2)/(1000*60*60*24));
}
Date.prototype.getOrderDate = function() {
	var dia = (Number(this.getDate())<10) ? "0" + this.getDate() : this.getDate();
	var mes = (Number(this.getRealMonth())<10) ? "0" + Number(this.getRealMonth()) : Number(this.getRealMonth());
	return this.getFullYear().toString() + mes.toString() + dia.toString();
}
Date.prototype.getRealMonth = function() {
	return Number(this.getMonth() + 1);
}

Object.prototype.AddMethods = function(methods) {
	for(var property in methods) {
		if(this.prototype!=null) {
			this.prototype[property] = methods[property];
		} else {
			this[property] = methods[property];
		}
	}
}
Object.prototype.Copy = function() {
	var c = new Object();
	for(var property in this) {
		c[property] = this[property];
	}
	return c;
}
Object.Extend = function(destination, source) {
	for(var property in source) {
		destination[property] = source[property];
	}
	return destination;
}

String.parseParameters = function(str) {
	var params = [];
	do {
		var paramstr = (str.indexOf("&")>=0) ? str.substring(0, str.indexOf("&")) : str;
		str = (str.indexOf("&")>=0) ? str.substring(str.indexOf("&")+1) : "";
		if(paramstr != "&") {
			var el = paramstr.split("=");
			params[el[0]] = el[1];
		}
	} while(str != "");
	return params;
}
String.prototype.findPercentVariables = function(){
	var str = this;
	var arr = str.split("%");
	var aux = "";
	for(var i=1; i<arr.length; i++) {
		aux += arr[i].replace(/(\w+)(.*)/, "$1") + ",";
	}
	aux = aux.substr(0, aux.length-1);
	aux = aux.split(",");
	return aux;
}
String.prototype.matchPattern = function(pattern) {
	return this.replace(pattern, '')=='';
}
String.prototype.hasWord = function(word, delimiter) {
	if(delimiter!=null) {
		return this.indexOf(delimiter+word+delimiter)>=0 || this==word;
	} else {
		var re = new RegExp("\\b"+word+"\\b");
		if(this.match(re)) {
			re = null;
			return true;
		} else {
			re = null;
			return false;
		}
	}
	//delimiter = (delimiter==null) ? " " : delimiter;
	//return this.indexOf(word+" ")>=0 || this.indexOf(" "+word)>=0 || this.indexOf("\t"+word)>=0 || this.indexOf(word+"\t")>=0 || this.indexOf(word+"\n")>=0 || this==word;
}
String.prototype.removeWord = function(word, delimiter) {
	var re = new RegExp("\\b"+word+"\\b");
	var str = this.replace(re, "");
	re = null;
	return str;
}
String.prototype.reverse = function() {
	var rev = "";
	for(var i=this.length-1; i>=0; i--) {
		rev += this.charAt(i);
	}
	return rev;
}
String.prototype.toDate = function() {
	var date = null;
	var time = null;
	try {
		if(this.indexOf(" ")>=0) {
			date = this.split(" ");
			time = date[1];
			date = date[0];	
		}
		date = date.split("/");
		if(date.length==3) {
			if(time==null) {
				return new Date(date[2], (Number(date[1])-1), date[0]);
			} else {
				time = time.split(":");
				return new Date(date[2], (Number(date[1])-1), date[0], time[0], time[1]);
			}
		}
		return false;
	} catch(e) {
		return false;
	}
}
String.prototype.trim = function() {
	var str = this;
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}

var Class = {
	Create: function() {
		return function() {
			this._constructor.apply(this, arguments);
			this.Extends = function(classVar) {
				for(var property in classVar) {
					this[property] = classVar[property];
				}
			}
		}
	},
	Extends: function(classVar) {
		var newClass = function() { this._constructor.apply(this, arguments); }
		for(var property in classVar.prototype) {
			newClass.prototype[property] = classVar.prototype[property];
		}
		return newClass;
	}
}

var DOMFunctions = {
	AddClass: function(className) {
		if(!(this.className.hasWord(className))) {
			this.className += " "+className;
		}
	},
	Hide: function() {
		this.style.display = "none";
	},
	RemoveClass: function(className) {
		if(this.className.hasWord(className)) {
			this.className = this.className.removeWord(className);
		}
	},
	SetContent: function(html) {
		this.innerHTML = html.replace(/\&amp;/g, "&");
		var s = html;
		var num = -1;
		while(s.toLowerCase().indexOf("<!--", num) > num) {
			num = s.toLowerCase().indexOf("<!--", num);
			if(s.toLowerCase().indexOf("<script", num)>=0 && s.toLowerCase().indexOf("</script>", num)>=0) {
				var numfim = s.toLowerCase().indexOf("</script>", num);
				var src = s.substring(num + "<!--".length, s.toLowerCase().indexOf("-->", numfim));
				src = src.substring(src.indexOf(">")+1, src.indexOf("</script>"));
				var script = document.createElement("script");
				script.setAttribute("type", "text/javascript");
				script.text = src;
				document.body.appendChild(script);
			}
		}
		var scripts = this.getElementsByTagName("script");
		var scriptsToInsert = [];
		for(var i=0; i<scripts.length; i++) {
			var s = document.createElement("script");
			s.setAttribute("type", "text/javascript");
			s.text = scripts[i].text;
			if(scripts[i].src!=null && scripts[i].src!="") {
				s.src = scripts[i].src;
			}
			scriptsToInsert.push([scripts[i], s]);
		}
		for(var i=0; i<scriptsToInsert.length; i++) {
			scriptsToInsert[i][0].parentNode.insertBefore(scriptsToInsert[i][1], scriptsToInsert[i][0]);
		}
		this.onContentSet(this, html);
	},
	onContentSet: function(el, html) {
	},
	Show: function() {
		this.style.display = "";
	},
	apply: function(obj) {
		if(arguments.length>1) {
			for(var i=0; i<arguments.length; i++) {
				this.apply(arguments[i]);
			}
		} else {
			if(typeof(obj)=="string") {
				obj = document.getElementById(obj);
			}
			if(isArray(obj)) {
				var newObjArr = [];
				for(var i=0; i<obj.length; i++) {
					newObjArr.push(this.apply(obj[i]));
				}
				return newObjArr;
			} else if(typeof(obj)=="object") {
				for(var property in this) {					
					if(property!="apply") {
						obj[property] = this[property];
					}
				}
				return obj;
			} else { return null; }
		}
	}
}

function donothing() {
}

document.getElementsByClassName = function(className, parentElement) {
	var elements = [];
	var children = ($(parentElement) || document.body).getElementsByTagName('*');
	for(var i=0; i<children.length; i++) {
		if(children[i].className.hasWord(className)) {
			elements.push(DOMFunctions.apply(children[i]));
		}
	}
	return elements;
}

document.getElementsByAttribute = function(attributeName, attributeValue, parentElement) {
	var elements = [];
	var children = ($(parentElement) || document.body).getElementsByTagName('*');
	for(var i=0; i<children.length; i++) {
		if((attributeValue==null && children[i].getAttribute(attributeName)!=null && children[i].getAttribute(attributeName).toString().length>0) || (attributeValue!=null && children[i].getAttribute(attributeName)==attributeValue)) {
			elements.push(DOMFunctions.apply(children[i]));
		}
	}
	return elements;
}

function $(elementID) {
	try {
		if(arguments.length>1) {
			var elements=[];
			for(var i=0; i<arguments.length; i++) {
				elements.push(DOMFunctions.apply(document.getElementById(arguments[i])));
			}
			return elements;
		} else if(typeof(elementID)=="string") {
			return DOMFunctions.apply(document.getElementById(elementID));
		} else if(typeof(elementID)=="object") {
			return DOMFunctions.apply(elementID);
		}
	} catch(e) {
		return false;
	}
}
function $A(attributeName, attributeValue, parentElement) {
	return document.getElementsByAttribute(attributeName, attributeValue, parentElement);
}
function $C(className, parentElement) {
	/*if(arguments.length>2) {
		var elements=[];
		for(var i=0; i<arguments.length; i++) {
			elements = elements.concat(document.getElementsByClassName(arguments[i], parentElement));
		}
		return elements;
	} else if(typeof(className)=="string") {*/
		return document.getElementsByClassName(className, parentElement);
	//}
}
function $N(name, parentElement) {
	return Array.ToArray(document.getElementsByName(name));
	//return Array.ToArray( ($(parentElement) || document.body).getElementsByName(name) );
}
function $T(tagName, parentElement) {
	var elements = [];
	parentElement = ($(parentElement) || document.body);
	if(typeof(tagName)=="string") {
		var neweles = parentElement.getElementsByTagName(tagName, parentElement);
		if(neweles.length>0) {
			elements = DOMFunctions.apply(neweles);
		}
	} else if(isArray(tagName)) {
		for(var i=0; i<tagName.length; i++) {
			var neweles = parentElement.getElementsByTagName(tagName[i], parentElement);
			if(neweles.length>0) {
				elements = elements.concat( DOMFunctions.apply(neweles) );
			}
		}
	}
	return elements;
}
function $V(elementID) {
	return document.getElementById(elementID).value;
}

drjs.loadComplete(drjs.files.core);