/* Scrolling Lists are used for displaying products in a small container on rooms and other pages */

if (typeof(myDeco) == "undefined")
	var myDeco = {};

myDeco.scrollingLists = {
	/**
	 * Initializing all scrolling lists available on the page and assigning events
	 * Pass list id or object if you need to add scrolling abilities for new, dynamically created list
	 */

	init : function () {
		try {
			console.log('Initializing scrolling lists...')
			
		  var listEls = $$('.scroller');
				
			var counter = 0;
			listEls._each(function(scrollingList){
				var dur = 1;
				scrollingList.prevButton = scrollingList.down('.prev-button');
				scrollingList.nextButton = scrollingList.down('.next-button');
				
				myDeco.scrollingLists.rebuildScroller(scrollingList);
				myDeco.scrollingLists.scrollToStart(scrollingList);
				scrollingList.locked = false;
				
				scrollingList.slideLeft = function() {
					var left = window.parseInt(scrollingList.list.getStyle('left'));
					if (left < 0) {
						if ((-left) > scrollingList.scrollDistance) {
							left = left + scrollingList.scrollDistance;
						}
						else {
							if (scrollingList.scrollDistance!=0)
							  dur = Math.max(-left / scrollingList.scrollDistance, 0.2);
								
							left = 0;
						}
						new Effect.Move(scrollingList.list ,{x:left, y:0, mode:'absolute', duration:dur, transition: Effect.Transitions.EaseTo});
					}
				};
				
				scrollingList.slideRight = function () {
					var left = window.parseInt(scrollingList.list.getStyle('left'));
					var dur = 1;

          // Checking, if list have callback function, that need to be called to request more data via AJAX
          // Callback is called when distance between current viewport position and the end of the list is less than 2 scrolling distances
					var dataFetched = true;
					if (scrollingList.dataNeeded && -(left-2*scrollingList.scrollDistance) > scrollingList.listWidth)
					{
						var dataFetched = false;
            // Let's check if this list is locked (one portion of data is already requested). To remove the lock you must call function rebuildScroller.
						if (scrollingList.locked)
						  return;

					  dataFetched = scrollingList.dataNeeded();
						if (!dataFetched)
						  scrollingList.locked = true;
					}
						
					if (-(left-scrollingList.scrollDistance) < scrollingList.listWidth) {
            // Checking, if list must be scrolled not to the full scrolling distance but only to the end of items.
						if (dataFetched && (scrollingList.listWidth + (left - scrollingList.scrollDistance) < scrollingList.scrollDistance))
						{
							if (scrollingList.scrollDistance!=0)
  							dur = Math.max((scrollingList.listWidth - (scrollingList.scrollDistance - left)) / scrollingList.scrollDistance, 0.2);
							left = -(scrollingList.listWidth - scrollingList.scrollDistance);
						}
						else
							left = left - scrollingList.scrollDistance;
					}
					new Effect.Move(scrollingList.list ,{x:left,y:0,mode:'absolute', duration:dur, transition: Effect.Transitions.EaseTo});
				};
				
				/* for products list */
				scrollingList.prevButton.observe('click', function(e){Event.stop(e);scrollingList.slideLeft()});
				scrollingList.nextButton.observe('click', function(e){Event.stop(e);scrollingList.slideRight()});
				
			});
			if (listEls.length == 0)
				console.log('No scrolling lists');
		}
		catch (e) {
			console.log('Error: scrolling-list.js : myDeco.scrollingLists.init : '+e);
		}
	},

	/**
	 * Calculating width of the scrolling list based on it's element count/width 
	 * Call this function when you've dynamically changed the list and want to update width of your scroller 
	 */

	rebuildScroller: function (scrollingList) {
		try {
			scrollingList.container = scrollingList.down('.list-container');
			
			if (!scrollingList.container || !scrollingList.container.firstDescendant())
			  return;
			
			scrollingList.locked = false;
			
			scrollingList.list = scrollingList.container.firstDescendant();

			scrollingList.scrollDistance = scrollingList.container.offsetWidth;
			scrollingList.listElements = scrollingList.select('li');								
			scrollingList.list.style.width = scrollingList.listElements.size() * 250 + 30 + "px";				

			scrollingList.listWidth = 0;
			scrollingList.listElements._each(function (item) {
        // Calculating actual width of the element list including margins
				scrollingList.listWidth += item.getWidth() +
                                   window.parseInt(item.getStyle('margin-left')) +
                                   window.parseInt(item.getStyle('margin-right'));
			});
			
			var fragment = document.createDocumentFragment();
			
			/* popup info */			
			scrollingList.select('li')._each(function(item){

				var bubble = item.down('.bubble');

				if (bubble) {
					item.info = bubble;
					item.info.remove();
					fragment.appendChild(item.info.hide());
					
					if(item.down('a'))
						item.img = item.down('a');
					else if(item.down('img'))
						item.img = item.down('img');
					else
						item.img = item.down('.trendwatch-item');
					item.img.alt = '';
					
					item.keepVisible = function(){
						if (item.timeout){
							clearTimeout(item.timeout);
							item.timeout = false;
						}
					}
	
					item.hideInfo = function(){
						new Effect.Fade(item.info, {duration:0.05});
					}
	
					item.showInfo = function(e){
						item.keepVisible();
						var srcElement = Event.element(e);
						item.info.style.display="block";

						var pos = srcElement.cumulativeOffset()
						item.info.style.left = pos.left - 10 + "px";
						if (srcElement.offsetHeight  >80)
						  item.info.style.top = pos.top - item.info.offsetHeight+20+"px";
						else
						  item.info.style.top = pos.top - item.info.offsetHeight+5+"px";
					}
					
					item.init = function() {
						var srcElement = item.img;
						item.info.style.position="absolute";
						//item.info.style.display="block";
						
						item.info.observe('mouseover',function(){item.keepVisible();})
						item.info.observe('mouseout',function(){clearTimeout(item.timeout);item.timeout = setTimeout(function(){item.hideInfo();}, 300)})
						item.hideInfo();
					}
					item.init();
					
					item.img.observe('mouseover',function(e){item.showInfo(e);})
					item.img.observe('mouseout',function(){clearTimeout(item.timeout);item.timeout = setTimeout(function(){item.hideInfo();},100)})						
				}
			});
			
			document.body.appendChild(fragment);
		}
		catch (e) {
			console.log('Error: scrolling-list.js : myDeco.scrollingLists.rebuildScroller : '+e);
		}
	},

	/**
	 * Scrolling list, passed in to the start 
	 * Call this function when you've completely replace content of your list and want to scroll it back
	 */
	scrollToStart: function (scrollingList) {
// IE behavior fix.		
		if (scrollingList && scrollingList.list)
		  scrollingList.list.style.left = "0px";				
	}
}

addLoadEvent(myDeco.scrollingLists.init);
