if (typeof(CP) == 'undefined') CP = {};



CP.Print = new Class({
	initialize: function(trigger) {
		if (!trigger) return false;
		
		trigger.addEvent('click', function(ev) {
			ev = new Event(ev).stop();
			window.print();
		});
	}
}); // CP.Print



CP.Gallery = new Class({

	photos: [],

	initialize: function() {
		this.featured = $E('#featured img');

		this.photos = $$('#photos a');
		this.photos.each(function(photo, i) {
			if (i == 0) {
				$(photo.parentNode).addClass('selected');
			}
			photo.addEvent('click', function(ev) {
				this.swap(photo, ev);
			}.bind(this));
		}.bind(this));

		$E('#previousphoto a').addEvent('click', function(ev){
			this.previousPhoto(ev);
		}.bind(this));

		$E('#nextphoto a').addEvent('click', function(ev){
			this.nextPhoto(ev);
		}.bind(this));

		$('close').addEvent('click', function(ev) {
			this.closeBox(ev);
		}.bind(this));
	},
	
	swap: function(photo, ev) {
		if (ev) ev = new Event(ev).stop();
		var image = photo.href;
		var featured = this.featured;

		featured.effect('opacity', { duration:500 }).start(0).chain(function() {
			new Asset.image(image, {
				onload: function() {
					featured.src = image;
					featured.effect('opacity', { duration:500 }).start(0,1);
				}
			});
		});

		this.setSelected(photo);
		this.disableControls();
	},
	
	previousPhoto: function(ev) {
		ev = new Event(ev).stop();
		this.photos.each(function(photo) {
			var div = $(photo.parentNode);
			if (div.hasClass('selected')) this.originalselected = photo;
		}.bind(this));
		var previousphoto = $(this.originalselected.parentNode).getPrevious();
		if (previousphoto) this.swap(previousphoto.getElement('a'));
	},
	
	nextPhoto: function(ev) {
		ev = new Event(ev).stop();
		this.photos.each(function(photo) {
			var div = $(photo.parentNode);
			if (div.hasClass('selected')) this.originalselected = photo;
		}.bind(this));
		var nextphoto = $(this.originalselected.parentNode).getNext();
		if (nextphoto) this.swap(nextphoto.getElement('a'));
	},

	disableControls: function() {
		var currentphoto = 0;
		this.photos.each(function(photo, i) {
			var div = $(photo.parentNode);
			if (div.hasClass('selected')) {
				currentphoto = i;
			}
		}.bind(this));

		var previousPhotoLink = $E('#previousphoto a');
		var nextPhotoLink = $E('#nextphoto a');
		if (currentphoto == 0) {
			previousPhotoLink.addClass('disabled');
			nextPhotoLink.removeClass('disabled');
		} else if (currentphoto == this.photos.length - 1) {
			nextPhotoLink.addClass('disabled');
			previousPhotoLink.removeClass('disabled');
		} else {
			previousPhotoLink.removeClass('disabled');
			nextPhotoLink.removeClass('disabled');
		}
	},

	setSelected: function(selectedphoto) {
		this.photos.each(function(photo) {
			var div = $(photo.parentNode);
			if (div.hasClass('selected')) div.removeClass('selected');
		});
		$(selectedphoto).parentNode.addClass('selected');
	},
	
	closeBox: function(ev) {
		ev = new Event(ev).stop();
		self.parent.TB_remove();
	}

}); // CP.Gallery


CP.StripeTable = new Class({
	initialize: function(table) {
		if (!(table)) return false;
		var rows = table.getElements('tr');
		rows.each(function(row, i) {
			if (i % 2 != 0) { row.addClass('alt'); }
		});
	}
}); // CP.StripeTable


window.addEvent('domready', function() {
	var body = $E('body');

	if (body.hasClass('details')) {
		var Gallery = new CP.Gallery;
		var Print = new CP.Print($E('#header li.print a'));
	}
	
	if (body.hasClass('facts')) {
		var Reports = new CP.StripeTable($E('#content table'));
	}
}); // window.addEvent