
// Used for changing the bg of the form.
var lastFormSection;

// Used for tracking if we need to tidy up shopping and gallery items.
var lastShopper, lastGallery;

var check;
var myclickedObject;
var galleryState;
var fbPopupTimer;

function openDesk() {
  $('lbDesk').setStyle('z-index', '777');
  $('lbDesk').setStyle('display', 'block');
}

function displayRegister() {
var ga = pageTracker._trackEvent('Registration', 'DisplayedForm');
  openDesk();
  $('lbContainer').setStyles({
filter: 'alpha(opacity=0)',
'-moz-opacity': 0,
'-khtml-opacity': 0,
opacity: 0
});
$('lbContainer').setStyle('display', 'block');
$('brochure').setStyle('display','none');
$('shopper').setStyle('display','none');
$('register').setStyle('display','block');
$('galleryViewer').setStyle('display','none');
$('lbContainer').setStyle('z-index', '999');
lbPosition();
$('lbContainer').fade(1);
}

function displayBrochure() {
var ga = pageTracker._trackEvent('Brochure', 'DisplayedForm');
  openDesk();
  $('lbContainer').setStyles({
filter: 'alpha(opacity=0)',
'-moz-opacity': 0,
'-khtml-opacity': 0,
opacity: 0
});
$('lbContainer').setStyle('display', 'block');
$('brochure').setStyle('display','block');
$('shopper').setStyle('display','none');
$('register').setStyle('display','none');
$('galleryViewer').setStyle('display','none');
$('lbContainer').setStyle('z-index', '999');
lbPosition();
$('lbContainer').fade(1);
}

function displayShopper(shopperID) {
  lastShopper = 'shopperItem' + shopperID;
  $(lastShopper).setStyle('display', 'block');
  openDesk();
  $('lbContainer').setStyles({
filter: 'alpha(opacity=0)',
'-moz-opacity': 0,
'-khtml-opacity': 0,
opacity: 0
});
$('lbContainer').setStyle('display', 'block');
$('brochure').setStyle('display','none');
$('shopper').setStyle('display','block');
$('register').setStyle('display','none');
$('galleryViewer').setStyle('display','none');
$('lbContainer').setStyle('z-index', '999');
lbPosition();
$('lbContainer').fade(1);
}

function displayGallery(galleryID) {
  var galSection = 'galleryItemImage' + galleryID;
  var imageURL = $(galSection).getStyle('background-image');
  $(galSection).setStyle('background-image', 'none');
  $(galSection).setStyle('background-image', imageURL);
  $(galSection).setStyle('background-repeat', 'no-repeat');
  if (lastGallery != undefined) {
    $(lastGallery).setStyle('display', 'none');
  }
  lastGallery = 'galleryItem' + galleryID;
  $(lastGallery).setStyle('display', 'block');
  openDesk();
  galleryState = 1;
  $('lbContainer').setStyles({
filter: 'alpha(opacity=0)',
'-moz-opacity': 0,
'-khtml-opacity': 0,
opacity: 0
});
$('lbContainer').setStyle('display', 'block');
$('brochure').setStyle('display','none');
$('register').setStyle('display','none');
$('shopper').setStyle('display','none');
$('galleryViewer').setStyle('display','block');
$('lbContainer').setStyle('z-index', '999');
lbPosition();
$('lbContainer').fade(1);
}

function closeDesk() {
  $('registerAnswer').set('html','');
  if (lastShopper != undefined) {
    $(lastShopper).setStyle('display', 'none');
  }
  if (lastGallery != undefined) {
    $(lastGallery).setStyle('display', 'none');
  }
  if (lastFormSection != null) {
    fadeFormSection(lastFormSection);
  }
  $('lbContainer').setStyles({
	filter: 'alpha(opacity=0)',
	'-moz-opacity': 0,
	'-khtml-opacity': 0,
	opacity: 0
	});
	$('lbDesk').setStyle('display', 'none');
	if( null != $$('div.fc-tbx')) {
		$$('div.fc-tbx').setStyle('display', 'none');
	}
}

function fullcloseDesk () {
	closeDesk();
	galleryState = 0;
}

function lbPosition() {
  var scroll = window.getScroll(), size = window.getSize(), lbSize = $('lbContainer').getSize();
  $('lbContainer').setStyle("left", scroll.x + (size.x / 2) - (lbSize.x / 2) );
  if ( lbSize.y < size.y ) {
    $('lbContainer').setStyle("top", scroll.y + (size.y / 2) - (lbSize.y / 2) );
  }else{
    $('lbContainer').setStyle("top", scroll.y);
  }
}

function fadeFormSection(section) {
  $(section).setStyle('background-color', '#ffffff');
}

function hilightFormSection(section) {
  if (lastFormSection != null) {
    fadeFormSection(lastFormSection);
  }
  $(section).setStyle('background-color', '#e2e2e2');
  lastFormSection = section;
}

function userLogon() {
  if ( userLogged != null) {
    // if the userlogged varable is set, then this is mostly a test.
    // so lets ensure we're showing the correct logout option, and return
    // true;
    $('nav_login').setStyle('display','none');
    $('nav_logoff').setStyle('display','inline');
  }else{
    displayRegister();
  }
}

function userLogoff() {
  var myHTMLRequest = new Request(
    {url:'/logout/',
onSuccess: function() {
  userLogged = null;
  userLightboxs = null;
  $('nav_login').setStyle('display','inline');
  $('nav_logoff').setStyle('display','none');
  $('greetname').set('html', 'Guest');
  Cufon.replace('h6', { fontFamily: 'Futura Std' });
  // We also kill the Lighter as this insures we don't have any garge left
  // should someone new login.
  // create the lighter if it doesn't exist yet.
  if ( $('lighter') != null ) {
    $('lighter').destroy;
  } 
  checkToRefresh();
},
onFailure: function() {
alert('Sorry, we are unable to process your request to log out at this point.');
}
});
 myHTMLRequest.post({'user_id:': userLogged});
}

function createLighter() {

  // Build elements first.. 
  var htmlLighter = new Element('div', {
    'id': 'lighter',
    'styles': {
    'background': '#fff',
    'position': 'absolute',
    'border': '3px solid #8D291B',
    'display': 'none',
	'padding' : '5px',
	'width' : '290px',
	'padding-right': '0px',
	'padding-top': '0px'
    }
	});
  var htmlLighterClose = new Element('div', {
    'id': 'lbClose',
    'styles': {
    'text-align': 'right'
    }
	});
  var htmlLighterCloseLink = new Element('a', {
	'href': 'javascript:closeLB();'
    }
    ); 
  var htmlLighterCloseImage = new Element('img', {
	'src': '/images/icon_close.png'
    });
  
  var htmlLighterForm = new Element('form', {
	'action': '/addtolightbox.php',
	'method': 'post',
	'name': 'lbForm',
	'id': 'lbForm'
	});  
  
  var htmlLighterHeader = new Element('h3', {
    'html': 'Add to Lightbox'
    }
    );
  var htmlLighterSelect = new Element('select', {
    'name': 'lightboxid',
    'id': 'lightboxOptions',
	'styles': {
	'width':'250px'
	}
    }
    );
  var htmlLighterPicID = new Element('input', {
    'type': 'hidden',
    'id': 'lightPID',
    'name': 'PhotoID'
    }
    );
  var htmlLighterCreateLink = new Element('a', {
    'html': 'Create new Lightbox',
	'id': 'v_toggle',
	'href': '#'
    }
    ); 
  
  
  var htmlLighterCreate = new Element('div', {
    'id': 'lbCreate'
	});
  
  var htmlLighterNameLabel = new Element('label', {
		'html': 'Name',							 
		'for':'lbName'							
	}
	);
  var htmlLighterName = new Element('input', {
    'type': 'text',
	'name':'lbName',
	'id': 'lbName',
	'styles': {
	'width':'250px'
	}
    }
	);
  var htmlLighterDescLabel = new Element('label', {
		'html': 'Description',							 
		'for':'lbDesc'						
	}
	);
  var htmlLighterDesc = new Element('input', {
    'type': 'text',
	'name':'lbDesc',
	'id':'lbDesc',
	'styles': {
	'width':'250px'
	}
    }
	);
  var htmlLighterButton = new Element('input', {
    'type': 'submit',
    'value': 'Add',
	'name': 'submit',
	'class': 'form_button'
    }
    );

  // Now lets build it all together.
  var el = $('page');
  htmlLighterClose.inject(htmlLighter);
  htmlLighterCloseLink.inject(htmlLighterClose);
  
  
  htmlLighterCloseImage.inject(htmlLighterCloseLink);
  htmlLighterForm.inject(htmlLighter);
  
  htmlLighterHeader.inject(htmlLighterForm);
  htmlLighterSelect.inject(htmlLighterForm);
  htmlLighterPicID.inject(htmlLighterForm);
  
  var htmlLighterBreak = new Element('br');
  htmlLighterBreak.inject(htmlLighterForm);  
  htmlLighterCreateLink.inject(htmlLighterForm);
  
  var htmlLighterBreak = new Element('br');
  htmlLighterBreak.inject(htmlLighterForm);
  
  htmlLighterCreate.inject(htmlLighterForm);
 
  
  htmlLighterNameLabel.inject(htmlLighterCreate);
  var htmlLighterBreak = new Element('br');
  htmlLighterBreak.inject(htmlLighterCreate);
  
  htmlLighterName.inject(htmlLighterCreate);
  var htmlLighterBreak = new Element('br');
  htmlLighterBreak.inject(htmlLighterCreate);

  
  htmlLighterDescLabel.inject(htmlLighterCreate);
  var htmlLighterBreak = new Element('br');
  htmlLighterBreak.inject(htmlLighterCreate);
  
  htmlLighterDesc.inject(htmlLighterCreate);
  
  var htmlLighterBreak = new Element('br');
  htmlLighterBreak.inject(htmlLighterForm);
  
  htmlLighterButton.inject(htmlLighterForm);
  htmlLighter.inject(el, 'after');	
  
  var myVerticalSlide = new Fx.Slide('lbCreate');
  myVerticalSlide.hide();  
  showlbCreate();
  
  new FormCheck('lbForm');
  
}

function closeLB() {
	$('lighter').setStyle('display','none');
}

function showlbCreate() {
	var myVerticalSlide2 = new Fx.Slide('lbCreate');
	
	$('v_toggle').addEvent('click', function(e){
		e.stop();
		
		myVerticalSlide2.toggle();
	});
}


function addToLightbox(imageID) {
  // create the lighter if it doesn't exist yet.
  if ( $('lighter') == null ) {
    createLighter();
  }
  
  myclickedObject = 'lighterTag'+imageID;

  var lbHider = new Fx.Slide('lbCreate');
  lbHider.hide();
  // Set the value of the lightPID element to be the ID of the picture.
  $('lightPID').set('value', imageID);
  // All the aboce is done no matter if the user is logged on or not, as
  // the Lighter will needed to be created anyhow, and the lightPID
  // element gives us a nice location to store the ID for the image.
  // if the user isnt' logged in yet then lets make them.
  if ( null == userLogged ) {
    // we need to log the user on first
    $('registerAnswer').set('html', '<p>You must login or register before you can add this item to your lightbox.</p>');
    userLogon(); // We pass the imageID to this can be called again later.
  } else {
    // Fill the lighter with id's of the lightbox's;
    var htmlLighterSelect = $('lightboxOptions');
    htmlLighterSelect.empty();
    for (key in userLightboxs) {
      var tmpEl = new Element('option',
        {
        'value': key,
        'html': userLightboxs[key]
        });
      tmpEl.inject(htmlLighterSelect);
	}
    $('lighter').setStyle("top", 5);
    $('lighter').setStyle("left", 5);
    $('lighter').setStyle("display", 'block');
    $('lighter').setStyle("z-index", '1008');
	// $('lighter').position();
	$('lighter').position({
   	relativeTo: $(myclickedObject),
   	ignoreMargins: true,
   	position: 'bottomLeft',
    edge: 'bottomLeft'
	});
  }
}

// The following code sets the name of the element when something is clicked
// on screen. Used by the lightbox for smart positioning.
var clickedObject;
window.addEvent('domready', function() {

  document.body.onclick= function(e){
  e= window.event || e;
  e.cancelBubble= true;
  if(e.stopPropagation) e.stopPropagation();
  e= e.target || e.srcElement;
  if(e.nodeType!= 1) e= e.parentNode;
  clickedObject = e.id || e.nodeName;
  //alert(clickedObject);
  }
});

function changeMail() {
 if (!document.getElementsByTagName) return false;
 var span = document.getElementsByTagName("span");
 for (var i=0; i < span.length; i++) {
  if (span[i].className.match('email')) {
   var innerspan = span[i].getElementsByTagName("span")[0].firstChild.nodeValue;
   span[i].innerHTML = "<a href='mailto:"+innerspan+"@sauna-spa.co.uk'>"+innerspan+"@sauna-spa.co.uk"
  } 
 }
}

function newWindow () {
	if (!document.getElementsByTagName) return false;
	var links = document.getElementsByTagName("a");
	for (var i=0; i < links.length; i++) {
		if(links[i].className.match('newwin')) {
			links[i].onclick =
			function() {
				window.open(this.href,'help','height=800,width=1000,menubar,scrollbars,resizable,toolbar,directories,status,location');
				return false;
			}
			links[i].title = "Opens in a new window";
		}
	}
}

function popUpMesg(mesg) {
	if ( $('fbPopUp') != null ) {
		$("fbPopUp").dispose();
	}

	if ( $('fbPopUp') == null ) {
		// If this doesn't exist then lets make it.

		// Build elements first.. 
		var htmlPopup = new Element('div', {
			'id': 'fbPopUp',
			'styles': {
				'background': '#fff',
				'position': 'absolute',
				'border': '3px solid #8D291B',
				'display': 'block',
				'padding' : '5px',
				'max-width' : '400px',
				'padding-right': '0px',
				'z-index': '3000',
				'padding-top': '0px'
			}
		});
		var htmlPopupClose = new Element('div', {
			'styles': {
				'text-align': 'right'
			}
		});
		var htmlPopupCloseLink = new Element('a', {
			'href': 'javascript:killPopup();'
		}
	); 
	var htmlPopupCloseImage = new Element('img', {
		'src': '/images/icon_close.png'
	});
	var htmlPopupText = new Element('div', {
		'id': 'fbPopUpText',
		'html': mesg,
		'styles': {
			'padding-left': '8px',
			'padding-right': '8px',
			'padding-top': '8px'
		}
	});


	// Now lets build it all together.
	var el = $('page');
	htmlPopupClose.inject(htmlPopup);
	htmlPopupCloseLink.inject(htmlPopupClose);
	htmlPopupCloseImage.inject(htmlPopupCloseLink);
	htmlPopupText.inject(htmlPopup);
	htmlPopup.inject(el, 'after');
}
$('fbPopUp').position();
if ( fbPopupTimer != null ) {
	$clear(fbPopupTimer);
}
fbPopupTimer = killPopup.delay(6000);
}

function killPopup() {
	if ( $('fbPopUp') != null ) {
		$("fbPopUp").fade(0);
	}
}

function storeLoaded() {
var plend = new Date();
var plload = plend.getTime() - plstart.getTime();
var ga = pageTracker._trackEvent('Response time', 'Page Loaded', window.location, plload);
}

//used to validate forms
window.addEvent('domready', function(){
	new FormCheck('contactForm');   
	new FormCheck('regiseterForm');
	new FormCheck('loginForm');	
	changeMail();
	newWindow();
});

