/**
 *
 * @author Bernardo Rufino <bermonruf@gmail.com>
 * @license Creative Commons Developing Nations: http://creativecommons.org/licenses/devnations/2.0/
 * @version 0.8
 * @description Script to add dynamic navigating functionality, it's framework independent
 *
 *       !Não retire essas informações!
 *       !Do not drop this information!
 */

//Just like the prototype, it returns a function wrapped in a context
Function.prototype.bind = function(context){
	var closure = this;
	var tmp_fnc = function(){
		return closure.apply(context, arguments);
	}
	return tmp_fnc;
}

Object.prototype.merge = function(object){
	for(attr in object){
		this[attr] = object[attr];
	}
}

document.navigators = {
	settings: {
		interval: 100, //The interval which the verification for new data will be done (in miliseconds)
		helper: true, //Specify if the onnavigate function will return the helper, do not change!
		iframe_src: "control.htm" //The location of the file control.htm, absolute path please
	},
	initialize: function(){
		this.is_ie = (navigator.userAgent.toLowerCase().indexOf("msie") > -1) ? true : false;
		if(this.is_ie){
			this.settings.iframe_src = this.settings.iframe_src.replace(/(\?|#).*$/, "");
			window.attachEvent("onload", (function(){
				//this.running = true; The iFrame that needs to make it running
				this.iframe = document.createElement("iframe");
				this.iframe.setAttribute("src", this.settings.iframe_src);
				this.iframe.style.display = "none";
				document.getElementsByTagName("body")[0].appendChild(this.iframe);
			}).bind(this));
		} else {this.running = true;}
	},
	//The function that is called by the user and puts new data in the hash to be called
	helper: function(data, force){
		var navs = document.navigators; var data = "[" + this.id + "]" + data;
		if(force){navs.data_buffer = "";} //If force is true then calling sequentially the same helper works
		if(navs.is_ie){navs.iframe.src = navs.settings.iframe_src + "?id=" + data;}
		else{window.location.hash = "#" + data;}
	},
	called: false,
	history: [],
	data_buffer: ""
};

function onnavigate(id, callback){
	var navs = document.navigators; navs[id] = {};
	if(!navs.running){navs.initialize();}
	setInterval(function(){if(navs.running){
		var url = window.location.href;
		var hash = window.location.hash.replace(/#/, "");
		var theid = "#[" + id + "]";
		if(url.indexOf(theid) > -1){ //If the call is with us!
			var data = url.split(theid)[1] || "";
			var new_data = theid + data;
			if(navs.data_buffer != new_data){ //If the old content is diferent
				callback(data); //Calling the callback function!
				navs.called = true; //To make sure was already called to fix the index state
				navs.history.push(new_data);
				navs.data_buffer = new_data;
			}
		} else if (!hash && navs.called){ //If the navigators was called and the page is in index state (no hashes), it means reload!
			navs.running = false; //To stop the others Intervals to dont make it the same way and broke something
			window.location = ((url.indexOf("#") > -1) ? url.split("#")[0] : url) + "#";
			window.location.reload();
		}
	}}, navs.settings.interval);
	navs[id].merge({callback: callback});
	if(navs.settings.helper){
		navs[id].helper = navs.helper.bind({id: id}); return navs[id].helper;
	}
}



//Funções ajax


function openAjax() {

	var ajax;
	try{
		ajax = new XMLHttpRequest();
	}catch(ee){
		try{
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(E){
				ajax = false;
			}
		}
	}
	return ajax;
}

function CpForm(FormName){
	comp = "document." + FormName;
	var frm = eval(comp);
	Cps = "";
	for (i=0; i<frm.length; i++){
		Cps = Cps + frm.elements[i].name + "=" + frm.elements[i].value + "&";
	}
	Cps = Cps.substring(0,Cps.length -1);
	return Cps;
}

function OpenAjaxPostCmd(pagina,camada,values,msg,divcarga,metodo,tpmsg) { 
				
	if(document.getElementById) {
		var ajax = openAjax();
		if(tpmsg=='1'){
			var exibeLoading = document.getElementById(divcarga);
		}
		var exibeResultado = document.getElementById(camada);
		if(metodo=='1'){
			ajax.open("POST", pagina, true);
			ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			ajax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			ajax.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			ajax.setRequestHeader("Pragma", "no-cache");
			valor = CpForm(values)
		}else{
			valor = null
			ajax.open("GET", pagina + values, true);
		}
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 1) {
				if(tpmsg=='1'){
					exibeLoading.style.display = 'inline';
					exibeLoading.innerHTML = msg
				}else{
					exibeResultado.innerHTML = msg
				}
			}
			if(ajax.readyState == 4) {
				if(tpmsg=='1'){
					exibeLoading.innerHTML = ""
					exibeLoading.style.display = 'none';
				}else{
					exibeResultado.innerHTML = ""
				}
				if(ajax.status == 200) {
					var resultado = null;
					resultado = ajax.responseText;
					resultado = resultado.replace(/\+/g," ");
					resultado = unescape(resultado);
					exibeResultado.innerHTML = resultado;
				} else {
					exibeResultado.innerHTML = "<br / ><br / ><center>Ocorreu um erro:</center><br / ><br / > <center>" + resultado + "</center>";
				}
			}
		}
		ajax.send(valor);
	}		 
}

abre_pagina = onnavigate("abre_pagina", function(data){
    OpenAjaxPostCmd(data,'conteudo','','Carregando página.','Status','2','1')
    //Refreshing the counter
     //var counter = $("counter")
     //counter.innerHTML = document.navigators.history.length + 1
});

abre_paginaDicas = onnavigate("abre_paginaDicas", function(data){
    OpenAjaxPostCmd(data,'conteudoDicas','','Carregando página.','Status','2','1')
});