/**
* AjaxNuke Application
* NOTA: para qualquer objeto criado é necessario passar os parametros: COMPONENT e SITE
*/
var engine = new AjaxNukeEngine();
function meuBanner(name){
	var painel = new XmlPanel(name);
	var banner = new XmlBanner(name);
	painel.addObject(banner);
}


/**
* Requista um banner via AjaxNuke
*/
function XmlBanner(name){
	this.name = name;
	this.tagName = "p";
	this.url = "module=jacotei.ajaxbanner&component=p&site=jacotei&nome=" + this.name;
}
XmlBanner.prototype = new XmlObject();

XmlBanner.prototype.ready = function(req){
	this.dom = req.loadedXml;
	if(req.loadedXml.firstChild.tagName != "message"){
		this.insert(this.dom.getElementsByTagName(this.tagName).item(0));
	}
};

XmlBanner.prototype.insert = function(xmlObject){
	if(this.current){
		for(var i = 0; i < xmlObject.childNodes.length; i++){
			var banner = xmlObject.childNodes.item(i);
			var bannerLink = this.getBannerObject(banner.getAttribute("imgsrc"), banner.getAttribute("bannerid"), banner.getAttribute("posicaobanner"), banner.getAttribute("descricao"));
			this.current.appendChild(bannerLink);
			if(banner.getAttribute("posicaobanner") == 'D'){
				var br = this.getBreakLineObject();
				this.current.appendChild(br);
				var img = this.getImageObject("http://images.jacotei.com.br/imgs/spacer.gif", "");
				img.setAttribute("width", "1");
				img.setAttribute("height", "5");
				this.current.appendChild(img);
			}
		}
	}else{
		engine.setError(new Object().message = "Dom Object will missing.");
	}
};
XmlBanner.prototype.getImageObject = function(src, alt){
	var img = document.createElement("img");
	img.setAttribute("border", "0");
	img.setAttribute("src", src);
	img.setAttribute("alt", alt);
	img.setAttribute("title", alt);
	return img;
};
XmlBanner.prototype.getLinkObject = function(){
	var a = document.createElement("a");
	a.setAttribute("href", "");
	a.setAttribute("target", "_blank");
	return a;
};
XmlBanner.prototype.getBreakLineObject = function(){
	var br = document.createElement("br");
	return br;
};
XmlBanner.prototype.getParagraphObject = function(){
	var p = document.createElement("p");
	return p;
};
XmlBanner.prototype.getBannerObject = function(imgsrc, bannerId, posicao, descricao){
	if(!descricao){
		descricao = '';
	}
	var url = "mod.php?module=jacotei.redirecionabanner&site=jacotei&xsl=page&xml=home&lang=pt-br&bannerid="+bannerId;
	var link = this.getLinkObject();
	link.setAttribute("href", url);
	if(posicao == 'D'){
		link.setAttribute("class", 'banner_direita');
	}
	var img = this.getImageObject(imgsrc, descricao);
	if(posicao == 'D'){
		img.setAttribute("class", 'banner_direita');
	}
	link.appendChild(img);
	return link;
};


/**
* Obter o nome do usuario logado
*/
function XmlUsuario(name){
	this.name = name;
	//nome da tag no xml
	this.tagName = "content";
	this.url = "module=jacotei.ajaxusuario&site=jacotei";
}
XmlUsuario.prototype = new XmlDispatcher();

XmlUsuario.prototype.insert = function(xmlObject){
	if(xmlObject.indexOf('VALIDE SEU CADASTRO') != -1)
	{
		this.current.href = 'mod.php?module=jacotei.validacao';
	}
	this.current.appendChild(document.createTextNode(xmlObject));
};



/**
* Obtem as cidades de um estado selecionado via AjaxNuke
*/
function XmlEstado(name){
	this.name = name;
	//nome da tag no xml
	this.tagName = "estado";
	this.htmlObject = document.getElementsByName(this.name).item(0);
	this.url = "module=jacotei.ajaxendereco&component=" + this.tagName + "&site=jacotei";
}
XmlEstado.prototype = new XmlObject();

XmlEstado.prototype.generateObject = function(current){
	this.current = current;
	var self = this;
	var url = this.url;
	function mudar(){
		self.url += "&estado=" + self.getEstadoSelecionado();
		self.selfRequest();
		self.url = url;
	}
	this.htmlObject.onchange = mudar;
	
};

XmlEstado.prototype.ready = function(req){
	this.dom = req.loadedXml;
	if(req.loadedXml.firstChild.tagName != "message"){
		this.insert(this.dom.getElementsByTagName(this.tagName).item(0));
	}
};

XmlEstado.prototype.insert = function(xmlObject){
	this.setCidades(xmlObject);
};

XmlEstado.prototype.setCidades = function(estadoNo){
	this.removerCidades();
	for(var i=0; i<estadoNo.childNodes.length; i++){
		this.addCidade(estadoNo.childNodes[i]);
	}
};

XmlEstado.prototype.getEstadoSelecionado = function(){
	return this.htmlObject.options[this.htmlObject.selectedIndex].value;
};

XmlEstado.prototype.addCidade = function(noCidade){
	var cidade = document.createElement('OPTION');
	cidade.appendChild(document.createTextNode(noCidade.getAttribute('nome')));
	cidade.setAttribute('value', noCidade.getAttribute('id'));
	this.current.appendChild(cidade);
};

XmlEstado.prototype.removerCidades = function(){
	var selectCidades = this.current;
	selectCidades.options.length = 1;
};