var hpmaisGaleria = new Class({
	Implements: [Options,Events],
	options: {
		'id':null,
		'thumb_width':null,
		'thumb_height':null,
		'width':765,
		'speed':5,
		'image_fx': Fx.Transitions.linear,
		'container_fx': Fx.Transitions.Bounce.easeOut,
		'loading':'loading-bar-black.gif'
	},
	montar_estrutura: function() {
		$(this.options.id).set('class','hpmaisGaleria').setStyle('width',this.options.width+'px');
		this.carrousel_container = new Element('div',{'class':'carrousel_container'}).inject($(this.options.id)).setStyles({'width':this.options.width+'px','height':this.options.thumb_height+'px'});
		this.carrousel_wrapper = new Element('div',{'class':'carrousel_wrapper'}).inject(this.carrousel_container).setStyles({'width':this.options.width+'px','height':this.options.thumb_height+'px'});
		this.carrousel = new Element('div',{'class':'carrousel'}).inject(this.carrousel_wrapper).adopt(this.thumbs).setStyles({'width':(this.thumbs.length*(this.options.thumb_width+5))+'px','height':this.options.thumb_height+'px'});
		this.image_container = new Element('div',{'class':'image_container'}).inject($(this.options.id));
		this.image = new Element('div',{'class':'image'}).inject(this.image_container);
		this.loading = new Element('img',{'src':PATH_IMG+this.options.loading}).inject(this.image).setStyle('display','none');
		this.carrousel_right = new Element('div',{'class':'carrousel_right'}).inject(this.carrousel_wrapper).setStyles({'opacity':0.5,'height':this.options.thumb_height+'px'}).addEvents({'mouseover':this.carrousel_next.bind(this),'mouseout':this.carrousel_stop.bind(this)});
		this.carrousel_left = new Element('div',{'class':'carrousel_left'}).inject(this.carrousel_wrapper).setStyles({'opacity':0.5,'height':this.options.thumb_height+'px'}).addEvents({'mouseover':this.carrousel_previous.bind(this),'mouseout':this.carrousel_stop.bind(this)});
		this.image_right = new Element('div',{'class':'image_right','html':'&nbsp;'}).inject($(this.options.id)).addEvents({
			'click':this.image_next.bind(this),
			'mouseover':function(){ $(this).set('class','image_right_over'); },
			'mouseout':function(){ $(this).set('class','image_right'); }			
		});
		this.image_left = new Element('div',{'class':'image_left','html':'&nbsp;'}).inject($(this.options.id)).addEvents({
			'click':this.image_previous.bind(this),
			'mouseover':function(){ $(this).set('class','image_left_over'); },
			'mouseout':function(){ $(this).set('class','image_left'); }			
		});
		this.image_container_hide = new Element('div').inject(document.body).setStyle('display','none');
		this.image_legenda_container = new Element('div').inject($(this.options.id)).setStyles({'width':this.options.width,'position':'relative'});
		this.carrousel_fx = new Fx.Move(this.carrousel);
	},
	start: function() {
		this.thumbs = $(this.options.id).getElements('a');
		if (this.thumbs.length>0) {
			this.montar_estrutura();
			this.thumbs.each(function(el,i){
				$(el).set('class','carrousel_thumb').store('index',i).addEvent('click',function(e) { this.thumb_click(e,el); }.bind(this));
			},this);
			this.carrousel_right.position({'relativeTo':this.carrousel_wrapper,'position':'upperright','edge':'upperright'});
			this.carrousel_left.position({'relativeTo':this.carrousel_wrapper,'position':'upperleft','edge':'upperleft'});
			this.select_image(0);
		}
	},	
	selectedIndex:null,
	thumbs:[],
	carrousel_fx:null,
	initialize: function(options) {
		this.setOptions(options);
		if (this.options.id!=null)
			this.start();
	},
	thumb_click: function(e,el) {
		var e = new Event(e);
		e.stop();
		this.select_image($(el).retrieve('index'));
	},
	select_image: function(index) {
		this.image_remove();
		this.image_loading();
		if (this.selectedIndex!=null)
			this.thumbs[this.selectedIndex].set('class','carrousel_thumb');
		this.selectedIndex = index;		
		this.image_atual = Asset.image(this.thumbs[index].get('href'),{
			'onLoad': this.image_load.bind(this)
		});
//		new Element('img',{'src':this.thumbs[index].get('href')}).inject(this.image_container_hide).addEvent('load',this.image_load.bind(this));
	},
	image_loading: function() {
		this.loading.setStyle('display','block').position({'relativeTo':this.image_container,'position':'center'});
	},
	image_load: function(e) {
//		var e = new Event(e);
		this.loading.setStyle('display','none');
		this.image_remove();
		this.image_g = this.image_atual; //$(e.target);
		this.image.adopt(this.image_g);
		var height = this.image_g.getCoordinates().height;
		this.image.setStyle('height',height);		
		this.image_arrows(height);
		this.thumbs[this.selectedIndex].set('class','carrousel_thumb_sel');
//		var height = $(e.target).height;
//		new Fx.Morph(this.image_container, {duration: (this.image_height!=height?700:0), transition: this.options.container_fx }).addEvent('complete',function(){
//		this.image_remove();
//		this.thumbs[this.selectedIndex].set('class','hpmaisGaleria_carrousel_a_sel');
//		this.image_container.adopt($(e.target).setStyle('opacity',0).set('morph',{transition:this.options.image_fx,'duration':350}).morph({'opacity':1}));
//		var height = $(e.target).getCoordinates().height;
//		this.image_container.setStyle('height',height);
//		this.image_height=height;
//		this.image_arrows(height);
		if (this.thumbs[this.selectedIndex].get('title')!=null) {
			this.image_legenda(this.thumbs[this.selectedIndex].get('title'));
			this.image_g.addEvents({
				'mouseover':this.image_legenda_show.bind(this),
				'mouseout':this.image_legenda_close.bind(this)
			});
			this.image_legenda_container.position({'position':'bottom','edge':'bottom','relativeTo':$(this.options.id)});
			this.image_legenda_show(2000);
		}
//		}.bind(this)).start({
//			'height':height
//		});
	},
	image_arrows: function(height) {
		if (this.selectedIndex>0)
			this.image_left.setStyles({'opacity':0.3,'height':height+'px','display':'block'}).position({'relativeTo':this.image_g,'position':'upperLeft','allowNegative':true});
		else {
			this.image_left.setStyle('display','none');
		}
			
		if (this.selectedIndex<(this.thumbs.length-1))
			this.image_right.setStyles({'opacity':0.3,'height':height+'px','display':'block'}).position({'relativeTo':this.image_g,'position':'upperRight','edge':'upperRight','allowNegative':true});
		else {
			this.image_right.setStyle('display','none');
		}
	},	
	image_remove: function() {
		// adicionar efeitos
		this.image_left.setStyle('display','none');
		this.image_right.setStyle('display','none');
		if (this.image_g!=null)
			this.image_g.dispose();
	},
	image_previous: function() {
		this.select_image(this.selectedIndex-1);
	},
	image_next: function() {
		this.select_image(this.selectedIndex+1);
	},
	image_legenda: function(legenda) {
		if (legenda!='') {
			this.image_legenda_container.empty().position({'relativeTo':$(this.options.id),'edge':'bottom','position':'bottom'});
			this.image_legenda_text = new Element('div',{'class':'hpmaisGaleria_legenda','html':'<p>'+legenda+'</p>'}).inject(this.image_legenda_container).setStyle('opacity',0);
		}
	},
	image_legenda_show: function(time) {
		this.image_legenda_text.set('morph',{transition:this.options.image_fx,'duration':350}).morph({'opacity':0.8});
		if (time>500) {
			this.image_legenda_close.delay(time,this);
		}
	},
	image_legenda_close: function() {
		this.image_legenda_text.set('morph',{transition:this.options.image_fx,'duration':200}).morph({'opacity':0});
	},
	carrousel_stop: function() {
		if (this.carrousel_fx!=null)
			this.carrousel_fx.pause();
		this.moving=false;
	},
	carrousel_previous: function() {
		this.carrousel_move('previous');
	},
	carrousel_next: function(){
		this.carrousel_move('next');
	},
	carrousel_move: function(direction) {
		if (this.moving!=true) {
			var carrousel = this.carrousel.getCoordinates();
			var wrapper = this.carrousel_wrapper.getCoordinates();
			if ((direction=='next' && carrousel.right>wrapper.right) || (carrousel.left<wrapper.left && direction=='previous')) {
				this.moving=true;
				if (direction=='next') {
					var speed = (parseInt(carrousel.right) - parseInt(wrapper.right)) * this.options.speed;
					this.carrousel_fx.start({
						'duration':speed,
						'relativeTo':this.carrousel_wrapper,
						'edge':'upperRight',
						'position':'upperRight',
						'allowNegative':true
					});
					this.carrousel_fx.start('left',carrousel.left - (carrousel.right-wrapper.right));
				} else if (direction=='previous') {
					var speed = (parseInt(wrapper.left) - parseInt(carrousel.left)) * this.options.speed;
					this.carrousel_fx.start({
						'duration':speed,
						'relativeTo':this.carrousel_wrapper,
						'edge':'upperLeft',
						'position':'upperLeft',
						'allowNegative':true
					});
					this.carrousel_fx.start('left',carrousel.left - (carrousel.right-wrapper.right));
				}
			}
		}
	}
});
