var Banner = new Class({
	initialize: function (container, options) {
		if (container.getTag() == 'div') {
			var containerSize = container.getSize();
			
			console.log(containerSize.size.width);
		}
	}
});

var DEVOUR = {
	slideshow: {
		index: 1,
		delay: 6, // seconds
		duration: 1, // seconds
		bannerContainer: '',
		bammerImage: '',
		images: [
			'images/banner_1.jpg',
			'images/banner_2.jpg',
			'images/banner_3.jpg',
			'images/banner_4.jpg'
		],
		links: [
			'contact', 
			'solutions', 
			'umpf', 
			'projects#today'
		],
		preloadImages: function () {
			new Asset.images(DEVOUR.slideshow.images, {
				onComplete: function () {
					DEVOUR.slideshow.start();
				}
			});
		},
		nextIndex: function () {
			DEVOUR.slideshow.index = (DEVOUR.slideshow.index % (DEVOUR.slideshow.images.length)) + 1;
			
			return DEVOUR.slideshow.index;
		},
		updateURL: function (index) {
			var bannerLink = $('banner_link');
			var bannerHref = '/' + DEVOUR.slideshow.links[index];

			bannerLink.setProperty('href', bannerHref);
		},
		createBannerImage: function () {
		
		},
		changeImage: function () {
			var banner = $('banner');
			var container = $('banner_container');
			var bannerFX = new Fx.Style(banner, 'opacity', {duration: DEVOUR.slideshow.duration * 1000});
			var bannerIndex = DEVOUR.slideshow.nextIndex();
			var bannerSource = 'images/banner_' + bannerIndex + '.jpg';
			
			container.setStyle('background-image', 'url(' + bannerSource + ')');
			
			bannerFX.start(0.0).chain(
				function () {
					banner.setProperty('src', bannerSource);
					banner.setOpacity(0.9999);
					setTimeout('DEVOUR.slideshow.changeImage()', DEVOUR.slideshow.delay * 1000);
				}
			);

			DEVOUR.slideshow.updateURL(bannerIndex - 1);
		},
		start: function () {
			DEVOUR.slideshow.changeImage();
		}
	},
	form: {
		calendar: '',
		validate: function () {
		
		},
		setValidation: function () {
		
		},
		hijack: function () {
			$$('form').each(
				function (form) {
					
					form.addEvent('submit', function (e) {
						new Event(e).stop();
						
						this.send({
							update: $('response'),
							onComplete: function() {
								form.setStyle('display', 'none');
								$('sent').setStyle('display', 'block');
							}
						});

					});
				}
			)
		},
		addCalendar: function () {
			if ($('date_picker_icon')) {
				$('date_picker_icon').addEvent('click', function(e){
					var event = new Event(e).stop();
					var x = event.client.x;
					var y = event.client.y;

					new MooCal("presentation_date").show(x,y);
				});
			}
		},
		modifyTimePicker: function () {
			if ($('presentation_hour')) {
				$('presentation_hour').addEvent('change', function (e) {
					var hour = $('presentation_hour').getProperty('value');
					var meridian = (hour > 11) ? 'PM' : 'AM';
					
					$('meridian').setText(meridian);
				});	
			}
		}
	},
	login: {
		window: '',
		attachToElement: function (el) {
			if (DEVOUR.login.window == '') {
				DEVOUR.login.createWindow();
			}
			
			el.addEvent('click', function (e) {
				new Event(e).stop();
				
				DEVOUR.login.window.setStatus('Log in to access members-only content.');
				DEVOUR.login.showWindow();
			});
			
			DEVOUR.login.window.addHideEvent(
				function () {
					//var loginLink = $$('.client_login')[0];
					el.setOpacity(0.9999);

					el.addEvent('click', function (e) {
						new Event(e).stop();
						
						DEVOUR.login.window.setStatus('Log in to access members-only content.');
						DEVOUR.login.showWindow();
					});
					$$('.moodows_status')[0].setStyle('color', '#12579f');
				}
			);
		},
		createWindow: function () {
			if (DEVOUR.login.window == '') {
				DEVOUR.login.window = new moodows({ 
					top: 200, 
					left: (window.getWidth() / 2) - 150, 
					width: 300, 
					height: 210, 
					closable: true, 
					minimize: false, 
					title: "<img class='title' src='images/title_emmausprojectslogin.gif' alt='Client login' width='168' height='15' /><img class='icon' src='images/icon_loginkey.gif' alt='' width='84' height='53' />", 
					status: 'Log in to access members-only content.'
				});
			}
			
			new Ajax('pages/login.php', {
				method: 'get',
				update: 'content_' + DEVOUR.login.window.id,
				onComplete: function () {
					$$('.client_login')[0].setStyle('visibility', 'visible');
					
					$('login_form').addEvent('submit', function (e) {
						new Event(e).stop();
						
						$$('.moodows_status')[0].setStyle('color', 'red');
						DEVOUR.login.window.setStatus('Incorrect username or password. <a id="login_help" href="contact">Contact us</a> for help.');
					});
				}
			}).request();

		},
		showWindow: function () {
			$$('.client_login')[0].setOpacity(0.4);
			$$('.client_login')[0].removeEvents();
			DEVOUR.login.window.show();
		},
		hideWindow: function () {
			DEVOUR.login.window.hide();
		}
	}
};


window.addEvent('domready', function () {
	new MooCal("presentation_date");
	
	DEVOUR.form.hijack();	
	DEVOUR.form.modifyTimePicker();
	DEVOUR.form.addCalendar();
	
	DEVOUR.login.attachToElement($$('.client_login')[0]);
});

window.addEvent('load', function () {
	DEVOUR.slideshow.preloadImages();
});