/* MooTools Classes */

// Slideshow class
// Creates an image slideshow with controls
var Slideshow = new Class({
	options: {
		showControls: true,
		showDuration: 3000,
		showTOC: true,
		tocWidth: 22,
		tocClass: 'toc',
		tocActiveClass: 'active'
	},
	Implements: [Options,Events],
	initialize: function(container,elements,options) {
		//settings
		this.container = $(container);
		this.elements = $$(elements);
		this.currentIndex = 0;
		this.interval = '';
		if(this.options.showTOC) this.toc = [];
		
		//assign
		this.elements.each(function(el,i){
			if(this.options.showTOC) {
				this.toc.push(new Element('a',{
					text: i+1,
					href: '#',
					'class': this.options.tocClass + '' + (i == 0 ? ' ' + this.options.tocActiveClass : ''),
					events: {
						click: function(e) {
							if(e) e.stop();
							this.stop();
							this.show(i);
						}.bind(this)
					},
					styles: {
						right: ((this.elements.length - i) * (this.options.tocWidth + 5) + 27)
					}
				}).inject(this.container));
			}
			if(i > 0) el.set('opacity',0);
		},this);
		
		//next,previous links
		if(this.options.showControls) {
			this.createControls();
			
		}
		//events
		this.container.addEvents({
			mouseenter: function() { this.stop(); }.bind(this),
			mouseleave: function() { this.start(); }.bind(this)
		});

	},
	show: function(to) {
		this.elements[this.currentIndex].fade('out');
		if(this.options.showTOC) this.toc[this.currentIndex].removeClass(this.options.tocActiveClass);
		this.elements[this.currentIndex = ($defined(to) ? to : (this.currentIndex < this.elements.length - 1 ? this.currentIndex+1 : 0))].fade('in');
		if(this.options.showTOC) this.toc[this.currentIndex].addClass(this.options.tocActiveClass);
	},
	start: function() {
		this.interval = this.show.bind(this).periodical(this.options.showDuration);
	},
	stop: function() {
		$clear(this.interval);
	},
	//"private"
	createControls: function() {
		var next = new Element('a',{
			href: '#',
			id: 'next',
			text: '>>',
			events: {
				click: function(e) {
					if(e) e.stop();
					this.stop(); 
					this.show();
				}.bind(this)
			}
		}).inject(this.container);
		var previous = new Element('a',{
			href: '#',
			id: 'previous',
			text: '<<',
			events: {
				click: function(e) {
					if(e) e.stop();
					this.stop(); 
					this.show(this.currentIndex != 0 ? this.currentIndex -1 : this.elements.length-1);
				}.bind(this)
			},
			styles: {
				right: ((this.elements.length + 1) * (this.options.tocWidth + 5) + 27)
			}

		}).inject(this.container);
	}
});

/*
---
script: CountDown.js
license: MIT-style license.
description: CountDown - a mootools countdown implementation.
copyright: Copyright (c) 2008 Thierry Bela
authors: [Thierry Bela]

requires: 
  core:1.2.3: 
  - Events
  - Options
provides: [CountDown]
...
*/

	var CountDown = new Class({
		
		
		options: {
	
		/* 
			onChange: $empty,
			onComplete: $empty,
			date: null,
			frequency: 1000, //define the update frequency (in ms), default to 1000
		*/
			
			countdown: true
		},

		Implements: [Options, Events],
		initialize: function (options) {
	
			this.setOptions(options);
			if(!this.options.date instanceof Date) this.options.date = new Date(this.options.date);
			
			this.time = this.options.date.getTime();
			
			this.timer = new PeriodicalExecuter(this[ this.options.countdown ? 'countdown' : 'count'].bind(this), (this.options.frequency || 1000) / 1000);
		},
		stop: function () {
		
			this.timer.stop();			
			return this
		},
		start: function () {
		
			this.timer.registerCallback();			
			return this
		},
		
		calcute: function (from, now) {
					
			var millis = Math.max(0, from - now),
				time = Math.floor(millis / 1000),
				countdown = {
			
					days: Math.floor(time / (60 * 60 * 24)), 
					time: time,
					millis: millis
				};
			
			time %= (60 * 60 * 24);
			
	        countdown.hours = Math.floor(time / (60 * 60));
			time %= (60 * 60);
	        countdown.minutes = Math.floor(time / 60);
	        countdown.second = time % 60;
			
			return countdown
			
		}.protect(),
		
		count: function () {
		
			this.fireEvent('onChange', this.calcute(new Date().getTime(), this.time));
		},
		
		countdown: function () {
		
			var countdown = this.calcute(this.time, new Date().getTime());
			
			this.fireEvent('onChange', countdown);
			
			if(countdown.time == 0) {
			
				this.timer.stop();
				this.fireEvent('onComplete');
			}
		}
	});
	
	
/*
Script: PeriodicalExecuter.js
	port of the Prototype.js timer to Mootools

	License: MIT-style license.
	Copyright: Copyright (c) 2007 Thierry bela <bntfr at yahoo dot fr>

	License:
		MIT-style license.

	Authors:
		Thierry Bela

	TODO: possibility to stop the timer when the window is idle ?
*/			
	var PeriodicalExecuter = new Class({
		// name: 'PeriodicalExecuter',
		initialize: function(callback, frequency) {
		
			this.callback = callback;
			this.frequency = frequency;
			this.currentlyExecuting = false;

			this.registerCallback()
		},

		registerCallback: function() {
		
			this.stop();
			this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
			return this
		},

		execute: function() {
		
			this.callback(this);
			return this
		},

		stop: function() {
		
			if (!this.timer) return this;
			clearInterval(this.timer);
			this.timer = null;
			return this
		},

		onTimerEvent: function() {
		
			if (!this.currentlyExecuting) {
			
				try {
				
					this.currentlyExecuting = true;
					this.execute();
				} finally {
				
					this.currentlyExecuting = false;
				}
			}
				
			return this
		}
	});

/* Various Functions */
	
// Function to show designer face
var showDesigner = function(){
	$('hover-designer').fade('out');
	$('coder').fade('.2');
}

// Function to hide designer face
var hideDesigner = function(){
	$('hover-designer').fade('in');
	$('coder').fade('in');
}

// Function to show coder face
var showCoder = function(){
	$('hover-coder').fade('out');
	$('designer').fade('.2');
}

// Function to hide coder face
var hideCoder = function(){
	$('hover-coder').fade('in');
	$('designer').fade('in');
}

/* Print ie6 message */
/* Function to print a message for ie6 browsers */
var ieMessage = function(e){
	
	//var text = '<style type="text/css">#nav,#wrapper,#footer-bg{display:none;} #ie-six{display:block;}</style>';
	// Hide main site
	$('nav').set('styles',{'display' : 'none'});
	$('wrapper').set('styles',{'display' : 'none'});
	$('footer-bg').set('styles',{'display' : 'none'});
	$('ecxthecssawards').set('styles',{'display' : 'none'});
	$('ie').set('styles',{'display' : 'block'});
	
	// Define our ie6 message
	text = '<div id="content">';
	text += '<h1>Ummm, I will try to say this nicely ...</h1>';
	text += '<div class="box two-thirds-right">	';			
	text += '<div class="box-left">	';			
	text += '<img src="/resources/images/raquel-welch.jpg" alt="Raquel Welch is hot!" />';				
	text += '</div>	';			
	text += '<div class="box-right">';				
	text += '<h2>You&rsquo;re using a browser from the Stone Age!</h2>';
	text += '<p class="highlight">Some things from the past are beautiful and timeless. Others, like IE6, are just an ugly waste of time!</p>';
	text += '<p>The prehistoric browser you&rsquo;re using is known as IE6 and it has a multitude of problems that have been plaguing web designers and developers like me for many years. It doesn&rsquo;t display websites properly, it requires hacks and it&rsquo;s not secure. Bottom line is, it&rsquo;s holding back progress on the web! The good news is that it&rsquo;s very easy to upgrade to a new and better browser like <a href="http://www.microsoft.com/windows/internet-explorer/default.aspx" target="_blank">IE8</a>, <a href="http://www.mozilla.com" target="_blank">Firefox</a>, <a href="http://www.google.com/chrome" target="_blank">Chrome</a>, <a href="http://www.apple.com/safari/" target="_blank">Safari</a> or <a href="http://www.opera.com" target="_blank">Opera</a>.</p>';
	text += '<p>Since I can&rsquo;t show you my website on your current browser, you&rsquo;ll have to settle for my <a href="/resources/downloads/resume-adham-dannaway.pdf" target="_blank">resume</a> instead (which is nowhere near as pretty).</p>';
	text += '</div>	';			
	text += '<div class="clr"></div>';
	text += '</div>';
	text += '</div>';
	text += '<ul class="circles right">';
	text += '<li class="blog"><a href="http://www.cre8ivecommando.com" target="_blank"><span>Blog</span></a></li>';
	text += '<li class="linked-in"><a href="http://au.linkedin.com/in/adhamdannaway" target="_blank"><span>Linked In</span></a></li>';
	text += '<li class="twitter"><a href="http://www.twitter.com/AdhamDannaway" target="_blank"><span>Twitter</span></a></li>';
	text += '<li class="email"><a href="mailto:me@adhamdannaway.com" target="_blank"><span>Email</span></a></li>';
	text += '</ul>';
	
	//write to the div
	$(e).set('html', text);
}

// Function to start counter from start date
var startCounter = function(e){

	var div = $(e),
		coundown = new CountDown({
	
			// date I satred making websites
			date: new Date("July 07, 1985 12:00:00"),
			//update every 100ms
			frequency: 100, 
			countdown: false,
			//update the div#counter
			onChange: function(counter) {
			
				var text = '';
				
				if(counter.days > 0){
					text += '<div class="years">';
					text += Math.floor(counter.days/365) + ' <span>years</span>';
					text += '</div><div class="days">';
					text += counter.days + ' days ';
					text += '</div>';
				}
				
				text += '<div class="hours">';
				text += counter.hours + counter.days*24 + ' hours ';
				text += '</div><div class="minutes">';
				text += counter.minutes + counter.days*24*60 + ' minutes ';
				text += '</div><div class="seconds">';
				text += counter.second + counter.days*24*60*60 + ' seconds';
				text += '</div>';
				text += '<div class="spent">This is my time life... Like it!</div>';
				
				div.set('html', text);
			},
			//complete
			onComplete: function () {
			
				div.set('text', 'Countdown completed.');
			}
		})
}

// Run functions when the page is ready
window.addEvent('domready',function() {

	// Create Slideshow
	if($('slideshow')){
		var slideshow = new Slideshow('slideshow','#slideshow img');
		slideshow.start();
	}
	
	// Validate Contact Form
	if($('contact-form')){
		var myFormValidator = new FormValidator.Inline($('contact-form'));
	}
	
	// Animate face
	if($('designer') && $('coder')){			
		$('designer').addEvent('mouseenter', showDesigner);
		$('designer').addEvent('mouseleave', hideDesigner);
		$('coder').addEvent('mouseenter', showCoder);
		$('coder').addEvent('mouseleave', hideCoder);
	}
	
	// Counter
	if($('counter')){
		startCounter('counter');
	}
	
	//ie6 message
	var ie6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false; 
	if(ie6) {
		ieMessage('ie');
	}
	
	// Fade in page
	$('wrapper').setStyle('opacity',0);
	$('wrapper').set('tween', {duration: 1000}).fade('in');
			
});


