// JavaScript Document paym
 
/***** Filter box control *****/
var resetPage   = "/mobile-phones/pink-mobile-phones/";

function updateNetworks()
{
	// Get the selected manufacturer.
	var chosenManu = $("#manu-sel").attr("value");
	
	// Get the selected model if any.
	var chosenModel = $("#model-sel").attr("value");
	
	var get = getQueryString();
	
	$.get(ajaxHandler + "?updatenetworks=true&fs=pink&cma=" + chosenManu + "&cmo=" + chosenModel, function(data)
	{	
		var newData = data.replace("everything", "");
		
		// Update networkSelect with the new list of networks that are specific to the current selections.
		if(get["net"])
		{
			$("#net-sel").html(newData.replace('<option value="' + get["net"] + '">', '<option selected="selected" value="' + get["net"] + '">'));
		}
		else
		{
			$("#net-sel").html(newData);
		}
	});
}

function updateModels(net)
{
	var chosenManu = $("#manu-sel").attr("value");
	
	// All is the value in the PAYM any options whereas PAYG uses nothing.
	if(chosenManu == 'all' || chosenManu == '')
	{
		// Disable the select box if Any is selected.
		$("#model-sel").html("<option value=\"\">Model:</option>");
		$("#model-sel").attr("disabled", "disabled");
		updateNetworks();
	}
	else
	{
		$.get("/quick_search.php?fs=pink&manu=" + chosenManu + "&net=" + net, function(data)
		{
			var get = getQueryString();
			
			// Update modelSelect with the new list of models, enable modelSelect and update the networks.
			if(get["model"])
			{
				$("#model-sel").html(data.replace('<option value="' + get["model"] + '">', '<option selected="selected" value="' + get["model"] + '">'));
			}
			else
			{
				$("#model-sel").html(data);
			}
			
			$("#model-sel").removeAttr("disabled");
			updateNetworks();
		});
	}
}

function getQueryString()
{
	var get = new Array();
	var queryString = window.location.search.substring(1);
	
	if(queryString != '')
	{
		var splitQueryString = queryString.split("&");
		
		for(var i = 0; i < splitQueryString.length; i++)
		{
			var params = splitQueryString[i].split("=");
			get[params[0]] = params[1];
		}
	}
	
	return get;
}

$(".featureSel").attr("disabled", "disabled");

$(document).ready(function()
{
	$(".featureSel").removeAttr("disabled");
	
	$(".featureSel").click(function()
	{
		$("#paym-filters").submit();
		$(".featureSel").attr("disabled", "disabled");
	});
	
	$(".autosel").change(function()
	{
		var submitForm = true;
		
		if(this.id == 'manu-sel')
		{
			if($(this).attr("value") == "")
			{
				submitForm = false;
			}
			else
			{
				$("#model-sel").attr("value", "");
				$("#net-sel").attr("value", "");
			}
		}
		
		if(this.id == 'net-sel')
		{
			if($("manu-sel").attr("value") == "")
			{
				submitForm = false;
			}
		}
		
		if(submitForm)
		{			
			$("#paym-filters").submit();
		}
		else
		{
			window.location = resetPage;
		}
	});
	
	var get = getQueryString();
	
	if(get["manu"])
	{
		$("#manu-sel").val(get["manu"]);
	}
	
	var totalFeatures = $(".featureSel");

	// Try and preserve selected features.
	for(var i = 1; i <= totalFeatures.length; i++)
	{
		if(get["f" + i])
		{
			$("#feature" + get["f" + i]).attr("checked", "checked");
		}
	}
	
	updateModels(get["net"]);
	
	$("#resetbtn").click(function()
	{
		window.location = resetPage;
	});
	
	var urlGet = getQueryString();
	
	if(urlGet['gift'])
	{
		$("#gift-sel").val(urlGet['gift']);
	}
});
/***** Filter box control end *****/

/***** AJAX Pagenation *****/
var ajaxRequestPage     = "";
var currentPage         = $("#currentPage").val();
var totalPages          = $("#totalPages").val();
var pageSelect          = document.getElementById("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 *****/