if (!document.ELEMENT_NODE) {
	document.ELEMENT_NODE = 1
	document.ATTRIBUTE_NODE = 2
	document.TEXT_NODE = 3
	document.CDATA_SECTION_NODE = 4
	document.ENTITY_REFERENCE_NODE = 5
	document.ENTITY_NODE = 6
	document.PROCESSING_INSTRUCTION_NODE = 7
	document.COMMENT_NODE = 8
	document.DOCUMENT_NODE = 9
	document.DOCUMENT_TYPE_NODE = 10
	document.DOCUMENT_FRAGMENT_NODE = 11
	document.NOTATION_NODE = 12
}

document._importNode = function(importedNode, deep) {
	switch (importedNode.nodeType) {
		case document.ELEMENT_NODE:
			var newNode = document.createElement(importedNode.nodeName)
			if (importedNode.attributes && importedNode.attributes.length > 0)
				each(importedNode.attributes, function(el) { newNode.setAttribute(el.nodeName, importedNode.getAttribute(el.nodeName)) })
			if (deep && importedNode.childNodes && importedNode.childNodes.length > 0)
				each(importedNode.childNodes, function(el) { newNode.appendChild(document._importNode(el, deep)) })
			return newNode
			break
		case document.TEXT_NODE:
		case document.CDATA_SECTION_NODE:
		case document.COMMENT_NODE:
			return document.createTextNode(importedNode.nodeValue)
			break
	}
}

var Inicial = {
	
	atualizandoPromocoes: false, 
	
	AtualizarPromocoes: function(e) { 
																																																											
		e.preventDefault()
																	 
		if (Inicial.atualizandoPromocoes) return false
		
		Inicial.atualizandoPromocoes = true
		
		var oPromocao = getElem('promocao')
		
		var sIdProduto = getAll('a', oPromocao)[0].getAttribute('href')
		sIdProduto = sIdProduto.substr(sIdProduto.indexOf('idProduto=') + 'idProduto='.length)
		
		var sDirecao = e.currentTarget.className.replace(/pr-/, '')
																											
		function changePromocao(status, xml) {
			oPromocao.removeChild(textAtualizando)
			if (status == 200 || status == 304) {
				if (xml.getElementsByTagName('produto')[0].hasChildNodes()) {
					
					var response = document._importNode(xml.getElementsByTagName('produto')[0], true)

					oPromocao.replaceChild(filter(response.getElementsByTagName('p'), function(el) { return el.getAttribute('id') == 'pr-foto' })[0], getElem('pr-foto'))
					oPromocao.replaceChild(filter(response.getElementsByTagName('p'), function(el) { return el.getAttribute('id') == 'pr-descricao' })[0], getElem('pr-descricao'))
					oPromocao.replaceChild(filter(response.getElementsByTagName('p'), function(el) { return el.getAttribute('id') == 'pr-compra' })[0], getElem('pr-compra'))
					oPromocao.replaceChild(filter(response.getElementsByTagName('p'), function(el) { return el.getAttribute('id') == 'pr-navegacao' })[0], getElem('pr-navegacao'))
					oPromocao.replaceChild(response.getElementsByTagName('h3')[0], getAll('h3', oPromocao)[0])

					if (!document.importNode) oPromocao.innerHTML = oPromocao.innerHTML;
		
					addEvent(getAll('a', 'pr-navegacao'), 'click', Inicial.AtualizarPromocoes)	
					
					Inicial.atualizandoPromocoes = false
						
				}
			}
		}
	
		var textAtualizando = document.createElement('p')
		textAtualizando.className = 'atualizando'
		var text = document.createTextNode('Buscando produto. Aguarde.')
		textAtualizando.appendChild(text)
		oPromocao.appendChild(textAtualizando)

		var objAjax = new AjaxObject('/_include/asp/xml.asp?Acao=Promocao&idProduto=' + sIdProduto + '&Direcao=' + sDirecao, changePromocao)
		objAjax.update()	
		
	},
		
	init: function() {
		
		if (getElem('pr-navegacao')) {
		
			var oLinksNavegacao = getAll('a', 'pr-navegacao')
			each(oLinksNavegacao, function(el) { el.setAttribute('href', '') })
			addEvent(oLinksNavegacao, 'click', Inicial.AtualizarPromocoes)
		
		}
		
	}
	
}