var Display = function(element) {
	var _element = null;
	
	var _clickable = false;
	
	var _pointer = 0;
	
	var _images = [];
	var _urls = [];
	var _description = [];
	
	var _animate = 200;
	var _halfTime = 100;
	var _delay = 3000;
	
	var _rotating = false;
	var _interval = null;
	
	var _handleImage = function(index) {
		var imageContainer = _element.find('.displayImage');
		
		if(imageContainer.length) {
			imageContainer.fadeOut(_halfTime, function(){
				_emptyContainers();
				imageContainer.css('display','block');
				
				var imageCollection = $(_images[index]);
				
				imageContainer.append(imageCollection);
				
				if(imageCollection.length) {
					imageCollection.css('display', 'none');
					imageCollection.fadeIn(_halfTime);
				}
			});
		}
	};
	var _handleUrl = function(index) {
		var anchorContainer = _element.find('.displayRead');
		
		if(anchorContainer.length) {
			if(anchorContainer.find('a').length) {
				anchorContainer.find('a').attr('href', _urls[index]);
			}
		}
	};
	
	var _iteration = function() {
		_advancePointer();
		_switchTo(_pointer);
	};
	
	
	var _switchTo = function(index) {
		_clickable = false;
		
		_element.find('.displayButtons').find('.displayButton').removeClass('greenButton15inactive').removeClass('greenButton15active').addClass('greenButton15inactive');
		_element.find('.displayButton_'+index).removeClass('greenButton15inactive').addClass('greenButton15active');
		
		_handleImage(index);
		_handleUrl(index);
		
		setTimeout(function() {
			_clickable = true;
		}, _animate);
	};
	
	var _advancePointer = function() {
		if(_pointer < (_images.length-1)) {
			_pointer++;
		}
		else {
			_pointer = 0;
		}
	};
	
	var _emptyContainers = function() {
		_element.find('.displayImage').empty();
	};
	
	var _bind = function() {
		_element.find('.displayButtons a').bind("click", function() {
			if(_clickable) {
				var button = $(this);
				var destinationIndex = button.attr("class").split(" ");
				
				for(var i = 0;i < destinationIndex.length;i++) {
					if(destinationIndex[i].indexOf('displayButton_') != '-1') {
						destinationIndex = parseInt(destinationIndex[i].split("_")[1]);
					}
				}
				
				_switchTo(destinationIndex);
				_pointer = destinationIndex;
			}
			
			return false;
		});
		_element.find('.displayImage img').live("mouseover", function() {
			if(!_rotating) {
				_rotating = true;
				
				_interval = setInterval(function(){
					_iteration();
				}, _delay);
			}
		});
		_element.find('.displayImage img').live("mouseout", function() {
			if(_rotating) {
				clearInterval(_interval);
				_rotating = false;
			}
		});
	};
	var _unbind = function() {
		_element.find('.displayButtons a').unbind("click");
		_element.find('.displayImage img').die("mouseover");
		_element.find('.displayImage img').die("mouseout");
	};
	
	var __construct = function(element) {
		if(typeof element != "object") {
			throw "Invalid argument: element value must contain a jQuery element with a display class";
		}
		
		if(element.length > 0 && element.hasClass('display')) {
			_element = element;
		}
		
		else if(element.length == 0) {
			throw "Invalid argument: required display element could not be found.";
		}
		else if(!element.hasClass('display')) {
			throw "Invalid argument: required display element does not contain the required CSS display class.";
		}
		else {
			throw "Invalid argument: element.";
		}
		
		_element.find('.displayButtons').empty();
	};
	
	this.initialize = function() {
		_switchTo(0);
	};
	
	this.add = function(image, url, description) {
		if(typeof image != "string") {
			throw "Invalid argument: image value must contain an image or false value.";
		}
		
		if(typeof url != "string") {
			throw "Invalid argument: url value must contain a string.";
		}
		
		if(typeof description != "string") {
			throw "Invalid argument: description value must contain string or false value.";
		}
		
		_unbind();
		
		_images.push(image);
		_urls.push(url);
		_description.push(description);
		
		_emptyContainers();
		
		var buttonMargin = "";
		
		if(_images.length > 1) {
			buttonMargin = "ml10";
		}
		
		_element.find('.displayButtons').append('<a class="greenButton15 displayButton displayButton_'+(_images.length - 1)+' greenButton15inactive ml5 '+buttonMargin+'" href="#"></a>');
		
		_bind();
	};
	
	this.setAnimation = function(ms) {
		_animate = ms;
		_halfTime = (parseInt(ms)/2);
	};
	this.getAnimation = function() {
		return _animate;
	};
	
	this.setDelay = function(ms) {
		_delay = ms;
	}
	this.getDelay = function() {
		return _delay;
	};
	
	__construct(element);
};
