$(document).ready(function(){
				
		function filter()
		{
//			var pricerange = jQuery.makeArray(post_pricerange);
//			alert(post_pricerange[0]);
//			alert(post_pricerange);
			$.post('post/powerplant.filter.php', '&filter=true&offset='+post_offset+'&brand='+post_brand+'&'+post_pricerange , function(xml)
			{					
						$('#products_list').empty();
						var html = '';
						var page_links = '';
						var header = $('details header', xml).text();
						var header2 = $('details header2', xml).text();
						var itemcount = $('details count', xml).text() * 1;
						
						// Start Page Counting
						var pages = Math.ceil(itemcount / items_per_page);

						for(i=0;i<pages;i++)
						{
							i2 = i + 1;

							if(i==currpage)							
							page_links += '<a href="#" class="selected" rel="'+currpage+'">'+i2+'</a>';
							else
							page_links += '<a href="#" rel="'+i+'">'+i2+'</a>';
						}
					
						$('#products_header h1').html(header)
						$('#products_header p').html(header2)
						$('#products_header .pages .links').html(page_links);
					
						initPages();
												
						var blocks = $('blocks block', xml);
						
						blocks.each(function(i)
						{
							var code = $('code', $(this)).text();
							var brand = $('brand', $(this)).text();
							var name = $('name', $(this)).text();
							var price = $('price', $(this)).text();
							var key_features = $('key_features', $(this)).text();
							var colors = $('colors', $(this)).text();
							var price_retail = $('price_retail', $(this)).text();
							var price_rebate = $('price_rebate', $(this)).text();
							var price_discount = $('price_discount', $(this)).text();
							
							if(key_features != '')
							key_features = key_features.split(',');
							
							var features = '';
							
							if(key_features.length > 0)
							$(key_features).each(function(i2)
							{
								features += '<li>'+key_features[i2]+'</li>';
							});
							
							if(colors){
								features += '<li>Also Available in - '+colors+'</li>';								
							}

							link = 'cellphone.php?brand='+brand+'&name='+name+'&code='+code;
							
							discounts = '';
							
							if (price_retail != 0.00)
							discounts = '<div class="retail">'+
														'<div class="left">Suggested Retail</div>'+
														'<div class="right">$'+price_retail+'</div>'+
													'</div>'

							if (price_discount != 0.00)																
							discounts += '<div class="discount">'+
														'<div class="left">Instant Discount</div>'+
														'<div class="right">-$'+price_discount+'</div>'+
													'</div>'

							if (price_rebate != 0.00)
							discounts += '<div class="rebate">'+
														'<div class="left">Mail-In Rebate</div>'+
														'<div class="right">-$'+price_rebate+'</div>'+
														'<div class="spacer"></div>'+
													'</div>'

							html = '<div class="block">'+
										'<div class="bl-left">'+
										'<div class="image"><img src="media/products/accessories/'+code+'/main.jpg" alt="'+name+'"></div>'+
										'<div class="details">'+
											'<h1><a href="'+link+'">'+name+'</a></h1>'+
											'<div class="features">'+
												'<ul>'+features+'<li><a href="'+link+'">Click here to see complete details!</a></li></ul>'+
											'</div>'+
											'<img src="images/blank.gif" class="dots-breaker">'+
											'<div class="spacer"></div>'+
										'</div>'+
										'</div>'+
										'<div class="bl-right">'+
											'<p><strong>'+name+'</strong></p>'+
											discounts+
											'<p class="price">$'+price+'</p>'+
											'<br />'+
											'<p><img src="images/add-to-cart.gif" alt="Add to Cart" /></p>'+
										'</div>'+
										'<div class="spacer"></div>'+
									'</div>';
							
							$('#products_list').append(html);

							$('#main .content').css('height','auto');
							
//							alert(obj_block.html());
						}); //end each
												
						$('#ajax_status').fadeOut('slow')
				}, 'xml')
				
			return false;
		}

		function initPages()
		{
			$('#products_header .pages a').click(function()
			{
				currpage = $(this).attr('rel') * 1;
				
				post_offset = currpage * items_per_page;
				
				$('#ajax_status').hide().fadeIn('normal', filter);
				return false;
			})

			$('.add-to-cart').click(function()
			{
				amount = $(this).parent().children('input[name="amount"]').val();
				code = $(this).parent().children('input[name="code"]').val();
				brand = $(this).parent().children('input[name="brand"]').val();
				link = 'accessory.php?brand='+brand+'&code='+code+'&amount='+amount;
				window.location = link;
			})
			
		}
		
		// Init stuffs
		$('#f_options').submit(function(){ reset(); post_pricerange = $('#filter_price input:checkbox').serialize().replace(/%5B%5D/gi, '[]'); $('#ajax_status').hide().fadeIn('normal', filter); return false;});		
		$('#filter_brand a').click(function() { reset(); post_brand = $(this).attr('rel'); $('#ajax_status').hide().fadeIn('normal', filter); return false;} )
		initPages();
		
		function reset()
		{
			currpage = 0; 
			post_offset = 0; 	
		}
		
});