var Asset={javascript:function(d,b){b=Object.append({document:document},b);
if(b.onLoad){b.onload=b.onLoad;delete b.onLoad;}var a=new Element("script",{src:d,type:"text/javascript"});var c=b.onload||function(){},e=b.document;delete b.onload;
delete b.document;return a.addEvents({load:c,readystatechange:function(){if(["loaded","complete"].contains(this.readyState)){c.call(this);}}}).set(b).inject(e.head);
},css:function(b,a){a=a||{};var c=a.onload||a.onLoad;if(c){a.events=a.events||{};a.events.load=c;delete a.onload;delete a.onLoad;}return new Element("link",Object.merge({rel:"stylesheet",media:"screen",type:"text/css",href:b},a)).inject(document.head);
},image:function(c,b){b=Object.merge({onload:function(){},onabort:function(){},onerror:function(){}},b);var d=new Image();var a=document.id(d)||new Element("img");
["load","abort","error"].each(function(e){var g="on"+e;var f=e.capitalize();if(b["on"+f]){b[g]=b["on"+f];delete b["on"+f];}var h=b[g];delete b[g];d[g]=function(){if(!d){return;
}if(!a.parentNode){a.width=d.width;a.height=d.height;}d=d.onload=d.onabort=d.onerror=null;h.delay(1,a,a);a.fireEvent(e,a,1);};});d.src=a.src=c;if(d&&d.complete){d.onload.delay(1);
}return a.set(b);},images:function(c,b){b=Object.merge({onComplete:function(){},onProgress:function(){},onError:function(){},properties:{}},b);c=Array.from(c);
var a=0;return new Elements(c.map(function(e,d){return Asset.image(e,Object.append(b.properties,{onload:function(){a++;b.onProgress.call(this,a,d,e);if(a==c.length){b.onComplete();
}},onerror:function(){a++;b.onError.call(this,a,d,e);if(a==c.length){b.onComplete();}}}));}));}};

var themes = [
	{
		'background': 'snowboard.jpg', 
		'color': 'black',
		'description': 'Snowboard',
		'source': 'http://wallbase.cc/wallpaper/222498'
	},
	{ 
		'background': 'leaf.jpg',
		'color': 'black',		
		'description': 'Leaf',
		'source': 'http://wallbase.cc/wallpaper/303312'
	},
	{ 
		'background': 'water.jpg',
		'color': 'black',		
		'description': 'Water',
		'source': 'http://wallbase.cc/wallpaper/1501397'
	},
	{ 
		'background': 'desert-road.jpg',
		'color': 'black',		
		'description': 'Desert Road',
		'source': 'http://wallbase.cc/wallpaper/1498269'
	},
	{ 
		'background': 'fields.jpg',
		'color': 'black',		
		'description': 'Fields',
		'source': 'http://wallbase.cc/wallpaper/1505445'
	}
];

var movies = [
{
	id: 'Dp0Bt2cbcc8',
	width: 360,
	height: 270
}];

var self;

var Capstone = new Class(
{
    initialize: function()
    {
		self = this;
		self.baseUrl = 'http://capstone.nl/wp-content/themes/capstone';
		self.loader = true;
		
		// Start
		self.initBackgroundSelectors();
		self.loadBackground(Math.floor(Math.random() * themes.length));
		//self.showSite();	
		self.initMenu();
		self.sendMail();
		self.initLogo();

		window.onresize = function() { self.setImageSize($('background').getFirst('img')) };
    },

	/* Inject background image selectors */
	initBackgroundSelectors: function()
	{
		for (i = 0; i < themes.length; i++)
		{
			new Element('span').inject('header');
		};
		
		self.imgSelectors = $$('#header span').reverse();
		
		self.imgSelectors.each(function(imgSelector)
		{
			imgSelector.addEvent('click', function(e)
			{
				if (imgSelector.hasClass('active'))
					return;
				
				self.loadBackground(self.imgSelectors.indexOf(imgSelector));
			});
		});

	},
    
    loadBackground: function(index)
    {
		self.loader = true;
		self.loadAnimation();
		
		Asset.image(self.baseUrl+'/images/'+themes[index].background, 
		{
			onLoad: function(img)
			{	
				self.loader = false;
				
				// Set background
				$('background').empty();
				img.fade('hide');
				img.inject('background');
				self.setImageSize(img);
				img.fade('in');
			
				// Set selector
				if (self.activeSelector)
					self.activeSelector.removeClass('active');
				
				self.activeSelector = self.imgSelectors[index];
				self.activeSelector.addClass('active');
			}
		});	
	},

	setImageSize: function(img)
	{
		if ( ! img) return;

		var winSize = window.getSize();

		// adjust image size
		imgWidth = winSize.x;
		imgHeight = (winSize.x / img.get('width')) * img.get('height');

		// check whether to inflate vertically
		if (imgHeight < winSize.y)
		{
			imgHeight = winSize.y;
			imgWidth = (winSize.y / img.get('height')) * img.get('width');
		}

		img.set('width', imgWidth);
		img.set('height', imgHeight );

	},
    
	/* Animation during loading of files */
	loadAnimation: function()
	{
		myFx = new Fx.Tween('header', 
		{
			property: 'opacity',
			link: 'chain',
			onComplete: function()
			{
				// keep going
				if (self.loader)
				{
					target = ($('header').getStyle('opacity') == 0) ? 1 : 0;
					myFx.start(target);
				}
				// complete animation if opacity is 0
				else
				{
					if ($('header').getStyle('opacity') == 0)
						myFx.start(1);
				}
			}
		}).start(0);		
	},

	/* Show site with animation */
	showSite: function()
	{
		var blocks = ['address', 'contact', 'main', 'recent', 'menu'];
		var timedelay = 0;

		blocks.each(function(item)
		{
			timedelay += 150;

			(function()
			{
				$(item).fade('hide');
				$(item).removeClass('invisible');
				$(item).fade('in');
			}).delay(timedelay);
		});
	},
	
	initMenu: function()
	{
		menuItems = $$('#menu li');
		
		menuItems.addEvent('click', function()
		{
			self.changeMenu(this.get('id'));
		});
		
		$('moreProjects').addEvent('click', function()
		{
			self.changeMenu('portfolio');
		});
	},
	
	changeMenu: function(trigger)
	{
		$$('.main div').addClass('hidden');
		menuItems.removeClass('active');
		
		$$('.'+trigger).removeClass('hidden');
		$(trigger).addClass('active');

		// Embed movie on profile page
		if (trigger == 'profile')
		{
			$('movie').empty();
			
			var obj = new Swiff('http://www.youtube.com/v/'+movies[0].id+'&autoplay=1&autohide=1&hd=1&modestbranding=1', 
			{
				id: 'myBeautifulMovie',
				width: movies[0].width,
				height: movies[0].height,
				params: {
					wMode: 'transparent'
				}
			}).inject('movie');
		}
	},
	
	/* Send mail through ajax */
	sendMail: function()
	{
		$('sendButton').addEvent('click', function(e)
		{
			e.stop();
			
			if ($('msg_name').get('html') == '')
				return;
			
			new Request(
			{
				url:self.baseUrl+'/sendmail.php',
				data: {
					msg_name: $('msg_name').get('html'),
					msg_email: $('msg_email').get('html'),
					msg_content: $('msg_content').get('html')
				},
				onSuccess: function(result)
				{
					new Element('span', 
					{
						'html': '<br><b>Bedankt voor je bericht!</b>',
						'class': 'clear'
					}).inject('contact', 'bottom');
					
					$$('#contact dl').addClass('hidden');
					
					(function() 
					{
						$('contact').fade('out');
					}).delay(3000);
				}
			}).send();
		});
	},
	
	initLogo: function()
	{
		$('logo').addEvent('click', function()
		{
			if ( ! self.funz)
			{
				self.funz = Asset.javascript(self.baseUrl+'/asteroids-2.js', 
				{
					onLoad: function()
					{
					}
				});
			}
			else
			{
				
			}
		});
	}
});

myCapstone = new Capstone();

