var URL = '/PAP/load_page.php';

$(document).ready(function(i){

	// preload loader gif
	if(document.images) {
		tImage = new Image; 
		tImage.src = "../images/ajax-loader.gif"; 
	}
						
	$('.pap').each(function(){

		var id = $(this).attr("id");
		var tParams = $(this).attr("params");
		tParams = tParams.split('|');
		$(this).html('<span class="loading"></span>');

		$.ajax({
			type: 'GET',
			url: URL,
			data:'url=' + tParams[1],
			success: function(tData) {
				PAPProcessPage(tData, id, tParams);														 
			}
		});
		
	});	
						
});


/*
 * PAPProcessPage
 *
 * Processes the data passed based on the paramaters.  Either checks stock, price or both.
 */
function PAPProcessPage(pData, pID, pParams) {
	var tPrice = '', tStock = '';
	if(pParams[0] == 1 || pParams[0] == 3) {
		tPrice = '<span class="price">&pound;' + number_format(PAPGetPrice(pData, urlDecode(pParams[2]), urlDecode(pParams[3])), 2) + '</span>';
	}
	if(pParams[0] == 2 || pParams[0] == 3) {
		if(PAPGetInStock(pData, urlDecode(pParams[4]), urlDecode(pParams[5]))) {
			if(typeof(pParams[6]) == 'undefined' || pParams[6] == '') {
				tStock = '<span class="in_stock">&nbsp;</span> ';
			} else {
				tStock = '<span class="in_stock">' + urlDecode(pParams[6]) + '</span> ';
			}
		} else {
			if(typeof(pParams[7]) == 'undefined' || pParams[7] == '') {
				tStock = '<span class="out_stock">&nbsp;</span> ';
			} else {
				tStock = '<span class="out_stock">' + urlDecode(pParams[7]) + '</span> ';
			}
		}
	}
	$('#' + pID).html(tStock + tPrice);
}

/*
 * PAPGetPrice
 *
 * Searches the given data string and attempts to extract the
 * price.
 */
function PAPGetPrice(pData, pClass, pOffset) {
	var tPrices, tPrice;
	pOffset = (typeof(pOffset) != 'undefined') ? pOffset - 1 : 0;
	if(typeof(pClass) != 'undefined' || pClass != '') {
		tSearchPattern = new RegExp(pClass, "i");
		tMatch = pData.search(tSearchPattern);
		if(tMatch != -1) {
			pData = pData.substr(tMatch);
		}
	}
	tPrices = pData.match(/\u00A3{0,1}[0-9]+\.[0-9]{2}|\u00A3[0-9]+/g);
	if(tPrices != null) {
		tPrices = tPrices.toString();
		tPrices = tPrices.split(',');
		tPrice = tPrices[pOffset];
		if(tPrice.search(/\u00A3/) != -1) {
			tPrice = tPrice.substr(1);
		}
		return tPrice;
	} else {
		return 0;
	}
}


/*
 * PAPGetPrice
 *
 * Searches the given data string and attempts to extract the
 * price.
 */
function PAPGetInStock(pData, pStockIn, pStockOut) {
	if(typeof(pStockOut) != 'undefined' && pStockOut != '') {
		if(pData.search(pStockOut) != -1)
			return false;
	}
	if(typeof(pStockIn) != 'undefined' && pStockIn != '') {
		if(pData.search(pStockIn) != -1)
			return true;
	}
	return false;
}

function urlDecode(str){
	if(typeof(str) != 'undefined') {
		str=str.replace(new RegExp('\\+','g'),' ');
		return unescape(str);
	} else {
		return '';
	}
}

function urlEncode(str){
	if(typeof(str) != 'undefined') {
		str=escape(str);
		str=str.replace(new RegExp('\\+','g'),'%2B');
		return str.replace(new RegExp('%20','g'),'+');
	} else {
		return '';
	}
}

function number_format(number, decimals, dec_point, thousands_sep) {
	var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
	var d = dec_point == undefined ? "." : dec_point;
	var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
	var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
	return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\\d{3})(?=\\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}