/***
 * For Photo Gallery Objects -MAIN CLASS-
 * @param {Object} options
 */

var GalleryViewer = function(options) {
	this.instanceName = null;
	this.type = 'category'; // type of gallery
	this.limit = 10; // limit images per page 
	this.cat_id = null; // category id of imgs to display[optional]
	this.gall_id = null; // list of img ids[optional]
	this.plugin = 'jquery_lightbox'; // which jQuery plugin to use.
	this.thumb_height = 120;
	this.thumb_width = 120;
	this.at_page = 0;
	this.at = 0;
	this.showTitle = true;
	this.showDecription = true;
	this.sort = 'none';
	
	jQ.extend(true, this, options); // merge interal vars with options
	
	if (this.limit) this.limit = parseInt(this.limit);
	if (this.thumb_height) this.thumb_height = parseInt(this.thumb_height);
	if (this.thumb_width) this.thumb_width = parseInt(this.thumb_width);
	
	//check for string booleans from editor
	for (var i in this) {
		if (this[i] === 'true') {
			this[i] = true; 
		} else if (this[i] === 'false') {
			this[i] = false;
		} else {
			if (typeof(this[i]) == 'object') {
				for (var j in this[i]) {
					if (this[i][j] === 'true') {
						this[i][j] = true;
					} else if (this[i][j] === 'false') {
						this[i][j] = false;
					}
				}
			}
		}
	}
	
	this.container = jQ('#'+this.instanceName+'_container'); // cached JQ selector

	if (!this.gall_id && this.type == 'playlist') { // BOO! Get playlist without a list
		cb.console('GalleryViewer: no gall id for type playlist');
		this.no_list_error();
		return;
	}
	
	if(!this.category && this.type == 'category') { // BOO! Get category without a category id
		cb.console('GalleryViewer: no cat id for type category');
		this.no_list_error();
		return;		
	}
	
	this.get_markup(); // INITIATE 
	
}

// Get Markup For Object
GalleryViewer.prototype.get_markup = function(post_data) {
	var action;
	if(this.type == 'category') {
		action = 'get_gallery_cat';
	} else if(this.type == 'category_list') {
		action = 'get_gallery_cat_list';
	} else if (this.type == 'playlist') {
		action = 'get_gallery_playlist';
	}
	
	var data = { module: 'gallery', action: action, user_id: userId, gall_id: this.gall_id, category_id: this.cat_id, category_list: this.cat_list, limit: this.limit, instanceName: this.instanceName, thumb_height: this.thumb_height, thumb_width: this.thumb_width, at: this.at, at_page: this.at_page, showTitle: this.showTitle, showDescription: this.showDescription }
	if (typeof(post_data) != 'undefined') {
		jQ.extend(true, data, post_data);
	} else if (window.location.hash.match('gallery~page~')) {
		//this.at_page = parseInt(window.location.hash.split('~')[2])-1; /* didn't seem to need the -1 */
		this.at_page = parseInt(window.location.hash.split('~')[2]);
		data.at_page = this.at_page;
	} else if (window.location.hash.match('gallerychooser~')) {
		var hash = window.location.hash.split('~');
		this.cat_id = hash[1].split('~')[0];
		data.category_id = this.cat_id;
		this.at_page = hash[3];
		data.at_page = this.at_page;
	}

	if(this.sort != "none") {
		data.sort = this.sort;
	}
	
	var GV = this; /* SCOPE!! */
	ONAPI.request({
		data: data,
		callback: function(data) {
			GV.container.html(data);
			if (!jQ('#topBar').length) {
				if (GV.plugin == 'jquery_lightbox') {
					var photos = GV.container.find('.photo_list a');
					//apply full size image height/width cap
					photos.each(function() {
						var dims = this.id.split('_');
						var h = dims[1];
						var w = dims[2];
						var maxh = jQ(window).height() - 200; 
						var maxw = jQ(window).width() - 100;
						if (h > maxh || w > maxw) this.href += '?h='+maxh+'&w='+maxw; 
					});
					photos.lightBox({
						imageLoading: '/resources/js/jquery/jquery_lightbox/lightbox-ico-loading.gif',
						imageBtnClose: '/resources/js/jquery/jquery_lightbox/lightbox-btn-close.gif',
						imageBtnPrev: '/resources/js/jquery/jquery_lightbox/lightbox-btn-prev.gif',
						imageBtnNext: '/resources/js/jquery/jquery_lightbox/lightbox-btn-next.gif',
						keyToClose: 'c',
						keyToPrev: 'left',
						keyToNext: 'right'
					});
				}
			} else {
				GV.container.find('.photo_list a').click(function() { return false; });
			}
			GV.bind_page_clicks();
			GV.container.find('.photo_list').fadeIn(500);
		},
		error: function(data) {
			GV.container.html('<h1>Error Loading Gallery</h1>');
		}
	});	
}

GalleryViewer.prototype.bind_page_clicks = function() {
	var GV = this;
	this.container.find('.gallery_pages a').not('eq('+this.at_page+')').unbind('click').click(function() {
		if (!jQ('#topBar').length) {
			GV.at_page = parseInt(this.id.split('_')[1]);
			GV.at = GV.at_page * GV.limit;
			window.location.hash = '#'+this.href.split('#')[1];
			jQ('.at_page').removeClass('at_page');
			jQ(this).addClass('at_page');
			jQ('.photo_list').fadeTo(500, 0.0);
			GV.get_markup();
		}
		return false;
	});
	
	if (this.type == 'category_list') {
		this.container.find('.gallery_chooser_area > li').click(function() {
			if (!jQ('#topBar').length) {
				GV.cat_id = this.id.split('_')[1];
				window.location.hash = '#gallerychooser~'+GV.cat_id+'~page~0';
				GV.get_markup();
			}
			return false;
		});
	}
}



//Do Error Handling (Playlist without a List)
GalleryViewer.prototype.no_list_error = function() {
	this.container.html('<h1>No Photos have been chosen.</h1>');
}
