if ( Object.isUndefined(CodeCompany) ) { var CodeCompany = { }; }

CodeCompany.AsyncPrices = Class.create({
	initialize: function(options)
	{
		this.itemids = [];

		this.options = Object.extend({
			successCallback: Prototype.emptyFunction,
			overviewType: 'normal',
			async: true
		}, options || { } );
		
		this.templates = 
		{
			normal: 
			{
				normal: new Template('Kr. #{price}'),
				offer: new Template('Nu Kr. #{price} <span class="specialprice">SPAR #{savings}</span>')
			},
			small:
			{
				normal: new Template('Kr. #{price}'),
				offer: new Template('Nu Kr. #{price}<br /><span class="specialprice">SPAR #{savings}</span>')
			},
			variant:
			{
				normal: new Template('#{price}'),
				offer: new Template('SPAR #{savings}')
			},
			product:
			{
				normal: new Template('#{price}'),
				offer: new Template('#{savings}'), 
				multiqty: new Template('<h2 class="title1" style="color:#ffffff;font-size:30px; margin-top:24px;">#{multiqty} stk</h2><h3 class="multiprice">&nbsp;#{multiprice},-</h3><br /><span class="multidiscount" style="color:#ffffff;">spar #{multisavings}</span>'),
				offertext: new Template('<p>Normalpris: #{price}</p>')
			}
		};
	},
	
	setSuccessCallback: function(fn)
	{
		this.options.successCallback = fn;
	},
	
	addItemId: function(itemid)
	{
		this.itemids.push(itemid);
	},
	
	render: function()
	{
		dk.ide.webservice.RelatedProducts.GetProductInformation(this.itemids.join(","), this.onSuccess.bind(this));
	},
	
	onSuccess: function(result)
	{
		var d = (!Object.isUndefined(result.d))?result.d:result;
		d.each(function(item){
			switch ( this.options.overviewType )
			{
				case 'variant':
				if(item.id!=''){
					$('priceinfo_'+item.id).update(this.templates[this.options.overviewType].normal.evaluate(item));
					if ( item.onoffer )
					{
						$('savings_'+item.id).update(this.templates[this.options.overviewType].offer.evaluate(item));
					}
					}
				break;
				
				case 'product':
					$('priceinfo_'+item.id).update(this.templates[this.options.overviewType].normal.evaluate(item));
					if ( item.onoffer )
					{
						$('savings_'+item.id).update(this.templates[this.options.overviewType].offer.evaluate(item));
						$('tilbudssplat_'+item.id).show();
					}
					if( item.multiplediscount ){
						$('tilbudssplat_'+item.id).update(this.templates[this.options.overviewType].multiqty.evaluate(item));
						$('tilbudssplat_'+item.id).show();
					}
					//Do stuff with shopcart? 
					if(!item.shopbable){
					$("no_shoppingcart_price_holder").show();
					}
					if( item.shopable){
					//show shopcart.
					$("add_to_shoppingcart_andor_price_holder").show();
					}
					if( item.bundleitem){
					//trigger systemitems :) 
					}
					//untill we have actual shopable items om the site;
					$("add_to_shoppingcart_andor_price_holder").show();
					$("no_shoppingcart_price_holder").hide();
					haveShownBundle = true;
					
					document.fire('custom:fontreplace');
				break;
				
				default:
					if ( item.onoffer )
					{
						$('tilbudssplat_'+item.id).show();
						$('priceinfo_'+item.id).update(this.templates[this.options.overviewType].offer.evaluate(item));
					}
					else
					{
						$('priceinfo_'+item.id).update(this.templates[this.options.overviewType].normal.evaluate(item));
					}
				break;
			}
		}, this);
		
		this.options.successCallback.delay();
	}
});
