site={
	themeURL: '/wp-content/themes/spm/',
	is_home: function () {
		if ($('home')) {
			return true;
		}
		return false;
	},
	init:function () {
		if (site.is_home()) {
			$('tourtext').setStyle('display', 'none');
			// Create header drop shadow and move it when window is resized
			var coords = $('header').getCoordinates();
			var shadow = new Element('div')
				.setStyles({
					background:'url(/wp-content/themes/spm/images/headerBG.gif) no-repeat',
					width:coords.width + 10,
					height:coords.height + 10,
					top:coords.top - 5,
					left:coords.left - 5,
					position:'absolute',
					zIndex:0
				})
				.injectTop(document.body);
			
			// Industry Title
			var industriesTitle = $('industries').getPrevious().setStyles({
				position:'absolute',
				top:coords.bottom,
				left:coords.left+138
			}).injectTop(document.body);
			
			window.addEvent('resize', function () {
				var coords = $('header').getCoordinates();
				shadow.setStyle('left', coords.left-5);
				industriesTitle.setStyle('left', coords.left+138);
			});
			
			// Industry Slides
			$$('#industries li div.panel').each(function(panel) {
				panel.setStyle('display', 'none');
				var title = panel.getPrevious();
				var slider = new Fx.Tween(panel, {
					duration:500,
					transition: Fx.Transitions.linear,
					onStart: function () {
						this.element.setStyle('display', '');
					},
					onComplete: function () {
						var left = parseInt(this.element.getStyle('left'));
						this.element.setStyle('display', left < 0 ? 'none' : '');
					}
				});
				slider.set('left', -980);
				var on = false;
				panel.addEvent('mouseover', function () {slider.cancel();});
				panel.addEvent('mouseout', function () {slider.cancel(); slider.start('left', -980);});
				title.addEvent('mouseover', function () {slider.cancel(); slider.start('left', 244);});
				title.addEvent('mouseout', function () {slider.cancel(); slider.start('left', -980);});
			});
			
			// Carousel
			new spmCarousel($$('#carousel li'), {controlsParent:'header'});		
		}	
		$$('.tour').addEvent('click', function (e) {
			new Event(e).stop();
			Slimbox.open([
				['/wp-content/themes/spm/images/tour/step1.jpg', 'Step 1: Outset Meeting'], 
				['/wp-content/themes/spm/images/tour/step2.jpg', 'Step 2: Baseline Audit & Analysis'], 
				['/wp-content/themes/spm/images/tour/step3.jpg', 'Step 3: Report'],
				['/wp-content/themes/spm/images/tour/step4.jpg', 'Step 4: Scheme Installation'], 
				['/wp-content/themes/spm/images/tour/step5.jpg', 'Step 5: Training'], 
				['/wp-content/themes/spm/images/tour/step6.jpg', 'Step 6: Scheme in Progress'],
				['/wp-content/themes/spm/images/tour/step7.jpg', 'Step 7: Scheme Illustration'], 
				['/wp-content/themes/spm/images/tour/step8.jpg', 'Step 8: Annual Audit'], 
				['/wp-content/themes/spm/images/tour/step9.jpg', 'Step 9: Enquire']
			], 0, {loop:true});
		});
		
		$('testBtn').addEvent('click', function () {window.location = '/take-the-test/';});
		
		$('loginBtn').addEvent('click', function () {
			new Element('div', {id:'overlay'})
			.setStyles({
				position:'absolute',
				top:0, left:0,
				width:window.getScrollSize().x, height:window.getScrollSize().y,
				background:'black',
				opacity:0.8,
				zIndex:999999
			})
			.addEvent('click', function () {$('login').dispose(); this.dispose();})
			.injectInside(document.body);
			new Element('form', {id:'login', action:'http://extranet.safetyprogrammemanager.co.uk/zend.php/auth/login/', method:'post'})
				.adopt(new Element('ul').addClass('inputs')
					.adopt(new Element('li')
						.adopt(new Element('label', {htmlFor:'username'}).set('text', 'Username:'))
						.adopt(new Element('input', {type:'text', id:'username', name:'username'}))
					)
					.adopt(new Element('li')
						.adopt(new Element('label', {htmlFor:'password'}).set('text', 'Password:'))
						.adopt(new Element('input', {type:'password', id:'password', name:'password'}))
					)
					.adopt(new Element('li')
						.adopt(new Element('input', {type:'submit', id:'login_btn', name:'login_btn', value:'Login'}))
					)
				)
				.adopt(new Element('a', {id:'recoverPassword'})
					.set('text', 'Forgotten your password?')
					.addEvent('click', function () {
						$('login').setProperty('action', 'http://extranet.safetyprogrammemanager.co.uk/zend.php/auth/forgotpassword/');
						$('password').getParent('li').dispose();
						$('login_btn').setStyle('backgroundImage', 'url(/wp-content/themes/spm/images/sendBtn.gif)').set('value', 'Send');
						this.dispose();
					})
				)
				.setStyle('zIndex', 9999999)
				.injectInside(document.body).center();
		});
		
		site.setFooterPosition();
/*		
//		new Element('div').setStyles({position:'absolute', top:0, right:0, width:10, height:10, cursor:'pointer'}).injectInside(document.body)
		$$('.tour')
			.addEvent('click', function () {
				var pos = 1;
				new Element('div')
					.injectBefore('product_tour')
					.setStyles({
						position:'absolute', 
						top:0, 
						left:0, 
						background:'#000', 
						width:'100%', 
						height:'100%',
						zIndex:11
					})
					.setOpacity(0.8)
					.addEvent('click', function () {
						$('product_tour').setStyle('display', 'none'); 
						this.dispose();
						$('forward').dispose();
						$('back').dispose();
					});
				
				var scroll = new Fx.Scroll('product_tour', {
					offset:{'x':-10, 'y':0},
					transition:Fx.Transitions.Quad.easeInOut
				});
				
				$('product_tour').setStyle('display', 'block').center().addEvent('click', function () {
					pos++;
					scroll.toElement('step'+(pos==10?1:pos));
				});
				var coords = $('product_tour').getCoordinates();
				new Element('img', {id:'back', src:'/wp-content/themes/spm/images/left.png'})
					.setStyles({position:'absolute', top:coords.top + 130, left:coords.left-35, zIndex:12})
					.addEvent('click', function () {
						if (pos != 0)	pos--;
						scroll.toElement('step'+pos);
					})
					.injectAfter('product_tour');
				new Element('img', {id:'forward', src:'/wp-content/themes/spm/images/right.png'})
					.setStyles({position:'absolute', top:coords.top + 130, left:coords.right, zIndex:12})
					.addEvent('click', function () {
						if (pos != 9)	pos++;
						scroll.toElement('step'+pos);
					})
					.injectAfter('product_tour');
			});
*/		
	},
	setFooterPosition: function () {
		var scrollHeight = window.getScrollSize().y;
		var windowHeight = window.getHeight();
		if (scrollHeight <= windowHeight) {
			$('footer').setStyles({
				position:'absolute',
				bottom:0
			});
		} else {
			$('footer').setStyle('position', 'static');
		}
	}
}

window.addEvent('domready', site.init);
window.addEvent('resize', site.setFooterPosition);

Element.implement({
	center: function () {
		var left = (window.getWidth()/2) - (this.getSize().x/2);
		var top = (window.getHeight()/2) - (this.getSize().y/2) + (window.getScrollTop());
		if (left < 0) left = 10;
		if (top < 0 || top < window.getScrollTop()) top = window.getScrollTop() + 10;
		
		this.setStyles({
			position:'absolute',
			left: left,
			top: top
		});
		if (this.overlay)		this.overlay.fitToWindow();
		return this;
	}
});

var Carousel = new Class({
	Implements: Options,
	options:{
		controls:true,
		controlsParent: $empty
	},
	
	initialize: function (screens, options) {
		this.setOptions(options);
		this.screens = screens.fade('hide');
		this.activeScreen = screens[0].fade('show');
		if (this.options.controls)	this.addControls();
		this.screens.getParent().addEvents({
			mouseover: function () {this.stop();}.bind(this),
			mouseout: function () {this.start();}.bind(this)
		});
		this.start();
	},
	start: function () {
		this.timer = (function () {
			this.activeScreen.fade('out');
			var pos = this.screens.indexOf(this.activeScreen);
				pos = (pos == this.screens.length-1) ? 0 : pos + 1;
			this.activeScreen = this.screens[pos].fade('in');
			if (this.options.controls) {
				this.controls.removeClass('on');
				this.controls[pos].addClass('on');
			}
		}.bind(this)).periodical(5000);
	},
	stop: function () {
		$clear(this.timer);
	},
	addControls: function (screens) {
		var controls = new Element('ul', {id:'carouselControls'});
		this.screens.each(function (screen) {
			controls.adopt(new Element('li').addEvent('click', function (e) {
				var control = new Event(e).stop().target;
				this.setScreen(control.getAllPrevious().length);
			}.bind(this)));
		}.bind(this));
		controls.getFirst().addClass('on');
		this.controls = controls.getChildren();
		$(this.options.controlsParent).adopt(controls);
	},
	setScreen: function (index) {
		this.stop();
		if (this.controls) {
			this.controls.removeClass('on');
			this.controls[index].addClass('on');
		}
		this.activeScreen.fade('hide');
		this.activeScreen = this.screens[index].fade('in');
		this.start();
	}
});

var spmCarousel = new Class({
	Extends: Carousel,
	initialize: function (screens, options) {
		this.parent(screens, options);
		this.screens.fade('show');
		this.screens.each(function (screen) {screen.getChildren('img').fade('hide');});
		this.screens.each(function (screen) {screen.getChildren('div').fade('hide');});
//		this.screens.each(function (screen) {screen.getChildren('div').tween('bottom', -300);});
		this.activeScreen.getChildren('img')[0].fade('in');
		this.activeScreen.getChildren('div')[0].fade('in');
//		this.activeScreen.getChildren('div')[0].tween('bottom', 0);
	},
	start: function () {
		this.timer = (function () {
//			this.activeScreen.getChildren('div')[0].tween('bottom', -300);
			this.activeScreen.getChildren('div')[0].fade('out');
			this.activeScreen.getChildren('img')[0].fade('out');
			var pos = this.screens.indexOf(this.activeScreen);
				pos = (pos == this.screens.length-1) ? 0 : pos + 1;
			this.activeScreen = this.screens[pos];
			this.activeScreen.getChildren('img')[0].fade('in');
			this.activeScreen.getChildren('div')[0].fade('in');
//			(function () {this.activeScreen.getChildren('div')[0].tween('bottom', 0);}).delay(1500, this);
			if (this.options.controls) {
				this.controls.removeClass('on');
				this.controls[pos].addClass('on');
			}
		}.bind(this)).periodical(10000);
	},
	setScreen: function (index) {
		this.stop();
		if (this.controls) {
			this.controls.removeClass('on');
			this.controls[index].addClass('on');
		}
		this.activeScreen.getChildren('div')[0].fade('out');
		this.activeScreen.getChildren('img')[0].fade('out');
		this.activeScreen = this.screens[index];
		this.activeScreen.getChildren('img')[0].fade('in');
		this.activeScreen.getChildren('div')[0].fade('in');
		this.start();
	}
});