/**
* $Id: itemCarousel.js 33621 2010-04-08 11:06:22Z iargent $
* @author Eber Herrera
* 
*/

function GSI_Widget_SearchCarousel(){
	this.name = 'searchcarousel';
	this.queryStr = null;
	this.carousel = null;
	this._length = 0;
	this.loadOrder = 1;
	
	var lastDrawn = 0;
	
	var _mthis = this;
	
	var loadSteps = 1;
	
	this.getLoadSteps = function(){
		return loadSteps;
	}
	
	//event handlers
	this.onReady = function(fn){
		$(_mthis).bind("ready",_mthis,fn);
		};
	//
	
	var load = function(){
		try{
			if (typeof GSI=="undefined") return false;
			if ($('#item-carrousel').length !=1) return false;
			if (!GSI.functions.isset($().jcarousel)) return false; 
			}
			catch(e){
			return false;
			}
		return true;
	};

	var showToolTip = function(){
		var tItem = GSI.se.getItem($('img', this).attr('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').css({position: "relative"});
		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($('img', this).get(0), null, tStr,null, tWidth);
						
	};
	
	var hideToolTip = function(){
		GSI.widgets.tooltip.hide();
	};
	this.hide = function(){
		$('#item-carrousel').hide();
	};
	
	this.show = function(){
		$('#item-carrousel').show();
	};
		
	var initGUI = function(){
		var tLength = GSI.functions.count(GSI.se.items);
		if (tLength <= 12 && tLength>0) {  // if results less than 12
			$("ul", "#item-carrousel").empty();
			var text = '';
			for (var anItem in GSI.se.items){
				text = initCarouselItem(GSI.se.items[anItem]);
				if (text) {
					var tElem = $("<li>" + text + "</li>").hover(showToolTip, hideToolTip);
					$("ul", "#item-carrousel").append(tElem).css("list-style", "none");
					$("#item-carrousel li").css("display", "inline");
				}
			}
			_mthis._length = tLength;
			_mthis.loader(false);
			$(_mthis).trigger('ready');
			return true;
		}
		
		var initDraw = function(carousel, state){
			_mthis.carousel = carousel;
			_mthis.draw();
		};
		var onLast = function (carousel,dom,index,state){
				if ((state != 'next' || index<_mthis._length -1) || GSI.se.data.pagination.links.next == 'disabled' ) return;
				if (!GSI.se.data.pagination.links.next) return;
				GSI.se.page++;
				$("#item-carousel-next").addClass('loading');
				GSI.se.search(_mthis.draw, null, true);
		};
		
		var onNextPrev = function(carousel, dom, state){
			if (state) $(dom).removeClass('disabled');
			else $(dom).addClass('disabled');
		};

		$("#item-carrousel").jcarousel({
				initCallback: initDraw,
				itemLastInCallback: onLast,
				buttonNextCallback: onNextPrev,
				buttonPrevCallback: onNextPrev,
				buttonNextHTML: '<div id="item-carousel-next"></div>',
				buttonPrevHTML: '<div id="item-carousel-prev"></div>',
				scroll: 4,
				size : tLength

    	});
    	
	};
	
	initCarouselItem = function (item){
			try{
				var sku = item.getData('id');
				var img = item.getImage('mini');
				var element = '<a href="item?sku='+sku+'"><img title="'+sku+'" alt="" width="59" height="64" src="'+img+'"/></a>';
				return element;
			}
			catch(e){
				_mthis.setError('initCarouselItem drawing item : '+item.getData('id')+'\n'+e.message, 'ERROR');
				return '<img src="'+GSI_CATALOG_IMG_PATH+'mini/notavailable.gif" alt="" title="">';
			}
		};
				
	this.draw = function(){
		_mthis.loader(true);
		$("#item-carousel-next").removeClass('loading');
		if (GSI.se.data['error']) _mthis.hide();
		var tLength = parseInt(GSI.se.data.response.docs.length) + parseInt(_mthis._length);
				
			var text = '';
			for (var i=_mthis._length; i<tLength; i++){
				var pos = i - _mthis._length;
				var tSku = GSI.se.data.response.docs[pos]['manufacturer_sku'];
				var tItem = GSI.se.items[tSku];
				text = initCarouselItem(tItem);
				if (text) {
					var tElem = $(text);
					tElem.hover(showToolTip, hideToolTip);
					_mthis.carousel.add(i, tElem);
				}
			}
			_mthis._length = i;
			_mthis.carousel.size(_mthis._length);
			
			_mthis.loader(false);
			$(_mthis).trigger('ready');
	};
	
	
	this.loader = function(on){
		if (on) {
			$('.loader','#item-carrousel').html('<img src="'+GSI_APP_IMG_PATH+'shared/loader_medium.gif">').show();
		} else {
			$('.loader','#item-carrousel').hide();
		}
	};
	
	this.init = function(){
		if (!load()) {
			GSI.updateProgressBar(_mthis.name +" "+ GSI.dict.getText('JSAPI', 'widget_initiation_error'));
			return false;
		}
		
		_mthis.loader(true);
		
		var searchFeedBack = function(){
			initGUI();
			_mthis.loaded = true;
		};
		
		GSI.updateProgressBar(_mthis.name +" "+ GSI.dict.getText('JSAPI', 'widget_initiated'));
		GSI.se.search(searchFeedBack, null, true);		
		return true;
	};
	
	
	
};
GSI_Widget_SearchCarousel.prototype=new GSI_Widget();

var tW = new GSI_Widget_SearchCarousel();
GSI.widgets.loadWidget(tW);
