/***** AJAX Pagenation *****/
var ajaxRequestPage     = "";
var currentPage         = $("#currentPage").val();
var totalPages          = $("#totalPages").val();
var pageSelect          = $("#selPageNo");
var pagenationLocation  = "start";
var resultsFadeSpeed    = "normal";
var loadingFadeSpeed    = "fast";
var controlLock         = false;
var selPageCurrent      = pageSelect.selectedIndex;
var selPageInitial      = pageSelect.selectedIndex;
var includesFeatured   = false;

if($("#selPageNo").val() == 0)
{
	includesFeatured = true;
}

function updateControls()
{
	checkTotalPages = totalPages;
	
	if(includesFeatured)
	{
		checkTotalPages = totalPages - 2;
	}
	
	if(totalPages == 1 || totalPages == 0)
	{
		$(".backLink").css({"color": "#cccccc", "cursor": "default"});
		$(".nextLink").css({"color": "#cccccc", "cursor": "default"});
		$("#selPageNo").attr("disabled", "disabled");
	}
	else
	{
		var selectedPageVal = $("#selPageNo").val();
		
		if(selPageCurrent == selPageInitial)
		{
			$(".backLink").css({"color": "#cccccc", "cursor": "default"});
			$(".nextLink").css({"color": "#333333", "cursor": "pointer"});
			pagenationLocation = "start";
		}
		else if(selectedPageVal == checkTotalPages)
		{
			$(".backLink").css({"color": "#333333", "cursor": "default"});
			$(".nextLink").css({"color": "#cccccc", "cursor": "pointer"});
			pagenationLocation = "end";
		}
		else
		{
			$(".backLink").css({"color": "#333333", "cursor": "pointer"});
			$(".nextLink").css({"color": "#333333", "cursor": "pointer"});
			pagenationLocation = "mid";
		}
	}
}

function changePageRequest(dir)
{
	var existingGet = window.location.search.substring(1);
	
	if(dir == "next")
	{
		currentPage++;
		$("#currentPage").val(currentPage);
		selPageCurrent++;
		pageSelect.selectedIndex = selPageCurrent;
	}
	
	if(dir == "back")
	{
		currentPage = currentPage - 1;
		$("#currentPage").val(currentPage);
		selPageCurrent = selPageCurrent - 1;
		pageSelect.selectedIndex = selPageCurrent;
	}
	
	if(dir == "jump")
	{
		currentPage = $("#selPageNo").val();
		$("#currentPage").val(currentPage);
		selPageCurrent = pageSelect.selectedIndex;
	}
	
	controlLock = true;
	$("#selPageNo").attr("disabled", "disabled");
	updateControls();
	
	$("#loadingIcon").fadeIn(loadingFadeSpeed, function()
	{
		$.get(ajaxRequestPage + "?" + existingGet + "&ajax=1&p=" + currentPage, function(data)
		{
			$("#handsetsArea").fadeOut(resultsFadeSpeed, function()
			{
				$("#handsetsArea").html(data);
				$("#loadingIcon").fadeOut(loadingFadeSpeed);
				$("#handsetsArea").fadeIn(resultsFadeSpeed);
				controlLock = false;
				$("#selPageNo").removeAttr("disabled");
			});
		});
	});
}

$(".pagenationArea span").click(function()
{
	if(controlLock == false)
	{
		var dirType = $(this).attr("class");
	
		if(dirType == "backLink" && pagenationLocation != "start")
		{
			changePageRequest("back");
		}
	
		if(dirType == "nextLink" && pagenationLocation != "end")
		{
			changePageRequest("next");
		}
	}
});

$("#selPageNo").change(function()
{
	changePageRequest("jump");
});

updateControls();
/***** AJAX Pagenation end *****/

//This function automatically submits the form when the manufacturers dropdown is changed
$("#manu-sel").change(function()
{
	$("#model-sel").val('');
	$("#paym-filters").submit();
});

$("#model-sel").change(function()
{
	$("#paym-filters").submit();
});

$("#resetbtn").click(function()
{
	var currentUrl = window.location.href;
	currentUrl = currentUrl.split("?");
	window.location = currentUrl[0];
});

$(document).ready(function()
{
	//This function puts the value from the parameter 'manu' in a variable then sets it equal to the manufacturers dropdown
	var manu = window.location.search.match(/manu=([0-9]+)&?/);
	var model = window.location.search.match(/model=([0-9]+)&?/);
	
	if (manu)
	{	
		$("#manu-sel").val(manu[1]);
	}
	
	if (model)
	{
		$("#model-sel").val(model[1]);
	}
	
	//Set the model to be greyed out if there's nothing in there
	var models = $("#model-sel").find('option');
	
	if(models.length == 1)
	{
		$("#model-sel").attr('disabled', 'disabled');
	}
});
