Epson.ProductBrowser =
{
	pagination_number: 16,
	data: [],
	temp_data: [], 
	url:'',
	lookup: {},
	sort: ['date','price','rating'],
	view_all: false,
	price_name_main: [],
	price_value_main: [],
	priceFilter:'',
	doctitle: '',
	current_facet:'All|radio',
	current_disabled: false,
	state:{},

	setup: function() 
	{
		var now = new Date();
		var time_stamp = now.getTime();
		
		// 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';
		var checkboxwidth = 0;
		var radiowidth = 0;

		// 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] + '&ts=' + time_stamp;

		// save document title
		Epson.ProductBrowser.doctitle = document.title;

		// restore state
		// var state = Epson.ProductBrowser.State.load();

		// set up radio filters
		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' )[0].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' )[0].checked ? value : '';
			else if ( jQuery( this ).find( 'input:radio' )[0].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 );
		} );

		// set up checkbox filters
		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' )[0].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' )[0].checked ? value : false;

			// bind events
			replacement.find( 'a' ).bind( 'click', Epson.ProductBrowser.Filtering.toggle );

			// swap elements
			jQuery( this ).replaceWith( replacement );
		} );

		// set up dropdown filters
		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 );
		} );

		// 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();

		// fix widths of sections
		jQuery( '.browseCheckboxes' ).each( function ()
		{
			checkboxwidth = 0;
			jQuery( this ).find( 'ul li.checkbox' ).each( function() { checkboxwidth += jQuery(this).width() } );
			var textwidth = jQuery( this ).find( 'h4' ).width();
			jQuery( this ).width( checkboxwidth >= textwidth ? checkboxwidth : textwidth );			
		} );

		jQuery( '.browseRadios' ).each( function()
		{
			radiowidth = 0;
			jQuery( this ).find( 'ul li.radio' ).each( function() { radiowidth += jQuery(this).width() + 5 } );
			var textwidth = jQuery( this ).find( 'h4' ).width();
			jQuery( this ).width( radiowidth >= textwidth ? radiowidth : textwidth );			
		} );		

		// populate data if previous state
		setTimeout( Epson.ProductBrowser.requestData, 100 ); // IE race condition

		Epson.ProductComparison.counter = 0;

		//jQuery('#pbf_price').width(150);
	},

	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:'POST', url:Epson.ProductBrowser.url, data:Epson.ProductBrowser.getAjaxParameters(), dataType:'json', success:Epson.ProductBrowser.loadData, error:Epson.ProductBrowser.UI.displayError } );

		// save request for state persistence
		Epson.ProductBrowser.State.update();

		// Display loading animation and disable facets
		Epson.ProductBrowser.UI.displayLoader();
	},

	loadData: function( json )
	{
		//Epson.ProductBrowser.UI.enableFilters( json.enable_filters);
		Epson.ProductBrowser.data = json.products;
		jQuery.each( json.products, function( i, product ) { Epson.ProductBrowser.lookup[ product.id ] = i } );

		Epson.ProductBrowser.current_disabled = false;

		// hide some of the filters if necessary
		Epson.ProductBrowser.UI.toggleFilters( json.hide_filters );
		Epson.ProductBrowser.UI.togglePriceFilter( json.price_range,  json.price_range_text);

		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 = {};
		Epson.ProductBrowser.State.data = {};

		for ( var x in Epson.ProductBrowser.Filtering.filters ) if ( Epson.ProductBrowser.Filtering.filters[x] ) Epson.ProductBrowser.State.data[x] = params[x] = Epson.ProductBrowser.Filtering.filters[x];
		params[ Epson.ProductBrowser.Sorting.criterion.name ] = Epson.ProductBrowser.State.data[ Epson.ProductBrowser.Sorting.criterion.name ] = Epson.ProductBrowser.Sorting.criterion.value;
		params.page = Epson.ProductBrowser.State.data.page = Epson.ProductBrowser.Pagination.page;
		params.originalFilters = jQuery( '#originalFilters' ).val();
		params.facetPropertyMap = jQuery( '#facetPropertyMap' ).val();
		if ( Epson.ProductBrowser.priceFilter == '' ) params.currentPriceFilter = jQuery( '#currentPriceFilter' ).val();
		else params.currentPriceFilter = Epson.ProductBrowser.priceFilter;
		params.current = Epson.ProductBrowser.State.data.current = Epson.ProductBrowser.current_facet;
		params.current_disabled = Epson.ProductBrowser.State.data.current_disabled = Epson.ProductBrowser.current_disabled;

		return params;
	},

	UI:
	{
		loader_height:0,
		loader_timer:null,

		renderItems: function()
		{
			// clear product browser
			jQuery( '#ProductList ul.productItemList, #ProductList ul.productItemListAutoHeight' ).remove();

			// hide loader
			Epson.ProductBrowser.UI.removeLoader();

			// 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;

			var list_row = null;
			for ( var x = Epson.ProductBrowser.Pagination.page * Epson.ProductBrowser.pagination_number; x < limit; x++ )
			{
				if ( !Epson.ProductBrowser.data[x] ) break;

				if ( x % 4 == 0 )
				{
					if ( list_row ) jQuery( '#ProductList' ).append( list_row );
					list_row = jQuery( '<ul class="productItemList productItemListAutoHeight clear"></ul>' );
				}

				var product = Epson.ProductBrowser.data[x];
				
				var highlights = ''; jQuery.each( product.highlights, function() { highlights += '<li>'+this+'</li>' } );

				var badge = product.badge ? '<img src="'+product.badge+'" class="itemBadge" />' : '';
	
				//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 ratings = '';
				//CRO:Enable/Disable of CRO feature 'ratings&reviews' for the products
				//This is condition is used to check whether to enable or disable of ratings for the products
                if(document.getElementById('ratingsreviews')!=null){
                ratings = product.rating ? '<p class="review"><img src="' + product.rating.imagePath + '" alt="' + product.rating.altText + '" />&nbsp;<span><a href="'+product.url+'#BVReviewsContainer">' + product.rating.reviewRating + '</a></span></p>':'';
                }

				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>' : '';
				
				
				if ( product.email )
				{
					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"><h4>' + Epson.Localisation.ReceiveAlert.TXT004 + '</h4><p>' + Epson.Localisation.ReceiveAlert.TXT005 + ' ' + product.email + '</p></div></div></div>' : '';
				}
				else
				{
					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="" value="' + product.email+ '" class="ft_text"/><a class="buttonBlue"><span>'+Epson.Localisation.ReceiveAlert.TXT003+'</span></a></p></div></div></div>' : '';
				}
				
				var quicklinks = '';
				if ( product.quicklinks ) jQuery.each( product.quicklinks, function() { quicklinks += '<div class="itemAttribute txtDarkSmall"><a href="'+ this.url+ '"'; if(this.newPage){quicklinks += ' target="_blank"'}; quicklinks +=' >' + 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>' : '';
				
				var comparison_button = '';
				if ((product.consumable).toString() == 'false') var comparison_button = ( product.added_to_compare ) ? '<div class="itemComparison remove"><a href="#" class="buttonSmall"><span>'+Epson.Localisation.ProductBrowser.TXT005+'</span></a></div>' : '<div class="itemComparison"><a href="#" class="buttonSmall"><span>'+Epson.Localisation.ProductComparison.TXT002+'</span></a></div>';

				// if product is added for comparison, adding the product to added product list if not already exists
				if ( product.added_to_compare && Epson.ProductComparison.counter == 0 ) {
					var prodExist;
					prodExist = false;
					for(var addedProductIndex = 0; addedProductIndex < productToAddToComparison.length; addedProductIndex++) {
						if(productToAddToComparison[addedProductIndex] == product.id) {
							prodExist = true;
							break;
						}
					}
					
					if(!prodExist)
						productToAddToComparison[productToAddToComparison.length] = product.id;
				}
				var rrp='';
				var nowprice ='';
				var textprice ='';
				//release 1.4.5:START:CRO:Below condition is used to enable/disable the Price:Vijay Bhaskar
				if(document.getElementById('displayPrice')!=null){
				rrp = product.showPrice && product.price.rrp ? '<div><span>' + Epson.Localisation.ProductBrowser.TXT010 + ':&nbsp;<strong>' + product.price.stringRRP+'</strong></span></div>' : '';

				nowprice = product.showPrice && product.price.now < product.price.rrp ? '<div class="txtInfo"><span>'+Epson.Localisation.ProductBrowser.TXT009+':&nbsp;<strong>'+ product.price.stringNOW + '</strong></span></div>' : '';
				
				textprice = '<div><span>'+product.textPrice+'</span></div>';
				}//END:CRO
				
				if (!jQuery.browser.msie) {
				
					if(product.showTextPrice) {
						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 class="highlights">'+highlights+'</ul><div class="itemPrice">'+textprice + '</strong>' + quicklinks + ratings + wheretobuy + reseller  + '</div>' + comparison_button + '</li>' );
					} else {
						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 class="highlights">'+highlights+'</ul><div class="itemPrice">'+rrp+ nowprice + '</strong>' + quicklinks + ratings + buynow + outofstock + wheretobuy + reseller  + '</div>' + comparison_button + '</li>' );						
					}
					
				} else {
				
					if(product.showTextPrice) {
						var item = jQuery( '<li id="pbi_'+product.id+'" class="productItem"><div class="itemImage">'+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 class="highlights">'+highlights+'</ul><div class="itemPrice">'+textprice + '</strong>' + quicklinks + ratings + wheretobuy + reseller  + '</div>' + comparison_button + '</li>' );
					} else {
						var item = jQuery( '<li id="pbi_'+product.id+'" class="productItem"><div class="itemImage">'+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 class="highlights">'+highlights+'</ul><div class="itemPrice">'+rrp+ nowprice + '</strong>' + quicklinks + ratings + buynow + outofstock + wheretobuy + reseller  + '</div>' + comparison_button + '</li>' );						
					}
				}
				item.find( '.itemImage a:last' ).append( jQuery( '<img src="'+product.image+'" alt="'+product.altText+'" 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 ( product.added_to_compare ) item.find( '.remove a' ).bind( 'click', Epson.ProductComparison.remove );
				else item.find( '.itemComparison a' ).bind( 'click', Epson.ProductComparison.add );

				list_row.append( item );
			}
			jQuery( '#ProductList' ).append( list_row );

			jQuery( '#ProductList .triggerPopup' ).each( function() { jQuery( this ).bind( 'click', Epson.UI.Popup.loadGeneric ) } );

			jQuery( '#ProductList .alertNotification' ).each( function() { new Epson.ProductAlerts( jQuery( this ) ) } );

			// set up where to buy popups
			jQuery( '#ProductList .whereToBuy' ).each( function() { new Epson.WhereToBuy( this ) } );
			
			// calling setup of product comparison
			if ( Epson.ProductComparison && jQuery( '#CompareItems' ).length && Epson.ProductComparison.counter == 0) {
				Epson.ProductComparison.setup(productToAddToComparison);
			}
			var compareButtons = jQuery("#ProductList").find(".itemComparison");
			if(compareButtons.length>0){
			 jQuery(".compareDropdown").show();
			 jQuery(".compareAndSortText").text(Epson.Localisation.ProductBrowser.TXT012);
			}
		},
		
		/* enableFilters : function ( filters )
		{
			if ( filters )
			{
				jQuery.each( filters, function ( i, filter )
				{
					jQuery( '#ProductBrowser .browseRadios li' ).each( function()
					{

						if ( this.className.indexOf('All') != -1)
						{
							jQuery(this).removeClass( 'active' );
						}
						if ( this.className.indexOf(filter) != -1 ) { 
							
							jQuery(this).addClass( 'active' );
							jQuery(this).find( 'a' ).unbind( 'click', Epson.ProductBrowser.Filtering.toggle ).bind( 'click', Epson.ProductBrowser.Filtering.doNothing );
						
						}

						
					} );


					jQuery( '#ProductBrowser .browseCheckboxes li' ).each( function()
					{

						if ( this.className.indexOf(filter) != -1 ) { 
							
							jQuery(this).addClass( 'active' );
							jQuery(this).find( 'a' ).unbind( 'click', Epson.ProductBrowser.Filtering.toggle ).bind( 'click', Epson.ProductBrowser.Filtering.doNothing );
						
						}

						
					} );

				});
			}
		},*/

		renderPagination: function()
		{
			// remove pagination
			jQuery( '#ProductBrowserPagination li[class!="next"][class!="previous"][class!="all"]' ).each( function() { jQuery( this ).remove() } );

			if ( Epson.ProductBrowser.Pagination.pages > 1 )
			{
				jQuery( '#ProductBrowserPagination' ).css( 'display','' )
				
				// 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 );
			}
			else jQuery( '#ProductBrowserPagination' ).css( 'display','none' );

			// 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 );
		},
		
		togglePriceFilter: function( priceRange,  priceRangeText )
		{
			if ( priceRange )
			{
				jQuery( '#priceFacetDropDown' ).css( { visibility:'visible' } );

				if ( priceRangeText )
				{
					if ( jQuery( '#pbf_price' ).length )
					{
						var price_name = [];
						var price_value = [];

						Epson.ProductBrowser.price_value_main = [];					
						jQuery.each( priceRange, function ( i, price )
						{
							price_value.push( price );
							Epson.ProductBrowser.price_value_main.push( price );
						});

						Epson.ProductBrowser.price_name_main = [];
						jQuery.each( priceRangeText, function ( i, priceText )
						{
							price_name.push( priceText );
							Epson.ProductBrowser.price_name_main.push( priceText );
						});

						var temp_price_filter	= '{';
						jQuery.each( priceRange, function ( i, price )
						{
							if ( i != 0 ) temp_price_filter = temp_price_filter + "price-" + price + "=" + price + ",";
						} );
						temp_price_filter = temp_price_filter.substring( 0, temp_price_filter.length - 1 );
						temp_price_filter = temp_price_filter + '}';
						Epson.ProductBrowser.priceFilter = temp_price_filter;

						// update dropdown
						var dropdown = jQuery( '#pbf_price' ).data( 'jDropdown' );
						var selected_index = jQuery('#pbf_price').next().val();
						var dropdown_options = dropdown.options = [];
						dropdown.selectedIndex = 0;
						jQuery.each( price_value, function(i)
						{
							dropdown_options[i] = [ price_value[i], price_name[i] ];
							if ( price_value[i] == selected_index ) dropdown.selectedIndex = i;
						} );
						dropdown.element.data( 'value', dropdown.options[dropdown.selectedIndex][0] );
						dropdown.element.val( dropdown.options[dropdown.selectedIndex][0] );
						dropdown.value = dropdown.options[dropdown.selectedIndex][0];
						//if ( selected_index != dropdown.options[dropdown.selectedIndex][0] )
						{
							dropdown.element.find( 'span' ).html( dropdown.options[dropdown.selectedIndex][1] );
							jQuery('#pbf_price').next().val( dropdown.options[dropdown.selectedIndex][0] );
						}
					}
				}
			}
		},
		
		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 )
			{
				// update checkboxes and radios
				jQuery.each( filters, function ( i, filter )
				{
					var filter_bar_item = [];

					if ( filter.match( '-' ) )
					{
						jQuery( '#ProductBrowserFilter .pbf_' + filter.match( /^([^\-]+)/ )[1] ).each( function()
						{
							var value = this.className.match( /pbfv_([^\s]+)/ )[1];
							if ( 'pbfv_' + value == ( 'pbfv_' + filter.match( /-(.+)$/ )[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 );
					}
				} );
			}
		},

		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, 2000 );
		},

		animateLoader: function()
		{
			var loader = jQuery( '#ProductBrowser .loaderPanel' );
		
			if ( !loader.length )
			{
				loader = jQuery( '<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>' );

				// display loader
				jQuery( '#ProductBrowser .compareAndSort' ).append( loader );
	
				// animate loader popping out
				Epson.ProductBrowser.UI.loader_height = loader.height();
				loader.css( { height:0, scrollTop:Epson.ProductBrowser.UI.loader_height } ).stop().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' ).stop().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' );
				var disabled = jQuery( this ).parent().parent().find( '.disabled' );

				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;
	
					// set last facet clicked and whether it is disabled
					Epson.ProductBrowser.current_facet = value+'|radio';
					if ( disabled.length ) Epson.ProductBrowser.current_disabled = true;

					// 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;

					// set last facet clicked and whether it is disabled
					Epson.ProductBrowser.current_facet = jQuery( parent ).hasClass( 'active' ) ? value+'|checkbox' : Epson.ProductBrowser.current_facet;
					Epson.ProductBrowser.current_disabled = false;
				}
			}
			else
			{
				var filter = jQuery( e.target ).data( 'name' );
				var value = jQuery( e.target ).data( 'value' );
				Epson.ProductBrowser.Filtering.filters[ filter ] = value;

				// set last facet clicked and whether it is disabled
				Epson.ProductBrowser.current_facet = value+'|price';
				Epson.ProductBrowser.current_disabled = false;
			}
			
			// get facets for title and cookie
			var active_facets = ['',[]];
			jQuery( '#ProductBrowserFilter li.active' ).each( function()
			{
				var classname = this.className;
				active_facets[0] += '&' + classname.match( /pbf_([^\s]+)/ )[1] + '=' + classname.match( /pbfv_([^\s]+)/ )[1];
				active_facets[1].push( jQuery( this ).text() );
			} );
			if ( active_facets[0].length ) active_facets[0] = active_facets[0].substr( 1 );

			// update title
			document.title = active_facets[0].length ? 'Epson - ' + active_facets[1].join( ' | ' ) + ' - ' + Epson.ProductBrowser.doctitle.substr(6) : Epson.ProductBrowser.doctitle;

			// 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;

			// save state
			Epson.ProductBrowser.State.data.page = Epson.ProductBrowser.Pagination.page;
			Epson.ProductBrowser.State.update();

			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 );
			}
		}
	},
	
	State:
	{
		data: {},

		load: function()
		{
			var cookie = false;
			var page_id = document.location.toString().toLowerCase().replace( /\#.+$/, '' ).replace( /http:\/\/.+?\/|[^\w]+|_/g, '' );

			if ( cookie = Epson.Cookies.read( 'PBS' ) )
			{
				if ( cookie.states[ page_id ] )
				{
					Epson.ProductBrowser.State.data = cookie.states[ page_id ];

					// set filter state
					jQuery.each( Epson.ProductBrowser.State.data, function( key, value )
					{
						var filter = jQuery( '#pbf_' + key );

						if ( filter.length )
						{
							if ( filter.is( '[type=checkbox]' ) ) filter[0].checked = 'checked';
							else if ( filter.is( 'select' ) )
							{
								var is_member = false;
							
								jQuery.each( filter[0].options, function(i)
								{
									if ( this.value == value )
									{
										filter[0].selectedIndex = i;
										is_member = true;
										return false;
									}
								} );
								
								if ( !is_member )
								{
									filter.find( 'option[selected]' ).removeAttr( 'selected' );
									filter.append( '<option value="' + value + '" selected="selected"> </option>' );
									filter[0].selectedIndex = filter[0].options.length - 1;
								}
							}
						}
						else
						{
							filter = jQuery( '#ProductBrowserFilter input[type=radio][value="' + value + '"]' );
							
							if ( filter.length )
							{
								filter.click();
								filter[0].checked = 'checked';
							}
						}
					} );

					// set sorting state
					if ( Epson.ProductBrowser.State.data.sort != 0 && jQuery( '#pbi_sort' ).length )
					{
						var sort = jQuery( '#pbi_sort' );

						jQuery.each( sort[0].options, function(i)
						{
							if ( this.value == Epson.ProductBrowser.State.data.sort )
							{
								sort[0].selectedIndex = i;
								return false;
							}
						} );
					}

					// set pagination state
					Epson.ProductBrowser.Pagination.page = Epson.ProductBrowser.State.data.page;

					// set up other state variables
					Epson.ProductBrowser.current_facet = Epson.ProductBrowser.State.data.current;
					Epson.ProductBrowser.current_disabled = Epson.ProductBrowser.State.data.current_disabled;

					return true;
				}
				else return false;
			}
			else return false;
		},

		update: function()
		{
			var cookie = false;
			var page_id = document.location.toString().toLowerCase().replace( /\#.+$/, '' ).replace( /http:\/\/.+?\/|[^\w]+|_/g, '' );

			if ( ( cookie = Epson.Cookies.read( 'PBS' ) ) )
			{
				if ( cookie.states[ page_id ] ) 
				{
					// move item to end of fifo array
					if ( cookie.ordered[4] != page_id )
					{
						var index = jQuery.inArray( page_id, cookie.ordered );
						cookie.ordered.splice( index, 1 );
					}
				}
				else
				{
					// pop oldest item off fifo array
					delete ( cookie.states[ cookie.ordered[0] ] );
					cookie.ordered.splice( 0, 1 );
				}
			}
			else
			{
				// initialise fifo array
				cookie = { ordered:[null,null,null,null,null], states:{} };
			}

			// push data onto fifo array
			cookie.ordered[4] = page_id;
			cookie.states[ page_id ] = Epson.ProductBrowser.State.data;

			// write cookie state data
			Epson.Cookies.write( 'PBS', cookie );
		}
	}
}


