/* #############################################################################

© Tesco.com 2008. All rights reserved.

NS:
	TESCODIRECT.lib.DOM.rect.Position.Animation.Carousel

dependancies:
	TESCODIRECT
	TESCODIRECT.lib.event
	TESCODIRECT.lib.DOM.rect.Position

History:
	July 2008	-  S. Harness	Created for Cross Sell Up Sell
	
############################################################################# */

TESCODIRECT.lib.DOM.rect.Position.Animation.Carousel = function(speed, container, orientation) {

	this.VERSION = "1.0.0";
	this.NAME = "TESCODIRECT.lib.DOM.rect.Position.Animation.Carousel";
	
	if ( !container || !orientation) return;

	var _container = container;
    var _id = _container.id;
	var _speed = speed;	
	var _previous;
	var _next;
	
	var _nextImage;
	var _previousImage;
	//var _navText;
	
	var _removeAll;
	var _counter;
	
	var _self = this;
	var _list;  
	var _self = this;
	var _animateObj;
	var _renderObj;
	var _operationsObj;
	var _configuration = {};
	var _form;
	
	function init() 
	{
	
		    //document.body.className = 'jsEnabled';
			
			(_previous = document.getElementById("carousel-previous-"+_id)).action = 1;  // set the action on the previous button
			(_next = document.getElementById("carousel-next-"+_id)).action = -1;		  // likewise for next


			
			_previous.style.display = "none";
			_next.style.display = "none";
			
			_nextImage = document.getElementById("carousel-next-image-"+_id);
			_previousImage = document.getElementById("carousel-previous-image-"+_id);
			
			_form = document.getElementsByClassName(_container,"featuredSpaceContainer")[0];	
			_list = _form.getElementsByTagName("ul")[0];
			
			_counter = document.getElementsByClassName(_container,"counter");
			_removeAll = document.getElementsByClassName(_container,"removeAll");
			//_navText = document.getElementById("navText-"+_id);	

			_animateObj = new TESCODIRECT.lib.DOM.rect.Position.Animation(_form, false);
		    _renderObj = new _render[orientation + "View"]();
		    _operationsObj = new _operations.defaultOps();
		    _operationsObj.setConfiguration();
		    _operationsObj.attachButtons();

		    _animateObj.scrollTo(0,0);  // reset the axis

		    _renderObj.resizeContainer();


			_operationsObj.adjustSelections();

	}

	var _operations = {
		"defaultOps" : function() {
			this.VERSION = "1.0.0";
			this.NAME = "TESCODIRECT.lib.DOM.rect.Position.Animation.Carousel.operations.defaultOps";
			
			//	attach events and handlers
			
			TESCODIRECT.lib.event.attach(_container, "click",
				function(e) 
				{
					var _control = e.target;
					if(!_control.action) _control=_control.parentNode;
					if (_control.action != undefined) 
					{
						_renderObj.actuate(_control.action);
						e.stopEvent();
					}
				},
				false);
				
			TESCODIRECT.lib.event.attach(window, "resize", function(e){_renderObj.resizeContainer()}, false);
            
            this.attachButtons = function()
            {
                var oButtonNext = document.getElementById("carousel-next-" + _id);
                var oButtonPrevious = document.getElementById("carousel-previous-" + _id);
                if ( oButtonNext )
                {
                    TESCODIRECT.lib.event.attach
                    (
                        oButtonNext, 
                        "click",
						function(e) 
						{
					    	var _control = e.target;
							if(!_control.action) _control=_control.parentNode;
							if (_control.action != undefined) 
							{
							    _renderObj.actuate(_control.action);
								e.stopEvent();
							}
						},
						false
					);
                }
                if ( oButtonPrevious )
                {
                    TESCODIRECT.lib.event.attach
                    (
                        oButtonPrevious, 
                        "click",
						function(e) 
						{
					    	var _control = e.target;
							if(!_control.action) _control=_control.parentNode;
							if (_control.action != undefined) 
							{
							    _renderObj.actuate(_control.action);
								e.stopEvent();
							}
						},
						false
					);
                }
                if ( _removeAll )
                {
                    TESCODIRECT.lib.event.attach
                    (
                        _removeAll[0], 
                        "click",
						function(e) 
						{
				            _operationsObj.removeAll();
							e.stopEvent();
						},
						false
					);
				}
				
				var _checkBoxes = _container.getElementsByTagName("input");
                if ( _checkBoxes )
                {
                    for ( var i=0 ; i < _checkBoxes.length ; i++)
                    {
                        if ( _checkBoxes[i].getAttribute("type") == "checkbox" )
                        {
                            TESCODIRECT.lib.event.attach
                            (
                                _checkBoxes[i], 
                                "click",
						        function(e) 
						        {   
				                    _operationsObj.adjustSelections();
						        },
						        false
					        );
                            
                        }
                    }
                }
            }    
			this.setConfiguration = function(){
				_configuration.totalItems = _list.getElementsByTagName("li").length - 
				TESCODIRECT.lib.DOM.node.getChildrenByTagName(_list,"li","carouselHide").length;
				_configuration.windowWidth = _list.parentNode.offsetWidth;
				
				
				_configuration.itemWidth = parseInt(_list.getElementsByTagName("li")[0].offsetWidth);
				
				//console.log(_configuration.itemWidth);
				
				_configuration.startItem = 1;
				_configuration.endItem = _configuration.itemsInWindow = Math.floor((_container.offsetWidth - 30)/_configuration.itemWidth);
				_list.style.width=(_configuration.totalItems+1)*_configuration.itemWidth + "px"; // add one for rounding safety
			};
			
			this.setButtonVisibility = function(){
			   //_previous.style.visibility = (_configuration.startItem == 1) ? "hidden" : "visible";
				//_next.style.visibility = (_configuration.endItem > _configuration.totalItems) ? "hidden" : "visible";
				_previous.style.display = (_configuration.startItem == 1) ? "none" : "block";
				_next.style.display = (_configuration.endItem > _configuration.totalItems) ? "none" : "block";
			
				//_operationsObj.setNavText();
			};
			
			TESCODIRECT.lib.event.attach(window, "click", function(e){_renderObj.resizeContainer()}, false);
			
			this.adjustSelections = function()
            {
                try
                {
                    var checkBoxes = _container.getElementsByTagName("input");
                    var count = 0;
                    for ( var i=0 ; i < checkBoxes.length ; i++)
                    {
                        if ( checkBoxes[i].getAttribute("type") == "checkbox" )
                        {
                            var id = 'label-' + _id + '-' + checkBoxes[i].getAttribute('value');
                            if ( checkBoxes[i].checked == true ) 
                            {
                                document.getElementById(id).firstChild.nodeValue="Selected";
                                count++;
                            }
                            else
                                document.getElementById(id).firstChild.nodeValue="Select Item";
                        }
                    }
                    var ct = document.getElementsByClassName(_container,'counter');
                    if ( _counter )
                    {
                        var counterText = ((count > 0 ) ? count + ' selected' : ' ');
                        _counter[0].innerHTML = counterText;
                    }
                    if ( _removeAll )
                    {
                        _removeAll[0].innerHTML = ( count > 0 ) ? 'Remove All' : " ";
                    }
                }
                catch(e)
                {
                    //alert('Whoops! ' + e.message);
                }
            }
            /*
            this.setNavText = function()
            {
                if ( _navText ) 
                {
                    _navText.innerHTML = '';
                    var start = _configuration.startItem;
                    var end = _configuration.endItem;
                    if ( (end-1) > _configuration.totalItems)  return;
                    _navText.innerHTML = 'Showing items ' + start + ' to ' + (end-1) + ' of ' + _configuration.totalItems;
                }
            }
            */
			this.removeAll = function()
			{
                try
                {
                        var checkBoxes = _container.getElementsByTagName("input");
                        var count = 0;
                        for ( var i=0 ; i < checkBoxes.length ; i++)
                        {
                            if ( checkBoxes[i].getAttribute("type") == "checkbox" )
                            {
                                checkBoxes[i].checked = false; 
                                var id = 'label-' + _id + '-' + checkBoxes[i].getAttribute('value');
                                document.getElementById(id).firstChild.nodeValue="Select Item";
                            }
                        }
                        if ( _counter ) _counter[0].innerHTML = ' '; 
                        if ( _removeAll ) _removeAll[0].innerHTML = ' ';
                }
                catch(e)
                {
                    //alert('Whoops! ' + e.message);
                }
            };
		}
	}
	
	var _render = {
		"horizontalView" : function() {
			
			this.VERSION = "1.0.0";
			this.NAME = "TESCODIRECT.lib.DOM.rect.Position.Animation.Carousel.operations.horizontalView";		
			
			this.actuate = function(action) {
				_configuration.startItem -= action;
				_configuration.endItem -= action;
				_animateObj.scroll( _configuration.itemWidth * (_configuration.startItem -1),  0, _speed);				
				_operationsObj.setButtonVisibility();
			};

			this.resizeContainer = function(){
				    var _pxWidth = Math.floor((_container.offsetWidth)/_configuration.itemWidth) * _configuration.itemWidth;
				    
				    
				    //TODO:: what do we do with this???
				    /*try{
				        list.parentNode.style.width = _pxWidth + "px";
				    }catch(e){
				        alert(_list.parentNode.style);
				    }*/
				    _configuration.itemsInWindow = Math.floor((_container.offsetWidth - 30)/_configuration.itemWidth);
				    _configuration.endItem = _configuration.startItem + _configuration.itemsInWindow;
				    _operationsObj.setButtonVisibility();
				
			};
		}
	}
	init();
}


//called onload
function initCarousels()
{
        // Note: If you want to use a list instead of a carousel then comment the following line out
        document.body.className = 'jsEnabled';
        var cList=document.getElementsByClassName(document.body,'featuredSpaceWrapper');		
		if (cList)
		{
			for (var idx=0;idx < cList.length; idx++){
				new TESCODIRECT.lib.DOM.rect.Position.Animation.Carousel(50, cList[idx], 'horizontal');
		    }		
		    hideTabCarousels();
		}
}


function hideTabCarousels()
{
    try
    {
	    var tabCarousels = document.getElementsByClassName(document.body,'sectionHolder');
	    if ( tabCarousels.length > 0 )
	    {
		    for ( var idx = 0 ; idx < tabCarousels.length ; idx++ ){
		    //TODO::too hacky.
		            tabCarousels[idx].parentNode.setAttribute("class","section");
		    }
	    }
	}
	catch(e)
	{
	}
}

