var velocidad = 1;

var moviendoseCSS = "overflow: hidden;background-position:center center;background-repeat:no-repeat;float:left;width:901px;height:320px;margin: 28px 0 0 65px;";

var indice_j = 0;

var incrementXRight = 1;
var incrementYRight = 0;
var incrementXLeft = 1;
var incrementYLeft = 0.6;

var timeOut = 3000;

function panoramicRight( X, Y, result, MINx, MAXx, MINy, MAXy )
{
	document.getElementById("banner_home").style.cssText = moviendoseCSS + ';background-position: ' + X + 'px ' + Y + 'px; background-image:url' + "('/sections/content/upload/" + result[indice_j][0] + "');";//background-color:transparent;

	if( MAXx - MAXy != 0 )
	{
		if ( MAXx == 0 && Y > MINy )
		{
			Y += -incrementXRight;
			setTimeout(function(){panoramicRight( X,Y,result, MINx, MAXx, MINy, MAXy)},velocidad);
		}
		else if ( MAXy == 0 && X > MINx )
		{
			X += -incrementXRight;
			setTimeout(function(){panoramicRight( X,Y,result, MINx, MAXx, MINy, MAXy)},velocidad);
		}
		//else if (X <= 0 && X > MINx && Y > MINy )
		else if (X <= 0 && X > MINx )
		{
			X += -incrementXRight;
			Y += -incrementYRight;
			setTimeout(function(){panoramicRight( X,Y,result, MINx, MAXx, MINy, MAXy)},velocidad);
		}
		else if( X == MINx && Y > MINy ) 
		{
			setTimeout(function(){panoramicLeft( X,Y,result, MINx, MAXx, MINy, MAXy)},velocidad);
		}
		else
		{
			indice_j++;
			
			if( indice_j == result.length )
			{
				indice_j = 0;
			}
			
			var MAXx = result[indice_j][1] - 901;
			var MAXy = result[indice_j][2] - 320;
			
			var MINx = MAXx * -1;
			var MINy = MAXy * -1;
			
			$( "banner_home_loading" ).style.display = '';	
			setTimeout(function(){$( "banner_home_loading" ).style.display = 'none'},timeOut);
						
			panoramicRight(0,0, result, MINx, MAXx, MINy, MAXy);
		}
	}
	else
	{
		if( result.length > 1 )
		{
			indice_j++;
			
			if( indice_j == result.length )
			{
				indice_j = 0;
			}
			
			var MAXx = result[indice_j][1] - 901;
			var MAXy = result[indice_j][2] - 320;
			
			var MINx = MAXx * -1;
			var MINy = MAXy * -1;
			
			$( "banner_home_loading" ).style.display = '';	
			setTimeout(function(){$( "banner_home_loading" ).style.display = 'none'},timeOut);	
			
			setTimeout(function(){panoramicRight( 0,0,result, MINx, MAXx, MINy, MAXy)},timeOut);
		}
	}
}

function panoramicLeft( X, Y,result, MINx, MAXx, MINy, MAXy ) 
{
	document.getElementById("banner_home").style.cssText = moviendoseCSS + ';background-position: ' + X + 'px ' + Y + 'px; background-image:url' + "('/sections/content/upload/" + result[indice_j][0] + "');";//background-color:transparent;
	
	if (Y <= 0 && Y > MINy )
	{
		Y -= incrementYLeft;
	}
	
	if (X < 0 )
	{
		X += incrementXLeft;
		setTimeout(function(){panoramicLeft( X,Y,result, MINx, MAXx, MINy, MAXy)},velocidad);
	}
	else if( X == 0 ) 
	{
		X = 0;
		setTimeout(function(){panoramicRight( X,Y,result, MINx, MAXx, MINy, MAXy)},velocidad);
	}
}	

window.addEvent('domready', function()
{
	IntranetAjax.ajaxRequestSend('/ajax.php','action=getHomeBanner','GET',	{onResponse: function(r)
	{ 
		var res = new Array();
		
		if( r )
		{
			var result = r.split('<!--*Separador*-->');

			for (var i = 0; i < result.length; i++)
			{
				res[i] = result[i].split('<!--**Separador**-->');
			}

			var MAXx = res[0][1] - 901;
			var MAXy = res[0][2] - 320;
			
			var MINx = MAXx * -1;
			var MINy = MAXy * -1;
			
			setTimeout(function(){$( "banner_home_loading" ).style.display = 'none'},timeOut);	

			panoramicRight(0,0, res, MINx, MAXx, MINy, MAXy);
		}
		else
		{
			$( "banner_home_loading" ).style.display = 'none';
			
			document.getElementById("banner_home").style.cssText = "background-image:url('/html/img/content_home.png');background-color:transparent;";
		}
	}});	

});		

