Enjin_GallerySlideshow = function(preset_id, images, data) {
	this.init(preset_id, images, data);
}

Enjin_GallerySlideshow.__instances = {};
Enjin_GallerySlideshow.getInstance = function(preset_id) {	
	return Enjin_GallerySlideshow.__instances[preset_id];
}

Enjin_GallerySlideshow.prototype = {
	preset_id: null,
	images: null,
	data: null,
	current_index: null,
	current_index_showing: null,
	el_main: null,
	el_main_img: null,
	timeout_id: null,
	playing: null,
	loaded_first_image: null,	
		
	init: function(preset_id, images, data) {
		var self = this;
		this.preset_id = preset_id;
		this.images = images;
		this.data = data;		
		this.current_index = 0;
		this.current_index_showing = 0;
		this.loaded_first_image = false;		
		
		this.el_main = $('.m_galleryslideshow_'+preset_id);
		this.el_main_img = this.el_main.find('img');
		this.timeout_id = 0;
		this.playing = true;		
		
		//prepare events
		this.el_main_img.bind('load', function() {
			if (!self.loaded_first_image) {
				var top = Math.floor((self.el_main_img.height()-30)*0.5);
				self.el_main.find('a.previous').css('top', top);
				self.el_main.find('a.next').css('top', top);
				self.loaded_first_image = true;
			}			
			self.callTimeout();
		});
		this.el_main_img.bind('error', function() {
			self.el_main_img.show();
			self.callTimeout();
		});
		this.el_main.find('a').bind('mouseover', function() {
			self.stopTimeout();
			self.playing = false;
			
			self.el_main.find('a.previous').show();
			self.el_main.find('a.next').show();			
		});
		this.el_main.find('a').bind('mouseout', function() {
			self.playing = true;
			self.callLoadImage();
			
			self.el_main.find('a.previous').hide();
			self.el_main.find('a.next').hide();			
		});
		
		this.el_main.find('a.previous').bind('click', function(event) {
			event.preventDefault()
			event.stopPropagation()
			self.goPrevious();
		});
		this.el_main.find('a.next').bind('click', function(event) {
			event.preventDefault()
			event.stopPropagation()
			self.goNext();
		});
				
		
		
		this.el_main.find('a.preview').bind('click', function() {
			var image = self.images[self.current_index_showing];
						
			if (self.data.open_popup == "1") {
				//call to popup
				var url = '/popup/m/'+self.data.gallery_preset_id+'/detail/'+image.image_id;
				this.popup_window = window.open(url,'gallery_detail','location=0,status=0,scrollbars=1,resizable=0,menubar=0,height=720,width=700');
			} else {
				window.location = '/'+self.data.page_url+'/m/'+self.data.gallery_preset_id+'/detail/'+image.image_id;
			}
		});		
		
		
		this.loadImage();
	},
	
	stopTimeout: function() {
		if (this.timeout_id > 0) {
			clearTimeout(this.timeout_id);
			this.timeout_id = 0;
		}
	},
	
	callTimeout: function() {
		var self = this;
		this.current_index++;
		
		if (this.current_index >= this.images.length)
			this.current_index = 0;
		
		this.callLoadImage();
	},
	
	callLoadImage: function() {
		var self = this;
		if (this.timeout_id > 0)
			this.stopTimeout();
			
		this.timeout_id = setTimeout(function() {
			if (self.playing)
				self.loadImage();
		}, parseInt(this.data.seconds)*1000);
	},
	
	goNext: function() {
		this.stopTimeout();
		this.current_index++;
		
		if (this.current_index >= this.images.length)
			this.current_index = 0;
		
		this.loadImage();
	},
	
	goPrevious: function() {
		this.stopTimeout();
		this.current_index = this.current_index_showing - 1;
		
		if (this.current_index < 0)
			this.current_index = this.images.length-1;
		
		this.loadImage();
	},	
	
	loadImage: function() {
		this.current_index_showing = this.current_index;
		this.el_main_img.attr('src', this.images[this.current_index].url);
	}
}
