var chkCompList;
var bShowSummaryCart = false;

// Make sure DOM is ready
document.observe('dom:loaded', function() {
	var wrapper = $('wrapper');
	var floatName = 'floating-cart';
	var elPosName = 'appHeader';
	var bodyWidth = wrapper.offsetWidth;
	var transSpd = 5;
	var checkEl = $(elPosName);
	var floatCartPlacementX = getXCoord(checkEl) + bodyWidth;
	var floatCartPlacementY = getYCoord(checkEl);
	
	wrapper.className="wrapperFloat";
	
	/* Draw floating cart */
	var floater = new FloatLayer(floatName,elPosName,bodyWidth,floatCartPlacementX,floatCartPlacementY,transSpd);
	floater.hide();
	floater.detach();

	// floater events
	Event.observe(window, 'resize', alignFloatLayers);
	Event.observe(window, 'scroll', alignFloatLayers);

	updateCompListSize();
	
	// Positions objects depending on browser dimensions
	var sizeChecker = Class.create({
		initialize: function( floater ) {
			this.floater = floater;
			this.windowWidth = "1000";
			this.viewportOffsetLeft = "10000";
			this.checkSize;
			this.windowWidthTmp;
			this.windowHeightTmp;
			this.overallContainer = $('overallContainer');
			this.wrapper = $('wrapper');
		},
		getViewportWidth: function() {
			var viewportwidth;
			// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
		 
			if (typeof window.innerWidth != 'undefined')
		 	{
		      viewportwidth = window.innerWidth;
		 	}
		 
			// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
		
			else if (typeof document.documentElement != 'undefined'
		 		  && typeof document.documentElement.clientWidth !='undefined' 
		     		  && document.documentElement.clientWidth != 0)
		 		{
		       		viewportwidth = document.documentElement.clientWidth;
		 		}
		 
		 		// older versions of IE
			else
			{
		   		viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
			}
			return viewportwidth;
		},
		getViewportHeight: function() {
			var viewportheight;
			// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
		 
			if (typeof window.innerHeight != 'undefined')
		 	{
		      viewportheight = window.innerHeight;
		 	}
		 
			// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
		
			else if (typeof document.documentElement != 'undefined'
		 		  && typeof document.documentElement.clientHeight !='undefined' 
		     		  && document.documentElement.clientHeight != 0)
		 		{
		       		viewportheight = document.documentElement.clientHeight;
		 		}
		 
		 		// older versions of IE
			else
			{
		   		viewportheight = document.getElementsByTagName('body')[0].clientHeight;
			}
			return viewportheight;
		},
		startInt: function() {
			var x = this;
			this.checkSize = setInterval(function(){x.getSize();}, 1000);
		},
		stopInt: function() {
			var x = this;
			clearInterval(x.checkSize);
		},
		getSize: function() {
			var bDoDimChange = false;
			this.windowWidthTmp = this.getViewportWidth();

			this.staticCart = $('summary-bottom');
			this.botFunctions = $('functions-controls-bottom');

			if (this.windowWidthTmp != this.windowWidth) {
				bDoDimChange = true;
				bShowSummaryCart = false;
				this.windowWidth = this.windowWidthTmp;

				var theFloater = $('floating-cart');

				if (this.windowWidth >= 1000 && this.windowWidth <= 1024) {
					this.floater.show();
					this.staticCart.style.display = 'none';
					this.botFunctions.firstDescendant().className = "botFunctionsRel";
					this.overallContainer.className = "overallContainerSmall";
					theFloater.setStyle({
						width: '210px'
					});
				} else if (this.windowWidth > 1024 && this.windowWidth < 1259) {
					this.floater.show();
					this.staticCart.style.display = 'none';
					this.botFunctions.firstDescendant().className = "botFunctionsRel";
					this.overallContainer.className = "overallContainerMid";
					theFloater.setStyle({
						width: '230px'
					});
				} else if (this.windowWidth < 1000) {
					bShowSummaryCart = true;
					this.floater.hide();
					this.staticCart.style.display = 'block';
					this.botFunctions.firstDescendant().className = "botFunctionsAbs";
					this.overallContainer.className = "overallContainerTiny";
					theFloater.setStyle({
						width: '230px'
					});
				} else {
					this.floater.show();
					this.staticCart.style.display = 'none';
					this.botFunctions.firstDescendant().className = "botFunctionsRel";
					this.overallContainer.className = "overallContainerLarge";
					theFloater.setStyle({
						width: '230px'
					});
				}
			}
			
			if (bDoDimChange) {
				bDoDimChange = false;
				alignFloatLayers();
				displayOverflow();
			}
		}
	});
	
	var checker = new sizeChecker( floater );
	setTimeout(function(){checker.startInt();}, 1500);
});

function updateCompListSize() {
	var delay = doDelay( 2000 );
	
	if (delay) {
		displayOverflow();
	}
}

function doDelay( time ) {
	var date = new Date();
	var curDate = null;
	var diff;
	
	do { 
		curDate = new Date();
		diff = curDate - date
	} while( curDate - date < time );
	
	if ( diff >= time ) {
		return true;
	} else {
		return false;
	}
}