
	$(document).ready(function()
	{
		initMenuBar();
	});

// MENU BAR

	function initMenuBar()
	{
		var menu = $('.menu:first');
		var list = menu.find('ul');
		var listOffset = Math.round((940 - list.width()) * 0.5);
		var logo = menu.find('.logo');
		var timeout = null;

		list.css('right', listOffset);
		logo.css('opacity', 0);

		$(window).scroll(function()
		{
			window.clearTimeout(timeout);

			if($(this).scrollTop() <= 130)
			{
				timeout = window.setTimeout(function()
				{
					list.animate({ right : listOffset }, { duration : 200, easing : 'quadEaseOut', queue : false });
					logo.animate({ opacity : 0 }, { duration : 200, easing : 'quadEaseOut', queue : false });
				}, 50);
			}
			else
			{
				timeout = window.setTimeout(function()
				{
					list.animate({ right : 10 }, { duration : 200, easing : 'quadEaseOut', queue : false });
					logo.animate({ opacity : 1.0 }, { duration : 200, easing : 'quadEaseOut', queue : false });
				}, 50);
			}

			if($(this).scrollTop() <= 20)
			{
				menu.css('box-shadow', 'none');
			}
			else
			{
				menu.css('box-shadow', '0px 1px 4px rgba(0, 0, 0, 0.4)');
			}
		});

		$(window).scroll();
	}

// HEADER

	var header_instance = null;

	function Header()
	{
		this.autoplayInterval = null;
		this.current = -1;
		this.images = new Array();
		this.titles = new Array();
		this.thumbs = new Array();
		this.total = 0;
		this.urls = new Array();

		this.buttonNext = null;
		this.buttonPrev = null;
		this.header = null;
		this.holder = null;
		this.image = null;
		this.panel = null;
		this.projectLink = null;
		this.subtitle = null;
		this.title = null;

		header_instance = this;
	}

	Header.prototype.add = function(title, image, url)
	{
		this.images.push(image);
		this.titles.push(title);
		this.urls.push(url);

		this.total++;
	}

	Header.prototype.autoplay = function(on)
	{
		if(on) this.autoplayInterval = window.setInterval('header_instance.showNext()', 5000);
		else window.clearInterval(this.autoplayInterval);
	}

	Header.prototype.scrollToTop = function()
	{
		window.scrollTo(0, 0);
	}

	Header.prototype.show = function(index, init, stopAutoplay, scrollTop)
	{
		if((index < 0 || index > (this.total - 1)) || index == this.current) return;
		else this.current = index;

		// content

		if(init)
		{
			this.image.attr('src', this.images[index]);

		}
		else
		{
			this.image.attr('src', this.images[index]);

			this.image.css('opacity', 0);
			this.image.animate({ opacity : 1 }, { duration : 400, easing : 'quadEaseOut', queue : false });
		}

		this.projectLink.attr('href', this.urls[index]);
		this.title.text(this.titles[index]);

		// active thumb

		$(this.thumbs).each(function(ind)
		{
			(ind == index) ? $(this).addClass('active') : $(this).removeClass('active');
		});

		// thumb animation

		if(this.total > 7)
		{
			if(index < 3)
			{
				this.holder.animate({ left : 20 }, { duration : 250, easing : 'quadEaseOut', queue : false });
			}
			else if(index > this.total - 4)
			{
				this.holder.animate({ left : (910 - this.holder.width() + 20) }, { duration : 250, easing : 'quadEaseOut', queue : false });
			}
			else
			{
				this.holder.animate({ left : (20 - (this.current - 3) * 130) }, { duration : 250, easing : 'quadEaseOut', queue : false });
			}
		}

		// options

		if(stopAutoplay) this.autoplay(false);
		if(scrollTop) this.scrollToTop();
	}

	Header.prototype.showNext = function()
	{
		this.show((this.current + 1 == this.total) ? 0 : this.current + 1);
	}

	Header.prototype.showPrev = function()
	{
		this.show((this.current - 1 < 0) ? this.total - 1 : this.current - 1);
	}

	Header.prototype.start = function(autoplay)
	{
		if(!this.total)
		{
			//window.alert('ERROR: No images added!');
		}
		else
		{
			// getting elements

			this.header = $('.header:first');
			this.image = $('#header-image');
			this.panel = this.header.find('.panel');
			this.title = this.header.find('.title');
			this.subtitle = this.header.find('.sub');
			this.projectLink = this.subtitle.find('a');
			this.buttonNext = this.panel.find('.button-next');
			this.buttonPrev = this.panel.find('.button-prev');
			this.holder = this.panel.find('.holder');

			// hide subtitle

			if(this.subtitle.html() == '')
			{
				this.subtitle.css('display', 'none');
				this.title.css('top', 42);
			}

			// create thumbs

			for(var i = 0; i < this.total; i++)
			{
				var thumb = $(document.createElement('a')).attr({ href : '#' + (i + 1) });
				this.holder.append(thumb);

				var image = $(document.createElement('img')).attr({ alt : '', src : this.images[i] });
				thumb.append(image);

				thumb.click(function()
				{
					header_instance.show(parseInt($(this).attr('href').substr(1)) - 1, false, true, true);
					return false;
				});

				this.thumbs.push(thumb);
			}

			// events

			this.header.mouseover(function()
			{
				header_instance.panel.animate({ bottom : 0 }, { duration : 250, easing : 'quadEaseOut', queue : false });
			});

			this.header.mouseout(function()
			{
				header_instance.panel.animate({ bottom : -100 }, { duration : 250, easing : 'quadEaseIn', queue : false });
			});

			this.buttonNext.click(function()
			{
				header_instance.autoplay(false);
				header_instance.scrollToTop();
				header_instance.showNext();
			});

			this.buttonPrev.click(function()
			{
				header_instance.autoplay(false);
				header_instance.scrollToTop();
				header_instance.showPrev();
			});

			// init

			this.holder.css('width', this.total * 130);
			this.panel.css('bottom', -100);

			this.show(0, true);
			this.autoplay(autoplay);
		}
	}

// OTHER

	function fixHeights(object1, object2)
	{
		if($(object1) != null && $(object2) != null)
		{
			if($(object1).height() >= $(object2).height())
			{
				$(object2).height($(object1).height());
			}
			else
			{
				$(object1).height($(object2).height());
			}
		}
	}

	function readWholeText(thisReference)
	{
		var parent = $(thisReference).parent().parent();
		var intro = $(parent).find('p:eq(0)');
		var fulltext = $(parent).find('p:eq(1)');
		var oldHeight = parent.height();
		var newHeight;

		intro.css('display', 'none');
		fulltext.css('display', 'block');
		newHeight = parent.height();

		parent.css('overflow', 'hidden');
		parent.height(oldHeight);
		parent.animate({ height : newHeight }, { duration : 200, easing : 'quadEaseOut', queue : false });
	}
	$(document).ready(function()
	{
		initMenuBar();
	});

// MENU BAR

	function initMenuBar()
	{
		var menu = $('.menu:first');
		var list = menu.find('ul');
		var listOffset = Math.round((940 - list.width()) * 0.5);
		var logo = menu.find('.logo');
		var timeout = null;

		list.css('right', listOffset);
		logo.css('opacity', 0);

		$(window).scroll(function()
		{
			window.clearTimeout(timeout);

			if($(this).scrollTop() <= 130)
			{
				timeout = window.setTimeout(function()
				{
					list.animate({ right : listOffset }, { duration : 200, easing : 'quadEaseOut', queue : false });
					logo.animate({ opacity : 0 }, { duration : 200, easing : 'quadEaseOut', queue : false });
				}, 50);
			}
			else
			{
				timeout = window.setTimeout(function()
				{
					list.animate({ right : 10 }, { duration : 200, easing : 'quadEaseOut', queue : false });
					logo.animate({ opacity : 1.0 }, { duration : 200, easing : 'quadEaseOut', queue : false });
				}, 50);
			}

			if($(this).scrollTop() <= 20)
			{
				menu.css('box-shadow', 'none');
			}
			else
			{
				menu.css('box-shadow', '0px 1px 4px rgba(0, 0, 0, 0.4)');
			}
		});

		$(window).scroll();
	}

// HEADER

	var header_instance = null;

	function Header()
	{
		this.autoplayInterval = null;
		this.current = -1;
		this.images = new Array();
		this.titles = new Array();
		this.thumbs = new Array();
		this.total = 0;
		this.urls = new Array();

		this.buttonNext = null;
		this.buttonPrev = null;
		this.header = null;
		this.holder = null;
		this.image = null;
		this.panel = null;
		this.projectLink = null;
		this.subtitle = null;
		this.title = null;

		header_instance = this;
	}

	Header.prototype.add = function(title, image, url)
	{
		this.images.push(image);
		this.titles.push(title);
		this.urls.push(url);

		this.total++;
	}

	Header.prototype.autoplay = function(on)
	{
		if(on) this.autoplayInterval = window.setInterval('header_instance.showNext()', 5000);
		else window.clearInterval(this.autoplayInterval);
	}

	Header.prototype.scrollToTop = function()
	{
		window.scrollTo(0, 0);
	}

	Header.prototype.show = function(index, init, stopAutoplay, scrollTop)
	{
		if((index < 0 || index > (this.total - 1)) || index == this.current) return;
		else this.current = index;

		// content

		if(init)
		{
			this.image.attr('src', this.images[index]);

		}
		else
		{
			this.image.attr('src', this.images[index]);

			this.image.css('opacity', 0);
			this.image.animate({ opacity : 1 }, { duration : 400, easing : 'quadEaseOut', queue : false });
		}

		this.projectLink.attr('href', this.urls[index]);
		this.title.text(this.titles[index]);

		// active thumb

		$(this.thumbs).each(function(ind)
		{
			(ind == index) ? $(this).addClass('active') : $(this).removeClass('active');
		});

		// thumb animation

		if(this.total > 7)
		{
			if(index < 3)
			{
				this.holder.animate({ left : 20 }, { duration : 250, easing : 'quadEaseOut', queue : false });
			}
			else if(index > this.total - 4)
			{
				this.holder.animate({ left : (910 - this.holder.width() + 20) }, { duration : 250, easing : 'quadEaseOut', queue : false });
			}
			else
			{
				this.holder.animate({ left : (20 - (this.current - 3) * 130) }, { duration : 250, easing : 'quadEaseOut', queue : false });
			}
		}

		// options

		if(stopAutoplay) this.autoplay(false);
		if(scrollTop) this.scrollToTop();
	}

	Header.prototype.showNext = function()
	{
		this.show((this.current + 1 == this.total) ? 0 : this.current + 1);
	}

	Header.prototype.showPrev = function()
	{
		this.show((this.current - 1 < 0) ? this.total - 1 : this.current - 1);
	}

	Header.prototype.start = function(autoplay)
	{
		if(!this.total)
		{
			window.alert('ERROR: No images added!');
		}
		else
		{
			// getting elements

			this.header = $('.header:first');
			this.image = $('#header-image');
			this.panel = this.header.find('.panel');
			this.title = this.header.find('.title');
			this.subtitle = this.header.find('.sub');
			this.projectLink = this.subtitle.find('a');
			this.buttonNext = this.panel.find('.button-next');
			this.buttonPrev = this.panel.find('.button-prev');
			this.holder = this.panel.find('.holder');

			// hide subtitle

			if(this.subtitle.html() == '')
			{
				this.subtitle.css('display', 'none');
				this.title.css('top', 42);
			}

			// create thumbs

			for(var i = 0; i < this.total; i++)
			{
				var thumb = $(document.createElement('a')).attr({ href : '#' + (i + 1) });
				this.holder.append(thumb);

				var image = $(document.createElement('img')).attr({ alt : '', src : this.images[i] });
				thumb.append(image);

				thumb.click(function()
				{
					header_instance.show(parseInt($(this).attr('href').substr(1)) - 1, false, true, true);
					return false;
				});

				this.thumbs.push(thumb);
			}

			// events

			this.header.mouseover(function()
			{
				header_instance.panel.animate({ bottom : 0 }, { duration : 250, easing : 'quadEaseOut', queue : false });
			});

			this.header.mouseout(function()
			{
				header_instance.panel.animate({ bottom : -100 }, { duration : 250, easing : 'quadEaseIn', queue : false });
			});

			this.buttonNext.click(function()
			{
				header_instance.autoplay(false);
				header_instance.scrollToTop();
				header_instance.showNext();
			});

			this.buttonPrev.click(function()
			{
				header_instance.autoplay(false);
				header_instance.scrollToTop();
				header_instance.showPrev();
			});

			// init

			this.holder.css('width', this.total * 130);
			this.panel.css('bottom', -100);

			this.show(0, true);
			this.autoplay(autoplay);
		}
	}

// OTHER

	function fixHeights(object1, object2)
	{
		if($(object1) != null && $(object2) != null)
		{
			if($(object1).height() >= $(object2).height())
			{
				$(object2).height($(object1).height());
			}
			else
			{
				$(object1).height($(object2).height());
			}
		}
	}

	function readWholeText(thisReference)
	{
		var parent = $(thisReference).parent().parent();
		var intro = $(parent).find('p:eq(0)');
		var fulltext = $(parent).find('p:eq(1)');
		var oldHeight = parent.height();
		var newHeight;

		intro.css('display', 'none');
		fulltext.css('display', 'block');
		newHeight = parent.height();

		parent.css('overflow', 'hidden');
		parent.height(oldHeight);
		parent.animate({ height : newHeight }, { duration : 200, easing : 'quadEaseOut', queue : false });
	}

