$(function(){

	var $divs	= $('div[data-skus]'),
		skus	= Array();
		
	$divs.each(function(){
		var $this = $(this);

		if($this.attr('data-skus') == ''){
			$this.hide();
		}
		else
		{
			skus = skus.concat($this.attr('data-skus').split(/[ ,]/));
		}
	});

	if(skus.length > 0)
	{
		$.post(
			'/page_info/get_featured',
			{
				'skus[]' : skus
			},
			function(data){
				$divs.each(function(){
					template(this, data);
				});
			}
		);
	}

	function template(el, data)
	{
		var $el			= $(el),
			template	= decodeURI($el.html()),
			variables	= template.match(/{=[a-zA-Z]*=}/g),
			skus		= $el.attr('data-skus').split(/[ ,]/),
			html		= '';

		for(var x in skus)
		{
			for(var j in data)
			{
				if(data[j].sku == skus[x])
				{
					var temp = template;

					$.each(variables, function(index, variable){
						variable = variable.replace(/[{=}]/g, '');

						if(typeof data[j][variable] != 'undefined')
						{
							temp = temp.replace('{=' + variable + '=}', data[j][variable]);
						}
					});

					html += temp;
				}
			}
		}

		$el.html(html);
	}

//	function in_array(needle, haystack)
//	{
//		needle = needle.toLowerCase();
//
//		for(var i in haystack)
//		{
//			if(needle == haystack[i].toLowerCase())
//			{
//				return true;
//			}
//		}
//
//		return false;
//	}
});
