var BuyPopUp = Class.create({
	
	initialize: function() {		
		this.updateBuyPopUpList();
		this.insertHTML();		
	},

	insertHTML: function() {
		var objBody = $$('body')[0];
		if(objBody){
	    objBody.appendChild(Builder.node('div', {id:'buyPopUpOverlay'}));
	    objBody.appendChild(Builder.node('div', {id:'buyPopUp'}, [
		    Builder.node('div', {id:'buyPopUpInner'}, [
			    Builder.node('div', {id:'buyPopUpTop'}),
			    Builder.node('div', {id:'buyPopUpBottom'}, [
				    Builder.node('div', {id:'buyPopUpBottomLeft'}, [
					    Builder.node('span', {id:'buyPopUpBottomLeftSpan'}, 'Continue Shopping')
				    ]),
				    Builder.node('div', {id:'buyPopUpBottomRight'}, [
				      Builder.node('a', {href:'/zpage/basket.htm'}, 'Checkout')
				    ])			
			    ])
	      ])
	    ]));
		
		var th = this;
    (function(){
      var ids = 'buyPopUpOverlay buyPopUp buyPopUpTop buyPopUpBottomLeft buyPopUpBottomRight buyPopUpBottomLeftSpan buyPopUpBottomRightSpan';   
      $w(ids).each(function(id){ th[id] = $(id); });
    }).defer();
  
    $('buyPopUpOverlay').hide();
		$('buyPopUp').hide()
    $('buyPopUpBottomLeftSpan').observe('click', (function() { this.close(); }).bind(this));
	}  },
	
	updateBuyPopUpList: function() {
		document.observe('click', (function(event){
			var target = event.findElement('input[id^=addToBagItem]');
			if (target) {
				event.stop();
				this.buy('item');	
			}
		}).bind(this));
		document.observe('click', (function(event){
			var target = event.findElement('input[id^=addToBagSuite]');
			if (target) {
				event.stop();
				this.buy('suite');	
			}
		}).bind(this));
	},
	
	buy: function(type) {
		var quantity = '1';
		var url = '/add_to_basket.xml';	
		new Ajax.Request(url, {
			method: 'post',
			parameters: {zaction:type, zquantity:quantity},
			onSuccess: function(transport) {
				var response = $(transport.responseXML);
				if (response) {
					var root = response.getElementsByTagName('addtobasket')[0];
					var itemString = 'items';
					if (root.getElementsByTagName('items')[0].lastChild.nodeValue == 1) {
						itemString = 'item';
					}
					$('buyPopUpTop').update('<p>You have added this item.</p><p>You have ' + root.getElementsByTagName('items')[0].lastChild.nodeValue + ' ' + itemString + ' in your basket.</p>');
					$('headerBagItems').innerHTML  = root.getElementsByTagName('items')[0].lastChild.nodeValue + " ITEMS - " + root.getElementsByTagName('total')[0].lastChild.nodeValue;
					new Effect.Appear($('buyPopUpOverlay'), { duration: 0, from: 0.0, to: 0.7 });
         	$('buyPopUp').show();
        }					
			}	
		});
		this.start();
	},
		
	start: function() {
		var arrayPageSize   = this.getPageSize();
		var arrayPageScroll = document.viewport.getScrollOffsets();
		var buyPopUpTop     = arrayPageScroll[1] + (document.viewport.getHeight() / 5);
		var buyPopUpLeft    = arrayPageScroll[0];
		
		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });
		
		this.buyPopUpOverlay.setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });		
		this.buyPopUp.setStyle({ top: buyPopUpTop + 'px', left: buyPopUpLeft + 'px' });
	},
	
	close: function() {
		this.buyPopUpOverlay.hide();
		this.buyPopUp.hide();
		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
	},
	
	getPageSize: function () {
		var xScroll, yScroll, windowWidth, windowHeight;		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else {
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}			
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		return [pageWidth,pageHeight];
	}	
});

document.observe('dom:loaded', function () { new BuyPopUp(); });