/*
 * controls the behaviour of the cart summary that appears on the rhs
 * when on a list screen and the page scrolls to half a page above the cart-summary
 * panel we want the panel to become positioned fixed so that it moves down the
 * page with scroll
 *
 * so we need to add some sort of position detection for the scroll and then
 * once it his that calculate its current height get what is roughly the centre
 * of teh page and detach the div with a fixed position
 */
var CartSummary = {

	cartClass: 'cart-summary',
	originalTop: 0,
	originalTopTrigger: 0,
	cartSummaryDetached:  false,
	counter: 0,

	init: function() {
		CartSummary.cartEl = $('column-right').getElement('.' + CartSummary.cartClass);
		if(CartSummary.cartEl) {

			
			// get the original top position of the cart element
			CartSummary.originalTop = CartSummary.getTopY(CartSummary.cartEl);
			// the point at which we trigger the detach of the summary
			CartSummary.originalTopTrigger = CartSummary.originalTop + (CartSummary.getSizeY(CartSummary.cartEl) / 2) -  (CartSummary.getWindowSizeY(CartSummary.cartEl) / 2);


			//CartSummary.originalBottom = CartSummary.getWindowScrollY() + 20 + CartSummary.getSizeY(CartSummary.cartEl);
			// the point which we wish to re-attach teh cart at the bottom
			//CartSummary.originalBottomTrigger = CartSummary.getWindowScrollSizeY() - 20 - CartSummary.getSizeY($('footer'));
			//CartSummary.debug();
			
			// stethe initial position of the cart summary panel
			CartSummary.setPosition(CartSummary.cartEl);
			// set up event to occur on scroll
			window.addEvent('scroll', function() {
				CartSummary.setPosition(CartSummary.cartEl);
				//CartSummary.debug();
				
			});
		}
	},

	debug: function() {

		console.log('cart detached? ' + (CartSummary.cartSummaryDetached));
		console.log('original top: ' + CartSummary.originalTop);

		console.log('height size: ' + CartSummary.getSizeY(CartSummary.cartEl));

		console.log('window size: ' + CartSummary.getWindowSizeY(CartSummary.cartEl))
		
		console.log('original top trigger: ' + CartSummary.originalTopTrigger);

		console.log('the scroll height of the summary panel: ' + CartSummary.getScrollY(CartSummary.cartEl));

		console.log('the scroll size of the summary panel: ' + CartSummary.getScrollSizeY(CartSummary.cartEl));

		console.log('the scroll height of the  footer: ' + CartSummary.getScrollY($('footer')));

		console.log('the scroll size of the footer: ' + CartSummary.getScrollSizeY($('footer')));

		console.log('top of footer y: ' + CartSummary.getTopY($('footer')));

		console.log('height of footer y: ' + CartSummary.getSizeY($('footer')));

		console.log('window scroll y: ' + CartSummary.getWindowScrollY());

		console.log('window scroll size y: ' + CartSummary.getWindowScrollSizeY());

		console.log('originalBottom: ' + (CartSummary.getWindowScrollY() + 20 + CartSummary.getSizeY(CartSummary.cartEl)));
		console.log('originalBottomTrigger: ' + (CartSummary.getWindowScrollSizeY() - 20 - CartSummary.getSizeY($('footer'))));

		CartSummary.debugConditions();
	},

	debugConditions: function() {
		console.log('scroll position less than trigger ' + (CartSummary.getWindowScrollY() > CartSummary.originalTopTrigger));
		console.log('scroll position greater than TOP trigger' + ((CartSummary.getWindowScrollY() < CartSummary.originalTopTrigger)));
		console.log('scroll position greater than BOTTOM trigger ' + ((CartSummary.originalBottom > CartSummary.originalBottomTrigger)));
	},
	/**
	 * Attaches and detaches the cart summary panel depending on scroll position
	 */
	setPosition: function()
	{
		// the scroll Y position is greater than the initial top position of the
		// cart summary panel
		if (CartSummary.getWindowScrollY() > CartSummary.originalTopTrigger) {
			//fixTheParentHeight = CartSummary.cartEl.parentNode.getStyle('height');
			CartSummary.cartEl.setStyles({
				'position': 'fixed',
				'top': '20px'
			});
			//CartSummary.cartEl.parentNode.setStyle('height', fixTheParentHeight);
			CartSummary.cartSummaryDetached = true;

		// else it is less than and we need to attach it again
		} else if ((CartSummary.getWindowScrollY() < CartSummary.originalTopTrigger) && CartSummary.cartSummaryDetached) {
			CartSummary.cartEl.setStyles({
				'position': 'static',
				'top': CartSummary.originalTop
			});
			//CartSummary.cartEl.parentNode.setStyle('height', '');
			CartSummary.cartSummaryDetached = false;

		}
	}, 

	getTopY: function(el) {
		/*CartSummary.counter++;
		console.log(CartSummary.counter);
		console.log(el);*/
		return el.getPosition().y;
	},

	getBottomY: function(el) {
		

		return el.getPosition().y + CartSummary.getSizeY(el);
	}, 

	getSizeY: function(el) {
		return el.getSize().y;
	},

	getScrollY: function(el) {
		return el.getScroll().y;
	},

	/**
	 * analagous to the height of the element
	 */
	getScrollSizeY: function(el) {
		return el.getScrollSize().y;
	},

	/**
	 * the distance between the absolute top of the window and the displayed top of the window
	 */
	getWindowScrollY: function() {
		return window.getScroll().y;
	},
	/**
	 * the height of the viewable area of the window
	 */
	getWindowSizeY: function() {
		return window.getSize().y;
	},
	/**
	 * the total height of the of the window including the bits we cannot see
	 */
	getWindowScrollSizeY: function() {
		return window.getScrollSize().y;
	}
}

window.addEvent('domready', function() {
	CartSummary.init();
});
