var Classe = {
	cria: function() {
		return function() {
			this.construtor.apply(this, arguments);
		}
	}
}

Array.prototype.find = function(p){
	for(c=0;c<this.length;c++){
		if(this[c][0] == p) return this[c];
	}
	return false;
}

Function.prototype.bind = function() {
 var self = this, args = $A(arguments), scope = args.shift();
 return function() {
 	return self.apply(scope, args.concat($A(arguments)));
 }
}

Element.prototype.hasClass = function(name) {
	return this.className.indexOf(name) != -1;
}

var $A = function(object) {
 var array = [];
 for(var i = 0; i < object.length; i++)
 	array[i] = object[i];
 return array;
}

function scrollToDiv(element) {
	element = $(element);
	window.scrollTo(0, cumulativeOffsetTop(element));
	return element;
}

function cumulativeOffsetTop(element) {
	var valueT = 0;
	do {
		valueT += element.offsetTop  || 0;
		element = element.offsetParent;
	} while (element);
	return valueT;
}
  
function $(id){
	return (document.getElementById(id));
};

function $class(el,c){
	els = document.getElementsByTagName(el);
	ret = [];
	for(i=0;i<els.length;i++){
		if(els[i].className == c)
			ret.push(els[i]);
	}
	if(ret.length > 0)
		return ret;
	else
		return false
}

function execScript(conteudo){
	var scripts = conteudo.getElementsByTagName("script");
    for(i = 0; i < scripts.length; i++){
        s = scripts[i].innerHTML;
        eval(s);
    }
};

function executaScript(texto){
	// inicializa o inicio ><
	var ini = 0;
	// loop enquanto achar um script
	while (ini!=-1){
		// procura uma tag de script
		ini = texto.indexOf('<script', ini);
		// se encontrar
		if (ini >=0){
			if( texto.indexOf('src="', ini) != -1 ){
				var iniSRC = texto.indexOf('src="', ini)+5;
				var fimSRC = texto.indexOf('"', iniSRC);
				
				var src = texto.substring(iniSRC, fimSRC);
				
				var _script = document.createElement("script");
				_script.src = src;
				
				if(document.getElementById("contentScript"))
					contentScript = document.getElementById("contentScript");
				else{
					contentScript = document.createElement("div");
					contentScript.id = "contentScript";
					contentScript.style.display="none";
					document.body.appendChild(contentScript);
				}
				contentScript.appendChild( _script );
			}
			
			// define o inicio para depois do fechamento dessa tag
			ini = texto.indexOf('>', ini) + 1;
			// procura o final do script
			var fim = texto.indexOf('</script>', ini);
			// extrai apenas o script
			codigo = texto.substring(ini,fim);
			// executa o script
			eval(codigo);				
		}
	}
}

/**
* 	"Objeto" que representa um xml a ser requisitado
* 	@param args [
*				url => url a ser chamado no request
*				obj => refer?ncia do obj para callback
*				func => fun??o de callback
*				args => args a serem passados para o callback
*			]
*/
function xmlObj(args) {
	this._args = args;
	this.load();
}

/**
* 	M?todo que carrega o xml
*/
xmlObj.prototype.load = function() {
	this._request = this._getXMLHTTPRequest();
	var _this = this;
    if (this._request.overrideMimeType) this._request.overrideMimeType('text/xml');
	this._request.onreadystatechange = function(){_this._onData()};
	this._request.open("GET",this._args.url, true);
	this._request.send(null);
}


/**
*	Met?do que ser? chamdo quando o request terminar
*/
xmlObj.prototype._onData = function() {
	if(this._request.readyState == 4) {
		if(this._request.status == "200") {
			var obj = this._args.obj;
			var func = this._args.func;
			var args = this._args.args;
			args.responseXML = this._request.responseXML;
			args.responseText = this._request.responseText;
			obj[func](args);
		}
		delete this._request;
	}
}

/**
*	Retorna um XMLHTTPRequest v?lido de acordo com o browser
*/
xmlObj.prototype._getXMLHTTPRequest = function(){
	var xmlHttp;
	try	{
		xmlHttp = new ActiveXObject("Msxml2.XMLHttp");
	} catch(e) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
		} catch(e2) {}
	}

	if(xmlHttp == undefined && (typeof XMLHttpRequest != 'undefined')) {
		xmlHttp = new XMLHttpRequest();
	}
	return xmlHttp;
}


/**
 * Version of |data| that doesn't include whitespace at the beginning
 * and end and normalizes all whitespace to a single space. (Normally
 * |data| is a property of text nodes that gives the text of the node.)
 *
 * @param txt  The text node whose data should be returned
 * @return     A string giving the contents of the text node with
 *             whitespace collapsed.
 */
function data_of(txt)
{
	var data = txt.data;
	// Use ECMA-262 Edition 3 String and RegExp features
	data = data.replace(/[\t\n\r ]+/g, " ");
	if (data.charAt(0) == " ")
		data = data.substring(1, data.length);
	if (data.charAt(data.length - 1) == " ")
		data = data.substring(0, data.length - 1);
	return data;
}


/**
 * 
 * @param name
 * @param value
 * @param days
 * @return
 */
function createCookie(args) {
	name = (args.name)?args.name:"GLB_VIDEO_WARNING";
	value = (args.value)?args.value:"OK";
	domain = (args.domain)?args.domain:((document.domain.indexOf(".globoi.com")!=-1)?".globoi.com":".globo.com");
	
	path = (args.path)?args.path:"/";
	if (args.days) {
		date = new Date();
		date.setTime(date.getTime()+(args.days*24*60*60*1000));
		var expires = date.toGMTString();
	}
	else var expires = false;
	document.cookie = name + "=" +escape( value ) +
					( ( expires ) ? ";expires=" + expires : "" ) + 
					( ( path ) ? ";path=" + path : "" ) + 
					( ( domain ) ? ";domain=" + domain : "" );
}

/**
 * 
 * @param name
 * @return
 */
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

var VersaoClassica = {
	cookie:"GLB_VERSAO_CLASSICA",
	go:function(url){
		createCookie({name:this.cookie,value:"VER"});	
		location.href=(url)?url:"http://www.globo.com";
	}
}

var TrackPageview = {
	add:function(url){
		if(typeof pageTracker != 'undefined')
			pageTracker._trackPageview(url);
		if(typeof _ATM != 'undefined')
			_ATM.TRACKURL(location.href);
	}
}

var History = function(){
	var _history = [];
	var _find = function(url){
		for(i=0;i<_history.length;i++){
			if(_history[i] == url)
				return [i,_history[i]];
		}
		return false;
	}
	var _go=function(url){
		if(h = _find(url)){
			while(h[0] < _history.length - 1)
				_history.pop();
			return true;
		}
		else{
			_history.push(url);
			return false;
		}
	}
	var _clear=function(){
		_history = [];
		return true;
	}
	return{
		go:_go,
		clear:_clear
	}
}();

