var siteEvents = Class.create({
	
	initialize: function() {
		Event.observe(window, 'load', this.bindInteractionEvents.bindAsEventListener(this));
		this.string = 'hello';
		this.curLink = 'Community Outreach';
		this.delayImageHtml;
		this.delayTitle;
		this.delayContent;
		this.canClick = true;
		this.pendingClick = null;
		var pic1 = new Image();
		pic1.src = 'images/clearlink-cares/slider-img-outreach.jpg';
		var pic2 = new Image();
		pic2.src = 'images/clearlink-cares/slider-img-environment.jpg';
		var pic3 = new Image();
		pic3.src = 'images/clearlink-cares/slider-img-education.jpg';
		var pic4 = new Image();
		pic4.src = 'images/clearlink-cares/slider-img-wellness.jpg';
		
		this.linkHasBeenClicked = false;
		
		this.initialTransition;
	},
	
	bindInteractionEvents: function() {
		$('slider_community').observe('click', this.sliderLinkClicked.bindAsEventListener(this));
		$('slider_environment').observe('click', this.sliderLinkClicked.bindAsEventListener(this));
		$('slider_education').observe('click', this.sliderLinkClicked.bindAsEventListener(this));
		$('slider_wellness').observe('click', this.sliderLinkClicked.bindAsEventListener(this));
		
		$('slider_community').down().removeAttribute('href');
		$('slider_environment').down().removeAttribute('href');
		$('slider_education').down().removeAttribute('href');
		$('slider_wellness').down().removeAttribute('href');
		
		this.initialTransition = new PeriodicalExecuter(this.autoTransition.bindAsEventListener(this), 5);
	},
	
	autoTransition: function() {
		var nextEl = this.getNextAutoTransitionElement();
		if(this.linkHasBeenClicked) {
			this.initialTransition.stop();
		}
		else {
			this.fade(nextEl);
		}			
	},
	
	getNextAutoTransitionElement: function() {
		switch(this.curLink) {
			case 'Community Outreach':
				return $('slider_environment').down();
			case 'Environment':
				return $('slider_education').down();
			case 'Education':
				return $('slider_wellness').down();
			default:
				return $('slider_community').down();
		}
	},
	
	sliderLinkClicked: function(event) {
		this.linkHasBeenClicked = true;		
		var link = Event.element(event);
		if (link.innerHTML != this.curLink) {
			this.fade(link);
		}
	},
	
	fade: function(link) {
		if (this.canClick) {
			this.canClick = false;
			
			link.up().siblings().each(function(el){
				el.removeClassName('selected');
			}, this);
			link.up().addClassName('selected');
			var el = $('slider_img');
			this.getNewImage(link.innerHTML);
			this.curLink = link.innerHTML;
			el.fade();
			$('slider_content').fade({
				afterFinish: this.appear.bindAsEventListener(this)
			});
		}
		else {
			this.pendingClick = link;
		}
	},
	
	appear: function(newHtml) {
		var el = $('slider_img');
		el.innerHTML = this.delayImageHtml;
		$('slider_content').down().down().innerHTML = this.delayTitle;
		$('slider_content').down().next().innerHTML = this.delayContent;
		el.appear();
		$('slider_content').appear({afterFinish: this.endAppear.bindAsEventListener(this)});
	},
	
	endAppear: function() {
		if(this.pendingClick!=null && this.pendingClick.innerHTML==this.curLink) {
			this.pendingClick = null;
		}
		this.canClick = true;
		if(this.pendingClick != null) {
			this.fade(this.pendingClick);
		}
	},
	
	getNewImage: function(linkText) {
		switch(linkText) {
			case 'Community Outreach':
				this.delayImageHtml = '<img src="images/clearlink-cares/slider-img-outreach.jpg"/>';
				this.delayTitle = 'Community Outreach';
				this.delayContent = 'CLEARLINK believes that creating a better world starts at the local level. Our employees are active participants in our communities - involved in everything from food drives and homeless shelters, to clothing drives for those who are in need. CLEARLINK encourages employees to volunteer by promoting opportunities and by making it easy for employees to participate. <span class="learn_more"><a href="http://www.clearlinkcares.org/community-outreach.html">Learn More</a></span>';
				break;
			case 'Environment':
				this.delayImageHtml = '<img src="images/clearlink-cares/slider-img-environment.jpg"/>';
				this.delayTitle = 'Environment';
				this.delayContent = 'Environmental education is one of our top priorities at CLEARLINK. We provide outreach programs to our employees giving everyone an opportunity to do their part in taking care of the environment. CLEARLINK has an extensive recycling program as part of our &quot;Green&quot; Initiative and engages in doing whatever we can to connect with our environment. <span class="learn_more"><a href="http://www.clearlinkcares.org/environment.html">Learn More</a></span>';
				break;
			case 'Education':
				this.delayImageHtml = '<img src="images/clearlink-cares/slider-img-education.jpg"/>';
				this.delayTitle = 'Education';
				this.delayContent = 'Education is top priority in today&rsquo;s world, which is why CLEARLINK makes it possible for our employees to accomplish all of their educational goals. With tuition reimbursement programs, continuing education and career advancement seminars for our employees, CLEARLINK wants to provide every opportunity to our employees. <span class="learn_more"><a href="http://www.clearlinkcares.org/education.html">Learn More</a></span>';
				break;
			default:
				this.delayImageHtml = '<img src="images/clearlink-cares/slider-img-wellness.jpg"/>';
				this.delayTitle = 'Wellness';
				this.delayContent = 'CLEARLINK is focused on putting the health of our employees at the forefront. Having a healthy body and mind are key to being successful in whatever you do, that is why CLEARLINK offers discounted gym memberships, has an onsite health center for all employees to use, and provides a nutritional specialist for those who are interested. CLEARLINK is committed to the health and well-being of our employees. <span class="learn_more"><a href="http://www.clearlinkcares.org/wellness.html">Learn More</a></span>';
		}
	}
	
});

new siteEvents();