Epson.ProductBrowser =
{
	pagination_number: 16,
	data: [],
	url:'',
	lookup: {},
	sort: ['date','price','rating'],
	view_all: false,
	price_name_main: [],
	price_value_main: [],
	doctitle: '',
		
	setup: function() 
	{
		// set up UI

		// preload dynamic images
		var image = new Image(); image.src = Epson.Path + 'images/ani_loader_normal.gif';
		var image = new Image(); image.src = Epson.Path + 'images/tsp_white_80.png';
		var image = new Image(); image.src = Epson.Path + 'images/bak_btn_filter_disabled.png';

		// get URL
		Epson.ProductBrowser.url = jQuery( '#ProductBrowser form' )[0].className.match( /pb-url-([^\s]+)/ );
		if ( !Epson.ProductBrowser.url ) return;
		Epson.ProductBrowser.url = Epson.ProductBrowser.url[1];

		jQuery( '#ProductBrowser .browseRadios li' ).each( function()
		{
			var name = jQuery( this ).find( 'input:radio' ).attr( 'name' );
			var value = jQuery( this ).find( 'input:radio' ).attr( 'value' );
			var classname = 'pbf_' + name + ' pbfv_' + value;
			if ( jQuery( this ).find( 'input:radio' ).attr( 'checked' ) ) classname += ' active';
			var replacement = jQuery( '<li class="radio ' + classname + '"><a href="#">' + jQuery( this ).find( 'label' ).text() + '</a></li>' );

			// save filters
			if ( !Epson.ProductBrowser.Filtering.filters[ name ] ) Epson.ProductBrowser.Filtering.filters[ name ] = jQuery( this ).find( 'input:radio' ).attr( 'checked' ) ? value : '';
			else if ( jQuery( this ).find( 'input:radio' ).attr( 'checked' ) ) Epson.ProductBrowser.Filtering.filters[ name ] = name;

			// bind events
			if ( !jQuery( this ).find( 'input:radio' ).attr( 'checked' ) ) replacement.find( 'a' ).bind( 'click', Epson.ProductBrowser.Filtering.toggle );

			// swap elements
			jQuery( this ).replaceWith( replacement );
		} );

		jQuery( '#ProductBrowser .browseCheckboxes li' ).each( function()
		{
			var name = jQuery( this ).find( 'input:checkbox' ).attr( 'name' );
			var value = jQuery( this ).find( 'input:checkbox' ).attr( 'value' );
			var classname = 'pbf_' + name + ' pbfv_' + value;
			if ( jQuery( this ).find( 'input:checkbox' ).attr( 'checked' ) ) classname += 'active';
			var replacement = jQuery( '<li class="checkbox ' + classname + '"><div class="backgroundContainer"><div class="cornerImage"></div><div class="subBackgroundContainer"><a href="#">' + jQuery( this ).find( 'label' ).text() + '</a></div></div></li>' );
			
			// save filters
			if ( !Epson.ProductBrowser.Filtering.filters[ name ] ) Epson.ProductBrowser.Filtering.filters[ name ] = jQuery( this ).find( 'input:checkbox' ).attr( 'checked' ) ? value : false;

			// bind events
			replacement.find( 'a' ).bind( 'click', Epson.ProductBrowser.Filtering.toggle );

			// swap elements
			jQuery( this ).replaceWith( replacement );
		} );

		jQuery(  '#ProductBrowserFilter .jsDropdown' ).each( function()
		{
			var name = jQuery( this ).attr( 'name' );
			var value = jQuery( this ).attr( 'value' );

			// save filters
			if ( !Epson.ProductBrowser.Filtering.filters[ name ] ) Epson.ProductBrowser.Filtering.filters[ name ] = value;

			// bind events
			jQuery( this ).bind( 'change', Epson.ProductBrowser.Filtering.toggle );
		} );

		jQuery(  '#ProductBrowserFilter h4' ).each( function()
		{
			jQuery( this ).append( jQuery( '<span>&nbsp;</span>' ).bind( 'mouseover', Epson.ProductBrowser.UI.Tooltips.open ) );
		} );

		// set up sorting
		if ( jQuery( '#pbi_sort' ) ) Epson.ProductBrowser.Sorting.setup();
		
		jQuery('#pbf_price option').each(function(i){
				Epson.ProductBrowser.price_name_main.push(jQuery(this).text());
				Epson.ProductBrowser.price_value_main.push(jQuery(this).val());
				
		 });
		
		// set up pagination
		Epson.ProductBrowser.Pagination.setup();

		Epson.ProductBrowser.doctitle = document.title;
	},

	loadProductData: function()
	{
		// load in data
		if ( Epson.ProductBrowser.data ) jQuery.each( Epson.ProductBrowser.data, function( i, product ) { Epson.ProductBrowser.lookup[ product.id ] = i } );
		Epson.ProductBrowser.Pagination.pages = Math.ceil( Epson.ProductBrowser.data.length / Epson.ProductBrowser.pagination_number );
	},

	requestData: function()
	{
		// JSON AJAX request
		jQuery.ajax( { type:'GET', url:Epson.ProductBrowser.url, data:Epson.ProductBrowser.getAjaxParameters(), dataType:'json', success:Epson.ProductBrowser.loadData, error:Epson.ProductBrowser.UI.displayError } );

		// Display loading animation and disable facets
		Epson.ProductBrowser.UI.displayLoader();
	},

	loadData: function( json )
	{
		Epson.ProductBrowser.data = json.products;
		jQuery.each( json.products, function( i, product ) { Epson.ProductBrowser.lookup[ product.id ] = i } );

		// hide some of the filters if necessary
		Epson.ProductBrowser.UI.toggleFilters( json.hide_filters );

		Epson.ProductBrowser.Pagination.view_all = false;

		// update pagination
		Epson.ProductBrowser.Pagination.pages = Math.ceil( json.products.length / Epson.ProductBrowser.pagination_number );
		Epson.ProductBrowser.Pagination.page = json.page ? json.page : 0;

		// display items
		Epson.ProductBrowser.UI.renderItems();

		// render last to get number of items properly
		Epson.ProductBrowser.UI.renderPagination();
	},

	getAjaxParameters: function() 
	{
		var params = {  };

		for ( var x in Epson.ProductBrowser.Filtering.filters ) if ( Epson.ProductBrowser.Filtering.filters[x] ) params[x] = Epson.ProductBrowser.Filtering.filters[x];
		params[ Epson.ProductBrowser.Sorting.criterion.name ] = Epson.ProductBrowser.Sorting.criterion.value;
		params.page = Epson.ProductBrowser.Pagination.page;
		params.originalFilters = jQuery( '#originalFilters' ).val();
		return params;
	},

	UI:
	{
		loader_height:0,
		loader_timer:null,

		renderItems: function()
		{
			// clear product browser
			jQuery( '#ProductList ul.productItemList' ).empty();

			// write in new products to browser
			var limit = Epson.ProductBrowser.Pagination.view_all ? Epson.ProductBrowser.data.length : Epson.ProductBrowser.Pagination.page * Epson.ProductBrowser.pagination_number + Epson.ProductBrowser.pagination_number;

			for ( var x = Epson.ProductBrowser.Pagination.page * Epson.ProductBrowser.pagination_number; x < limit; x++ )
			{
				if ( !Epson.ProductBrowser.data[x] ) break;

				var product = Epson.ProductBrowser.data[x];
				
				var highlights = ''; jQuery.each( product.highlights, function() { highlights += '<li>'+this+'</li>' } );

				var badge = product.badge ? '<a href="'+product.url+'"><img src="'+product.badge+'" alt="image" class="itemBadge" /></a>' : '';
				
				var ratings = '';
				for ( var y = 0; y < product.rating; y++ ) ratings += '<img src="' + Epson.Path + 'images/ico_ratingstar_on.png" alt="*" height="11" width="11" /> ';
				for ( var y = product.rating; y < 5; y++ ) ratings += '<img src="' + Epson.Path + 'images/ico_ratingstar_off.png" alt="*" height="11" width="11" />';
				
				var buynow = product.buynow ? '<div class="itemAttribute"><a class="buttonBlue" href="' + product.buynow.url + '"><span>' + product.buynow.title + '</span></a></div>' : '';
				
				var wheretobuy = product.wheretobuy ? '<div class="itemWhereToBuy txtDarkSmall"><a class="whereToBuy wtb-product_name-' + product.wheretobuy.product_name + ' wtb-country_code-' + product.wheretobuy.country_code + ' wtb-filters-' + product.wheretobuy.filters + ' wtb-data_source-' + product.wheretobuy.data_source + ' wtb-product_url-' + product.wheretobuy.product_url + ' wtb-error_url-' + product.wheretobuy.error_url + ' wtb-email_url-' + product.wheretobuy.email_url + '" id="wtb-' + product.wheretobuy.id + '" href="' + product.wheretobuy.product_url + '">' + product.wheretobuy.title + '</a></div>' : '';
				
				var outofstock = product.outofstocks ? '<div class="itemAttribute txtSmall"><p class="upper">'+Epson.Localisation.ProductBrowser.TXT008+'</p> <a class="alertNotification an-url-'+ product.outOfStockURL +'" href="#">('+Epson.Localisation.ProductBrowser.TXT011+')</a><div class="alertNotificationPopup"><div class="alertNotificationPopupContent"><p>'+Epson.Localisation.ReceiveAlert.TXT001+'</p><p class="txtError hidden">'+Epson.Localisation.ReceiveAlert.TXT002+'</p><p><input type="text" name="" class="ft_text"/><a class="buttonBlue"><span>'+Epson.Localisation.ReceiveAlert.TXT003+'</span></a></p></div></div></div>' : '';
				
				var quicklinks = ''; jQuery.each( product.quicklinks, function() { quicklinks += '<div class="itemAttribute txtDarkSmall"><a href="'+ this.url+ '">' + this.title + '</a></div>' });
				
				var reseller = product.reseller ? '<div class="itemAttribute txtDarkSmall"><a class="triggerPopup tp-url-' + product.reseller.url + '" href="'+ product.reseller.url+ '">' + product.reseller.title + '</a></div>' : '';
				
				if ((product.consumable).toString() == 'false')
				{
					var comparison_button = ( Epson.ProductComparison.data && Epson.ProductComparison.data[ product.id ] ) ? '<div class="itemComparison remove"><a href="#" class="buttonSmall disabled"><span>'+Epson.Localisation.ProductBrowser.TXT005+'</span></a></div>' : '<div class="itemComparison"><a href="#" class="buttonSmall"><span>'+Epson.Localisation.ProductComparison.TXT002+'</span></a></div>';
				}
				else
				{
					var comparison_button = '';
				}
				
				var rrp = product.showPrice && product.price.rrp ? '<div>' + Epson.Localisation.ProductBrowser.TXT010 + ': <strong>' + Epson.Localisation.Currency.TXT001 + product.price.rrp+'</strong></div>' : '';

				var nowprice = product.showPrice && product.price.now < product.price.rrp ? '<div class="txtInfo">'+Epson.Localisation.ProductBrowser.TXT009+': <strong>'+ Epson.Localisation.Currency.TXT001 + product.price.now : '';

				var item = jQuery( '<li id="pbi_'+product.id+'" class="productItem"><div class="itemImage loader">'+badge+'<a href="'+product.url+'"></a></div><div class="itemDetails"><div class="productTitle"><a href="'+product.url+'">'+product.name+'</a></div><div class="itemDescription">'+product.headline+'</div><ul clss="highlights">'+highlights+'</ul><div class="itemPrice">'+rrp+ nowprice + '</strong></div>' + quicklinks + buynow + outofstock + wheretobuy + reseller + comparison_button + '</li>' );
				
				item.find( '.itemImage a:last' ).append( jQuery( '<img src="'+product.image+'" class="itemProductImage" />' ).bind( 'load', function() { if ( this ) { jQuery( this ).parents( '.itemImage' ).removeClass( 'loader' ); if ( !jQuery.browser.msie ) jQuery( this ).parents( '.itemImage' ).css( { opacity:0 } ).animate( { opacity:1 }, 300, 'swing' ) } } ) );
				if ( Epson.ProductComparison.data && Epson.ProductComparison.data[ product.id ] ) item.find( '.remove a' ).bind( 'click', Epson.ProductComparison.remove );
				else item.find( '.itemComparison a' ).bind( 'click', Epson.ProductComparison.add );

				jQuery( '#ProductList ul.productItemList' ).append( item );
				jQuery( '.triggerPopup' ).each( function() { jQuery( this ).bind( 'click', Epson.UI.Popup.loadGeneric ) } );				
			}

			jQuery( '.alertNotification' ).bind('click', function() { Epson.ProductAlerts( jQuery( this ) ) } );

			// set up where to buy popups
			jQuery( '#ProductList .whereToBuy' ).each( function() { new Epson.WhereToBuy( this ) } );

			// hide loader
			Epson.ProductBrowser.UI.removeLoader();
		},

		renderPagination: function()
		{
			// remove pagination
			jQuery( '#ProductBrowserPagination li[class!="next"][class!="previous"][class!="all"]' ).each( function() { jQuery( this ).remove() } );

			// display new pagination
			for ( var x = 0; x < Epson.ProductBrowser.Pagination.pages; x++ )
			{
				var page = x != Epson.ProductBrowser.Pagination.page ? jQuery( '<li><a href="#filterBbar">' + ( x + 1 ) + '</a></li>' ) : jQuery( '<li class="current"><strong>' + ( x + 1 ) + '</strong></li>' );
				if ( page.find( 'a' ).length ) page.find( 'a' ).bind( 'click', Epson.ProductBrowser.Pagination.paginate );
				jQuery( page ).insertBefore( '#ProductBrowserPagination ul li:nth-child(' + ( jQuery( '#ProductBrowserPagination ul li' ).length - 1 ) + ')' );
			}

			// reset show all button
			jQuery( '#ProductBrowserPagination li[class!="all"]' ).css( { display:'' } );
			jQuery( '#ProductBrowserPagination li[class="all"] a' ).removeClass( 'buttonMinus' ).addClass( 'buttonPlus' ).find( 'span' ).text( Epson.Localisation.ProductBrowser.TXT002 );

			// update pagination info
			Epson.ProductBrowser.Pagination.view_all = false;
			jQuery( '#ProductBrowser .sortContainer h4' ).text( Epson.Localisation.ProductBrowser.TXT003 + ' ' + ( ( Epson.ProductBrowser.Pagination.page * Epson.ProductBrowser.pagination_number ) + 1 ) + ' - ' + ( ( ( Epson.ProductBrowser.Pagination.page * Epson.ProductBrowser.pagination_number ) ) + jQuery( '#ProductList li.productItem' ).length ) + ' ' + Epson.Localisation.ProductBrowser.TXT006 + ' ' + Epson.ProductBrowser.data.length + ' ' + Epson.Localisation.ProductBrowser.TXT004 );
		},
		
		toggleFilters: function( filters )
		{
			// reveal all filters
			jQuery( '#ProductBrowserFilter li.disabled' ).each( function()
			{
				jQuery( this ).removeClass( 'disabled' );
				jQuery( this ).find( 'a' ).unbind( 'click', Epson.ProductBrowser.Filtering.doNothing ).bind( 'click', Epson.ProductBrowser.Filtering.toggle );
			} );

			if ( filters )
			{
				
				jQuery.each( filters, function ( i, filter )
				{
					var filter_bar_item = [];

					if ( filter.match( '-' ) )
					{
						jQuery( '#ProductBrowserFilter .pbf_' + filter.match( /^([^\-]+)/ )[1] ).each( function()
						{
							if ( this.className.indexOf( 'pbfv_' + filter.match( /-(.+)$/ )[1] ) != -1 ) filter_bar_item = jQuery( this );
						} );
					}
					else filter_bar_item = jQuery( '#ProductBrowserFilter .pbf_' + filter );

					if ( filter_bar_item.length )
					{
						filter_bar_item.addClass( 'disabled' );
						filter_bar_item.find( 'a' ).unbind( 'click', Epson.ProductBrowser.Filtering.toggle ).bind( 'click', Epson.ProductBrowser.Filtering.doNothing );
					}
				} );
				
				if (jQuery('#pbf_price').length)
				{
					price_filter = [];
					price_name = [];
					price_value = [];
					html_string = '';
					filter_param = '';
					selected_index = '';
					
					//push filter values for price dropdown
					jQuery.each(filters, function(){
						if (this.match( /price-([^\s]+)/ ))
						{
							price_filter.push(this.match( /price-([^\s]+)/ )[1]);
						}		
					});
					
					//create filtered out arrays
					jQuery.each(Epson.ProductBrowser.price_value_main, function(i)
					{
						var param = Epson.ProductBrowser.price_value_main[i];
						
						if ( jQuery.inArray(param, price_filter)== -1 )
						{
							price_value.push(Epson.ProductBrowser.price_value_main[i]);
							price_name.push(Epson.ProductBrowser.price_name_main[i]);
						}
					});

					//create new dropdown
					jQuery( '#pbf_price' ).replaceWith( '<select name="price" id="pbf_price"></select>' );
					
					//populate options
					jQuery.each(price_value, function(i){
						selected_index = jQuery('#pbf_price').next().val();
						var attr = (this == selected_index) ? 'selected="selected"' : '';
						html_string += '<option value="' + price_value[i] + '" ' + attr + '>' + price_name[i] + '</option>';				
					});
		
					var item = jQuery(html_string);
					jQuery( '#pbf_price' ).addClass( 'jsDropdown' ).append( item );

					//add jsDropdown 
					jQuery( '#pbf_price' ).each( function() { var dropdown = this; setTimeout( function() { new Epson.GenericDropdown( dropdown ) }, 1 ) } );
					jQuery( '#pbf_price' ).bind( 'change', Epson.ProductBrowser.Filtering.toggle );
				}
			}
		},

		displayLoader: function()
		{
			// disable facets
			jQuery( '#ProductBrowserFilter li.radio:not(.disabled), #ProductBrowserFilter li.checkbox:not(.disabled)' ).addClass( 'load disabled' ).find( 'a' ).unbind( 'click', Epson.ProductBrowser.Filtering.toggle ).bind( 'click', Epson.ProductBrowser.Filtering.doNothing );
			jQuery( '#ProductBrowserFilter .jsDropdown:not(.disabled)' ).addClass( 'load disabled' ).attr( 'disabled', 'disabled' );

			// disable product area
			jQuery( '#ProductList' ).append( '<div class="loaderPanelBackground"></div>' );
			jQuery( '#ProductBrowser .compareAndSort' ).append( '<div class="loaderPanelBackground"></div>' );
			jQuery( '#ProductBrowserPagination' ).append( '<div class="loaderPanelBackground"></div>' );
			
			Epson.ProductBrowser.UI.loader_timer = setTimeout( Epson.ProductBrowser.UI.animateLoader, 500 );
		},

		animateLoader: function()
		{
			// display loader
			jQuery( '#ProductBrowser .compareAndSort' ).append( '<div class="loaderPanel genericRoundedCorners"><div class="content"><h4>' + Epson.Localisation.ProductBrowser.TXT007 + '</h4></div><div class="bottom"><div class="bl"></div><div class="br"></div><div class="b"></div></div></div>' );

			// animate loader popping out
			Epson.ProductBrowser.UI.loader_height = jQuery( '#ProductBrowser .loaderPanel' ).height();
			jQuery( '#ProductBrowser .loaderPanel' ).css( { height:0, scrollTop:Epson.ProductBrowser.UI.loader_height } ).animate( { height:Epson.ProductBrowser.UI.loader_height, scrollTop:0 }, 300, 'swing' );
		},

		removeLoader: function()
		{
			clearTimeout( Epson.ProductBrowser.UI.loader_timer );

			// remove load flag from load disabled facets
			jQuery( '#ProductBrowserFilter li.load' ).removeClass( 'load' );
			jQuery( '#ProductBrowserFilter .jsDropdown.load.disabled' ).remove( 'load' ).removeClass( 'disabled' ).attr( 'disabled', false );

			// hide loader
			jQuery( '#ProductBrowser .loaderPanel' ).animate( { height:0, scrollTop:Epson.ProductBrowser.UI.loader_height }, 300, 'swing', function() { jQuery( this ).remove() } );
			jQuery( '#ProductBrowser .loaderPanelBackground' ).remove();
		},

		displayError: function()
		{
			// re-enable filters
			jQuery( '#ProductBrowserFilter li.load.disabled' ).each( function() { jQuery( this ).removeClass( 'disabled' ).find( 'a' ).unbind( 'click', Epson.ProductBrowser.Filtering.doNothing ).bind( 'click', Epson.ProductBrowser.Filtering.toggle ); } );

			// hide loader
			Epson.ProductBrowser.UI.removeLoader();
		},

		Tooltips:
		{
			open: function(e)
			{
				// position
				var offset = jQuery( this ).offset( { border:true, padding:true } );
				var top = offset.top;
				var left = offset.left - 30;

				Epson.ToolTip.open( top, left, jQuery( this ).parent().siblings( 'p' ).text() );

				jQuery( this ).bind( 'mouseout', Epson.ToolTip.close );
			}
		}
	},

	Sorting:
	{
		criterion: {},

		setup: function()
		{
			// save initial sort criterion
			Epson.ProductBrowser.Sorting.criterion.name = jQuery( '#pbi_sort' ).attr( 'name' );
			Epson.ProductBrowser.Sorting.criterion.value = jQuery( '#pbi_sort option[selected]' ).attr( 'value' );

			// bind event to dropdown
			jQuery( '#pbi_sort' ).bind( 'change', Epson.ProductBrowser.Sorting.sort );
		},

		sort: function( value )
		{
			Epson.ProductBrowser.Sorting.criterion.value = jQuery( '#pbi_sort' ).data( 'value' );//jQuery( '#pbi_sort option[selected]' ).attr( 'value' );

			Epson.ProductBrowser.Pagination.page = 0;
			Epson.ProductBrowser.requestData();
		}
	},
	
	Filtering:
	{
		filters: {},

		toggle: function(e)
		{
			if ( !e.target.className.match( /jsDropdown/ ) )
			{
				var parent = this.parentNode; while( parent.nodeName != 'LI' ) parent = parent.parentNode;
				var radio_active = jQuery( parent ).hasClass( 'radio' ) && jQuery( parent ).hasClass( 'active' );

				if ( jQuery( parent ).hasClass( 'radio' ) && !radio_active )
				{
					var filter = parent.className.match( /pbf_([^\s]+)/ )[1];
					var value = parent.className.match( /pbfv_([^\s]+)/ )[1];
					
					// remove from filter list
					Epson.ProductBrowser.Filtering.filters[ filter ] = value;
	
					// remove active class and unbind click event
					jQuery( parent.parentNode ).find( 'li.active' ).removeClass( 'active' ).find( 'a' ).unbind( 'click', Epson.ProductBrowser.Filtering.doNothing ).bind( 'click', Epson.ProductBrowser.Filtering.toggle );
	
					// set active class and bind click event
					jQuery( parent ).addClass( 'active' ).find( 'a' ).unbind( 'click', Epson.ProductBrowser.Filtering.toggle ).bind( 'click', Epson.ProductBrowser.Filtering.doNothing );
				}
				else if ( jQuery( parent ).hasClass( 'checkbox' ) )
				{
					var filter = parent.className.match( /pbf_([^\s]+)/ )[1];
					var value = parent.className.match( /pbfv_([^\s]+)/ )[1];
	
					// toggle visual checkbox
					jQuery( parent ).toggleClass( 'active' );
	
					// remove from filter list
					Epson.ProductBrowser.Filtering.filters[ filter ] = jQuery( parent ).hasClass( 'active' ) ? value : false;
				}

				
			}
			else
			{
				var filter = jQuery( e.target ).data( 'name' );
				var value = jQuery( e.target ).data( 'value' );
				Epson.ProductBrowser.Filtering.filters[ filter ] = value;
			}
			
			// set doc title
			var temp = [];
			jQuery('li.active').each(function() { temp.push(jQuery(this).text())});
			temp.pop();
			document.title = 'Epson - ' + temp.join('|') + ' - ' + Epson.ProductBrowser.doctitle.substr(6);

			// fetch filtered data from server
			if ( !radio_active )
			{
				Epson.ProductBrowser.Pagination.page = 0;
				Epson.ProductBrowser.requestData();
			}

			e.preventDefault();
		},

		doNothing: function(e)
		{
			e.preventDefault();
		}
	},

	Pagination:
	{
		pages: 1,
		page: 0,

		setup: function()
		{
			// bind events to all pagination elements inside the pagination strip
			jQuery( '#ProductBrowserPagination ul li a' ).bind( 'click', Epson.ProductBrowser.Pagination.paginate );

			jQuery( 'li.previous a' ).css('cursor','default').bind( 'click', function(){ return false })
			jQuery( 'li.next a' ).css('cursor','pointer');
		},

		paginate: function(e)
		{
			var page = jQuery( this ).parent('li').attr('class');

//			Epson.ProductBrowser.Pagination.view_all = false;

			if ( page.match( /next/ ) ) Epson.ProductBrowser.Pagination.page = Epson.ProductBrowser.Pagination.page < Epson.ProductBrowser.Pagination.pages - 1 ? Epson.ProductBrowser.Pagination.page + 1 : Epson.ProductBrowser.Pagination.pages - 1;
			else if ( page.match( /previous/ ) ) Epson.ProductBrowser.Pagination.page = Epson.ProductBrowser.Pagination.page > 0 ? Epson.ProductBrowser.Pagination.page - 1 : 0;
			else if ( page.match( /all/i ) ) { Epson.ProductBrowser.Pagination.page = 0; Epson.ProductBrowser.Pagination.view_all = Epson.ProductBrowser.Pagination.view_all ? false : true  }
			else Epson.ProductBrowser.Pagination.page = jQuery( this ).text() - 1;

			if ( !Epson.ProductBrowser.data.length ) return Epson.ProductBrowser.requestData();

			// display items
			Epson.ProductBrowser.UI.renderItems();
			
			var lowerLimit = Epson.ProductBrowser.Pagination.page * Epson.ProductBrowser.pagination_number;
			var upperLimit = lowerLimit + Epson.ProductBrowser.pagination_number > Epson.ProductBrowser.data.length ? Epson.ProductBrowser.data.length : lowerLimit + Epson.ProductBrowser.pagination_number;

			if (Epson.ProductBrowser.Pagination.view_all) 
			{
				lowerLimit = 0;
				upperLimit = Epson.ProductBrowser.data.length;
			}

			// update showing products info
			jQuery( '#ProductBrowser .sortContainer h4' ).text( Epson.Localisation.ProductBrowser.TXT003 + ' '  + ( ( lowerLimit ) + 1 ) + ' - ' + upperLimit + ' ' + Epson.Localisation.ProductBrowser.TXT006 + ' ' + Epson.ProductBrowser.data.length + ' ' + Epson.Localisation.ProductBrowser.TXT004);

			// update pagination bar
			if ( !Epson.ProductBrowser.Pagination.view_all )
			{
				jQuery( '#ProductBrowserPagination li.current strong' ).replaceWith( jQuery( '<a href="#filterBbar">' + jQuery( '#ProductBrowserPagination li.current strong' ).text() + '</a>' ).bind( 'click', Epson.ProductBrowser.Pagination.paginate ) );
				jQuery( '#ProductBrowserPagination li.current' ).removeClass( 'current' );
				jQuery( jQuery( '#ProductBrowserPagination li' )[Epson.ProductBrowser.Pagination.page + 1] ).addClass( 'current' );
				jQuery( jQuery( '#ProductBrowserPagination li' )[Epson.ProductBrowser.Pagination.page + 1] ).find( 'a' ).replaceWith( jQuery( '<strong>' + jQuery( jQuery( '#ProductBrowserPagination li' )[Epson.ProductBrowser.Pagination.page + 1] ).find( 'a' ).text() + '</strong>' ) );

				// reset show all button
				jQuery( '#ProductBrowserPagination li[class!="all"]' ).css( { display:'' } );
				jQuery( '#ProductBrowserPagination li[class="all"] a' ).removeClass( 'buttonMinus' ).addClass( 'buttonPlus' ).find( 'span' ).text( Epson.Localisation.ProductBrowser.TXT002);

				if ( Epson.ProductBrowser.Pagination.page == 0 ) {
					jQuery( 'li.previous a' ).css('cursor','default').bind( 'click', function(){  return false } );
					jQuery( 'li.next a' ).css('cursor','pointer');
				}
				else if (Epson.ProductBrowser.Pagination.page == Epson.ProductBrowser.Pagination.pages - 1 )
				{
					jQuery( 'li.next a' ).css('cursor','default').bind( 'click', function(){ return false })
					jQuery( 'li.previous a' ).css('cursor','pointer');
				}
				else 
				{
					jQuery('li.previous a').css('cursor','pointer')
					jQuery( 'li.next a' ).css('cursor','pointer');
				}						
			}
			else
			{
				// hide all pagination except for show 16 button
				jQuery( '#ProductBrowserPagination li[class!="all"]' ).css( { display:'none' } );
				jQuery( '#ProductBrowserPagination li[class="all"] a' ).removeClass( 'buttonPlus' ).addClass( 'buttonMinus' ).find( 'span' ).text( Epson.Localisation.ProductBrowser.TXT002 );
			}
		}
	}
}


