/**** COMMENT OUT AS CAUSING PROBLEMS IN IE6
if (document.location.protocol == "http:") { 
  document.write('<script type="text/javascript" src="http://s7.addthis.com/js/152/addthis_widget.js"></script>\n');
} 
***/

	var popupWindow = new Object();
	var popupState	= 'closed';
	var popupActive	= '';
	var popupLast	= '';
	var currentTotalHeight	= 0;
	var currentTotalWidth	= 0;
	var xOffset	= 0;
	var yOffset	= 0;
	var contentTop = '';
	
/***** GET THE FUNCTIONS STARTED *****/

	popupWindow.Start = function(popupId,xOffset,yOffset) {
		var popupId = arguments[0];

		//K3BTG START
		if (!(popupId == '' || popupId == null)) {
		  showContent(popupId);
		}
		//K3BTG END
		
	// Activate the popup
		popupWindow.Open(popupId,xOffset,yOffset);
	
	// Record the current total height of the window
		currentTotalHeight	= getTotalHeight();
		currentTotalWidth	= getTotalWidth();
	
	// On resizing the page, run this function to reset the values
		Event.observe(window, 'resize', function() { popupWindow.Change(); });
		Event.observe(window, 'scroll', function() { popupWindow.Centering(); });
	
	// Blur all links on the page
		var linkList = $$('a');
		for(var i = 0; i < linkList.length; i++) {
			linkList[i].blur();
		}
	}


/***** VERTICALLY ALIGN CENTRE THE CONTENT *****/

	popupWindow.contentPos = function() {
		
		if(popupActive) {
			$(popupActive).show();
		}

	// How far down the content area needs to go
		var windowHeightHalf	= (getWindowHeight() / 2);
		var contentHeightHalf	= (getOffsetHeight($('popup_content')) / 2);
	
	// How far across the content area needs to go
		var windowWidthHalf		= (getWindowWidth() / 2);
		var contentWidthHalf	= (getOffsetWidth($('popup_content')) / 2);
		
	// Set the values in the CSS
		
		if(contentTop == '') {	
			$('popup_content').style.top = (windowHeightHalf - contentHeightHalf + yOffset + getScrollTop()) + 'px';
			contentTop = (windowHeightHalf - contentHeightHalf + yOffset + getScrollTop()) + 'px';
		} else {
			$('popup_content').style.top = contentTop;
		}
		$('popup_content').style.left = (windowWidthHalf - contentWidthHalf + xOffset) + 'px';
		
	// Main elements
		var top 	= $$('#popup_content .top')[0];
		var left 	= $$('#popup_content .left')[0];
		var bottom 	= $$('#popup_content .bottom')[0];
		var right	= $$('#popup_content .right')[0];

		var tl 	= $$('#popup_content .tl')[0];
		var tr 	= $$('#popup_content .tr')[0];
		var bl 	= $$('#popup_content .bl')[0];
		var br	= $$('#popup_content .br')[0];
		
		top.style.width 	= getOffsetWidth($(popupActive)) + 'px';
		bottom.style.width	= getOffsetWidth($(popupActive)) + 'px';
		left.style.height 	= getOffsetHeight($(popupActive)) + 'px';
		right.style.height	= getOffsetHeight($(popupActive)) + 'px';
	}

/***** OPEN / CLOSE THE POPUP WINDOW *****/

	popupWindow.Open = function(popupId,x,y) {
		xOffset	= x;
		yOffset	= y;

		if(popupState == 'closed' && $(popupId)) {
			popupState = 'transition';

		// Reveal the outermost area of the popup
			$('popup_area').show();
			$('popup_area').style.width		= '100%';
			$('popup_area').style.height	= getTotalHeight() + 'px';
			
			if(popupActive != '') {
				appearTime = 0.1;
			} else {
				appearTime = 0.3;
			}
			
			popupLast	= popupActive;
			popupActive = popupId;
			
		// Begin to show the faded popup background
			if($('popup_bg')) {
				new Effect.Appear('popup_bg', { beforeStart: function() {
				
				// Set the background width and height
					$('popup_bg').style.width	= '100%';
					$('popup_bg').style.height	= getTotalHeight() + 'px';
				},
				duration: appearTime, from: 0, to: 0.40, afterFinish: function() {
					new Effect.Appear('popup_content', { beforeStart: function() {
					// Centre the content area in the window
					// Timeout allows the element a chance to activate, otherwise positioning would be impossible
						setTimeout("popupWindow.contentPos()", 50);
					},
					duration: appearTime, from: 0, to: 1, afterFinish: function() {
						popupState = 'open';
						
					// If anywhere outside of the content area is clicked, close the popup
						Event.observe($('popup_bg'), 'click', popupWindow.Close);
						Event.observe($('popup_area'), 'click', popupWindow.Close);
					} })		 
				} });
			} else {
				new Effect.Appear('popup_content', { beforeStart: function() {
				// Timeout allows the element a chance to activate, otherwise positioning would be impossible
					setTimeout('popupWindow.contentPos()', 50);
				},
				duration: appearTime, from: 0, to: 1, afterFinish: function() {
					popupState = 'open';
					
				// If anywhere outside of the content area is clicked, close the popup
					Event.observe($('popup_bg'), 'click', popupWindow.Close);
				} })	
			}
		} else if($(popupActive)) {
			popupState = 'transition';
			
			if(popupId) {
				fadeTime = 0.1;
			} else {
				fadeTime = 0.3;
			}
			
		// Firstly fade the content
			new Effect.Fade('popup_content', { duration: fadeTime, from: 1, to: 0, afterFinish: function() {
				$(popupActive).hide();
				contentTop = '';
				if($('popup_bg')) {
				// Then fade the background
					new Effect.Fade('popup_bg', { duration: fadeTime, from: 0.40, to: 0, afterFinish: function() {
						$('popup_area').hide();
						
						popupState = 'closed';
						if(popupId) {
							if(popupId == 'back' && popupLast != '') {
								popupId = popupLast;
							}
							popupWindow.Open(popupId,xOffset,yOffset);
						} else {
							popupActive = '';
						}
					}})
				} else {
					$('popup_area').hide();
					popupState = 'closed';
					if(popupId) {
						if(popupId == 'back' && popupLast != '') {
							popupId = popupLast;
						}
						popupWindow.Open(popupId,xOffset,yOffset);
					} else {
						popupActive = '';
					}
				}
			}})
		}
	}

/***** CLOSE THE POPUP IF CLICKED OUTSIDE THE CONTENT AREA *****/

	popupWindow.Close = function(e) {
		var theElement 	= Event.element(e);

	// Rerun the main function to close the popup only if the background is clicked
		if(theElement.id == 'popup_bg' && popupState != 'transition') {
			popupWindow.Open();
		}
	}


/***** RESET VALUES WHEN THE PAGE RESIZES OR CHANGES *****/

	popupWindow.Centering = function() {
	// How far down the content area needs to go
		var windowHeightHalf	= (getWindowHeight() / 2);
		var contentHeightHalf	= (getOffsetHeight($('popup_content')) / 2);
	
	// How far across the content area needs to go
		var windowWidthHalf		= (getWindowWidth() / 2);
		var contentWidthHalf	= (getOffsetWidth($('popup_content')) / 2);
		
		if(yOffset == 0) {
			$('popup_content').style.top = (windowHeightHalf - contentHeightHalf + yOffset + getScrollTop()) + 'px';
		}
	}


/***** RESET VALUES WHEN THE PAGE RESIZES OR CHANGES *****/

	popupWindow.Change = function() {
	
	// Reset window height settings
		setTimeout('popupWindow.ChangeHeight()', 50);
	}
	popupWindow.ChangeHeight = function() {
	
	// Reset height values
		if(currentTotalHeight > getWindowHeight()) {
			$('popup_bg').style.height		= getTotalHeight() + 'px';
			$('popup_area').style.height	= getTotalHeight() + 'px';
		} else {
			$('popup_bg').style.height		= getWindowHeight() + 'px';
			$('popup_area').style.height	= getWindowHeight() + 'px';
		}

	// Reset window width settings
		setTimeout('popupWindow.ChangeWidth()', 100);	
	}
	popupWindow.ChangeWidth = function() {
	
	// Reset width values
		$('popup_bg').style.width	= '100%';
		$('popup_area').style.width	= '100%';
	
	// Reset content area position
		popupWindow.contentPos();
	}
	
/*** K3BTG - To get content of pop-up by an ajax call ***/
var xmlhttp;
var contentdiv;
var inh3tag;

function showContent(contentid) {

  var urlparam;
  
  inh3tag = false;
  contentdiv = contentid
  
  //tempstring = contentid
  var tempstring = contentid.replace(new RegExp("[0-9]"), "");
  
  switch(tempstring) {
    case 'pop_delivery-returns' : urlparam = 'popdeliveryreturns'; break;
    case 'pop_security-code' : urlparam = 'popsecuritycode'; break;
    case 'pop_privacy-policy' : urlparam = 'popprivacypolicy'; break;
    case 'pop_sizing-info' : urlparam = 'popsizinginfo'; inh3tag = true; break;
    case 'pop_sizing-infopanel' : urlparam = 'popsizinginfo'; inh3tag = true; break;
    case 'pop_add-to-wish-list-confirm_registered' : urlparam =  'popaddtowishlistconfirmregistered'; break;
    case 'pop_forgotten-password-confirm' : urlparam = 'forgottenpasswordconfirm'; break;
    case 'pop_add-to-wish-list-logged-confirm' : urlparam = 'popaddtowishlistloggedconfirm'; break;
    case 'pop_add-to-wish-list-not-logged-confirm' : urlparam = 'popaddtowishlistnotloggedconfirm'; break;
    case 'popup_signed_up' : urlparam = 'popupsignedup'; break;
    case 'popup_signed_error' : urlparam = 'popupsignederror'; break;
    case 'pop_forgotten-password-confirm' : urlparam = 'popforgottenpasswordconfirm'; break;
    case 'pop_forgotten-password-error' : urlparam = 'popforgottenpassworderror'; break;
    case 'pop_forgotten-password-error-format' : urlparam = 'popforgottenpassworderrorformat'; break;
    case 'pop_send-to-a-friend-confirm' : urlparam = 'popsendtoafriendconfirm'; break;
    case 'pop_share-bookmark' : urlparam = 'popsharebookmark'; break;
    case 'pop_size-not-available' : urlparam = 'popsizenotavailable'; break;
    case 'drop_down_message' : urlparam = 'dropdownmessage'; break;
    case 'samplerequest_confirm_send' : urlparam = 'samplerequestconfirmsend'; break;
    case 'pop_terms-and-conditions' : urlparam = 'poptermsandconditions'; break;
    case 'select_size' : urlparam = 'selectsize'; break;
    case 'select_colour' : urlparam = 'selectcolour'; break;
    case 'no_items_added' : urlparam = 'noitemsadded'; break;
    default : return false;
  }
  
  xmlhttp=GetXmlHttpObject();
  if (xmlhttp==null) {
    alert ("Your browser does not support XMLHTTP!");
    return false;
  }
  
  var url="sitedata.aspx?content=" + urlparam; 
  xmlhttp.onreadystatechange=stateChanged;
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
}

function stateChanged() {
  if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") {
    if (!inh3tag)  {
        $(contentdiv).innerHTML = xmlhttp.responseText;
    } else {
        $(contentdiv).getElementsByTagName('h3')[0].innerHTML = xmlhttp.responseText;
    }
  }
}

function GetXmlHttpObject() {
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
  }
  if (window.ActiveXObject) {
    // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
  
  return null;
}
