$(document).ready(function(){
		function filter()
		{
//			var pricerange = jQuery.makeArray(post_pricerange);
//			alert(post_pricerange[0]);
//			alert(post_pricerange);
			$.post('post/refinery.filter.php', '&filter=true&offset='+post_offset+'&brand='+post_brand+'&provider='+post_provider+'&contract='+post_contract+'&'+post_pricerange , function(xml)
			{					
//				alert($(xml).text());
						$('#products_list').empty();
						var html = '';
						var page_links = '';
						var header = $('details header', xml).text();
						var filters = $('details filters', 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:eq(0)').html(header)
						$('#products_header p:eq(0)').empty().append(filters).append('Still not what you are looking for? <strong>Limit your results by using the filter options to your left.</strong>')
						$('.pages .links').html(page_links);
					
						initPages();
												
						var blocks = $('blocks block', xml);
						
						blocks.each(function(i)
						{
							var code = $('code', $(this)).text();
							var imgcode = $('imgcode', $(this)).text();
							var brand = $('brand', $(this)).text();
							var provider = $('provider', $(this)).text();
							var contract = $('contract', $(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 isPromo = $('isPromo', $(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;
							price_html = (isPromo=1) ? '<p class="price" style="color: #f00; font-size: 16px;">Special Price:<br />$' + price + '</p>': '<p class="price">$' + price + '</p>'

							html = '<div class="block">'+
										'<div class="bl-left">'+
										'<div class="image"><img src="media/products/phones/'+imgcode+'/main-thumb.jpg" alt="'+name+'"></div>'+
										'<div class="details">'+
											'<h1><a href="'+link+'">'+name+'</a></h1>'+
											'<div class="features">'+
												'<ul>'+features+'<li><a href="'+link+'">More 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>'+
											'<div class="retail">'+
												'<div class="left">Retail</div>'+
												'<div class="right">$'+price_retail+'</div>'+
											'</div>'+
											'<div class="discount">'+
												'<div class="left">Instant Discount</div>'+
												'<div class="right">-$'+price_discount+'</div>'+
											'</div>'+
											'<div class="rebate">'+
												'<div class="left">Mail-In Rebate</div>'+
												'<div class="right">-$'+price_rebate+'</div>'+
												'<div class="spacer"></div>'+
											'</div>'+
											price_html+
											'<div class="spacer"><br /></div>'+
											'<p><a href="'+link+'"><img src="images/add-to-cart.gif" alt="Add to Cart" /></a></p>'+
											'<div class="spacer"></div>'+
										'</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()
		{
			$('.pages a').click(function()
			{
				currpage = $(this).attr('rel') * 1;
				
				post_offset = currpage * items_per_page;
				
				$('#ajax_status').hide().fadeIn('normal', filter);
				return false;
			})
			
		}
		
		// 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;} )
		$('#filter_provider a').click(function() { reset(); post_provider = $(this).attr('rel'); $('#ajax_status').hide().fadeIn('normal', filter); return false;} )
		$('#filter_contract a').click(function() { reset(); post_contract = $(this).attr('rel'); $('#ajax_status').hide().fadeIn('normal', filter); return false;} )
		initPages();
		
		function reset()
		{
			currpage = 0; 
			post_offset = 0; 	
		}

		$('.ads2').cycle({delay:100})

});