/**
* pagination.js
*
* @author
* KKIM_( at )_aeg_ world_ wide_( dot )_com
* 11/04/2010
*
* @internal
* this file activate pagination for any modules that requires it if the html file have the right class names and params
*
*/
var pagination = ( function ( )
{
	var object = this;

	object.paginate = function( params )
	{
		var defaults = { "container":null, "selector":null, "numItems":0, "numItemsDisp":null, "numButtonsDisp":null, "initialPage":0 };
		
		var param = checkParams( params, defaults );
		
		// creates pagination controls and pagination displays
		$( param.container ).pagination
		( 
			params.numItems,
			{
				"items_per_page":param.numItemsDisp,
				"num_display_entries":param.numButtonsDisp,
				"current_page":param.initialPage,
				"link_to":"javascript:;",
				"next_text":'&nbsp;',
				"prev_text":'&nbsp;',
				"callback":function( page_index, page_container )
				{
					// varibales that state which items to start displaying
					var show_start = page_index * param.numItemsDisp;

					// varibales that state which items to stop displaying at
					var hide_start = param.initialPage * param.numItemsDisp;

					// loop that will unhide and add animation effects to the displayable items
					for( var x = hide_start; x < ( hide_start + param.numItemsDisp ); x++ )
					{
						// hides currently displayed items and shows requested items
						if( $( param.selector ).eq( x ).length )
						{
							$( param.selector ).eq( x ).hide( 'blind', '', 'fast', function( )
							{
								$( param.selector ).eq( show_start ).show( 'blind', '', 'fast' );
								show_start++;
							} )
						} // end if
					
						// shows the items that are requested but runs even if the page did not have required rows length
						else
						{
							$( param.selector ).eq( show_start ).show( 'blind', '', 'fast' );
							show_start++;
						} // end else
					}
				
					// sets new current page
					param.initialPage = page_index;

					// display message variables
					var itemMax = ( $( param.selector ).children( ).length != 0 ) ? $( param.selector ).children( ).length : $( param.selector ).length;
					var itemBeg = ( param.initialPage + 1 ) * param.numItemsDisp * $( param.selector ).eq( 0 ).children( ).length - param.numItemsDisp * $( param.selector ).eq( 0 ).children( ).length +1;
					var itemEnd = ( param.initialPage + 1 ) * ( param.numItemsDisp * $( param.selector ).eq( 0 ).children( ).length );
				
					// checks if the last item is less than predicted last item for display message
					itemEnd = ( itemEnd < itemMax ) ? itemEnd : itemMax;

					if( ( $( '.pagination' ).children( ).length - 2 ) > 1 )
					{
						// prepends message that tells how many items are showing out of total items in the list
						$( '.pagination' ).prepend( '<span id="paginate_showing"> ' + itemBeg + ' - ' + itemEnd + ' of ' + itemMax + '</span>' );
					}
					else{ $( '.pagination' ).html( '<span id="paginate_showing"> ' + itemBeg + ' - ' + itemEnd + ' of ' + itemMax + '</span>' ); }
				} // end callback
			} // end parameters
		); // end pagination
	}; // end paginate

	return function( ){ return object; }
} ) ( ); // end pagination

$( function( )
{
	// creates the pagination tools object
	var object = new pagination;

	// parses parameters that are set in the html id field	
	var params = jQuery.parseJSON( $( '.paginateControl' ).attr( 'id' ) );

	// default variables if not in the id parameters field
	var defaults = 
	{
		"currentPage":0,
		"items_per_page":3,
		"num_of_buttons":5 
	}

	// checks for valid params
	var param = checkParams( params, defaults );

	// removes params id field withing paginateControl class div so that it is not apparent for people that views source
	$( '.paginateControl' ).removeAttr( 'id' );


	/** directs the pagination system to highlight and go to the page that the current detail page is at **/
	
	// gets uri page as the index for which list item to highlight and what page of the pagination it is under
	var detailPage = ( location.href.substring( ( location.href.lastIndexOf( '/' ) ) + 1 ) );
	
	// jquery selector
	var selector = '#' + detailPage;
	
	// if the selector matches a list item highlight the item and set pagination page to where the item is at
	if( $( selector ).children( ).length )
	{
		// calculates the start page
		var startPage = $( selector ).index( );
		startPage = Math.floor( startPage / param.items_per_page );
		
		// sets the start page
		param.currentPage = startPage;

		// removes hover effects created by global js or innate
		$( selector ).children( ).eq( 0 ).unbind( 'mouseenter mouseleave' );
		
		// highlights item
		$( selector ).find( 'span' ).eq( 0 ).css
		( 
			{
				"background-image":"url(/media/images/bg_white50.png)",
				"color":"black"
			} 
		); // end highlight item
	} // end selector match
	/** end pagination system highlight **/
	
	
	// gets the amount of items that are going to be displayed through pagination
	var itemCount = $( '.paginate_item' ).length;

	// paginates the list
	object.paginate( 
	{ 
		"container":".paginateControl", 
		"selector":".paginate_item", 
		"numItems":itemCount, 
		"numItemsDisp":param.items_per_page, 
		"numButtonsDisp":param.num_of_buttons, 
		"initialPage":param.currentPage 
	} ); // end paginate

	// if filter option select is present, load the sorting js and filter when needed
	if( $( '.filterOptions' ).length )
	{
		// loads the sorting script and runs a callback to filter when done
		generic.loadModule( '/lib/application/sorting.js', function(  )
		{
			// creates a sorting object
			var objectSort = new sorting;
			
			// gets current url
			var url = location.pathname.substring( 1 );
			var url = url.split( '/' );
	
			// grabs parts of the url
			var page = url.pop( );
			var section = url.reverse( ).pop( );
			var subSection = url.join( '/' );
			
			// outer template
			var listTemplate = '<div class="listRow paginate_item"><!--||LISTING||--></div>';
			
			// inner template
			var listItemTemplate = '';
			
			// gets the list of venues/music/ whatever list items that is needed to display
			var itemList = objectSort.getFileContents( '/data/json/' + section + '.listing.json' );

			// checks if the file exists
			if( !empty( itemList ) )
			{
				if($.parseJSON( itemList ) != null) {
					itemList = $.parseJSON( itemList );
				}
				
				// gets the inner template
				listItemTemplate = objectSort.getFileContents( '/content/templates/section.listing.item.template.html' );
			} // end file exist

			// checks if all neccessary data exists, if it does filter list, display list, and paginate
			if( !empty( itemList ) && is_array( itemList ) && !empty( listItemTemplate ) )
			{
				// gets the specified list item needed for the page
				itemList = itemList[ page ];

				// gets the list of locations availavle to be filtered
				menuList = objectSort.filterMenu( { "filterList":itemList, "filterType":"region" } );

				// filters the location menu dropdown for selection
				if( menuList )
				{
					for( var x in menuList ){ $( '.filterOptions' ).find( 'option[value=' + menuList[ x ] + ']' ).removeAttr( 'disabled' );	}
				} // end filter

				// on filter select change, filters the list and displays the list with pagination
				$( '.filterOptions' ).change( function( )
				{
					var region = $( this ).attr( 'value' );
					var filteredList = objectSort.filterList( { "fullList":itemList, "filterType":"region", "filterBy":region } );
					var count = 0;
					var html = '';
					var display = '';

					// check if filter list exist
					if( !empty( filteredList ) )
					{			
						// loop through filter list to create html string for display
						for( var y in filteredList )
						{
							var link = filteredList[ y ].image;
			
							link = str_replace( [ 'jpeg', 'jpg', 'png', 'gif' ], '', link );
							link = '/' + section + '/' + page + '/' + link;
			
							html += str_replace( 
							[ '<!--||LISTING_REGION||-->', '<!--||LISTING_LINK||-->', '<!--||LISTING_IMAGE||-->', '<!--||LISTING_TITLE||-->', '<!--||LISTING_LOCATION||-->'	],
							[ filteredList[ y ].region, link, '/media/images/list/' + filteredList[ y ].image, filteredList[ y ].title, filteredList[ y ].location ],
							listItemTemplate );
				
							if( count >= 2 )
							{ 
								display += str_replace( '<!--||LISTING||-->', html, listTemplate );
								html = '';
								count = 0;
							}
							else{ count++; }
						} // end loop through filtered list

						if( count <= 3 && count >= 1 ){ display += str_replace( '<!--||LISTING||-->', html, listTemplate ); }

						// writes new list of items
						$( '#paginate_contents' ).html( display );
						
						// clears previous pagination controls
						$( '.paginateControl' ).html( '' );

						// gets new item count after write
						itemCount = $( '.paginate_item' ).length;
						
						// paginate list
						object.paginate( 
						{ 
							"container":".paginateControl", 
							"selector":".paginate_item", 
							"numItems":itemCount, 
							"numItemsDisp":param.items_per_page, 
							"numButtonsDisp":param.num_of_buttons, 
							"initialPage":param.currentPage 
						} ); //end paginate
					
					} // end display list and paginate
					
					// returns a error message if there are list items from filtering
					else{ $( '#paginate_contents' ).html( "Sorry no " + page + " exists in " + $( this ).find( 'option[value="' + region + '"]' ).html( ) ); }
				} ); // end filter select change
			} // end data exist check
		} ); // end loading of sorting script and callback
	} // end check if filter select exist
}); // end function

