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,
      disabled_status: 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>' );
//            var replacement = jQuery( '<li class="radio ' + classname + '"><a href="#">' + jQuery( this ).find( 'input' ).attr('value') + '</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>' );
//            var replacement = jQuery( '<li class="checkbox ' + classname + '"><div class="backgroundContainer"><div class="cornerImage"></div><div class="subBackgroundContainer"><a href="#">' + jQuery( this ).find( 'input' ).attr('value') + '</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 );			
		} );		
		requestData();
		// populate data if previous state
		//setTimeout( Epson.ProductBrowser.requestData, 100 ); // IE race condition

//		Epson.ProductComparison.counter = 0;

		//jQuery('#pbf_price').width(150);
	},
	
	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('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;
			}
			
			if (!jQuery( parent).hasClass('disabled'))
			{
			// 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();
			    //alert(disabled.length);
                //if (disabled.length==0)	
				requestData();
			}
			}

			e.preventDefault();
		},
		
		doNothing: function(e)
		{
			e.preventDefault();
		}
	}
	
	

}