
$(document).ready(function(){

	//pricing classes
	$('.filterableJs .col4').each(function(){
		var postpaidPriceClass = '';
		var prepaidPriceClass = '';
		var grabNGoPriceClass = '';
		var postpaid = parseFloat($(this).find('.pricing_postpaidJs .priceTag').text().replace('$','').trim());
		var prepaid = parseFloat($(this).find('.pricing_prepaidJs .priceTag').text().replace('$','').trim());
		var grabNGo = parseFloat($(this).find('.pricing_grabAndGoJs .priceTag').text().replace('$','').trim());
		if(postpaid < 50){
			postpaidPriceClass = 'postPrice1Js';
		}
		else if(postpaid < 100){
			postpaidPriceClass = 'postPrice2Js';
		}
		else{
			postpaidPriceClass = 'postPrice3Js';			
		}

		if(prepaid  < 50){
			prepaidPriceClass = 'prePrice1Js';
		}
		else if(prepaid  < 100){
			prepaidPriceClass = 'prePrice2Js';
		}
		else{
			prepaidPriceClass = 'prePrice3Js';			
		}

		if(grabNGo < 50){
			grabNGoPriceClass = 'grabPrice1Js';
		}
		else if(grabNGo < 100){
			grabNGoPriceClass = 'grabPrice2Js';
		}
		else{
			grabNGoPriceClass = 'grabPrice3Js';			
		}

		$(this).addClass(postpaidPriceClass);
		$(this).addClass(prepaidPriceClass);
		$(this).addClass(grabNGoPriceClass);
	});

	//filter first set from parameters
	var id = '';
	var requestDefined = false;
	if(!(typeof(idRequestFilter)=='undefined')){
		id = '#filters_'+idRequestFilter+' ';
	}
	if(!(typeof(brandRequestFilter)=='undefined')){
		$('.filterBox '+id+'.brands option[value="'+brandRequestFilter+'"]').attr('selected', 'selected');
		requestDefined = true;
	}
	if(!(typeof(planRequestFilter)=='undefined')){
		$('.filterBox '+id+'.contractTerm option[value="'+planRequestFilter+'"]').attr('selected', 'selected');
		requestDefined = true;
	}


	if(!(typeof(androidRequestFilter)=='undefined')){
		$('.filterBox '+id+'input:checkbox[name="Android"]').attr('checked', 'checked');
	}
	if(!(typeof(appleRequestFilter)=='undefined')){
		$('.filterBox '+id+'input:checkbox[name="Apple"]').attr('checked', 'checked');
	}
	if(!(typeof(blackberryRequestFilter)=='undefined')){
		$('.filterBox '+id+'input:checkbox[name="BlackBerry"]').attr('checked', 'checked');
	}
	if(!(typeof(palmRequestFilter)=='undefined')){
		$('.filterBox '+id+'input:checkbox[name="Palm"]').attr('checked', 'checked');
	}
	if(!(typeof(symbianRequestFilter)=='undefined')){
		$('.filterBox '+id+'input:checkbox[name="Symbian"]').attr('checked', 'checked');
	}
	if(!(typeof(windowsRequestFilter)=='undefined')){
		$('.filterBox '+id+'input:checkbox[name="Windows"]').attr('checked', 'checked');
	}


	if(!(typeof(qwertyRequestFilter)=='undefined')){
		$('.filterBox '+id+'input:checkbox[name="qwerty"]').attr('checked', 'checked');
	}
	if(!(typeof(touchscreenRequestFilter)=='undefined')){
		$('.filterBox '+id+'input:checkbox[name="touchScreen"]').attr('checked', 'checked');
	}
	if(!(typeof(globalRequestFilter)=='undefined')){
		$('.filterBox '+id+'input:checkbox[name="Global_Roaming"]').attr('checked', 'checked');
	}
	if(!(typeof(wifiRequestFilter)=='undefined')){
		$('.filterBox '+id+'input:checkbox[name="wifi"]').attr('checked', 'checked');
	}


	if(!(typeof(price1RequestFilter)=='undefined')){
		$('.filterBox '+id+'input:checkbox[name="price1"]').attr('checked', 'checked');
	}
	if(!(typeof(price2RequestFilter)=='undefined')){
		$('.filterBox '+id+'input:checkbox[name="price2"]').attr('checked', 'checked');
	}
	if(!(typeof(price3RequestFilter)=='undefined')){
		$('.filterBox '+id+'input:checkbox[name="price3"]').attr('checked', 'checked');
	}
	
	if(requestDefined || $('.filterBox '+id+':checked').size()>0){
		if(id == ''){
			$('.filterableJs').each(function(){filter($(this).attr('id'));});
		}
		else{
			filter(idRequestFilter);
		}
	}
	if(id != ''){
		$.scrollToElement('#'+idRequestFilter,{offSet:100});
	}

});


//Filter the cellphones with the selected criterias
filter = function(filterId) {
	$.ajaxLoader.show();
	var $filters = $('#filters_'+filterId);
	var $items = $('#'+filterId+' .col4');	
	var contractType = 'postpaidJs';
	var brand = '';
	var features = '';
	var phoneOS = ':not(.AndroidOSJs):not(.AppleOSJs):not(.BlackBerryOSJs):not(.PalmOSJs):not(.SymbianOSJs):not(.WindowsOSJs)';
	var postPrice = ':not(.postPrice1Js):not(.postPrice2Js):not(.postPrice3Js)';
	var prePrice = ':not(.prePrice1Js):not(.prePrice2Js):not(.prePrice3Js)';
	var grabPrice = ':not(.grabPrice1Js):not(.grabPrice2Js):not(.grabPrice3Js)';
	var priceChecked = false;
	var osChecked = false;
	$items.addClass('hide');
	$filters.find('input').each(function(){

		if($(this).attr('name')=='price1' && $(this).attr('checked')){
			postPrice=postPrice.replace(':not(.postPrice1Js)','');
			prePrice=prePrice.replace(':not(.prePrice1Js)','');
			grabPrice=grabPrice.replace(':not(.grabPrice1Js)','');
			priceChecked = true;
		}		
		if($(this).attr('name')=='price2' && $(this).attr('checked')){
			postPrice=postPrice.replace(':not(.postPrice2Js)','');
			prePrice=prePrice.replace(':not(.prePrice2Js)','');
			grabPrice=grabPrice.replace(':not(.grabPrice2Js)','');
			priceChecked = true;
		}
		if($(this).attr('name')=='price3' && $(this).attr('checked')){
			postPrice=postPrice.replace(':not(.postPrice3Js)','');
			prePrice=prePrice.replace(':not(.prePrice3Js)','');
			grabPrice=grabPrice.replace(':not(.grabPrice3Js)','');
			priceChecked = true;
		}

		if($(this).attr('name')=='qwerty' && $(this).attr('checked')){
			features += '.qwertyJs';	
		}
		if($(this).attr('name')=='touchScreen' && $(this).attr('checked')){
			features += '.touchScreenJs';	
		}

		if($(this).attr('name')=='wifi' && $(this).attr('checked')){
			features += '.wifiJs';	
		}

		if($(this).attr('name')=='Global_Roaming' && $(this).attr('checked')){
			features += '.Global_RoamingJs';	
		}


		if($(this).attr('name')=='Android' && $(this).attr('checked')){
			phoneOS = phoneOS.replace(':not(.AndroidOSJs)','');
			osChecked = true;	
		}
		if($(this).attr('name')=='Apple' && $(this).attr('checked')){
			phoneOS = phoneOS.replace(':not(.AppleOSJs)','');
			osChecked = true;		
		}

		if($(this).attr('name')=='BlackBerry' && $(this).attr('checked')){
			phoneOS = phoneOS.replace(':not(.BlackBerryOSJs)','');
			osChecked = true;		
		}
		if($(this).attr('name')=='Palm' && $(this).attr('checked')){
			phoneOS = phoneOS.replace(':not(.PalmOSJs)','');
			osChecked = true;		
		}
		if($(this).attr('name')=='Symbian' && $(this).attr('checked')){
			phoneOS = phoneOS.replace(':not(.SymbianOSJs)','');
			osChecked = true;		
		}
		if($(this).attr('name')=='Windows' && $(this).attr('checked')){
			phoneOS = phoneOS.replace(':not(.WindowsOSJs)','');
			osChecked = true;		
		}


	});	
		
	brand='.'+$filters.find('select.brands :selected').val()+'Js';
	contractType='.'+$filters.find('select.contractTerm :selected').val()+'Js';

	if(brand=='.AllJs'){		
		brand='';
	}	

	//create the pricing filter
	var priceFilter = '';
	if(priceChecked){
		if(contractType == '.postpaidJs'){
			priceFilter = postPrice;
		}
		else if(contractType == '.prepaidJs'){
			priceFilter = prePrice;
		}
		else{
			priceFilter = grabPrice;
		}
	}

	//OS filter empty
	if(!osChecked){
		phoneOS='';
	}

	//filter the phones	
	$('#'+filterId+' .col4'+contractType+features+brand+phoneOS+priceFilter).removeClass('hide');

	//remove the class dot from contractType
	contractType = contractType.replace('.','');

	//change the price
	$('#'+filterId+' .pricingInfo:not(.pricing_'+contractType+')').hide();
	$('#'+filterId+' .pricingInfo.pricing_'+contractType).show();

	//change the links
	$('#'+filterId+' .filterLinkJs:not(.link_'+contractType+')').hide();
	$('#'+filterId+' .filterLinkJs.link_'+contractType).show();


	//change the list size 
	$('#nbItems_'+filterId).text($('#'+filterId+' .col4:not(.hide)').size());	
	
	//reset the display
	$.scroller.resetNavigation('#'+filterId);	
	$.setClassToNthPosition('#'+filterId+' .col4:not(.hide)', 3, 'colLast');
	$('#'+filterId).parents('.cBox:first').scroller({fillSpace:false,toggle:true});
	$.ajaxLoader.hide();
}


