var Site = {
	initialize: function() {
		Site.body = document.getElement("body");
		Site.smoothScroll = new SmoothScroll();

		Site.initializeRecentProjects();
		Site.initializeProjects();
		Site.initializeMemberImages();
		Site.initializeInputs();
		Site.initializeGoogleMaps($("google-maps"));

		// new Request({method: "get", url: "/twitter/update/"}).send();
	},
	
	print: function() {
		window.print();
	},

	initializeGoogleMaps: function(googleMaps) {
		if(googleMaps && GBrowserIsCompatible()) {
			var map = new GMap2(googleMaps);

			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());

			var wijnjewoudeLocation = new GLatLng(53.055692, 6.201032);
			var garijpLocation = new GLatLng(53.149635,5.970283)
			var centerLocation = new GLatLng(53.107217,6.075783);
			map.setCenter(centerLocation, 10);

			map.addOverlay(new GMarker(wijnjewoudeLocation));
			map.addOverlay(new GMarker(garijpLocation));
		}
	},

	initializeInputs: function() {
		Site.initializeInput($("search-query-field"));
		Site.initializeInput($("newsletter-e-mail-address-field"));
	},

	initializeInput: function(input) {
		input.addEvent("focus", Site.inputFocus.bind(input));
		input.addEvent("blur", Site.inputBlur.bind(input));
		if(input.get("value").length <= 0) {
			input.addClass("empty");
		}
	},

	inputFocus: function() {
		this.removeClass("empty");
	},

	inputBlur: function() {
		if(this.get("value").length <= 0) {
			this.addClass("empty");
		}
	},

	initializeRecentProjects: function() {
		var recentProjects = $("recent-projects");

		if(recentProjects) {
			new Carousel(recentProjects);
		}
	},

	initializeMemberImages: function() {
		var members = Site.body.getElements(".member-images");
		members.each(function(member) {
			new MemberImages(member);
		});
	},

	initializeProjects: function() {
		var items = Site.body.getElements(".project");
		items.each(function(item) {
			new Project(item);
		});
	}
};

/**
 * Member
 */
var MemberImages = new Class({
	initialize: function(element) {
		this.element = $(element);

		this.images = this.element.getElement(".images");
		this.activeItem = this.images.getFirst();

		this.navigation = new Element("p", {"class": "navigation"}).inject(this.images, "after");
		this.previousLink = new Element("a", {
			"href": "#",
			"class": "previous-link",
			"html": "<span>Vorige</span>",
			"events": {
				"click": this.previousClick.bindWithEvent(this)
			}}).inject(this.navigation);
		this.nextLink = new Element("a", {
			"href": "#",
			"class": "next-link",
			"html": "<span>Volgende</span>",
			"events": {
				"click": this.nextClick.bindWithEvent(this)
			}}).inject(this.navigation);

		this.updateNavigation();
	},

	next: function(event) {
		event.preventDefault();

		this.image.addClass("more");
		this.image = this.image.getNext().removeClass("more");
	},

	previous: function(event) {
		event.preventDefault();

		this.image.addClass("more");
		this.image = this.image.getPrevious().removeClass("more");
	},

	next: function() {
		this.to(this.activeItem.getNext());
	},

	previous: function() {
		this.to(this.activeItem.getPrevious());
	},

	to: function(item) {
		if(item) {
			this.activeItem.addClass("more");
			this.activeItem = item;
			this.activeItem.removeClass("more");
		}

		this.updateNavigation();
	},

	updateNavigation: function() {
		if(this.activeItem) {
			if(this.activeItem.getNext()) {
				this.nextLink.removeClass("disabled");
			} else {
				this.nextLink.addClass("disabled");
			}

			if(this.activeItem.getPrevious()) {
				this.previousLink.removeClass("disabled");
			} else {
				this.previousLink.addClass("disabled");
			}
		} else {
			this.nextLink.addClass("disabled");
			this.previousLink.addClass("disabled");
		}
	},

	nextClick: function(event) {
		event.preventDefault();

		this.next();
	},

	previousClick: function(event) {
		event.preventDefault();

		this.previous();
	}
});

/**
 * Project
 */
var Project = new Class({
	initialize: function(element) {
		this.element = $(element);

		this.result = this.element.getElement(".result");

		if(this.result) {
			this.preview = this.result.getElement(".preview");
			this.previewImage = this.preview.getElement(".preview-image");

			this.list = this.result.getElement(".preview-list");
			this.list.getElements("a").each(function(link) {
				if(!this.activeLink) {
					this.activeLink = link.addClass("active");
				}

				link.addEvent("click", this.linkClick.bindWithEvent(this, link));
			}.bind(this));
		}
	},

	linkClick: function(event, link) {
		event.preventDefault();

		if(this.activeLink) {
			this.activeLink.removeClass("active");
		}

		this.activeLink = link.addClass("active");

		this.previewImage.fade("hide");
		this.previewImage.set("src", link.get("href"));
		this.previewImage.fade("in");
	}
});

/**
 * Carousel
 */
var Carousel = new Class({
	initialize: function(element) {
		this.element = $(element);

		this.wrap = this.element.getElement(".wrap");

		this.width = this.wrap.getCoordinates().width;
		this.position = this.wrap.getScroll();

		this.items = this.wrap.getElement(".items");
		this.activeItem = this.items.getFirst();

		this.scroll = new Fx.Scroll(this.wrap);

		var navigation = new Element("p", {"class": "navigation"}).inject(this.element, "top");

		this.previousLink = new Element("a", {
			"href": "#",
			"class": "previous-link",
			"html": "<span>Vorige</span>",
			"events": {
				"click": this.previousClick.bindWithEvent(this)
			}
		}).inject(navigation);

		this.nextLink = new Element("a", {
			"href": "#",
			"class": "next-link",
			"html": "<span>Volgende</span>",
			"events": {
				"click": this.nextClick.bindWithEvent(this)
			}
		}).inject(navigation);

		this.updateNavigation();
    },

	next: function() {
		this.to(this.activeItem.getNext(), this.width, 0);
	},

	previous: function() {
		this.to(this.activeItem.getPrevious(), -this.width, 0);
	},

	to: function(item, deltaX, deltaY) {

		if(item) {
			this.activeItem = item;
			this.position.x += deltaX;
			this.position.y += deltaY;
			this.scroll.start(this.position.x, 0);
		}

		this.updateNavigation();
	},

	updateNavigation: function() {
		if(this.activeItem) {
			if(this.activeItem.getNext()) {
				this.nextLink.removeClass("disabled");
			} else {
				this.nextLink.addClass("disabled");
			}

			if(this.activeItem.getPrevious()) {
				this.previousLink.removeClass("disabled");
			} else {
				this.previousLink.addClass("disabled");
			}
		} else {
			this.nextLink.addClass("disabled");
			this.previousLink.addClass("disabled");
		}
	},

	nextClick: function(event) {
		event.preventDefault();

		this.next();
	},

	previousClick: function(event) {
		event.preventDefault();

		this.previous();
	}
});


window.addEvent("domready", Site.initialize);