$(function(){
	$('#news_container').cycle({
		fx:     'fade',
		timeout: 6000, 
		delay:  -2000,
		next: 	"#news_next_button", 
		prev: 	"#news_prev_button"
	});
	    
	$('#events_container').cycle({
		fx:      'scrollHorz',
		timeout: 0, 
		prev:    '#event_prev_link',
		next:    '#event_next_link'
	});
	
	/**
	Detect and configure behavior for the home page slideshow.
	*/
	$.extend(featured, {
		interval: false,
		timer: 8000,
		paused: false,
		theIndex: -1,
		choose: function(index){
			this.theIndex = index;
			var data = this.getSlide(index);
			this.each(function(){
				$(this).css("background-image", "url("+featured.getSlide(index + 1).image+")");
				$("#parent_featured_toggler a").filter('.active').removeClass('active').end().eq(((index-1) % featured.data("slides").length)).addClass('active');
				$("#parent_featured_content .title", this).text(data.title);
				$("#parent_featured_content > .content p", this).html(data.content);
				$("#featured_new_img", this).show().attr("src", data.image);
        $("#parent_featured div.content, #parent_featured_toggler a.active").css('opacity', 0.85);
			});
			// this.play();
		},
		nextSlide: function(){
			if(!this.paused){
				$("#featured_new_img", this).fadeOut("slow", function(){
					featured.choose(featured.theIndex + 1);
				});
			}
		},
		pause: function(){
			// handle pause/play link.
			$(".featured_pauseplay", $(this)).text('play').unbind('click').bind('click', function(e){
				e.preventDefault();
				featured.play();
			});
			// do pause action.
			this.paused = true;
			clearInterval(this.interval);
		},
		play: function(){
			// handle pause/play link.
			$(".featured_pauseplay", $(this)).text('pause').unbind('click').bind('click', function(e){
				e.preventDefault();
				featured.pause();
			});
			// do play action.
			this.paused = false;
			if(this.interval != false){
				clearInterval(this.interval);
			}
			this.setTheInterval();
		},
		getSlide: function(index){
			return this.data("slides")[((index-1) % this.data("slides").length)];
		},
		setTheInterval: function(){
			this.interval = setInterval(function(){
				featured.nextSlide();
			}, this.timer);
		}
	});
	$.each(featured.data("slides"), function(i,n){
		var index = i+1;
		// preload images.
		var img = new Image();
		img.src = n.image;
		// populate togglers.
		$('<a href="#"></a>').text(index).bind('click', function(e){
			e.preventDefault();
			featured.pause();
			featured.choose(index);
		}).appendTo($("#parent_featured_toggler"));
	});
	// choose the first one on page load.
	featured.choose(1);
	featured.play();
	
	/**
	Detect and configure behavior for "home_secondary" blocks
	*/
	var secondary = $("#parent_featured #featured_blocks");
	$.extend(secondary, {
		theIndex: -1,
		triggers: [],
		contentareas: [],
		init: function(){
			// initialize
			var ref = this;
			this.triggers = $("h3.title a", $(this));
			this.contentareas = $("div.home_secondary", $(this));
			if(this.triggers.length != this.contentareas.length){
				this.log("Not the same amount of content areas as triggers.");
			}
			this.triggers.click(function(e){
				e.preventDefault();
				ref.choose(ref.triggers.index(this) + 1);
			});
		},
		choose: function(index){
		  if(this.theIndex == index){
		    return false;
		  }
			this.theIndex = index;
			var chosen = this.getSlide(this.theIndex);
			var trigger = chosen[0], contentarea = chosen[1];
			this.triggers.parent().removeClass('active');
			trigger.parent().addClass('active');
			this.contentareas.hide();
			contentarea.show();
			return true;
		},
		getSlide: function(index){
			if(this.triggers.length == 0 && this.contentareas.length == 0){
				// initialize
				this.init();
			}
			var ind = ((index-1) % this.triggers.length);
			if(typeof(this.triggers.eq(ind)) == 'undefined' || typeof(this.contentareas.eq(ind)) == 'undefined'){
			  return false;
			}
			return [this.triggers.eq(ind), this.contentareas.eq(ind)];
		}
	});
	secondary.choose(1);
	
});
