// JavaScript Document
var hpmaisDestaque = new Class({
	Implements:[Options],
	container:null,
	images:[],
	index:-1,
	interval:null,
	options: {
		'tituloalign':'bottomleft',
		'width':'200px',
		'height':'200px',
		'menuheight':'13px',
		'menuposition':'bottomright',
		'menubtsel': 'destaques_bt_sel.png',
		'menubt': 'destaques_bt.png',
		'menubtalign':'right',
		'effect':'fade',
		'loading': 'ico_loading_circle.gif',
		'images':[],
		'startIndex':0,
		'duration':10,
		'effectduration':600,
		'autostart':true
	},	
	getTransition: function() {
		switch(this.options.effect) {
			case 'fade':
				return transition = Fx.Transitions.linear.easeOut;
				break;
			case 'slide':
				return transition = Fx.Transitions.linear.easeOut;
				break;
			case 'down':
				return transition = Fx.Transitions.linear.easeOut;
				break;
			case 'up':
				return transition = Fx.Transitions.linear.easeOut;
				break;
			case 'elastic':
				return transition = Fx.Transitions.Elastic.easeOut;
				break;
			case 'down-elastic':
				return transition = Fx.Transitions.Elastic.easeOut;
				break;
			case 'up-eslatic':
				return transition = Fx.Transitions.Elastic.easeOut;
				break;
		}
	},
	initialize: function(div,options) {
		this.setOptions(options);
		this.options.path=PATH_JS+'hpmaisDestaque/';
		this.container = $(div).setStyles({'overflow':'hidden','position':'relative','width':this.options.width,'height':this.options.height});
		var transition = this.getTransition();
		if (this.options.images.length>0) {
			this.createImages();
		}
		// autostart
		if (this.options.autostart==true) { this.start(); }
	},
	createImages: function() {
		var menubar = new Element('div',{'class':'destaques_menubar','align':this.options.menubtalign});
		this.options.images.each(function(el,i){
			var tmp = new Element('div',{'class':'destaques_img'});
			if (el.url!='') {
				tmp.setStyle('cursor','pointer');
				var a = new Element('a',{'class':'destaques_a','border':'0','target':el.target,'href':el.url}).inject(this.container);
				tmp.inject(a);
				if (typeof(_gaq)!=='undefined' && el.track==true) {
					a.addEvent('click',function(){
						_gaq.push(['_trackEvent', 'Destaques', 'Destaque Click', 'Destaque: '+el.id+' '+el.trackname]);
					}.bind(this));
				}
			} else tmp.inject(this.container);
			tmp.setStyles({'margin':'0','opacity':'0','background':'url('+this.options.path+this.options.loading+') no-repeat center','width':this.options.width,'height':this.options.height,'overflow':'hidden'})
				.set('tween', {'transition': transition,'duration':this.options.effectduration});
			this.images.include(tmp);
			
			var bt = new Element('img',{'align':'absmiddle','src':this.options.path+this.options.menubt,'class':'destaques_menubar_bt'})
				.inject(menubar)
				.addEvent('click',function(){ this.showImage(i); }.bind(this));
			this.images[i].store('bt',bt);
			this.images[i].store('id',el.id);
			this.images[i].store('track',el.track);
			this.images[i].store('trackname',el.trackname);

			if (el.titulo!='') {
				
				if (this.options.tituloalign.indexOf('right')!==-1)
					var align='right';
				else if (this.options.tituloalign.indexOf('left')!==-1)
					var align='left';
				else
					var align='center';
				
				var divTitulo = new Element('div',{'class':'destaque_div_titulo','align':align}).inject(this.container).setStyle('display','none');
				new Element('span',{'html':el.titulo,'class':'destaque_titulo'}).inject(divTitulo);
				if (el.subtitulo!='') {
					new Element('br').inject(divTitulo);
					new Element('span',{'html':el.subtitulo,'class':'destaque_subtitulo'}).inject(divTitulo);
				}
				this.images[i].store('titulo',divTitulo);
			}
			
			var ext = el.img.split('.').pop();
			if (ext=='swf') {
				this.images[i].setStyle('background','none');
				var div_flash_tmp_id = 'dest_swf_'+Math.ceil((Math.random()*30000));
				var div_flash_tmp = new Element('div',{'id':div_flash_tmp_id}).inject(this.images[i]);
				embedswf(el.img,div_flash_tmp_id,this.options.width,this.options.height,{},{'wmode':'transparent'},{});
			} else {
				var img_tmp = Asset.image(el.img,{onLoad:function(){
					this.images[i].setStyle('background','none');
					img_tmp.inject(this.images[i]);
				}.bind(this)});
			}
		},this);
		menubar.setStyles({'width':this.options.width,'height':this.options.menuheight}).inject(this.container)
			.position({
				'position':this.options.menuposition,
				'edge':this.options.menuposition,
				'relativeTo':this.container
			});

	},	
	start: function() {
		if (this.images.length>0) { this.showImage(this.options.startIndex); }
	},
	stop: function() {
		clearInterval(this.interval);
	},
	moveNext: function() {
		if (this.images.length>1) {
			var index = this.index+1;
			if (index>(this.images.length-1))
				index=0;
			this.showImage(index);
		}
	},
	btSel: function(index) {
		if (this.index>=0)
			this.images[this.index].retrieve('bt').set('src',this.options.path+this.options.menubt);
		this.images[index].retrieve('bt').set('src',this.options.path+this.options.menubtsel);
		if (this.images[index].retrieve('visto')==null) {
			this.images[index].store('visto',true);
			if (typeof(_gaq)!=='undefined' && this.images[index].retrieve('track')==true)
				_gaq.push(['_trackEvent', 'Destaques', 'Destaque View', 'Destaque: '+this.images[index].retrieve('id')+' '+this.images[index].retrieve('trackname')]);
		}
	},
	escondeTitulo: function() {
		if (this.images[this.index].retrieve('titulo')!=null) {
			this.images[this.index].retrieve('titulo').setStyle('display','none');
		}
	},
	mostraTitulo: function() {
		if (this.images[this.index].retrieve('titulo')!='') {
			this.images[this.index].retrieve('titulo').setStyle('display','block').position({'relativeTo':this.container,'edge':this.options.tituloalign,'position':this.options.tituloalign});
		}
	},
	showImage: function(index) {
		if (index!=this.index) {
			if (this.index>=0) {
				this.escondeTitulo();
				this.images[this.index].setStyle('z-index',1);
				var myFx = new Fx.Tween(this.images[this.index]);
				myFx.start('opacity',0);
			}
			switch (this.options.effect) {
				case 'elastic':
					this.images[index].setStyles({'margin-left':this.options.width,'opacity':1});
					this.images[index].setStyle('z-index',2).tween('margin-left','0px');
				break;
				case 'fade':
					this.images[index].setStyle('z-index',2).tween('opacity',1);
				break;
				case 'slide':
					this.images[index].setStyles({'margin-left':this.options.width,'opacity':1});
					this.images[index].setStyle('z-index',2).tween('margin-left','0px');
				break;
				case 'up':
					this.images[index].setStyles({'margin-top':this.options.height,'opacity':1});
					this.images[index].setStyle('z-index',2).tween('margin-top','0px');
				break;
				case 'down':
					this.images[index].setStyles({'margin-top':'-'+this.options.height,'opacity':1});
					this.images[index].setStyle('z-index',2).tween('margin-top','0px');
				break;
				case 'up-elastic':
					this.images[index].setStyles({'margin-top':this.options.height,'opacity':1});
					this.images[index].setStyle('z-index',2).tween('margin-top','0px');
				break;
				case 'down-elastic':
					this.images[index].setStyles({'margin-top':'-'+this.options.height,'opacity':1});
					this.images[index].setStyle('z-index',2).tween('margin-top','0px');
				break;
			}
			this.btSel(index);
			this.index=index;
			this.mostraTitulo();
			if (this.images.length>1) {
				this.stop();
				this.interval = this.moveNext.periodical((this.options.duration*1000),this);
			}
		}
	}
});
