Enjin_GallerySlideshow = function(preset_id, data) {
	this.init(preset_id, data);
}

Enjin_GallerySlideshow.__instancesPreset = {};
Enjin_GallerySlideshow.getInstanceForPreset = function(preset_id) {	
	return Enjin_GallerySlideshow.__instancesPreset[preset_id];
}

Enjin_GallerySlideshow.prototype = {
	preset_id: 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, data) {
		Enjin_GallerySlideshow.__instancesPreset[data.gallery_preset_id] = this;
	
		var self = this;
		this.preset_id = preset_id;
		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 pd = Enjin_Gallery_Popup_Detail.getInstance(self.data.gallery_preset_id);
			var image = pd.getImageIndex(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;
			}*/
			
			pd.detailShow(image.image_id);
		});		
		
		if ($.browser.msie) {
			//need to set explicit width/height
			this.el_main_img.css('width', this.el_main.width()+"px");			
		}
		this.el_main_img.show();
		
		this.loadImage();
	},
	
	stopTimeout: function() {
		if (this.timeout_id > 0) {
			clearTimeout(this.timeout_id);
			this.timeout_id = 0;
		}
	},
	
	callTimeout: function() {
		var self = this;
		var pd = Enjin_Gallery_Popup_Detail.getInstance(this.data.gallery_preset_id);
		this.current_index++;
		
		if (this.current_index >= pd.getImagesLength())
			this.current_index = 0;
		
		delete pd;
		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++;
		var pd = Enjin_Gallery_Popup_Detail.getInstance(this.data.gallery_preset_id);
		
		if (this.current_index >= pd.getImagesLength())
			this.current_index = 0;
		
		delete pd;
		this.loadImage();
	},
	
	goPrevious: function() {
		var pd = Enjin_Gallery_Popup_Detail.getInstance(this.data.gallery_preset_id);
		this.stopTimeout();
		this.current_index = this.current_index_showing - 1;
		
		if (this.current_index < 0)
			this.current_index = pd.getImagesLength()-1;
		
		delete pd;
		this.loadImage();
	},	
	
	loadImage: function() {
		var pd = Enjin_Gallery_Popup_Detail.getInstance(this.data.gallery_preset_id);
		this.current_index_showing = this.current_index;
		this.el_main_img.attr('src', pd.getImageIndex(this.current_index).url);
	},
	
	removeImage: function(image_id) {
		this.stopTimeout();
		this.playing = false;
		var pd = Enjin_Gallery_Popup_Detail.getInstance(this.data.gallery_preset_id);
		var images_length = pd.getImagesLength();
		var changed_index = false;
		
		if (this.current_index == pd.getLastRemovedIndex()) {
			changed_index = true;
		}

		if (this.current_index > images_length) {
			this.current_index = 0;
			changed_index = true;
		}
		
		if (images_length > 0) {
			//start playing again
			this.playing = true;
			
			if (changed_index)
				this.loadImage();
			else
				this.callLoadImage();
				
		} else {
			//hide all
			this.el_main.hide();
		}
	},
	
	addImage: function() {
		this.playing = false;
		this.stopTimeout();		
		
		//always will be at start
		if (this.current_index == 0) {
			this.current_index = 2;
		}
		
		this.playing = true;
		this.callLoadImage();
	}
}
