/*
*	Revista Summa - www.revistasumma.com
*	
*	filename:	/js/ads.js
*	info:		Ads JS style
*	
*	developer:	Tony Córdova <tonyrcordova@yahoo.com>
*/

var ads_pth = '/';

var ads_loc = /^http:\/\/www\.revistaincae\.com\//;

var ads_srv = 'http://ads.revistaincae.com/';

var ads_ajx = new Array;

var ads_list = null;

function ads()
{
	ads_list = ads_getdivlist();
	
	var req = document.location.toString();
	
	req = req.replace( ads_loc, '' );
	req = req.replace( /\/\d*-.*\.html$/, '' );
	req = req.replace( /\/$/, '' );
	req = req.replace( /\//gi, '.' );
	
	if( req == '' )
		req = 'portada';

	
	for( var i=0 ; i<ads_list.length ; i++ )
	{
		var dte = new Date();
		
		var url = ads_pth + 'ad/' + ads_list[i].width+'x'+ads_list[i].height + '/' + req + '/?t=' + dte.getTime();
		
		ads_ajx[i] = ads_ajax();
		
		ads_ajx[i].open( "GET", url, true );
		
		ads_ajx[i].onreadystatechange =  new Function('onready(ads_ajx[' +i+ '], ' + i + ' );');
		
		ads_ajx[i].send(null);
	}
}

function onready( obj, pos )
{
	if( obj.readyState == 4 )
		if( obj.status == 200 )
		{
			if( obj.responseText != '' )
			{
				var nodes = obj.responseText.split('|');
				var ext = nodes[2].split('.');
				var params = nodes[3].split(';');
				switch( ext[1] )
				{
					case 'jpg':
					case 'gif':
						var des = params[0].replace('description=','');
						var img = new Image;

						img.title = pos;
						img.alt = des;
						
						img.onload = function ()
						{
							ads_list[this.title].div.innerHTML = '<a href="' + ads_srv + 'ir-a/' + nodes[0] + '/' + nodes[2] + '.redirect" target="_blank"><img src="'+this.src+'" title="' + this.alt + '" border="0" /></a>';
						}
						img.src = ads_srv + 'media/' + nodes[1] + '/' + nodes[0] + '/' + nodes[2];
					break;
					case 'swf':
						ads_list[pos].div.innerHTML = '<embed src="' + ads_srv + 'media/' + nodes[1] + '/' + nodes[0] + '/' + nodes[2] + '" flashvars="weburl=' + escape(ads_srv + 'ir-a/' + nodes[0] + '/' + nodes[2]) + '.redirect&amp;clickTag=' + escape(ads_srv + 'ir-a/' + nodes[0] + '/' + nodes[2]) + '.redirect" type="application/x-shockwave-flash" width="' + ads_list[pos].width + '" height="' + ads_list[pos].height + '"></embed>';
					break;
					case 'html':
					case 'php':
						ads_list[pos].div.innerHTML = '<iframe scrolling="no" frameborder="0" width="' + ads_list[pos].width + '" height="' + ads_list[pos].height + '" src="' + ads_srv + 'media/' + nodes[1] + '/' + nodes[0] + '/' + nodes[2] + '"></iframe>';
					break;
				}
				
			}
		}
}

function ads_onready()
{
	if( ads_ajx.readyState == 4 )
		if( ads_ajx.status == 200 )
		{
			alert( ads_ajx.responseText );
		}
}

function ads_ad( )
{
	var width	= null;
	var height	= null;
	var div		= null;
}

function ads_getdivlist( )
{
	var j	= 0;
	var itm	= document.getElementsByTagName('div');
	var lst	= new Array();
	var rex	= /ad(\d+)x(\d+)/
	for( var i=0 ; i<itm.length ; i++ )
		if( itm[i].className.match(rex) )
		{
			lst[j] = new ads_ad();
			lst[j].width	= RegExp.$1;
			lst[j].height	= RegExp.$2;
			lst[j].div		= itm[i];
			j++;
		}
	return lst;
}

function ads_ajax( )
{
	var ajx = false;
	
	try	{	ajx	=	new ActiveXObject("Msxml2.XMLHTTP");	}
	catch(e)
	{
		try {	ajx = new ActiveXObject("Microsoft.XMLHTTP");	}
		catch(E)	{	ajx = false;	}
	}
	if( !ajx && typeof XMLHttpRequest != 'undefined' )
		ajx = new XMLHttpRequest();
	
	return ajx;
}


