/*
 * IO Pagination Module
 */

(function($){
	$.fn.IOPagination = function(options){

		//Attibutes
			var defaults = {
				amountItemsVisible: 10,
				realAmountVisible: 10,
				itemList: new Array(),
				itemListLength: 0,
				infoContainer: ".info",
				infoText: "",
				numberOfPages: 1,
				currentPage: 1,
				paginationHolder: "",
				prevButtonClassName: "prev",
				nextButtonClassName: "next",
				pageLinkContainer: ".pages .links",
				categoryContainer: ".category",
				showBottomPager: false,
				inCmsBackend: false,
				disabled: false
			};

			options = $.extend(defaults, options);

		return this.each(function(){

			obj = $(this);
			
			if(!options.disabled){
				obj.find(options.categoryContainer).parent().hide();
				if(options.inCmsBackend){
					if(typeof manipulateCmsElements == 'function'){
						manipulateCmsElements({action:"hideAll", paginationObject: obj});
					}
				}
				options.itemList = getElementsByType();
				options.itemListLength = options.itemList.length;
				getPagination();
				displayCurrentPageElements();
			}

			if(!options.showBottomPager){
				obj.find("."+options.paginationHolder).eq(1).hide()
			}

			//Function for filling the car list
			function getElementsByType(){
				var elements = new Array();
				var category = obj.find("select option:selected").attr("value");
					category = ","+category+",";
					category = category.toString().toLowerCase();


				//For each content(table) entry
				obj.find(options.categoryContainer).each(function(i){
						//Add the entry's "id" to the array
						elements.push(i);
				});
				options.currentPage = 1;
				return elements
			}

			//fucntion for displaying the pagination
			function getPagination(){
				options.itemListLength = options.itemList.length;

				//Build the pagination
				buildPagination();

				//Set real amount of visible items
				if(options.itemListLength <= options.amountItemsVisible){
					options.realAmountVisible = options.itemListLength;
				}else{
					var currentAmountDisplayed = obj.find(options.categoryContainer).parent(":visible").length;
					if((currentAmountDisplayed < options.amountItemsVisible)&& (options.currentPage == options.numberOfPages)){
						options.realAmountVisible = currentAmountDisplayed;
					}else{
						options.realAmountVisible = options.amountItemsVisible;
					}
				}

				//Build the "x von y" message and fill it into the respective element
				obj.find(options.infoContainer).text(options.infoText.replace("x", options.realAmountVisible).replace("y", options.itemListLength));

				//Highlight the current pages number in the pagination
				markCurrentPage();

			}

			//function for building the pagination
			function buildPagination(){

				//calculate the number of pages
				options.numberOfPages = Math.ceil(options.itemListLength / options.amountItemsVisible);

					obj.find("."+options.prevButtonClassName).each(function(i){
						//Check if the current page number is one or less
						if(options.currentPage <= 1){
							//hide the link
							$(this).find("a").hide();
						}else{
							//Check if the link is invisible
							if(!$(this).find("a").is(":visible")){
								//show the link
								$(this).find("a").show();
							}
						}
					});

					obj.find("."+options.nextButtonClassName).each(function(i){
						//Check if the current page number is greater or equal the amount of pages
						if(options.currentPage >= options.numberOfPages){
								$(this).find("a").hide();
						}else{
								//Check if the link is invisible
								if(!$(this).find("a").is(":visible")){
									//show the link
									$(this).find("a").show();
								}
						}
					});


				//Check if the amount of pages is greater than one
				if(options.numberOfPages > 1){
					//var pageLinkContainerWidth = 0;
					//Check if the page link container is empty
					if(obj.find(options.pageLinkContainer).children().length != options.numberOfPages){
						if(!obj.find(options.pageLinkContainer).is(":empty")){
							obj.find(options.pageLinkContainer).empty();
						}

						for(var i = 0; i < options.numberOfPages; i++){
							if(i == (options.numberOfPages-1)) {
								obj.find(options.pageLinkContainer).append("<a href=\"#p"+(i + 1)+"\">"+(i + 1)+"</a>");	
							} else {
								obj.find(options.pageLinkContainer).append("<a href=\"#p"+(i + 1)+"\">"+(i + 1)+"</a>...");
							}
						}

						if(options.showBottomPager){
							obj.find(options.pageLinkContainer).eq(1).innerHTML = obj.find(options.pageLinkContainer).eq(0).innerHTML;
						}

						var theWidth = 0;
						var j = 0;

						obj.find(options.pageLinkContainer).eq(0).find("a").each(function(k,v){
							theWidth = theWidth + parseInt($(v).width());
							theWidth = theWidth + parseInt($(v).css("padding-left").replace("px", ""));
							j++;
						});

						obj.find(options.pageLinkContainer).css("width", theWidth+"px");
						if($.browser.msie && $.browser.version == "7.0"){
							obj.find(options.pageLinkContainer).parent().css("width", theWidth+"px");
						}
						
					}
					
				}else{
					//Hide all the page links
					obj.find(options.pageLinkContainer).empty();
				}

			}



			//Function for marking the current page number
			function markCurrentPage(){
				obj.find(options.pageLinkContainer+" a").removeClass("current");
				obj.find(options.pageLinkContainer+" a").eq(options.currentPage - 1).addClass("current");
				if(options.showBottomPager){
					obj.find(options.pageLinkContainer).eq(1).find("a").removeClass("current");
					obj.find(options.pageLinkContainer).eq(1).find("a").eq(options.currentPage - 1).addClass("current");
				}
			}
			
			//Function for showing the Elements for the current page (number)
			function displayCurrentPageElements(){
				var startImg = options.realAmountVisible * (options.currentPage - 1)
				var endImg = startImg + options.realAmountVisible;

				//Hide all entries
				obj.find(options.categoryContainer).parent().hide();
				if(options.inCmsBackend){
					if(typeof manipulateCmsElements == 'function'){
						manipulateCmsElements({action:"hideAll", paginationObject: obj});
					}
				}


				for(var e = startImg; e < endImg; e++){
					var toggledElement  = obj.find(options.categoryContainer).eq(options.itemList[e]).parent().show();
					
					if(options.inCmsBackend){
						if(typeof manipulateCmsElements == 'function'){
							manipulateCmsElements({action:"showSingle", paginationObject: obj, toggledElementClass: toggledElement.attr("class"), toggledItemId: options.itemList[e]});
						}
					}
				}

			}
			
			obj.find("select").change(function(){
				obj.find(options.pageLinkContainer).css("margin-left", "0px");
				options.realAmountVisible = options.amountItemsVisible;
				options.itemList = getElementsByType();
				options.currentPage = 1;
				displayCurrentPageElements();
				getPagination();
			});

			obj.find("."+options.prevButtonClassName+" a").live('click',function(){
				if(options.currentPage > 1){
					options.currentPage = options.currentPage - 1;
				}
				getPagination();
				displayCurrentPageElements();
				return false;
			});

			obj.find("."+options.nextButtonClassName+" a").live('click',function(){
				if(options.currentPage < options.numberOfPages){
					options.currentPage = options.currentPage + 1;
				}
				displayCurrentPageElements();
				getPagination();
				return false;
			});

			obj.find(options.pageLinkContainer+" a").live('click',function(){
				var previousPage = options.currentPage;
				options.currentPage = parseInt($(this).text());
				getPagination();
				displayCurrentPageElements();
				return false;
			});
				
		});
	};
})(jQuery);