var rotation_time = 6500;
var rotateMe = 0;
var is_carousel = false;
function attach_click_event_to_li() {
	jQuery("ol.carousel > li > a.standard-click").click(function(){
		
		//if(isrotating==false)omnitureTrack("tdi:main:toggle");
		jQuery(this).parent().siblings().children().removeClass("active");
		jQuery(this).addClass("active");
		var targetId = jQuery(this).attr("id").substring(("featured".length -1));
		switch_featured_story(targetId);
	});
	jQuery("ol.carousel > li > a.carousel-fwd").click(function(){
		//if(isrotating==false)omnitureTrack("tdi:main:toggle");
		click_next();
		return false;
	});
	jQuery("ol.carousel > li > a.carousel-back").click(function(){
		//if(isrotating==false)omnitureTrack("tdi:main:toggle");
		var active_button = jQuery("ol.carousel > li > a.active");
		var parent = active_button.parent();
		var index = jQuery("ol.carousel > li").index(parent);
		var button_count = active_button.parent().siblings().size();
		if ( index == 1 ) {
			// it is the first button, go to the last
			var button_to_click = (button_count - 1);
		}
		else {
			// it is not the first, so move back one
			var button_to_click = (index - 1);
		}
		jQuery("ol.carousel > li:eq(" + button_to_click + ") > a").trigger("click");

		return false;
	});
}
var firstfilter=0;
function click_featured_story_filter_button(list) {
	// OMNITURE TRACKING, CALLING /js/omnituretrack.js
	if(list=="truth" && firstfilter==1){
		omnitureTrack("tdi:main:truths");
	}else if(list=="dare" && firstfilter==1){
		omnitureTrack("tdi:main:dares");
	}else if(list=="all" && firstfilter==1){
		omnitureTrack("tdi:main:allstories");
	}
	if(firstfilter==0)firstfilter=1;
	
	var active_cat = get_active_filter_button();
	if (active_cat != list) {
		// deactivate buttons
		//jQuery("ul.story-toggles > li > a.active").removeClass("active");
		// addClass to the one that was clicked
		//jQuery("li.story-toggles-"+list+" > a").addClass("active");
		
		if (jQuery("ul#featured-story-list-"+list).children().length > 2) {
			// Inside here is for if there are featured stories for that category...
			if (is_carousel) {
				// Carousel is on home page ... getting into this case means you
				//   are on the HOME PAGE
				stop_rotation();
				jQuery("ol.carousel").fadeOut("normal",function(){
					jQuery(this).html(jQuery("#featured-story-list-"+list).html());
					attach_click_event_to_li();
					jQuery("ol.carousel > li:eq(1) > a").addClass("active");
					jQuery(this).fadeIn("normal",function(){
						get_first_story();
						filter_posts(list); 
						change_nav_buttons(list);
						// Do a check here to see if there's only one story
						//   If there's only one story, then don't restart the rotation.
						if (jQuery("#featured-story-list-"+list).children().length > 3) {
							start_rotation();
						}
					});
				});
			}
			else {
				// This case means that you are not on the homepage, 
				//   but on a paginated one.
				filter_posts(list);
				change_nav_buttons(list);
			}
		}
		else {
			// You got here because there were no featured posts in the category
			//   But we still need to filter the rest of the list...
			if (is_carousel) {
				// On home page so we have to do the carousel bit
				stop_rotation();
				jQuery("ol.carousel").fadeOut("normal",function(){
					jQuery("#target-for-featured-posts").fadeOut(function(){
						jQuery(this).html('').fadeIn();
					});
					filter_posts(list);
					change_nav_buttons(list);
				});
			}
			else {
				// Not on home page, so we can just filter.
				filter_posts(list);
				change_nav_buttons(list);
			}
		}
		return false;
	}
	return false;
}
function get_active_filter_button() {
	return jQuery("ul.story-toggles > li > a.active").parent().attr("class").substring(17);
}
function filter_posts(list) {
	if (list == "all") {
		if (jQuery(".post-type-truth").is(":hidden")) {
			jQuery(".post-type-truth").fadeIn();
		}
		if (jQuery(".post-type-dare").is(":hidden")) {
			jQuery(".post-type-dare").fadeIn();
		}
	}
	else if (list == "truth") {
		if (jQuery(".post-type-dare:visible").length == 0) {
			jQuery(".post-type-truth:hidden").fadeIn();			
		}
		else {
			jQuery(".post-type-dare:visible").fadeOut("normal",function(){
				jQuery(".post-type-truth:hidden").fadeIn();			
			});
		}
	}
	else if (list == "dare") {
		if (jQuery(".post-type-truth:visible").length == 0) {
			jQuery(".post-type-dare:hidden").fadeIn();
		}
		else {
			jQuery(".post-type-truth:visible").fadeOut("normal",function(){
				jQuery(".post-type-dare:hidden").fadeIn();
			});		
		}
	}
	return;
}
function switch_featured_story(targetId) {
	var newHtml = jQuery("#featured-"+targetId).html();
	jQuery("#target-for-featured-posts").fadeOut("normal",function(){
		jQuery(this).html(newHtml);
		
		jQuery(this).fadeIn("normal");
		
		//fix for IE 6
		var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
		if(badBrowser){
			jQuery(this).find('.entry-background').css({height:(jQuery(this).find('.entry-background').parent().height() + 24) + "px"});
		}
		
		jQuery(this).find('.video-image').click(function(e) {
			e.preventDefault();
			jQuery(this).hide();
			jQuery(this).parent().find('.video-media').show();
			jQuery(this).parent().parent().parent().find('.entry-summary').hide();
			jQuery('.module-featured-collapsed').removeClass('module-featured-collapsed');
			stop_rotation();
		});
	});
}
function get_first_story() {
	var targetId = jQuery("ol.carousel > li:eq(1) > a").addClass("active").attr("id").substring(("featured".length -1) );
	switch_featured_story(targetId);
}
function rotate_featured_stories() {
	var active_button = jQuery("ol.carousel > li > a.active");
	var parent = active_button.parent();
	var index = jQuery("ol.carousel > li").index(parent);
	var button_count = active_button.parent().siblings().size();
	if ( index == (button_count - 1) ) {
		// it is the last button, go to the first
		var button_to_click = 1;
	}
	else {
		// it is not the last, so move on one
		var button_to_click = (index + 1);
	}
	jQuery("ol.carousel > li:eq(" + button_to_click + ") > a").trigger("click");
}
function click_next() {
	rotate_featured_stories();
}
var isrotating=false;
function start_rotation() {
	isrotating=true;
	rotateMe = setInterval("rotate_featured_stories()",rotation_time);
	jQuery("#featured-story-module-inside").hover(
		function(){
			stop_rotation();
		},
		function(){
			return;
		}
	);
}
function stop_rotation() {
	isrotating=false;
	clearInterval(rotateMe);
}
function change_nav_buttons(filter) {
		// Get original link and append the #truth or #dare
		if(jQuery("a.button-fwd").size()) {
			// do stuff for fwd button here
			anchor_fwd = jQuery("a.button-fwd");
			existing_link_fwd = anchor_fwd.attr("href");
			existing_link_fwd_hash_loc = existing_link_fwd.indexOf("#");
			if ( existing_link_fwd_hash_loc == -1 ) {
				anchor_fwd.attr("href",existing_link_fwd+"#"+filter);				
			}
			else if ( filter == "truth" || filter == "dare" ) {
				// existing link has hash already, strip and then append
				anchor_fwd.attr("href",existing_link_fwd.substring(0,existing_link_fwd_hash_loc)+"#"+filter);
			}
			else if ( filter == "all" ) {
				anchor_fwd.attr("href",existing_link_fwd.substring(0,existing_link_fwd_hash_loc));
			}
		}
				
		if(jQuery("a.button-back").size()) {
			// do stuff for back button here
			anchor_back = jQuery("a.button-back");
			existing_link_back = anchor_back.attr("href");
			existing_link_back_hash_loc = existing_link_back.indexOf("#");
			if ( existing_link_back_hash_loc == -1 ) {
				anchor_back.attr("href",existing_link_back+"#"+filter);
			}
			else if ( filter == "truth" || filter == "dare" ) {
				// existing link has hash already, strip and then append
				anchor_back.attr("href",existing_link_back.substring(0,existing_link_back_hash_loc)+"#"+filter);
			}
			else if ( filter == "all" ) {
				anchor_back.attr("href",existing_link_back.substring(0,existing_link_back_hash_loc));
			}
		}
}
function parse_url_for_filter() {
	hash_location = document.location.href.indexOf("#");
	if ( hash_location == -1 ) {
		return "all";
	}
	else {
		return document.location.href.substring((hash_location + 1));
	}
}
jQuery(function(){
	attach_click_event_to_li();
	// Check for carousel
	if (jQuery("ol.carousel").is("ol")) {
		// There is a carousel so do the story, and rotation etc... 
		//   (means we're on the HOME PAGE)
		is_carousel = true;
		if (jQuery("#featured-story-list-all").children().length > 3) {
			start_rotation();
		}
		
		jQuery("#target-for-featured-posts embed").click(function() {
			stop_rotation();
		})
		
		jQuery(".module-featured").hover(
			function(){
				stop_rotation();
			},
			function(){
				return;
			}
		);
		
		//for initial load only
	jQuery(this).find('.video-image').click(function(e) {
		e.preventDefault();
		jQuery(this).hide();
		jQuery(this).parent().find('.video-media').show();
		jQuery(this).parent().parent().parent().find('.entry-summary').hide();
		jQuery('.module-featured-collapsed').removeClass('module-featured-collapsed');
		stop_rotation();
	});
	
	current_filter = parse_url_for_filter();
	jQuery("li.story-toggles-"+current_filter+" > a").trigger("click");
	
	var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	if(badBrowser){
		fixCarouselBG();
	}
	}
	
	//get_first_story();
});

function fixCarouselBG(){
	jQuery('.module-featured').find('.entry-background').css({height:(jQuery('.module-featured').find('.entry-background').parent().height() + 24) + "px"});
}

function onYouTubePlayerReady(playerId) {
	ytplayer = document.getElementById("youtube-"+playerId);
	ytplayer.playVideo();
	alert('ready');
}


