this.tmpl = function tmpl(str, data)
{
    return str.replace(/{([^{}]*)}/g,
        function (a, b) {
            var r = data[b];
            return typeof r === 'string' || typeof r === 'number' ?r : a;
        });
 };


function generateHTML(item, template){
	//console.log(item);
	var link = "";
	if (item.trackingLinks )
	{
		linkppv = item.trackingLinks.trackingLink.ppv;
		linkppc = item.trackingLinks.trackingLink.ppc;
		link = linkppc;

		image = "";
		imagesmall = "";
		imagemedium = "";
		imagelarge = "";
		
		if (item.image) 
		{
			if (item.image.small)
			{
				image  = item.image.small;
				imagesmall = item.image.small;
				
			}
			else if(item.image.medium)
			{
				image  = item.image.medium;
				imagemedium = item.image.medium;
			}
			else if(item.image.large)
			{
				image  = item.image.large;
				imagelarge  = item.image.large;
				
			}
		}
		
		var description = "";
		var descriptionLong = "";
		if(item.description) description = item.description;
		if(item.descriptionLong) descriptionLong = item.descriptionLong;
		
		// content --> Probleme mit IE
	//	content1 = tmpl("{program} <br/><span class='currencyField' style='font-weight:bold'>{price}</span><strong> {currency}</strong>",{program:item.program.$ ,price:item.price , currency: item.currency});
	//	contentHTML =  tmpl('<div style="border: 1px solid #ddd ; margin:10px 10px 0 10px; padding:10px;"><div style="float:right;margin:0 0.5em 0.25em 0.5;width:10em;height:10em;text-align:center;"><img style="padding-left:1em" height="100px" src="{itemimagesmall}"/></div><h4 style="padding-top:0px;margin-top:0px;"">{name}</h4>{description}<br/>{content}<a href="{link}"><br/><br/>Details</a></div><br style="clear:both"/>',{program:item.program.$ ,price:item.price , currency: item.currency, itemimagesmall:itemimagesmall ,name:item.name , content: content1, link:link , description:description });
		contentHTML =  tmpl(template,{program:item.program.$, price:item.price, currency: item.currency, image:image,imagesmall:imagesmall,imagemedium:imagemedium,imagelarge:imagelarge,name:item.name ,link:link, linkppv:linkppv ,linkppc:linkppc , description:description, descriptionLong:descriptionLong  });
		return contentHTML;
	}
	return "";
}

function addScript(q, url, template)
{

		var content = "";
		var contentHTML = "";
		var array = new Array();
		
		if (jQuery('span#zanoxify[title]').attr('title'))
		{
			q = jQuery('span#zanoxify[title]').attr('title');
		}

		jQuery.getJSON(url + "&q=" + q + "&callback=?", function(data)
		{
			//console.log(data);
			if (data.items ==1 )
			{
				contentHTML =generateHTML(data.productItems.productItem);
			} else
			{
				jQuery.each(data.productItems.productItem, function(i, item)
				{
						//console.log(item);
						contentHTML = contentHTML + generateHTML(item, template);
				});
			}
			jQuery('span#zanoxify').replaceWith(contentHTML );
			jQuery('.currencyField').formatCurrency(); 
			
			jQuery('.imgContainer').each(function()
			{
				console.log( jQuery(this).find("img").attr("src") );
				if(jQuery(this).find("img").attr("src") == "")
					jQuery(this).remove();
			});
			

		});
}


(function($) {
	
	$.fn.formatCurrency = function(settings) {
		settings = jQuery.extend({
		    name: "formatCurrency",
		    global: true
		}, settings);
		
		return this.each(function() {
		    var num = "0";
		    num = $(this).html();
		    num = num.replace(/\$|\,/g, '');
		    if (isNaN(num))
		        num = "0";
		    sign = (num == (num = Math.abs(num)));
		    num = Math.floor(num * 100 + 0.50000000001);
		    cents = num % 100;
		    num = Math.floor(num / 100).toString();
			
		    if (cents < 10)
		        cents = "0" + cents;
		    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
		        num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
		
		    $(this).html(((sign) ? '' : '-') + '' + num + ',' + cents);
		});
	};

})(jQuery);
