/*
portfolio.js
Steve Boerner
Modified Nov. 29, 2010
*/

var PortfolioXML = function (theFile, random) {
	var that = this;
	this.collection;
	this.index;
	this.toggle;
	this.a;
	this.b;
	this.random = random;
	
	$.ajax({
		type: 'GET',
		url: theFile,
		dataType: 'xml',
		error: that.error,
		success: function (data) {
			that.success(data);
		}
	});
}

PortfolioXML.FADE = 500;

PortfolioXML.prototype.error = function () {
	alert('Error: Unable to load XML file.');
}

PortfolioXML.prototype.success = function (data) {
	var that = this;
	var cache = [];
	this.toggle = true;
	this.collection = $(data).find('image');
	// Preload images
	var collection_length = this.collection.length;
	for (var i = collection_length - 1; i >= 0; i--) {
		var image = document.createElement('img');
		image.src = this.collection.eq(i).attr('path');
		cache.push(image);
	}
	// Display initial image
	this.index = this.random?
	Math.floor(Math.random() * (this.collection.length)): 0;
	this.update(this.collection.eq(this.index));
	// Play button handlers
	$('#btn_forward a').click(function () {
		that.index = that.random?
		Math.floor(Math.random() * (that.collection.length)):
		that.inc();
		that.update(that.collection.eq(that.index));
	});
	$('#btn_back a').click(function () {
		that.index = that.random?
		Math.floor(Math.random() * (that.collection.length)):
		that.dec();
		that.update(that.collection.eq(that.index));
	});
	$('#btn_start a').click(function () {
		that.index = 0;
		that.update(that.collection.eq(that.index));
	});
	$('#btn_end a').click(function () {
		that.index = that.collection.length - 1;
		that.update(that.collection.eq(that.index));
	});
	// Collection button handlers
	var set_collection = function (collection) {
		that.collection = collection;
		that.index = 0;
		that.update(that.collection.eq(that.index));
	}
	$('#btn_magazines a').click(function () {
		set_collection($(data).find('image[category="magazines"]'));
	});
	$('#btn_brochures a').click(function () {
		set_collection($(data).find('image[category="brochures"]'));
	});
	$('#btn_annual a').click(function () {
		set_collection($(data).find('image[category="annual_reports"]'));
	});
	$('#btn_invitations a').click(function () {
		set_collection($(data).find('image[category="invitations"]'));
	});
	$('#btn_posters a').click(function () {
		set_collection($(data).find('image[category="posters"]'));
	});
	$('#btn_all a').click(function () {
		set_collection($(data).find('image'));
	});
}

PortfolioXML.prototype.update = function (image) {
	var that = this;
	this.a = (this.toggle)? 0: 1;
	this.b = (this.toggle)? 1: 0;
	this.toggle = ! this.toggle;
	// Fade out and remove previous image and text
	$('#main_image .img_wrapper').eq(this.a).fadeOut(PortfolioXML.FADE, 'swing', function () {
		$('#main_image .img_wrapper').eq(that.a).find('span.title').remove();
		$('#main_image .img_wrapper').eq(that.a).find('span.description').remove();
		$('#main_image .img_wrapper').eq(that.a).find('span.contributor').remove();
		$('#main_image .img_wrapper').eq(that.a).find('img.image').attr('src', '');
	});
	// Path to new image
	var path = $(image).attr('path');
	$('#main_image .img_wrapper').eq(this.b).find('img.image').attr('src', path);
	// Optional image attributes: title, description, contributor
	if ($(image).find('title').length !== 0) {
		var title = $(image).find('title').text();
		$('#main_image .img_wrapper').eq(this.b).find('div.title').append('<span class="title">' + title + '</span>');
	}
	if ($(image).find('description').length !== 0) {
		var description = $(image).find('description').text();
		$('#main_image .img_wrapper').eq(this.b).find('div.description').append('<span class="description">' + description + '</span>');
	}
	if ($(image).find('contributor').length !== 0) {
		var contributor = $(image).find('contributor').text();
		$('#main_image .img_wrapper').eq(this.b).find('div.contributor').append('<span class="contributor">' + contributor + '</span>');
	}
	// Fade in new image
	$('#main_image .img_wrapper').eq(this.b).fadeIn(PortfolioXML.FADE, 'swing');
}

PortfolioXML.prototype.inc = function () {
	this.index += 1;
	if (this.index === this.collection.length) {
		this.index = 0;
	}
	return this.index;
}

PortfolioXML.prototype.dec = function () {
	this.index -= 1;
	if (this.index < 0) {
		this.index = this.collection.length - 1;
	}
	return this.index;
}

var Button = function (name, parent) {
	var that = this;
	var cache = [];
	this.name = name;
	this.parent = parent;
	this.s1;
	// Up
	this.s2;
	// Over
	this.s3;
	// Down
	
	// Preload images
	for (var i = 3; i > 0; i--) {
		var image = document.createElement('img');
		image.src = this[ "s" + i] = "sbtd_img/" + name + "_s" + i + ".png";
		cache.push(image);
	}
	// Display up state
	$(this.parent + ' a img').attr('src', "sbtd_img/" + name + "_s1.png");
	// Event handlers
	$(this.parent).mouseover(function () {
		$(that.parent + ' a img').attr('src', that.s2);
	});
	$(this.parent).mouseout(function () {
		$(that.parent + ' a img').attr('src', that.s1);
	});
	$(this.parent).mousedown(function () {
		$(that.parent + ' a img').attr('src', that.s3);
	});
	$(this.parent).click(function () {
		$(that.parent + ' a img').attr('src', that.s1);
	});
}


