jQuery(function(){
	initCufon();
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassLight: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
});
function initCufon() {
	Cufon.replace('#header ul.list > li > a', { fontFamily: 'GillSans', hover: true });
	Cufon.replace('ul.menu > li > a, ', { fontFamily: 'GillSans', hover: true });
	Cufon.replace('ul.buttons > li > a', { fontFamily: 'GillSans', hover: true });
	Cufon.replace('.promo h1, .downloads h2, .news .heading h2, #sidebar .box h2, .main-heading h1, .centre h2, .main-heading .head, .find h2, .form-info label, .form-info .btn-submit, .popup h2,.form-info2 label, .form-info2 p', { fontFamily: 'GillSans' });
	Cufon.replace('.promo h1 strong', { fontFamily: 'GillSans-Bold' });
	
}

function clearFormFields(o)
{
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filterClass) o.filterClass = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass) == -1) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass) == -1) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}

// font resize script
fontResize = {
	options: {
		maxStep:1.5,
		defaultFS: 1.1,
		resizeStep: 0.1,
		resizeHolder: 'body',
		cookieName: 'fontResizeCookie'
	},
	init: function() {
		this.domReady(function(){
			this.setDefaultScaling();
			this.addDefaultHandlers();
		});
		return this;
	},
	addDefaultHandlers: function() {
		this.addHandler('increase','inc');
		this.addHandler('decrease','dec');
		this.addHandler('reset');
	},
	setDefaultScaling: function() {
		if(this.options.resizeHolder == 'html') { this.resizeHolder = document.documentElement; }
		else { this.resizeHolder = document.body; }
		var cSize = this.getCookie(this.options.cookieName);
		if(cSize) {
			this.fSize = parseFloat(cSize,10)
		} else {
			this.fSize = this.options.defaultFS;
		}
		this.changeSize();
	},
	changeSize: function(direction) {
		if(typeof direction !== 'undefined') {
			if(direction == 1) {
				this.fSize += this.options.resizeStep;
				if (this.fSize > this.options.defaultFS * this.options.maxStep) this.fSize = this.options.defaultFS * this.options.maxStep;
			} else if(direction == -1) {
				this.fSize -= this.options.resizeStep;
				if (this.fSize < this.options.defaultFS / this.options.maxStep) this.fSize = this.options.defaultFS / this.options.maxStep;
			} else {
				this.fSize = this.options.defaultFS;
			}
		}
		this.resizeHolder.style.fontSize = this.fSize + 'em';
		this.updateCookie(this.fSize.toFixed(2));
		return false;
	},
	addHandler: function(obj, type) {
		if(typeof obj === 'string') { obj = document.getElementById(obj); }
		if(typeof obj !== 'undefined') {
			switch (type) {
				case 'inc':
					obj.onclick = this.bind(this.changeSize,this, [1]);
					break;
				case 'dec':
					obj.onclick = this.bind(this.changeSize,this, [-1]);
					break;
				default:
					obj.onclick = this.bind(this.changeSize,this, [0]);
			}
		}
	},
	domReady: function(fn) {
		var scope = this, calledFlag;
		(function(){
			if (document.addEventListener) {
				document.addEventListener('DOMContentLoaded', function(){
					if(!calledFlag) { calledFlag = true; fn.call(scope); }
				}, false)
			}
			if (!document.readyState || document.readyState.indexOf('in') != -1) {
				setTimeout(arguments.callee, 9);
			} else {
				if(!calledFlag) { calledFlag = true; fn.call(scope); }
			}
		}());
	},
	updateCookie: function(scaleLevel) {
		this.setCookie(this.options.cookieName,scaleLevel);
	},
	getCookie: function(name) {
		var matches = document.cookie.match(new RegExp("(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"));
		return matches ? decodeURIComponent(matches[1]) : undefined;
	},
	setCookie: function(name, value) {
		var exp = new Date();
		exp.setTime(exp.getTime()+(30*24*60*60*1000));
		document.cookie = name + '=' + value + ';' +'expires=' + exp.toGMTString() + ';' +'path=/';
	},
	bind: function(fn, scope, args){
		return function() {
			return fn.apply(scope, args || arguments);
		}
	}
}.init();

var _flagOpen = true;
var _destination = '';
function initPopup() {
	jQuery('.marker-holder').each(function() {
		var _this = jQuery(this);
		var _marker = _this.find('.marker');

		var _marker1 = _this.find('.marker2').html();
		_marker1 = _marker1.replace('&amp;', '');
		_marker1 = _marker1.replace('  ', '')+'box';
		_marker1 = '#' + _marker1.replace(' ', '');
		
		var _popup = jQuery('body').find(_marker1).clone();
		var _close = _popup.find('.btn-close');
		var _pos;
		_this.mouseenter(
			function() {
			if (_flagOpen) {
				_this.addClass('hover-state');
				_pos = _this.offset();
				var _marker2 = _this.find('.marker2').html();
				_marker2 = _marker2.replace('&amp;', '');
				_marker2 = _marker2.replace('  ', '');
				_marker2 = '#' + _marker2.replace(' ', '');
				jQuery(_marker2).addClass('hover-state');
				_pos.top -= 40;
				_pos.left -= 42;
				jQuery(_marker2).offset(_pos);
			}
		})
		_this.mouseleave(
			function() {
			if (_flagOpen) {
				_this.removeClass('hover-state');
				var _marker3 = _this.find('.marker2').html();
				_marker3 = _marker3.replace('&amp;', '');
				_marker3 = _marker3.replace('  ', '');
				_marker3 = '#' + _marker3.replace(' ', '');
				jQuery(_marker3).removeClass('hover-state');
				_pos.left = -9999;
				jQuery(_marker3).offset(_pos);
			}
		}
		);
		_this.bind('click', function() {
			hidePopup(this);
			_flagOpen = false;
			_this.removeClass('hover-state');
			var _marker3 = _this.find('.marker2').html();
			_marker3 = _marker3.replace('&amp;', '');
			_marker3 = _marker3.replace('  ', '');
			_marker3 = '#' + _marker3.replace(' ', '');
			jQuery(_marker3).removeClass('hover-state');
			var _pos1 = jQuery(_marker3).offset();
			_pos1.left = -9999;
			jQuery(_marker3).offset(_pos1);
			_popup.appendTo(jQuery('body'));
			_popup.css({
				top: _this.offset().top - _popup.outerHeight(true),
				bottom: 'auto',
				left: _this.offset().left
			});
			return false;
		});
		_close.bind('click', function() {
			_flagOpen = true;
			_popup.css({ left: -9999 });

			return false;
		});

	});
}
function hidePopup(e) {
	var _e = jQuery(e.target);
	if (!_e.parents('.box-text').length) {
		_flagOpen = true;
		jQuery('.box-text').css({ left: -9999 });
	}
}

function initLightBox() {
	if (typeof jQuery().simpleLightbox == 'function') {
		jQuery('a.open-popup').simpleLightbox({
			faderOpacity: 0.7,
			faderBackground: '#000000',
			closeLink: 'a.btn-close'
		});
	}
}
jQuery(document).ready(function() {
	jQuery.fn.simpleLightbox = function(_options) {
		var _options = jQuery.extend({
			lightboxContentBlock: '.lightbox',
			faderOpacity: 0.5,
			faderBackground: '#ffffff',
			closeLink: 'a.close-btn',
			href: true,
			animSpeed: (jQuery.browser.msie && jQuery.browser.version < 9) ? 0 : 300,
			onClick: null
		}, _options);

		return this.each(function(i, _this) {
			var _this = jQuery(_this);
			if (!_options.href)
				_this.lightboxContentBlock = _options.lightboxContentBlock;
			else _this.lightboxContentBlock = _this.attr('href');
			if (_this.lightboxContentBlock != '' && _this.lightboxContentBlock.length > 1) {
				_this.faderOpacity = _options.faderOpacity;
				_this.faderBackground = _options.faderBackground;
				_this.closeLink = _options.closeLink;
				var _fader;
				var _lightbox = jQuery(_this.lightboxContentBlock);
				if (!jQuery('div.lightbox-fader').length)
					_fader = jQuery('body').append('<div class="lightbox-fader"></div>');
				_fader = jQuery('div.lightbox-fader');
				_lightbox.css({
					'zIndex': 991
				});
				_fader.css({
					opacity: _this.faderOpacity,
					backgroundColor: _this.faderBackground,
					display: 'none',
					position: 'absolute',
					top: 0,
					left: 0,
					zIndex: 990,
					textIndent: -9999
				}).text('$nbsp');
				_lightbox.shownFlag = false;
				_this.click(function(e) {
					if (jQuery.isFunction(_options.onClick)) {
						_options.onClick.apply(_this);
					}
					_destination = jQuery(this).attr('destination');
					jQuery('#destination_').val(jQuery(this).attr('destination'));
					jQuery('#popupTitle').html('sign up for our newsletter and special offers');
					_lightbox.shownFlag = true;
					_lightbox.hide();
					jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
					_fader.fadeIn(_options.animSpeed, function() {
						_lightbox.fadeIn(_options.animSpeed);
						jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
					});
					jQuery('span.playButton').click();
					if (jQuery.browser.msie && jQuery.browser.version < 7) jQuery('#wrapper select').hide();
					hidePopup(e);
					return false;
				});
				jQuery(_this.closeLink).click(function() {
					_lightbox.fadeOut(_options.animSpeed, function() {
						_fader.fadeOut(_options.animSpeed);
						_scroll = false;
						if (popRedirect)
							window.location = _destination;

					});

					if (jQuery.browser.msie && jQuery.browser.version < 7) jQuery('#wrapper select').show();
					return false;
				});
				_fader.click(function() {
					_lightbox.fadeOut(_options.animSpeed, function() {
						_fader.fadeOut(_options.animSpeed);
					});
					if (jQuery.browser.msie && jQuery.browser.version < 7) jQuery('#wrapper select').show();
					return false;
				});
				var _scroll = false;
				jQuery.fn.simpleLightbox.positionLightbox = function(_lbox) {
					if (!_lbox.shownFlag) return false;
					var _height = 0;
					var _width = 0;
					var _minWidth = jQuery('body > div:eq(0)').outerWidth();
					if (window.innerHeight) {
						_height = window.innerHeight;
						_width = window.innerWidth;
					} else {
						_height = document.documentElement.clientHeight;
						_width = document.documentElement.clientWidth;
					}
					var _thisHeight = _lbox.outerHeight();
					var _page = jQuery('body');
					if (_lbox.length) {
						if (_width < _minWidth) { _fader.css('width', _minWidth); } else { _fader.css('width', '100%'); }
						if (_height > _page.innerHeight()) _fader.css('height', _height); else _fader.css('height', _page.innerHeight());
						if (_height > _thisHeight) {
							if (jQuery.browser.msie && jQuery.browser.version < 7) {
								_lbox.css({
									position: 'absolute',
									top: (document.documentElement.scrollTop + (_height - _thisHeight) / 2) + "px"
								});
							} else {
								_lbox.css({
									position: 'fixed',
									top: ((_height - _lbox.outerHeight()) / 2) + "px"
								});
							}
						}
						else {
							var _fh = parseInt(_fader.css('height'));
							if (!_scroll) {
								if (_fh - _thisHeight > parseInt(jQuery(document).scrollTop())) {
									_fh = parseInt(jQuery(document).scrollTop())
									_scroll = _fh;
								} else {
									_scroll = _fh - _thisHeight;
								}
							}
							_lbox.css({
								position: 'absolute',
								top: _scroll
							});
						}
						if (_width > _lbox.outerWidth()) _lbox.css({ left: ((_width - _lbox.outerWidth()) / 2 + 10) + "px" });
						else _lbox.css({ position: 'absolute', left: 0 });
					}
				}

				jQuery(window).resize(function() {
					if (_lightbox.is(':visible'))
						jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
				});
				jQuery(window).scroll(function() {
					if (_lightbox.is(':visible'))
						jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
				});

				jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
				jQuery(document).keydown(function(e) {
					if (!e) evt = window.event;
					if (e.keyCode == 27) {
						_lightbox.fadeOut(_options.animSpeed, function() {
							_fader.fadeOut(_options.animSpeed);
						});
					}
					if (jQuery.browser.msie && jQuery.browser.version < 7) jQuery('#wrapper select').hide();
				});
			}
		});
	}
	initLightBox();
	initPopup();
	jQuery('body').bind('click', hidePopup);

});
