Enjin_Gallery = function(preset_id, images, numbers_row, detail_url) {
	this.init(preset_id, images, numbers_row, detail_url);
}


Enjin_Gallery.__instances = {};
Enjin_Gallery.getInstance = function(preset_id) {	
	return Enjin_Gallery.__instances[preset_id];
}

Enjin_Gallery.prototype = {
	main_el: null,
	el_container: null,
	images: null,
	preset_id: null,
	thumb_width: 180,
	border_width: 0,
	thumbs_per_row: 0,
	thumb_scale_width: 0,
	numbers_row: 0,
	images_pending: null,
	images_processed: null,	
	images_processed_index: null,	
	pages: null,
	current_page: null,
	number_el_page: null,
	detail_url: null,
	mode_small: null,
	popup_window: null,
		
	init: function(preset_id, images, numbers_row, detail_url) {
		var self = this;
		Enjin_Gallery.__instances[preset_id] = this;
		
		this.detail_url = detail_url;
		this.preset_id = preset_id;
		this.images_processed = {};
		this.images_processed_index = 0;
		this.images = images;
		this.images_pending = this.images.concat([]); //copy of array
		
		if ($('.m_gallery_'+preset_id).width() < 500) {
			this.mode_small = true;
			this.border_width = 3;
			this.el_container = $('.m_gallery_'+preset_id+' .section-minimal .block-container-images .items');
			this.main_el = $('.m_gallery_'+preset_id+' .section-minimal');			
		} else {
			this.mode_small = false;
			this.border_width = 5;
			this.el_container = $('.m_gallery_'+preset_id+' .section-full .block-container-images .items');
			this.main_el = $('.m_gallery_'+preset_id+' .section-full');
		}
		
		this.main_el.show();
		this.numbers_row = numbers_row;
				
		this.current_page = 1;
		this.prepareImages();		
		
		$(window).bind('resize', function(event) {
			self.updateGrid();
		});
	},
	
	prepareGrid: function() {
		var available_width = this.el_container.width();
		
		this.thumbs_per_row = Math.ceil((available_width - this.border_width) / (this.thumb_width + this.border_width));
		this.thumb_scale_width = (available_width - (this.border_width * (this.thumbs_per_row))) / this.thumbs_per_row;
		
		//console.log("THUMBS: "+available_width+" -- "+this.border_width+" -- "+this.thumbs_per_row+" -- "+this.thumb_scale_width);
		this.updatePager();		
	},
	
	/* pager next */
	pageNext: function() {
		if (this.current_page < this.pages) {
			this.current_page++;
			this.updatePageLinks();
			this.updateItemsPager();
		}		
	},
	
	pagePrevious: function() {
		if (this.current_page > 1) {
			this.current_page--;
			this.updatePageLinks();
			this.updateItemsPager();
		}		
	},
	
	updateItemsPager: function() {
		//hide previous items
		var start_index = (this.current_page-1) * this.number_el_page;
		var end_index = (this.current_page * this.number_el_page)-1;
		var i;
		
		end_index = Math.min(this.images.length, end_index);
		
		//console.log("START INDEX: "+start_index+" -- "+end_index+" -- "+this.number_el_page+" -- "+this.images_processed_index);
		//alert("START INDEX: "+start_index+" -- "+end_index+" -- "+this.number_el_page+" -- "+this.images_processed_index);
		
		this.el_container.find('.image:lt('+start_index+')').hide();
		this.el_container.find('.image:gt('+end_index+')').hide();
		
		if (end_index >= this.images_processed_index) {
			//we need to add more data
			var remain = end_index - this.images_processed_index + 1;
			this.addBatchImages(remain);
		}
		
		//show items
		for (i=start_index; i<=end_index; i++) {
			this.el_container.find('.image:eq('+i+')').show();
		}		
	},
	
	updatePager: function() {
		//get pages		
		this.number_el_page = this.thumbs_per_row * this.numbers_row;
		this.pages = Math.ceil(this.images.length / (this.number_el_page*1.0));
		//console.log("NUMBER: "+this.number_el_page+" -- "+this.images.length+" -- "+this.pages);
		
		if (this.current_page > this.pages) {
			//special case when we are run out of pages
			this.current_page = Math.max(1, this.pages);
			this.updateItemsPager();
		}
		
		this.updatePageLinks();
	},
	
	updatePageLinks: function() {
		var number_links = 0;
		
		//update links
		this.main_el.find('.view-more .prev').hide();
		this.main_el.find('.view-more .separator').hide();
		this.main_el.find('.view-more .next').hide();
		
		if (this.current_page > 1) {
			number_links++;
			this.main_el.find('.view-more .prev').show();
		}
		
		if (this.current_page < this.pages) {
			number_links++;
			this.main_el.find('.view-more .next').show();			
		}
		
		
		if (number_links == 2) {
			//show separator
			this.main_el.find('.view-more .separator').show();
		}		
	},
	
	/* end of pager function */
	
	updateGrid: function() {
		var self = this;
		
		this.prepareGrid();
		jQuery.each(this.images_processed, function(image_id, el) {
			$(el).find('img').css('width', self.thumb_scale_width);
		});
		
		//update pager
		this.updateItemsPager();
	},
	
	prepareImages: function() {
		var self = this;
		var remain = 0;		
		
		//calculate size
		this.prepareGrid();
		this.images_processed = {};
		this.images_processed_index = 0;
		
		this.el_container.html('');
		if(this.images.length == 0) this.el_container.html('<div class="no-pictures">There are no images in this gallery.</div>');		
		remain = this.number_el_page;
		this.addBatchImages(remain);
	},
	
	addBatchImages: function(remain) {
		var keep_adding = true;
		var image;
		
		if (this.images_pending.length < 1)
			return; //nothing to do
		
		do {
			//console.log("REMAIN: "+remain+" -- "+this.thumbs_per_row+" -- "+this.numbers_row);
			image = this.images_pending.shift();
			this.addImage(image);
			if (this.images_pending.length < 1)
				keep_adding = false; //no more elements
						
			remain--;			
			if (remain < 1)
				keep_adding = false; //showed a page
			
		} while(keep_adding);		
	},
	
	addImage: function(image, prepend) {
		if(this.el_container.find('.no-pictures').length != 0) this.el_container.html('');
		
		var url = this.detail_url.replace('IMAGEID', image.image_id);		
		var html_image = '<div class="image image-'+image.image_id+'">';
		
		if (use_gallery_v3) {
			html_image += '<a href="javascript:void(0);" onclick="Enjin_Gallery_Popup_Detail.getInstance('+this.preset_id+').detailShow('+image.image_id+')">';
		} else {		
			if (this.mode_small)				
				html_image += '<a href="javascript:void(0);" onclick="Enjin_Gallery.getInstance('+this.preset_id+').openImagePopup('+image.image_id+')">';
			else
				html_image += '<a href="'+url+'">';
		}
			
		html_image += '<img src="'+image.url+'" style="width: '+this.thumb_scale_width+'px; " /></a></div>';
		if (prepend) {
			//new image
			this.images.unshift(image);
			this.el_container.prepend(html_image);
		} else {
			this.el_container.append(html_image);
		}
		
		this.images_processed[image.image_id] = this.el_container.find('.image-'+image.image_id);
		this.images_processed_index++;
		
		if (prepend) {
			this.updatePager();
			this.updatePageLinks();
			this.updateItemsPager();			
		}
	},
	
	openImagePopup: function(image_id) {
		if (this.popup_window)
			this.popup_window.close();
		
		var url = '/popup/m/'+this.preset_id+'/detail/'+image_id;
		this.popup_window = window.open(url,'gallery_detail','location=0,status=0,scrollbars=1,resizable=0,menubar=0,height=720,width=830');
	},
	
	removeImage: function(image_id) {
		var nimages = [];
		var i;
		
		for (i=0; i<this.images.length; i++) {
			if (this.images[i].image_id != image_id) {
				nimages.push(this.images[i]);
			}
		}
		
		this.images = nimages;	
		
		//remove small images
		this.el_container.find('.image-'+image_id).remove();
		this.images_processed_index--;
		
		this.updateGrid();
		this.updatePager();
		this.updatePageLinks();
		this.updateItemsPager();
		
		if(this.images.length == 0) this.el_container.html('<div class="no-pictures">There are no images in this gallery.</div>');		
	}
}

