/*
	Slider class 
	============

	Sample use:
		var slideshow;
		slideshow = slider();
		slideshow.start();

*/
 

function slider()
{
	var instance_object;

	// Container
	this.container = null;

	// Base variables
	this.total_width = 0;
	this.max_height = 0;
	this.index = 0;
	this.last = 0;
	this.length = 0;
	this.width_offset = 0;

	// Base arrays
	this.imgs = [0];
	this.alts = Array();
	this.image_widths = Array();
	this.imgobj = Array();
	this.overlay_heights = Array();

	// Adjustments
	this.base_offset = 500;
	this.increment_offset = 10;
	this.inactive_opacity = .15;
	this.inactive_opacity_rate = "fast"
	this.active_opacity_rate = "fast"
	this.caption_fade_in_rate = "slow"
	this.active_opacity = 1.0;
	this.slide_rate = 300;
	this.easing_callback = 'easeInOutQuad';
	this.caption_id = '#caption';
	this.overlay_id = '#overlay';
	this.image_container = '#images';
	this.set_caption_margin = false;
	this.caption_fade_in_delay = 300;
	
}

slider.prototype.start = function ()
{

	this.total_width = $(document).width();
	this.container = $($(this.image_container));

	instance_object = this;
	counter = 0;

	this.container.find('img').each(instance_object._image_handler);
	/* container calculations */
	total_offset = this.width_offset+this.base_offset;
	position = parseInt((this.total_width - this.image_widths[this.index])/2)

	/* set container */
	this.container.css('height', this.max_height+'px');
	this.container.css('width', total_offset+'px');

	this.container.css('margin-left', position+'px');

	this.set_caption();	
}

/* Initialization methods */
		slider.prototype._image_handler = function()
		{
			var width = $(this).width();
			var height = $(this).height();
			if (instance_object.max_height < height) 
				instance_object.max_height = height;
			
			instance_object.width_offset += width+instance_object.increment_offset;
			instance_object.length++;
			instance_object.image_widths.push(width);
			instance_object.imgs.push(instance_object.width_offset);
			instance_object.imgobj.push($(this));
			instance_object.alts.push($(this).attr('alt'));
			$(this).click(instance_object._click_handler);

			if (counter != 0) 
				$(this).fadeTo(instance_object.inactive_opacity_rate, instance_object.inactive_opacity);
			counter++;		
		}

		slider.prototype._click_handler = function (e)
		{
			if (e.pageX > instance_object.total_width/2)
				instance_object.next_item();
			else
				instance_object.previous_item();
		}


slider.prototype.set_caption = function () 
{
	image_width = this.image_widths[this.index];
	left_margin = (this.total_width - image_width)/2;
	overlay_flag = false;
	raw_text = this.alts[this.index]
	clean_text = raw_text.replace('[overlay]','')

	show_id = this.caption_id
	if(clean_text != raw_text)
	{
		overlay_flag = true;
		show_id = this.overlay_id
	}

	//$(this.caption_id).css('margin-left',left_margin);
	//animate({ opacity: "hide" }, "slow");

	set_width = image_width;


	if (overlay_flag)
	{
		set_width = image_width - 70;
		left_margin = left_margin + 25;
		$(show_id).css('position','relative');
		//$(show_id).css('background-image','url("/images/white50.png")');
		clean_text = clean_text

	}

	if(this.set_caption_margin)
	{
		$(show_id).css('margin-left',left_margin);
	}

	$(show_id).css('width',set_width);


	$(show_id).html(clean_text);

	if (overlay_flag && this.set_caption_margin)
	{
		overlay_height = $(show_id).height()
		if(this.overlay_heights[this.index])
		{
			overlay_height = this.overlay_heights[this.index];
		}
		$(show_id).css('bottom',overlay_height+50)
		this.overlay_heights[this.index] = overlay_height;
	}

	$(this.overlay_id).hide()
	$(this.caption_id).hide()
	$(show_id).delay(this.caption_fade_in_delay).animate({ opacity: "show" }, this.caption_fade_in_rate,this.easing_callback);

	
}

slider.prototype.previous_item = function ()
{
	if (this.index > 0)
	{
		this.index = this.index - 1;
		this.change_item();
	}
}

slider.prototype.next_item = function ()
{
	if (this.index == this.length-1)
		this.index = 0;
	else
		this.index++;
	
	this.change_item();
}



slider.prototype.change_item = function () 
{
	if (this.last != this.index) 
		this.imgobj[this.last].fadeTo(this.inactive_opacity_rate, this.inactive_opacity);
	
	var position = parseInt( (this.total_width - this.image_widths[this.index])/2) - (this.imgs[this.index] );
	var index = this.index;
	var last = this.last

	this.set_caption();
	
	var that = this;

	this.container.animate({ marginLeft: position}, this.slide_rate, this.easing_callback, function()
	{
		that.container.css('margin-left',position+'px');	
		that.imgobj[index].fadeTo(that.active_opacity_rate, that.active_opacity);
	});	
	
	this.last = this.index;
}



var slideshow;

function initial_slideshow(){
	slideshow = new slider();

	slideshow.active_opacity_rate = "slow"
	slideshow.slide_rate = 400;
	slideshow.inactive_opacity = .18;
	slideshow.set_caption_margin = true;
	slideshow.start()
}

$(document).ready(initial_slideshow);

// #EMAIL OBFUSCATION

function show_email(string)
{
	var string_backwards = "";
	var email;
	for(count=string.length; count >= 0; count--)
	{
	string_backwards+=string.substring(count,count-1);
	}
	email = string_backwards.replace(/_at_/g,'@').replace(/_dot_/g,'.')
	return email;
}
