if(!window.modal) {
	var modal = {
		size : {
			width : null,
			height : null
		},
		position : {
			top : null,
			left : null
		},
		background : {
			color : null,
			opacity : null
		},
		HTML : null,
		show : function(options) {
			this.setPosition();
			var pu_ContainerObj = document.getElementById('pup_container');
			pu_ContainerObj.style.display = 'block';
		},
		close : function() {
			var pu_ContainerObj = document.getElementById('pup_container');
			pu_ContainerObj.style.display = 'none';
		},
		setPosition : function(options) {
			if(options && options != 'undefined') {
				this.position.top = (options['top'])?options['top']:'middle';
				this.position.left = (options['left'])?options['left']:'middle';
			}
			
			pu_windowObj = document.getElementById('pu_window');
			
			var theTop, theLeft;
			
			theTop = (this.position.top == 'middle')?((c_page.getSize().h > c_page.getSize(pu_windowObj).h)?((c_page.getSize().h - c_page.getSize(pu_windowObj).h) / 2):0) + 'px':this.position.top;
			theLeft = (this.position.left == 'middle')?((c_page.getSize().w > c_page.getSize(pu_windowObj).w)?((c_page.getSize().w - c_page.getSize(pu_windowObj).w) / 2):0) + 'px':this.position.left;
			
			pu_windowObj.style.top = theTop;
			pu_windowObj.style.left = theLeft;
		},
		setSize : function(options) {
			if(options && options != 'undefined') {
				this.size.width = (options['width'])?options['width']:'auto';
				this.size.height = (options['height'])?options['height']:'auto';
			}
			
			pu_windowObj = document.getElementById('pu_window');
			
			pu_windowObj.style.width = this.size.width;
			pu_windowObj.style.height = this.size.height;
		},
		setBackground : function(options) {
			if(options && options != 'undefined') {
				this.background.color = (options['bgColor'])?options['bgColor']:'transparent';
				this.background.opacity = (options['bgOpacity'])?options['bgOpacity']:0;
			}
			pu_backgroundObj = document.getElementById('pup_background');
			
			pu_backgroundObj.style.background = this.background.color;
			if(!document.all) {
				pu_backgroundObj.style.opacity = this.background.opacity;
			} else {
				pu_backgroundObj.style.filter = 'alpha(opacity = ' + (this.background.opacity * 100) + ')';
			}
		},
		setContent : function(cHtml) {
			this.HTML = (cHtml)?cHtml:'';
			
			var pu_windowInnerObj = document.getElementById('pu_inner');
			if(pu_windowInnerObj) {
				pu_windowInnerObj.innerHTML = this.HTML;
			}
		},
		_createLayout : function() {
			// Obtain the body to put the popup
			var bodyTag = document.getElementsByTagName('body');
			bodyTag = (bodyTag)?bodyTag[0]:null;

			if(!bodyTag) {
				setTimeout(this._createLayout, 200);
				return false;
			}

			// If pup_container don't exist, then I crate that
			if(!document.getElementById('pup_container')) {
				// Create the popup container and put it in body
				var pu_Container = '<div class="pup_container" id="pup_container"></div>';
				//bodyTag.innerHTML += pu_Container;
				jQuery(bodyTag).append(pu_Container);
			}
			
			// Set the pup container attributes
			var pu_ContainerObj = document.getElementById('pup_container');
			pu_ContainerObj.style.display = 'none';
			
			pu_ContainerObj.style.position = (document.all)?'absolute':'fixed';
			
			pu_ContainerObj.style.width = '100%';
			pu_ContainerObj.style.height = '100%';
			
			pu_ContainerObj.style.top = '0';
			pu_ContainerObj.style.left = '0';
			pu_ContainerObj.style.zIndex = '99999999';
			
			// If pup_background don't exist, then I crate that
			if(!document.getElementById('pup_background')) {
				// Create the modal background
				var pu_background = '<div class="pup_background" id="pup_background"></div>';
				pu_ContainerObj.innerHTML += pu_background;
			}
			
			var pu_backgroundObj = document.getElementById('pup_background');
			pu_backgroundObj.style.position = 'absolute';
			
			pu_backgroundObj.style.width = '100%';
			pu_backgroundObj.style.height = '100%';
			
			pu_backgroundObj.style.top = '0';
			pu_backgroundObj.style.left = '0';
			
			if(!document.getElementById('pu_window')) {
				// Create the modal window
				var pu_window = '<div class="pu_window" id="pu_window"><div class="pu_inner" id="pu_inner"></div>';
				pu_ContainerObj.innerHTML += pu_window;
			}
			var pu_windowObj = document.getElementById('pu_window');
			
			pu_windowObj.style.position = 'absolute';
		},
		ie_onScroll : function() {
			var pu_ContainerObj = document.getElementById('pup_container');
			if(!pu_ContainerObj) return;
			
			pu_ContainerObj.style.top = c_page.getScrollPosition().t;
		},
		init : function(options) {
			this._createLayout();
			
			if(options && options != 'undefined') {
				this.setSize(options);
				this.setPosition(options);
				this.setBackground(options);
				this.setContent((options['HTML'])?options['HTML']:'');
			}
			
			var _this = this;
			var pu_backgroundObj = document.getElementById('pup_background');
			
			// Set events
			if(document.all) {
				window.attachEvent('onscroll',_this.ie_onScroll);
				window.attachEvent('onresize',function() {
					_this.setPosition();
				});
				pu_backgroundObj.attachEvent('onclick',_this.close);
			} else {
				window.addEventListener('resize',function() {
					_this.setPosition();
				},true);
				pu_backgroundObj.addEventListener('click',_this.close,false);
			}
		}
	}
}
////////////////////////////////////////////////////////////////////////////////////
// Add to cart via ajax function
function dinamicAddToCart(itemId, qty, itOptions, couponC) {
	itemId = itemId.replace('addtocart','');
	var req = AJAXObject();
	var inputItem = document.getElementById('item' + itemId + 'qty');
	
	req.onreadystatechange = function() {
		if(req.readyState  == 4) {
			if(req.responseText.indexOf('Unavailable Options') == -1) {
				if(couponC) {
					setCookie('sc_promocode', '15OFF');
				}
				showModal('<div class="innerPopup"><a class="ico_closeButton" onclick="modal.close()" href="javascript:;">Close</a><h2 class="title">A New Item(s) Has Been Added<br/>To Your Shopping Cart</h2><div class="popupContent"><img width="498" height="62" src="/site/images/popup_content.gif"/></div><div class="popupButtons"><span class="btn_continueShopping"><a onclick="document.location.href = document.location.href;" class="c_button" href="javascript:;">Continue Shopping</a></span><span class="btn_proceedToCheckout"><a class="c_button" href="/s.nl/c.436469/sc.3/.f">Proceed to Checkout</a></span></div></div>');
			} else {
				showModal('Unavailable options for this item.<br/><a href="javascript:;" onclick="showAddedItemsPopup()">Accept</a>');
			}
		} else {
			showModal('Adding item...');
		}
	}
	req.open('GET','/app/site/backend/additemtocart.nl?buyid=' + itemId + '&qty=' + qty + '&c=436469' + ((couponC)?'&promocode=15OFF':'') + ((itOptions)?'&' + itOptions:''));
	if(document.all) {
		req.send();
	} else {
		req.send(null);
	}
}

function showModal(html) {
	modal.setContent(html);
	modal.show();
}

function startModalComponent() {
	// Usage
	modal.init({
		//'width' : '200px',
		//'height' : '200px',
		'top' : 'middle',
		'left' : 'middle',
		'bgColor' : 'white',
		'bgOpacity' : 0.75
	});

	// Links to add behaviour
	/*var addToCartBtns = _getElementsByClass('addtocart');

	for(var inButton in addToCartBtns) {
		if(document.all) {
			addToCartBtns[inButton].attachEvent('onclick', function() {
				dinamicAddToCart(this.id);
			});
			jQuery(addToCartBtns[inButton]).removeAttr('onclick');
		} else {
			addToCartBtns[inButton].addEventListener('click', function() {
				dinamicAddToCart(this.id);
			}, true);
		}
	}*/
}
/* 
if(document.all) {
	window.attachEvent('onload',function() {
		startModalComponent();
	});
} else {
	window.addEventListener('load',function() {
		startModalComponent();
	}, true);
} */