var isIE = false;
var req;
var tpl;
var rowTpl;
var currWordIdx = 0;

function loadXMLDoc(url) {
	
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }

}

function processReqChange() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            processResult();
         } else {
            //oops!
         }
    }
}

function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
        	if (result.firstChild != null ){
            	return result.firstChild.nodeValue;    		
        	}
        }
    } else {
        return "n/a";
    }
}

function searchPromos(){
	
	var url = promoUrlBase+"jm/searchXml?as_word="+tags[currWordIdx]+"&as_site_id="+promoSiteId;
	loadXMLDoc(url);

	if ( currWordIdx++ >= tags.length-1 ) currWordIdx = 0;
	setTimeout("searchPromos()", queryDelay);

}

function processResult() {

    var xml = req.responseXML;
    var qty = xml.getElementsByTagName("listing").item(0).getAttribute("items_total");
    var toShow = Math.min(qty,itemsQty);
    var items = xml.getElementsByTagName("item");
    var rows = "";
    
    for (var i=0; i < toShow; i++ ) {
    
		tmp = rowTpl;
		
        id = items.item(i).getAttribute("id");    
    	title      = document.createTextNode(getElementTextNS("", "title", items[i], 0)).data;
    	title	   = title.substring(0,40);
		price      = document.createTextNode(getElementTextNS("", "price", items[i], 0)).data;
		currency   = document.createTextNode(getElementTextNS("", "currency", items[i], 0)).data;
		imageUrl   = document.createTextNode(getElementTextNS("", "image_url", items[i], 0)).data;
      	if ( imageUrl == "undefined" ) imageUrl = promoUrlBase+"jm/img?s="+promoSiteId+"&f=artsinfoto.gif&v=P";
      	
		tmp = tmp.replace("##DESCR##", title);
		tmp = tmp.replace("##TITLE##", title);
		tmp = tmp.replace("##LINK##", promoUrlBase+"jm/item?site="+promoSiteId+"&id="+id);
		tmp = tmp.replace("##IMG##", imageUrl);
		tmp = tmp.replace("##PRECIO##", currency+price);
				
		rows += tmp;		
				
    }    
    document.getElementById("galTpl").innerHTML = tpl.replace("##ROWS##", rows)
    document.getElementById("galTpl").style.visibility = "";

}

function actTicker(){
	tpl = document.getElementById("galTpl").innerHTML; 
	rowTpl = document.getElementById("rowTpl").innerHTML; 
	setTimeout("searchPromos()", 300);
}
