Pod1 = new Object();
Pod1.TopCart= {
    initialize: function (container) {
        this.container = $(container);
        this.element = this.container.up(0);
        this.elementHeader = this.container.previous(0);
        this.intervalDuration = 4000;
        this.interval = null;
        this.onElementMouseOut = this.handleMouseOut.bindAsEventListener(this);
        this.onElementMouseOver = this.handleMouseOver.bindAsEventListener(this);
        //this.onElementMouseClick = this.handleMouseClick.bindAsEventListener(this);

        this.element.observe('mouseout', this.onElementMouseOut);
        this.elementHeader.observe('mouseover', this.onElementMouseOver);
        //this.elementHeader.observe('click', this.onElementMouseClick);

    },

    handleMouseOut: function (evt) {
        if($(this.elementHeader).hasClassName('expanded')) {
            this.interval = setTimeout(this.hideCart.bind(this), this.intervalDuration);
        }
    },
	handleMouseOver: function (evt) {
        if (!$(this.elementHeader).hasClassName('expanded') && !$(this.container.id).hasClassName('process') )  {
            this.showCart();
        }
    },
    /*handleMouseOver: function (evt) {
        if (this.interval !== null) {
             clearTimeout(this.interval);
             this.interval = null;
        }
    },*/
	
    /*handleMouseClick: function (evt) {
        if (!$(this.elementHeader).hasClassName('expanded') && !$(this.container.id).hasClassName('process') )  {
            this.showCart();
        }
        else {
            this.hideCart();
        }
    },*/

    showCart: function (timePeriod) {
        this.container.parentNode.style.zIndex=10000;
        new Effect.SlideDown(this.container.id, { duration: 0.5,
            beforeStart: function(effect) {$( effect.element.id ).addClassName('process');},
            afterFinish: function(effect) {$( effect.element.id ).removeClassName('process'); }
            });
        $(this.elementHeader).addClassName('expanded');
        if(timePeriod) {
            this.timePeriod = timePeriod*1000;
            this.interval = setTimeout(this.hideCart.bind(this), this.timePeriod);
        }
    },

    hideCart: function () {

        if (!$(this.container.id).hasClassName('process') && $(this.elementHeader).hasClassName('expanded')) {
            new Effect.SlideUp(this.container.id, { duration: 0.5,
                beforeStart: function(effect) {$( effect.element.id ).addClassName('process');},
                afterFinish: function(effect) {
                    $( effect.element.id ).removeClassName('process');
                    effect.element.parentNode.style.zIndex=1;
                    }
                });
        }
        if (this.interval !== null) {
            clearTimeout(this.interval);
            this.interval = null;
        }
        $(this.elementHeader).removeClassName('expanded');
    }
};



Pod1.sideScroll = {

	obj: null,

	initialise: function(scrollableObject, holderClass, pageClass, scrollerId) {

		var holderWidth = 0;
		var objCount = 0;
		var pageCount = 0;
		var objPerPage = 0;
		var objWidth = 0;
		
		$(scrollableObject).select(holderClass + ' ' + pageClass).each(function(obj){
			objWidth = parseInt(obj.getStyle('width'));
			holderWidth += objWidth;
			objCount ++;
		});
		if (parseInt($(scrollableObject).getStyle('width')) < holderWidth) {
			//alert($(scrollerId).parentNode);
			$(scrollerId).parentNode.style.visibility='visible';
			//$(scrollerId).parentNode.setStyle({ visibility: 'visible' });
			//return;
		}
		$(scrollableObject).select(holderClass).each(function(obj){
			obj.setStyle({ width: holderWidth + 'px' });
		});

		objPerPage = Math.round(parseInt($(scrollableObject).getStyle('width')) / objWidth);

		pageCount = Math.ceil(objCount / objPerPage);
		$(scrollableObject).select('.left').each(function(obj){
			Event.observe(obj, 'mouseover', function(){
				this.setStyle({ backgroundPosition: '0 100%' });
			});
			Event.observe(obj, 'mouseout', function(){
				this.setStyle({ backgroundPosition: '0 0%' });
			});
			Event.observe(obj, 'click', function(){
				$(scrollableObject).select('.holder').each(function(obj){
					var currentPosition = Math.round(parseInt(obj.getStyle('margin-left')) * -1);
					var currentPage = 1;
					if (currentPosition > 0) {
						currentPage = Math.ceil(currentPosition / (objWidth * objPerPage));
					}
					if (currentPage > 0) {
						var newPosition = (currentPage-1) * objWidth * objPerPage * -1;
						obj.setStyle({ marginLeft: newPosition + 'px' });
						$(scrollableObject).select('.draggable').each(function(obj){
							var newDragPos = parseInt($(scrollerId).getStyle('width'));
							newDragPos = (newDragPos / (pageCount-1)) * (currentPage-1);
							newDragPos -= draggableWidth * (currentPage-1) / (pageCount-1);
							obj.setStyle({ left: newDragPos + 'px' });
						});
					}
				});

			});
		});


		$(scrollableObject).select('.right').each(function(obj){
			Event.observe(obj, 'mouseover', function(){
				this.setStyle({ backgroundPosition: '0 100%' });
			});
			Event.observe(obj, 'mouseout', function(){
				this.setStyle({ backgroundPosition: '0 0%' });
			});

			Event.observe(obj, 'click', function(){
				$(scrollableObject).select('.holder').each(function(obj){
					var currentPosition = Math.round(parseInt(obj.getStyle('margin-left')) * -1);
					var currentPage = Math.ceil(currentPosition / (objWidth * objPerPage));
					if (currentPosition % (objWidth * objPerPage) == 0) {
						currentPage++;
					}

					if (currentPage < pageCount) {
						var newPosition = currentPage * objWidth * objPerPage * -1;
						obj.setStyle({ marginLeft: newPosition + 'px' });
						$(scrollableObject).select('.draggable').each(function(obj){
							var newDragPos = parseInt($(scrollerId).getStyle('width'));
							newDragPos = (newDragPos / (pageCount-1)) * currentPage;
							newDragPos -= draggableWidth * (currentPage) / (pageCount-1);
							obj.setStyle({ left: newDragPos + 'px' });
						});
					}
				});
			});
		});

		var draggableWidth = 0;
		$(scrollerId).select('.draggable').each(function(obj){
			draggableWidth = parseInt($(obj).getStyle('width'));
		});

		var totalObjectsToFillPages	= objCount;
		if (objCount % objPerPage > 0) { totalObjectsToFillPages += (objPerPage - (objCount % objPerPage)); };
		var scrollRange = (objWidth * totalObjectsToFillPages) - (objWidth * objPerPage);

		this.obj =  new Control.Slider(
			$(scrollerId).down('.draggable'),
			$(scrollerId), {
				range: $R(0, scrollRange),
				sliderValue: 0,
				onSlide: function(value) {

					$(scrollableObject).select('.holder').each(function(obj){
						obj.setStyle({ marginLeft: (value * -1) + 'px' });
					});
      			},
				onChange: function(value) {
					$(scrollableObject).select('.holder').each(function(obj){
						obj.setStyle({ marginLeft: (value * -1) + 'px' });
					});
				}
			}
		);
	}

}

Event.observe(window, 'load', function(){
    $$('a').each(function(obj){
        if(obj.href == document.location.href){
            obj.addClassName('pod1ActiveLink');
        }
    });
});


function updateCartMini(addToCartUrl){
	new Ajax.Request( addToCartUrl,
    		{
    		method:'get',
		    onSuccess: function(transport){
    	 		var response = transport.responseText || "no response text";
    	 		 var myStart = response.indexOf('<!-- START POD1 TOP CART -->') + ('<!-- START POD1 TOP CART -->').length;
    	 		 var myEnd =  response.indexOf('<!-- END POD1 TOP CART -->');
       	 		 var myUpdate = response.substring(myStart, myEnd);
       	 		 $('topCartContent').up().innerHTML = myUpdate;
       	 		 Pod1.TopCart.initialize('topCartContent');
				 Pod1.TopCart.showCart(3);
    	 		  
		    },
		    onFailure: function(){ alert('Something went wrong...') }
	});
}
