/**
 * Constructor description
 * @author Tom Cool @ FrenzyMedia B.V. - http://frenzymedia.eu
 * @class Namespace object
 * @constructor
 * @param {jQuery} $ The jQuery object - to prevent conflicts with similar libraries
 */
var collection = (function ($) {

	/**
	 * Additional css-selectors are stored seperately
     * @namespace Private properties
     */
	var selectors = {

		viewoptions		: {},		
		currentview		: '',

		viewselector	: '#collection_selector',
		viewselectors	: '#collection_selector .icon',
		container		: '#collection_container',
		collectionitems : '#collection_container .collection_item',
		colectionitemimg: '.coll_item_img img',

		navigation		: 'nav.pagenavigation',
		prev			: 'nav.pagenavigation a.btnPaginationPrev',
		next			: 'nav.pagenavigation a.btnPaginationNext',
		currentpage		: 'nav.pagenavigation span.current_page',
		totalpages		: 'nav.pagenavigation span.total_pages',

		active			: 'activeview',
		hidden			: 'hidden'
	},	

	/**
     * @namespace Private properties and methods
     */
	privs = {

		/**
         * Collection items
         * @type Array
         * @private
         */
		collection : [],

		/**
         * Number of collection items
         * @type Integer
         * @private
         */
		total : 0,

		/**
         * Number of pages according to the current view
         * @type Integer
         * @private
         */
		pages : 0,

		/**
         * The current page within the pagination
         * @type Integer
         * @private
         */
		currentpage : 1,

		/**
		 * Calculates the number of pages for the current view
		 * @returns {Integer} The number of pages
		 * @private
		 */
		setPages : function () {
			return Math.ceil(privs.total / selectors.viewoptions[selectors.currentview]);
		},

		/**
		 * Sets an active-css-class on the current view-selector
		 * @private
		 */
		setActiveSelector : function () {

			//remove any active-css-class from elements within the viewselector
			$('.'+selectors.active, selectors.viewselector).removeClass(selectors.active);

			//bind active-css-class to the current viewselector item
			$('#'+selectors.currentview, selectors.viewselector).addClass(selectors.active);
		},

		/**
		 * Sets the prefix for each image, according to the current view
		 * @private
		 */
		setImagePrefix : function (item) {
			
			var imgobj = $(item).find(selectors.colectionitemimg),

				imgsrc	= imgobj.attr('src');
				img		= imgsrc.substring(imgsrc.lastIndexOf('/') + 1, imgsrc.length);
				img		= img.substring(img.indexOf('_'), img.length);

				prefix	= selectors.currentview;
				prefix	= prefix.replace('_','-');
			
			$(imgobj).attr('src','/uploads/images/' + prefix + img);
		},

		/**
         * Binds click-switch event to view-selector items
		 * @returns {Boolean} false : To overwrite html-anchor standard behaviour
         * @private
         */
		switchView : function () {

			$(selectors.viewselectors).each( function () {
				$(this).click( function () {

					//define witch view to set
					var view = this.hash.substring( 1, this.hash.length);
					if (selectors.currentview != view) {

						//set private variable : the current view
						selectors.currentview = view;

						//set an active-css-class on the current view-selector
						privs.setActiveSelector();

						//set private variable : reset the current page to the first page
						privs.currentpage = 1;

						//set private variable : the number of pages according to new view
						privs.pages = privs.setPages();

						//hide the collection container and it's content
						$(selectors.container).fadeOut(400, function () {

							//set additional pagination
							privs.setPagination();
						
							$.each(privs.collection, function (index, item) {

								//loop through any view options set
								for (var option in selectors.viewoptions) {

									//remove additional view-classes
									if ($(item).hasClass(option)) {
										$(item).removeClass(option);
									}

									//remove additional hidden-class
									if ($(item).hasClass(selectors.hidden)) {
										$(item).removeClass(selectors.hidden);
									}
								}

								//add a hidden-class to elements outside of the page-scope
								if (index >= selectors.viewoptions[view]) {
									$(item).addClass(selectors.hidden);
								}

								//add the view-class
								$(item).addClass(view);
								
								//set image prefix according to current view
								privs.setImagePrefix(this);
							});

							//show collection container with it's sorted content
							$(selectors.container).fadeIn(400);
						});
					}
					return false;
				});
			});
		},

		/**
         * Checks wether to show or hide the next/previous buttons
         * @private
         */
		setPaginationNavigation : function () {

			//empties and sets the current page
			$(selectors.currentpage).empty().append(privs.currentpage);

			//empties and sets the total of pages
			$(selectors.totalpages).empty().append(privs.pages);
			
			//when loacted on the first page, the previous-button is hidden
			if (privs.currentpage === 1) {
				$(selectors.prev).hide();
			} else {
				$(selectors.prev).show();
			}

			//when loacted on the last page, the next-button is hidden
			if (privs.currentpage == privs.pages) {
				$(selectors.next).hide();
			} else {
				$(selectors.next).show();
			}
		},

		/**
         * Binds click-switch event to pagination next-button
		 * @returns {Boolean} false : To overwrite html-anchor standard behaviour
         * @private
         */
		nextPage : function () {

			var start		= privs.currentpage * selectors.viewoptions[selectors.currentview],
				end			= (privs.currentpage + 1) * selectors.viewoptions[selectors.currentview],
				pageitems	= privs.collection.slice(start, end);

			//update current page
			privs.currentpage += 1;

			//hide all collection items
			privs.collection.addClass(selectors.hidden);

			//show page items
			pageitems.removeClass(selectors.hidden);

			//check wether to show or hide the next/previous buttons
			privs.setPaginationNavigation();

			return false;
		},

		/**
         * Binds click-switch event to pagination previous-button
		 * @returns {Boolean} false : To overwrite html-anchor standard behaviour
         * @private
         */
		previousPage : function () {

			var end			= (privs.currentpage - 1) * selectors.viewoptions[selectors.currentview],
				start		= end - selectors.viewoptions[selectors.currentview],
				pageitems	= privs.collection.slice(start, end);

			//update current page
			privs.currentpage -= 1;

			//hide all collection items
			privs.collection.addClass(selectors.hidden);

			//show page items
			pageitems.removeClass(selectors.hidden);

			//check wether to show or hide the next/previous buttons
			privs.setPaginationNavigation();
		},

		/**
         * Sets a collection pagination-bar
         * @private
         */
		setPagination : function () {
			
			//only if there are more items a page is alowed to show, a pagination is added
			if (privs.total > selectors.viewoptions[selectors.currentview]) {

				//binds click-switch event to pagination previous-button
				//click event needs to be unbound, to avoid double clicks on view-switch
				$(selectors.prev).unbind('click').click( function () { privs.previousPage(); return false; } );

				//click event needs to be unbound, to avoid double clicks on view-switch
				//binds click-switch event to pagination next-button
				$(selectors.next).unbind('click').click( function () { privs.nextPage(); return false; } );
				
				//check wether to show or hide the next/previous buttons
				privs.setPaginationNavigation();
				
				//show pagination contents
				$(selectors.navigation).removeClass(selectors.hidden);
				
			} else {
				//hide pagination contents
				$(selectors.navigation).addClass(selectors.hidden);
			}
		}
	};

	/**
	 * @namespace Public properties and methods
     * @scope collection
     */
	return {

		/**
         * collection constructor, called on body.onload() from HTML
         * @param {String} currentview The current collection view
		 * @param {String} viewoptions Optional collection view options
         */
		initiate : function (currentview, viewoptions) {

			//set private css-selector
			selectors.currentview = currentview;

			//set active-css-class on the viewselector
			privs.setActiveSelector();

			//create an onbject for storing the number of items per view
			viewoptions = viewoptions.split(',');
			var options = {};
			for (var i in viewoptions) {
				var option = viewoptions[i].split(':');
				options[option[0]] = parseInt(option[1]);
			}
			selectors.viewoptions = options;

			//set private variable : group collection items
			privs.collection = $(selectors.collectionitems);

			//set private variable : colection item total
			privs.total = privs.collection.length;

			//set private variable : the number of pages according to view
			privs.pages = privs.setPages();
			
			//bind onclick-switch event on viewselector items
			privs.switchView();

			//sets additional pagination
			privs.setPagination();
		}
	};

})(jQuery);
