/****************** MAIN OBJECT ********/
window.addEvent('domready', function() {
	browserEngine = new browserObj( );
} );

window.addEvent('resize', function() {
	browserEngine.resizePlot();
} );

window.addEvent('unload', function() {
	browserEngine = null;
} );

function browserObj( )
{
	this.loops = new Array();
	this.loaded = new Array();
	this.initDocument();
}

browserObj.prototype.initDocument = function( )
{
	var thisObj = this;
	
	//// MENU
	has = $$('.haschildrens');
	for (var i=0; i<has.length; i++)
	{
		has[i].a = has[i].getElement('a');
		has[i].a.subs = has[i].getElement('ul');
		has[i].a.subs.fx = new Fx.Slide(has[i].a.subs, {duration: 500}).hide();
		/*if (has[i].a.subs.hasClass('sub-selected'))
			has[i].a.subs.fx.show();*/
		has[i].a.addEvent('click', function(event) {
			new Event(event).stop();
			this.subs.fx.toggle();
		} );
	}
	
	//// PLOT
	thisObj.resizePlot();
	thisObj.plot.fx = new Fx.Style(thisObj.plot, 'opacity').set(0);
	
	thisObj.loopIndex = 0;
	thisObj.thumbs = $('thumbs');
	if (thisObj.thumbs)
	{
		thisObj.thumbsCont = $('thumbs-container');
		thisObj.thumbsCont.setStyles( { 'marginTop': -thisObj.thumbs.getSize().size.y, 'height': thisObj.thumbs.getSize().size.y } );
		thisObj.thumbs.fx = new Fx.Style(thisObj.thumbs, 'marginTop', {duration: 600, transition: Fx.Transitions.Quad.easeInOut}).set( thisObj.thumbs.getSize().size.y );
	}
	thisObj.imgs = $$('.exec');
	for (var i=0; i<thisObj.imgs.length; i++)
	{
		thisObj.imgs[i].index = i;
		thisObj.imgs[i].addEvent('click', function(obj) {
			this.loopIndex = obj.index+1;
			if (this.loopIndex >= this.imgs.length)
				this.loopIndex = 0;
			this.changeImage( obj, 0 );
			this.onPause( );
		}.pass(thisObj.imgs[i], thisObj) );
	}
	
	thisObj.playStatus = true;
	if ($('play'))
	{
		thisObj.play = $('play');
		thisObj.play.addEvent('click', function() {
			this.togglePlay();
		}.bind(thisObj) );
		thisObj.play.addEvent('mouseenter', function(play) {
			play.setStyle('opacity', 1);
		}.pass(thisObj.play) );
		thisObj.play.addEvent('mouseleave', function(play) {
			play.setStyle('opacity', 0.001);
		}.pass(thisObj.play) );
	}
	else
		thisObj.noPlay;
	thisObj.loop();
}

browserObj.prototype.resizePlot = function( )
{
	var thisObj = this;
	
	// resize image plot
	thisObj.plot = $('plot');
	thisObj.plot.setStyle( 'height', (window.getScrollHeight()-70) );
	thisObj.plot.setStyle( 'width', (window.getScrollWidth()-24) );
	if ($('play'))
	{
		var play = $('play');
		var left = (thisObj.plot.getSize().size.x / 2) - (play.getSize().size.x / 2);
		var top = (thisObj.plot.getSize().size.y / 2) - (play.getSize().size.y / 2) + 70;
		play.setStyles( { 'position': 'absolute', 'opacity': 0.001, 'left': left, 'top': top } );
	}
}

browserObj.prototype.loop = function( )
{
	var thisObj = this;
	if (thisObj.playStatus)
	{
		thisObj.changeImage( thisObj.imgs[thisObj.loopIndex], 1 );
		thisObj.loopIndex++;
		if (thisObj.loopIndex >= thisObj.imgs.length)
			thisObj.loopIndex = 0;
		// preload next image
		imgPreload = new Image();
		imgPreload.src = thisObj.getURL(thisObj.imgs[thisObj.loopIndex]);
	}
}

browserObj.prototype.changeImage = function( obj, loop )
{
	var thisObj = this;
	
	thisObj.plot.fx.start(1, 0).chain( function() {
		for (var i=0; i<thisObj.imgs.length; i++)
		{
			if (thisObj.imgs[i].hasClass('img')) thisObj.imgs[i].setStyle('opacity', .5);
			thisObj.imgs[i].removeClass('img-selected');
		}
		if (obj.hasClass('img'))  obj.setStyle('opacity', 1);
		obj.addClass('img-selected');
		thisObj.plot.setStyle('background-image', "url('template/loading.gif')");
		var url = thisObj.getURL(obj);
		imgPreload = new Image();
		imgPreload.onload = function() {
			thisObj.plot.setStyle('background-image', "url('"+url+"')");
			thisObj.plot.fx.start(0, 1);
			if (loop)
			{
				if (thisObj.timer)	$clear(thisObj.timer);
				thisObj.timer = thisObj.loop.delay(4000, thisObj);
			}
		}
		imgPreload.src = url;
		if (obj.hasClass('gourl'))
		{
			thisObj.plot.setStyle('cursor', 'pointer');
			thisObj.plot.onclick = function() {
				self.location.href = obj.href;
			}
		}
	}.pass(obj, thisObj) );
}

browserObj.prototype.getURL = function( obj )
{
	var url = 'img.php?f='+obj.getProperty('name').replace(',','/')+'&width='+(window.getScrollWidth()-24)+'&height='+(window.getScrollHeight()-70);
	return url;
}

browserObj.prototype.togglePlay = function( )
{
	var thisObj = this;
	if (thisObj.playStatus)
	{
		thisObj.onPause();
	}
	else
	{
		thisObj.onPlay();
	}
}

browserObj.prototype.onPause = function( )
{
	var thisObj = this;
	if (thisObj.play)
	{
	thisObj.play.setHTML('Pausa');
	thisObj.play.addClass('pause');
	}
	if (thisObj.playStatus != false)
		thisObj.thumbs.fx.start(thisObj.thumbs.getSize().size.y, 0);
	thisObj.playStatus = false;
}

browserObj.prototype.onPlay = function( )
{
	var thisObj = this;
	if (thisObj.play)
	{
	thisObj.play.setHTML('Play');
	thisObj.play.removeClass('pause');
	}
	if (thisObj.playStatus != true)
		thisObj.thumbs.fx.start(0, thisObj.thumbs.getSize().size.y);
	thisObj.playStatus = true;
	if (thisObj.timer)	$clear(thisObj.timer);
		thisObj.timer = thisObj.loop.delay(2000, thisObj);
}