﻿// ----------------------------------------------
// File:		GetImagesService.js
// Author:		Nathan Derksen
// Description:	A class that holds the available service methods, along with response handlers.
// Example:
// GetProductsService.getInstance().getProducts(searchCriteria, 0, 12);
// ----------------------------------------------


// ----------------------------------------------
// Function:	GetImagesService
// Author:		Nathan Derksen
// Description:	Base class
// Inputs:		<none>
// Returns:		<nothing>
// ----------------------------------------------
function GetImagesService()
{
}

// ----------------------------------------------
// Function:	GetProductsService.getProducts
// Author:		Nathan Derksen
// Description:	Calls the service to retrieve a set of products
// Inputs:		<Object> searchCriteria - An object of properties to search on
//				<Number> startIndex - The zero-based index of which row to start at in the total results
//				<Number> maxResults - The maximum number of results to return
// Returns:		<nothing>
// ----------------------------------------------
GetImagesService.prototype.getData = function(queryString)
{
	try
	{
		var currentPage = Number(URLFactory.extractValue(queryString, "currentPage"));
		var pageSize = Number(URLFactory.extractValue(queryString, "pageSize"));
		var searchTerm = URLFactory.extractValue(queryString, "searchTerms");
		var useCategory = URLFactory.extractValue(queryString, "useCategory");
		var pageQueryString = window.location.search.split("?").join("");
		var cid = URLFactory.extractQueryStringValue(pageQueryString, "cid");
		var mcat = URLFactory.extractQueryStringValue(pageQueryString, "mcat");
		var refine = document.getElementById("chkRefine").checked;
		
		if (cid == "")
		{
			cid = "0";
		}
		
		if (mcat == "")
		{
			mcat = "0";
		}
		
		if (isAjaxEnabled() == true)
		{
			if (searchTerm != "")
			{
				if (refine == false || useCategory == "false")
				{
					cid = "0";
					mcat = "0";
				}	
				PageMethods.GetItemsXmlBySearchQS(URLFactory.convertHashToServiceHash(queryString), refine, cid, mcat, this.onResult, this.onError);
			}
			else
			{
				PageMethods.GetItemsXmlByCategoryID(cid, mcat, currentPage, pageSize, this.onResult, this.onError);
			}
//			PageMethods.GetItemsXmlBySearchQS("i", false, cid, mcat, this.onResult, this.onError);

//			if (URLFactory.extractQueryStringValue(pageQueryString, "search_params") != "")
//			{
//				PageMethods.GetItemsXmlBySearchQS(URLFactory.convertHashToServiceHash(queryString), false, cid, mcat, this.onResult, this.onError);
//			}
//			else
//			{
//				PageMethods.GetItemsXmlByCategoryID(URLFactory.extractQueryStringValue(pageQueryString, "cid"), mcat, currentPage, this.onResult, this.onError);
//			}
		}

//		var productsGrid = ViewLocator.getInstance().getView("productsGrid");
//		productsGrid.setProducts(tempImageList);

//		var model = ProductModel.getInstance();
//		model.setNumProducts(40);
//		model.setProducts(tempImageList);
	}
	catch (err)
	{
		Debug.error(err);
	}
};

// ----------------------------------------------
// Function:	GetImagesService.onResult
// Author:		Nathan Derksen
// Description:	Callback from the successful completion of the service call
// Inputs:		<XMLElement> result - Handle to the results xml object
// Returns:		<nothing>
// ----------------------------------------------
GetImagesService.prototype.onResult = function(result)
{
	try
	{
		if (result)
		{
			var resultElements = result.documentElement;
			var resultArray = ProductFactory.convertXMLToArray(resultElements);
			var numHits = ProductFactory.getTotalNumProducts(resultElements);
			var numRows = ProductModel.getInstance().getProductGrid("viewPaged").numRows;

			var productsGrid = ViewLocator.getInstance().getView("productsGrid");
			productsGrid.setMaxRows(numRows);
			productsGrid.setProducts(resultArray);
			
			document.getElementById("container").style.height = (535 + (numRows - 1) * 150) + "px";

			var model = ProductModel.getInstance();
			model.setNumProducts(numHits);
			model.setProducts(resultArray);

			if(numHits == 0)
			{
				DisplayNoResultMessage();
			}
			else
			{
				RemoveNoResultMessage();
			}
		}
		else
		{
			var tempArray = new Array();
			var productsGrid = ViewLocator.getInstance().getView("productsGrid");
			productsGrid.setProducts(tempArray);
			
			var error = new Object();
			error.name = "Service 'GetProductsService' returned with no results";
			error.message = error.name;
			error.fileName = "itemPage/GetProductsService.js";
			error.lineNumber = "";
			Debug.error(error);
		}
	}
	catch (err)
	{
		Debug.error(err);
	}
};

// ----------------------------------------------
// Function:	GetProductsService.onError
// Author:		Nathan Derksen
// Description:	Callback from the unsuccessful completion of the service call
// Inputs:		<XMLElement> result - Handle to the results xml object
// Returns:		<nothing>
// ----------------------------------------------
GetImagesService.prototype.onError = function(result)
{
	var tempArray = new Array();
	var productsGrid = ViewLocator.getInstance().getView("productsGrid");
	productsGrid.setProducts(tempArray);
	
	var error = new Object();
	error.name = "Service 'GetImagesService' returned with an error";
	error.message = result.get_message();
	error.fileName = "itemPage/GetImagesService.js";
	error.lineNumber = "";
	Debug.error(error);
};
