//Author: Kim Ronemus Design
//Project: Featured Articles Gallery
//Version: .05

function KRD_Featured() {
	this.image;
	this.link;
	this.title;
	this.numArticles;
	this.eventStyle = 'mouseover';
	this.durationIn	=	1.0;
	this.durationOut = 	0.5;
	this.titling 	=	true;
	this.current	= 	0;
	this.buttons;
	this.curButton;
	this.old;
	this.load = init;
	this.buttonSet = installButtons;
	this.gallery = $('featured_article').immediateDescendants();
	this.showImage = showImage;
	this.hideImage = hideImage;
	this.setImage = setImage;
	this.setLink = setLink;
	this.setTitle = setTitle;
	
}

function init() {
	this.numArticles = $('featured_article').immediateDescendants().length;
	this.gallery = $('featured_article').immediateDescendants();
	this.numArticles = this.gallery.length;
	this.buttons = "<p id=\"buttons\">";
	for(i=0; i<this.numArticles; i++) {
		var num = (i+1);
		var id = this.gallery[i].id;
		if(i != 0) {
			$(id).hide();
		}
		this.buttons += "<a href=\"#\" id=\"a"+num+"\">&bull\;</a>";
	}
	this.buttons += "<span id=\"titling\"></span></p>";
	this.buttonSet();
	this.setLink();
	
	if(this.titling) {
		this.setTitle();	
	}
}



function installButtons() {
	//alert(this.buttons);
	new Insertion.Bottom('featured_article', this.buttons);
	for(i=0; i<this.numArticles; i++) {
		var num = i+1;
		var id = 'a'+num;
			$(id).observe(this.eventStyle, gotoFeature.bind(features));

	}
}		


function gotoFeature(e) {
	var element = Event.element(e);
	var clicked = element.id.substring(1,2) - 1;
	this.old = this.current;
	this.current = clicked;
	var old = $(this.gallery[this.old].id).down(0).src;
	this.image = $(this.gallery[this.current].id).down(0).src;
	if(this.old != this.current) {
		this.showImage();
		this.hideImage();
		this.setLink();
		if(this.titling) {
			this.setTitle();
		}
		
	}

	
}

function showImage() {
	//alert(this.duration);
	var id = this.gallery[this.current].id;
	new Effect.Appear(id, {
		duration: this.durationIn
	});
}

function hideImage() {
	var id = this.gallery[this.old].id;
	new Effect.Fade(id, {
		duration: this.durationOut
	});
}

function setImage(img) {
	this.image = img;
}

function setTitle() {
	this.title = $(this.gallery[this.current].id).down(2).title;
	$('titling').innerHTML = this.title;
	
	
}

function setLink() {
	this.link = $(this.gallery[this.current].id).down(2).href;
	this.curButton = $('buttons').down(this.current).id;
	$(this.curButton).href = this.link;
}

var features = new KRD_Featured();
features.load();



