/**
* $Id: RVPBox.js 33621 2010-04-08 11:06:22Z iargent $
*
*/

function GSI_Widget_RVPBox(){
	
	this.name = 'rvpbox';
	this.loadOrder = 1;
	
	var _mthis = this;
	
	var load = function(){
		try{
			if (typeof GSI=="undefined") return false;
			if ($('#rvp').length !=1) return false;
			}
			catch(e){
			return false;
			}
		return true;
	};
	
	this.loader = function(on){
		if(on){
			$('#rvp-contents').hide();
			$('#rvp-loader').show();
		}
		else{
			$('#rvp-loader').hide();
			$('#rvp-contents').show();
		}
	};

	this.show = function (){
		$('#rvp-contents').slideDown();
	};
	
	this.hide = function (){
		$('#rvp-contents').slideUp();
	};
	
	this.refresh = function(callback){
		var refreshFeedback = function(){
			_mthis.loader(false);
			if (GSI.rvp.data['error']) {
				drawError();
				callback();
				return false;
			}
			draw();
			callback();
		};
		if (!GSI.functions.isset(callback)) callback=function(){};
		_mthis.loader(true);
		GSI.rvp.retrieve(refreshFeedback);
	};
	
	var drawError = function(){
		$('#rvp-contents').val(GSI.rvp.data['error']);
	};
	
	var draw = function(){
		var tStr = '';
		var cnt = 0;

		try{
			for (var anItem in GSI.rvp.items){
				if (anItem == 'indexOf') continue;
				var item = GSI.rvp.items[anItem];
				cnt++;
				tStr += '<a class="image-link"><img class="image-link" src="'+item.getImage('mini')+'" alt="'+item.getData('name')+'" title="'+item.getData('id')+'"></a>';
			}
			
			if (cnt>0) $("#rvp").show();
			
			$('#rvp-contents').empty();
			$('#rvp-contents').append(tStr);
			$("img" , "#rvp-contents").click(function(){
				showExpressItem(this.title);
			}).hover(
				function(){
					
					var tItem = GSI.rvp.getItem(this.title);
					var orgPrice = tItem.getData('orgprice');
					var offrPrice = tItem.getData('offrprice');
					var tWidth= 100;
					
					
					if (offrPrice){
					$(".item-box-contents-price-offer, .item-tooltip-offer-percentage", "#item-tooltip-thumb").show();
					$(".item-box-contents-price", "#item-tooltip-thumb").addClass('originalprice');
					tWidth= 100;
					}
					else {
						$(".item-box-contents-price-offer, .item-tooltip-offer-percentage", "#item-tooltip-thumb").hide();
						$(".item-box-contents-price", "#item-tooltip-thumb").removeClass('originalprice')
							.css({position: "relative"});
					}

					var name  = tItem.getData('name');
					if (name.length >=40) name = name.substr(0,40)+'...';
					var tooltipTemplate = $("#item-tooltip-thumb").html(); 
		
					var tooltipTemplate = $("#item-tooltip-thumb").html(); 
					var tStr = tooltipTemplate.replace(/__itemPriceOrg__/g,GSI.functions.moneyFormat(orgPrice, tItem.getData('currencyid')));
					if (offrPrice) tStr = tStr.replace(/__itemPriceOffr__/g,GSI.functions.moneyFormat(offrPrice, tItem.getData('currencyid')));
					tStr = tStr.replace(/__itemName__/g,name);
					tStr = tStr.replace(/__itemImage__/g,'');
					
					GSI.widgets.tooltip.show(this, null, tStr,null, tWidth);
									
				},function(){
					GSI.widgets.tooltip.hide();
				});
			
		}catch(e){
			GSI.setError('ERROR @ drawing the RVPBox','INFO');
			return;
		}
		
		var resizeSearchBox = function(){ 
			var tW =GSI.widgets.getWidget('searchbox');
			if(tW && cnt>0) tW.refreshParamNavHeight();
		};
		
		setTimeout(resizeSearchBox, 800);
	};
	
	this.init = function(){
		if (!load()) return false;
		var tXpress = GSI.widgets.getWidget('expressitem');
		if (tXpress) var originalCollection = tXpress.getCollection();
		_mthis.refresh();
		_mthis.loaded = true;
		return true;
	};
}
GSI_Widget_RVPBox.prototype=new GSI_Widget();

var tW = new GSI_Widget_RVPBox();
GSI.widgets.loadWidget(tW);
