$(document).ready(function()
{
	$('#ajax-inner').hide();
	
	$('.leftMenu select').change(function(){ doFilter() });
	$('.leftMenu input[type=checkbox]').change(function(){ doFilter() });
	
	if(network=='')
	{
		$('.searchNetworks img').click(function()
		{
			var allOn = true;
			$('.searchNetworks img').each(function()
			{
				if( $('#net'+$(this).attr('id')).val()=='' )
					allOn = false;
			});
			if(allOn)
			{
				$('.searchNetworks img').each(function()
				{
					$('#net'+$(this).attr('id')).val('');
					var i = $(this).attr('src').split('.')
					$(this).attr('src',i[0]+'-off.png');
				});
				$('#net'+$(this).attr('id')).val('on');
				var i = $(this).attr('src').split('-off')
				$(this).attr('src',i[0]+'.png');
			}
			else
			{
			
				if( $('#net'+$(this).attr('id')).val()=='' )
				{
					$('#net'+$(this).attr('id')).val('on');
					var i = $(this).attr('src').split('-off')
					$(this).attr('src',i[0]+'.png');
				}
				else
				{
					$('#net'+$(this).attr('id')).val('');
					var i = $(this).attr('src').split('.')
					$(this).attr('src',i[0]+'-off.png');
				}
			}
			$('#limit').val('8');
			$('#offset').val('0');
			doFilter();
		});
	}
	else
	{
		$('#Tmobile').click(function()
		{
			if(network != 't-mobile-phones')
				window.location.href='/networks/t-mobile-phones';
		});
		$('#Vodafone').click(function()
		{
			if(network != 'vodafone-mobile-phones')
				window.location.href='/networks/vodafone-mobile-phones';
		});
		$('#Orange').click(function()
		{
			if(network != 'orange-mobile-phones')
				window.location.href='/networks/orange-mobile-phones';
		});
		$('#Three').click(function()
		{
			if(network != '3-mobile-phones')
				window.location.href='/networks/3-mobile-phones';
		});
		$('#O2').click(function()
		{
			if(network != 'o2-mobile-phones')
				window.location.href='/networks/o2-mobile-phones';
		});
		$('#Virgin').click(function()
		{
			if(network != 't-mobile-phones')
				window.location.href='/networks/virgin-mobile-phones';
		});
	}
	doFilter();
});

function doFilter()
{
	$('#ajax-inner').show();
	var dataString = '';
	if( $('#netVodafone').val()=='on' )
		dataString += 'netVodafone=on&';
	if( $('#netOrange').val()=='on' )
		dataString += 'netOrange=on&';
	if( $('#netO2').val()=='on' )
		dataString += 'netO2=on&';
	if( $('#netTmobile').val()=='on' )
		dataString += 'netTmobile=on&';
	if( $('#netThree').val()=='on' )
		dataString += 'netThree=on&';
	if( $('#netVirgin').val()=='on' )
		dataString += 'netVirgin=on&';
	if( $('#checkFeature17').attr('checked') )
		dataString += 'checkFeature17=1&';
	if( $('#checkFeature14').attr('checked') )
		dataString += 'checkFeature14=1&';
	if( $('#checkFeature16').attr('checked') )
		dataString += 'checkFeature16=1&';
	if( $('#checkFeature9').attr('checked') )
		dataString += 'checkFeature9=1&';
	if( $('#checkFeature4').attr('checked') )
		dataString += 'checkFeature4=1&';
	if( $('#checkFeature10').attr('checked') )
		dataString += 'checkFeature10=1&';
	if( $('#checkFeature5').attr('checked') )
		dataString += 'checkFeature5=1&';
	if( $('#checkFeature15').attr('checked') )
		dataString += 'checkFeature15=1&';
	if( $('#checkFeature20').attr('checked') )
		dataString += 'checkFeature20=1&';
	if( $('#checkFeature21').attr('checked') )
		dataString += 'checkFeature21=1&';
	if( $('#checkFeature22').attr('checked') )
		dataString += 'checkFeature22=1&';
	if( $('#checkFeature23').attr('checked') )
		dataString += 'checkFeature23=1&';
	
	dataString += 'selectSort='+$('#selectSort').val()+'&'
				  +'selectPrice='+$('#selectPrice').val()+'&'
				  +'selectFeature19='+$('#selectFeature19').val()+'&'
				  +'selectFeature3='+$('#selectFeature3').val()+'&'
				  +'limit='+$('#limit').val()+'&'
				  +'offset='+$('#offset').val()+'&'
				  +'sort='+$('#sort').val()+'&'
				  +'order='+$('#order').val()+'&'
				  +'manufacturer='+manufacturer+'&'
				  +'category='+category;
	$.ajax
	({
		type:"POST",
		data:dataString,
		url:"/resources/includes/table-phones.inc.php",
		success:function(msg)
		{
			var IE = /*@cc_on!@*/false;
			IE ? document.getElementById('pageFiller').innerHTML = msg : $('#pageFiller').html(msg);
			$('#ajax-inner').hide();
		}
	})
				  
}

function doPage(n)
{
	switch(n)
	{
		case 1:
		  $('#offset').val(parseInt($('#offset').val()) + parseInt($('#limit').val()));
		  break;
		case -1:
		  $('#offset').val(parseInt($('#offset').val()) - parseInt($('#limit').val()));
		  break;
		case 100:
			$('#limit').val('');
			$('#offset').val('0');
			break
		case -100:
			$('#limit').val('8');
			$('#offset').val('0');
			break;
	}	
	$(document).scrollTo($('.main'), 800, {queue:true} );
	doFilter();
}

function doSort(s)
{
	if($('#sort').val()==s)
	{
		if($('#order').val()=='asc')
			$('#order').val('desc');
		else if($('#order').val()=='desc')
			$('#order').val('asc');
	}
	else
	{
		$('#sort').val(s);
		if(s=='texts' ||  s=='minutes')
			$('#order').val('desc');
		else
			$('#order').val('asc');
	}
	$('#offset').val('0');
	doFilter();
}
