var http_request = false;
var BANNER_PATH = "assets/banners/";

// Author: Patricio Ferreira
// TEMPLATE Output: Use lo siguiente ->
/*<table align="center" cellpadding="0" cellspacing="0" width="100%">
	<tr>
		<td height="100"></td>
		<td height="100"></td>
		<td height="100"></td>
		<td height="100"></td>
		<td height="100"></td>
	</tr>
	<tr>
		<td height="100"></td>
		<td height="100"></td>
		<td height="100"></td>
		<td height="100"></td>
		<td height="100"></td>
	</tr>
</table> */

function makeRequest(url, parameters) {
	
	http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}

	http_request.onreadystatechange = onResultsBanners;
	http_request.open('GET', url + parameters, true);
	http_request.send(null);

}

function onResultsBanners() {
	
	var container = document.getElementById( 'container_banners' );
	var bgtype = document.getElementById( 'background-td' );
	
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			
			var xmldoc = http_request.responseXML;
			RemoveWhitespace( xmldoc ); // Clean WhiteSpaces
			var root = xmldoc.getElementsByTagName('result').item(0);
			var result = "";
			var swfObjs = new Array();
			
			//alert('CHILD NODES LENGTH: ' + root.childNodes.length );
			
			// Select Background Type ( Single | Double )
			if( root.childNodes.length > 6 ) { // Double
				
				bgtype.style.backgroundImage = "url(images/banner_background_double.jpg)"; // Change BackgroundImage
				bgtype.style.height = "225px";
				container.style.height = "225px"; // Change Height's Container
				container.style.marginTop = "20px";
				
			} else { // Single
				
				bgtype.style.backgroundImage = "url(images/banner_background_single.jpg)"; // Change BackgroundImage
				bgtype.style.height = "106px";
				container.style.height = "106px"; // Change Height's Container
				container.style.marginTop = "20px";
				
			}
			
			for( var iNode = 0; iNode < root.childNodes.length; iNode++ ) {
				
				var node = root.childNodes[iNode];
				
				if( -1 != navigator.userAgent.indexOf("MSIE") ) {
					
					if( node.attributes[1].nodeValue.indexOf("swf") != -1 ) { // BannerType -> SWF
						
						result += '<div class="sban"><div id="banner'+iNode+'"></div>';
						
						// Se usa ( swfObject ) para embeber el loader del banners
						var flashvars = {};
						flashvars.srclink = node.attributes[2].nodeValue;
						flashvars.srcswf = node.attributes[1].nodeValue;
						
						swfObjs['banner'+iNode] = flashvars;
						
					} else { // BannerType -> JPG or GIF or PNG
						
						result += '<div class="sban" id="banner'+iNode+'">';
						
						if( node.attributes[2].nodeValue != "" ) {
							
							result += '<a href="' + node.attributes[2].nodeValue + '" target="_blank">';
							result += '<img src="' + BANNER_PATH + node.attributes[1].nodeValue + '" border="0" height="90" width="120" /></a>';
							
						} else {
							
							result += '<img src="' + BANNER_PATH + node.attributes[1].nodeValue + '" border="0" height="90" width="120" />';
							
						}
						
					}
					
					result += '</div>\n';
					
				} else { // if ( Firefox )
					
					//if( node.nodeName != "#text" ) {
						
						if( node.attributes["src"].nodeValue.indexOf("swf") != -1 ) { // BannerType -> SWF
							
							result += '<div class="sban"><div id="banner'+iNode+'"></div>';
							// Se usa ( swfObject ) para embeber el loader del banners
							var flashvars = {};
							flashvars.srclink = node.attributes["link"].nodeValue;
							flashvars.srcswf = node.attributes["src"].nodeValue;
							
							swfObjs['banner'+iNode] = flashvars;
							
						} else { // BannerType -> JPG or GIF or PNG
							
							result += '<div class="sban" id="banner'+iNode+'">';
							
							if( node.attributes["link"].nodeValue != "" ) {
								
								result += '<a href="' + node.attributes["link"].nodeValue + '" target="_blank">';
								result += '<img src="' + BANNER_PATH + node.attributes["src"].nodeValue + '" border="0" height="90" width="120" /></a>';
								
							} else {
								
								result += '<img src="' + BANNER_PATH + node.attributes["src"].nodeValue + '" border="0" height="90" width="120" />';
								
							}
							
						}
						
						result += '</div>\n';
						
					//}
					
				}
				
			} // END FOR
			
			container.innerHTML = result;
			
			// FIX on SWFObject Embed
			for( var key in swfObjs ) {
				
				var params = {};
				var attributes = {};
				swfobject.embedSWF('loaderbanner.swf', key, '120', '90', '9.0.0', 'expressInstall.swf', swfObjs[key], params, attributes);
				//alert( key + ": " + swfObjs[key].srcswf );
				
			}
			
		} else {
			alert('No pueden Cargarse los banners');
		}
	}
}

function getBanners( category, program ) {
	makeRequest('queries/getbanners.php', '?category=' + category + '&program=' + program );
}

/***********************************
* @method RemoveWhitespace
* @param {Object} xml
************************************/
function RemoveWhitespace(xml)
{
	var loopIndex;
	for (loopIndex = 0; loopIndex < xml.childNodes.length; loopIndex++) {
		var currentNode = xml.childNodes[loopIndex];
		if (currentNode.nodeType == 1){RemoveWhitespace(currentNode);}
		if (((/^\s+$/.test(currentNode.nodeValue))) && (currentNode.nodeType == 3))
		{
			xml.removeChild(xml.childNodes[loopIndex--]);
		}
	}
}

