$(document).ready(function(){	
	function recalculate_width(){
		var totalWidth = 0;
		$("div#featured-events-slider div.event").each(function(){
			totalWidth+= parseInt($(this).width()) + parseInt($(this).css("marginRight"));
		})

		$("div#featured-events-slider").css("width", totalWidth + 10);		
	}
	
	var loadingTimeout;
	
	$(".image-container").SexySlider($.fn.sexyslider_config);
	var initialclass = $('.image-container').find('img').eq(0).attr('class');
	$('.sexyslider-title').addClass(initialclass);
	$('.slider-control').eq(0).addClass('active');

	$("#unit-event-browser ul a").click(function(){
		if($(this).hasClass('disabled')) {
			return false;
		}
		//don't allow the user to click on the same thing twice, breaking the scrolling
		if ( $(this).hasClass("selected") ) {
			return;
		}
		var old_selected = $("#unit-event-browser li.selected").attr("id").replace(/category-idx-/, "");
		old_selected = parseInt(old_selected);
		
		loadingTimeout = setTimeout('$("div#featured-events-loading").css("display", "block")', 100);
		
		$("#unit-event-browser li.selected").removeClass("selected");
		$(this).parent().addClass("selected");
		
		var new_selected = $(this).parent().attr("id").replace(/category-idx-/, "");
		new_selected = parseInt(new_selected);

		//clear up any animations already in progress
		$("div#featured-events-slider").stop(true);
		$("div#featured-events-slider ul:not(.active-event)").remove();
				
		var category_id = $(this).attr("id").replace(/event-browser-tab-/, "");
		var category_plural = $(this).find("span:first").html();
			
		if( pageTracker ) {
			pageTracker._trackEvent('Homepage Feature Picker', 'Tab Selected', category_plural );
		}
				
		$.get("/mmlib/includes/warwick/ajax.getfeaturedevents.php", {category: category_id}, function(response){
			var eventUL = $("div#featured-events-slider div.active-event:first");
			var newLeftPosition;
			clearTimeout(loadingTimeout);

			if(new_selected > old_selected){
				$(eventUL).after(response);
				newLeftPosition = -710;//$(eventUL).width() * -1;
			}else if(new_selected < old_selected){
				$(eventUL).before(response);
				newLeftPosition = 0;
				$("div#featured-events-slider").css("left", -710);
			}else{
				return;
			}
			
			recalculate_width();
			if(category_id == "recommended"){
				category_id = "";
				category_plural = "events";
			}
			
			$("a#more-events-link").attr("href", "/events/" + category_id);
			$("a#more-events-link").html("View all " + category_plural);

			$("div#featured-events-loading").css("display", "none");
			$("div#featured-events-slider").animate({
				left: newLeftPosition
			}, 500, "swing", function(){
				if(new_selected > old_selected){
					$(eventUL).next().addClass("active-event");
				}else{
					$(eventUL).prev().addClass("active-event");									
				}
				$(eventUL).remove();
				$("div#featured-events-slider").css("left", "0");
				//$("div#featured-events-slider li").fitted();								
			});

			if( $.fn.clickable ) {
				$('#event-browser-content .event').clickable();
			}
			
			if(typeof $('.image-container').get(0) !== 'undefined') {
				$.get("/mmlib/includes/warwick/ajax.loadmainfeatureimage.php", {category: category_id}, function(response) {
				
					if(typeof $('.image-container').get(0) !== 'undefined') {
						$('.image-container').get(0).SSObject.clearslider();
					}
				
					$('#main-image').animate({
						opacity: 0
					}, 500, function() {
						$('.image-container').remove();
						$('#main-image').html(response);
						$('#main-image-navigation').html('');
					
						var sexySliderConfig = $.fn.sexyslider_config;
					
						if($('.image-container').children('img').length < 2) {
							sexySliderConfig.delay = 900000;
						} else {
							sexySliderConfig.delay = 5000;
						}
					
						$('.image-container').SexySlider(sexySliderConfig);
					
						var initialclass = $('.image-container').find('img').eq(0).attr('class');
						$('.sexyslider-title').addClass(initialclass);
						$('.slider-control').eq(0).addClass('active');
					}).animate({
						opacity: 1
					}, 500);
				
					if(typeof $('.image-container').get(0) !== 'undefined') {
						$('.slider-control').hoverIntent($.fn.hoverintent_config);
					}
				
				});
			}
		});
		
		return false;
	});
	
	recalculate_width();
	
	if(typeof $('.image-container').get(0) !== 'undefined') {
		$('.slider-control').hoverIntent($.fn.hoverintent_config);
	}
});

$.fn.sexyslider_config = {
	width : 710,
	height : 354,
	delay : 5000,
	strips : 15,
	autopause : false,
	effect : 'wave',
	titleOpacity: 1,
	onTitleHide: function() {
		var currentimg = this.img[this.imgInc];
		var currentsliderclass = $('img[src$="' + currentimg + '"]').attr('class');
		$('.sexyslider-title').attr('class', 'sexyslider-title');
		
		$('.slider-control').removeClass('active');
		$('.slider-control[rel=' + this.imgInc + ']').addClass('active');
		
		$('.sexyslider-title').addClass(currentsliderclass);
	}
}

$.fn.hoverintent_config = {
	over: function() {
		$('.image-container').get(0).SSObject.transition($(this).attr('rel'));
		$('.image-container').get(0).SSObject.holdslider();
	},
	out: function() {
		$('.image-container').get(0).SSObject.resumeslider();
	},
	interval: 250
}
