window.addEvent('domready', function() {
	
	function setupAddToBaskets() {
		// add the paggy stuff 
		var buttons = $$('div.btnBuy a');

		buttons.each(function(item) {
				item.addEvent('click', function() {
					addToBasket(item.get('href'));
					return false;
				});
		});
	}

	function addToBasket(href){
		href = href + '/js';
		$('minibasket').addClass('ajaxloading');
		new Request({
					url:href,
					onSuccess: function(responseText, responseHTML) {
						$('minibasket').removeClass('ajaxloading');
						$('minibasket').empty();
						$('minibasket').set('html', responseText);
					}
			}).send();
		
		makeConfirmLayer();
	}

	setupAddToBaskets();
	
	function makeConfirmLayer(){
		
		var w = 212;
		var h = 158;
		var divIdName = 'basketLayer';
		//check if object already exists
		var tObj = document.getElementById(divIdName);
		if (tObj) {
			closeMe(divIdName);
		}
		var ni = document.getElementById('content');
		var newdiv = document.createElement('div');
		var content = "<div>";
		content += '<h5>Item added to basket</h5>';
		content += '<p><a href="#" id="continueshopping">Continue Shopping</a>';
		content += ' <br/><br/> or <br/><br/>';
		content += '<a href="/retail/view-basket">View your basket</a></p>';
		content += "</div>";
		newdiv.setAttribute('id',divIdName);
		newdiv.innerHTML = content;
		newdiv.style.position = "absolute";
		ni.appendChild(newdiv);
		positionMe(divIdName, w, h);

		$('continueshopping').addEvent('click', function() {
			document.getElementById("content").removeChild(newdiv);
			return false;
		});
	}
	
	function positionMe(id, width, height){
		width = width / 2;
		height = height * 1.5;
		var IE = document.all?true:false;
		if(navigator.userAgent.indexOf('Netscape/7.0') != -1) var ns7 = true;
		layer = document.getElementById(id);
		
		if(IE)
		{
			//fix for IE6 appearing above form elements and looking crap
			var fix = 0;
			// Split the string into part [0] and part [1]
			temp = navigator.appVersion.split('MSIE');
			ieVer = parseInt(temp[1]);
			// Is it greater than 6?
			if(ieVer = 6){
				var p = document.body.parentNode.scrollTop;
				if(p < 200){
					fix = (200 - p) + 10;
				}
			}
			layer.style.top = document.body.parentNode.scrollTop + height + fix;
			layer.style.left = ( document.documentElement.scrollWidth / 2 ) - width;
		}
		else
		{
			var newTop = document.body.parentNode.scrollTop + height;
			var newLeft = ( document.body.parentNode.clientWidth / 2 ) - width;
			newTop += 'px';
			newLeft += 'px';
			layer.style.top = newTop;
			layer.style.left = newLeft;
		}
		
		if(ns7)
		{			
			sNetscapeLeft = (window.innerWidth / 2) - witdth + 'px';
			sNetscapeTop = window.pageYOffset + height + 'px';
			layer.style.left = sNetscapeLeft;
			layer.style.top = sNetscapeTop;
		}
		
		if(navigator.userAgent.indexOf('MSIE 5.5') != -1) 
		{
			sIE5Left = (document.body.clientWidth / 2) - width + 'px';
			layer.style.left = sIE5Left;
		}
	}

	function closeMe(id){
		var holder = document.getElementById("content");
		var olddiv = document.getElementById(id);
		holder.removeChild(olddiv);
	}
	
});