
(function($){ // --------------------------------------------------------------------------------------------------------------------- START 	- jQuery Closure for $ -------------


/* ------------------------------------------------------------------------------------------------------------------------------------------------

	jQuery.BCCartSum 1.0 [Static plugin]

	Overview: This function extracts the link, quantity and total out of the string for the cart summary passed in as a parameter and returns it as object literal with the following properties:
	
		url			- string with url to cart summary
		quantity	- number(integer) representing quantity
		total		- number(float) representing total P.S.: No dollar($) sign
		ok			- boolean(true) indicating whether all valus returned are correct
	
	
	-------------------------------------------------------------------------------------------------------------------------------------------------------
	Example of cart summary mark up supported:
	
		<table cellspacing="0" class="cartSummaryTable">
			<tr>
				<td class="cartSummaryItem">18 item(s), Total: $360.00 <a class="cartSummaryLink" href="/OrderRetrievev2.aspx?CatalogueID=14223">View Cart</a></td>
			</tr>
		</table>
	
	
	
	-------------------------------------------------------------------------------------------------------------------------------------------------------
	List of options supported:
	
	anchorQuery				//jQuery string - query string of anchor tag. 	
	summaryItemQuery		//jQuery string - query string of holder of the text sum.
	sumTextDivider			//string - delimiter for total and quantity in sum string
	

*/


//---------------------------------------------------------------------------------------- START	- BCCartSumExtractor
	
	$.BCCartSum = function(str){
		
		if(arguments.length == 1)arguments[1] = {};
		var o = $.extend({
			anchorQuery: 'a',
			summaryItemQuery: '.cartSummaryItem',
			sumTextDivider: ','	
		},arguments[1]);
	
	
// -----------------------------------------------------------------------------------------------------------------------	START	- PRIVATE PROPERTIES

		var oHtml = $('<div></div>').html(str);
		
		var ok = true;
		var url = getLink();
		var quantity = getQuantity();
		var total = getTotal();

// -----------------------------------------------------------------------------------------------------------------------	END		- PRIVATE PROPERTIES

// -----------------------------------------------------------------------------------------------------------------------	START	- PRIVATE MEMBERS

		function getSumText()//Returns string of cart summary in html
		{
			var e = oHtml.clone().find(o.summaryItemQuery);
			e.find('a').remove();//remove anchor from item query
			return oHtml.text();	
		}
		
		function getLink()//Returns link of cart summary
		{
			var sHref = oHtml.find(o.anchorQuery).attr('href');
			if(typeof sHref == 'string' && sHref.length > 0) return sHref;
			ok = false;
			return null;			
		}
		
		function getQuantity()//Returns quantity of items in cart
		{
			var oText = $.trim(getSumText().split(o.sumTextDivider)[0]).split(' ');	
			for(var i = 0; i < oText.length; i++)
			{
				if(!isNaN(oText[i]))return parseInt(oText[i]);	
			}
			ok = false;
			return null;
		}
		
		function getTotal() //Returns total
		{
			var oText = getSumText().split(o.sumTextDivider);
			if(oText.length == 2)
			{
				oText = $.trim(oText[1]).split(' ');
				for(var i = 0; i < oText.length; i++)
				{
					if(oText[i].indexOf('$') != -1 && !isNaN(oText[i].replace('$','')))return parseFloat(oText[i].replace('$',''));	
				}			
			}
			ok = false;
			return null;
		}
	
// -----------------------------------------------------------------------------------------------------------------------	END		- PRIVATE MEMBERS

// -----------------------------------------------------------------------------------------------------------------------	START	- EVALUATION

	
	
// -----------------------------------------------------------------------------------------------------------------------	END		- EVALUATION
	
		return {
			url: url,
			quantity: quantity,
			total: total,
			ok: ok	
		};
	
	};
	
	
//---------------------------------------------------------------------------------------- END		- BCCartSumExtractor
	
})(jQuery); // ----------------------------------------------------------------------------------------------------------------------- END 		- jQuery Closure for $ -------------
