var index = 0;

$(document).ready(function(){

	
	// handles the top slice

	$('#fhs').cycle({  // change to '#fhs ul' for new carousel version
		fx:     'scrollHorz', 
		prev:   '#featured-humid a.prev', 
		next:   '#featured-humid a.next',
		speed:  500, 
		timeout: 0 
	});

	$('#featured-humid a').click(function(){
		
		track_event('Featured Humid Widget', 'Browse', null, null);
	});

	$('#featured-humid .fh').not('#featured-humid .ad').click(function(){
		
		window.location = $(this).find('a:last').attr('href');
	});




	// handles the spotlight-features
	
	$('#spotlight-features').data('curr', 0);
	$('#spotlight-features').data('offset', 0);
	$('#spotlight-features').data('count', $('#spotlight-features div').length);

	$('#spotlight-features') 
		.after('<div id="spot-nav">') 
		.cycle({ 
			fx:     'scrollHorz',
			speed:  450, 
			timeout: 9000,
			after:  change_spot,
			pager:  '#spot-nav' 
	});

	function change_spot(){
		
		var rel = $(this).attr('rel');
		do_change_spot(rel);
	}

	function do_change_spot(rel){
		
		$('#spot-nav a').find('img').show();
		$('#spot-nav a').removeClass('activeSlide');
		$('#spot-nav a:eq(' + rel + ')').find('img').hide();
		$('#spot-nav a:eq(' + rel + ')').addClass('activeSlide');

		$('#spotlight-features').data('curr', rel);
		
		var n_per_view	= 7;
		var count		= parseInt($('#spotlight-features').data('count'));
		var offset		= parseInt($('#spotlight-features').data('offset'));

		if ((offset * -1) + n_per_view - 1 == rel){ // furthest right has been selected

			if (rel != count - 1){

				offset = offset - 1;
				slide_spots(offset);
			}
		
		} else if ((offset * -1) == rel){ // furthest left has been selected

			if (rel != 0){

				offset = offset + 1;
				slide_spots(offset);
			}
		}

		if (rel > 0){

			$('#spot-left').show();
		
		} else {
			
			$('#spot-left').hide();
		}

		if (rel < count -1){

			$('#spot-right').show();
		
		} else {
			
			$('#spot-right').hide();
		}

		track_event('Home Spotlight', 'Browse', null, null);
	}

	function slide_spots(offset){
		
		var gutter = 85;
		$('#spot-slide').animate({ left : offset * gutter + 'px' }, 300);
		$('#spotlight-features').data('offset', offset);
	}

	$('#spotlight-features a').not('.edit').each(function(i){
		
		var r		= env == 'dev' ? 'http://nola.humidbeings.com' : '';
		var show	= i > 0 ? '' : 'style="display: none;"';
		$('#spot-nav a:eq(' + i + ')').html('<img ' + show + ' src="/_ui/images/spot-overlay.png" width="75" height="55" border="0" />');
		$('#spot-nav a:eq(' + i + ')').css('background', '#FFFFFF url(' + r + '/_user/images/thumbs/' + $(this).attr('rel') + ')');
		$('#spot-nav a:eq(' + i + ')').attr('rel', i);
	});

	$('#spot-nav a').hover(
		
		function(){
			
			$(this).find('img').hide();
		},
		
		function(){
		
			if (!$(this).hasClass('activeSlide')){
				
				$(this).find('img').show();
			}
	});

	$('#spot-nav a').click(function(){
		
		$('#spotlight-features').cycle('pause');
		$(this).blur();
	});

	$('#spot-nav').wrap("<div id=\"spot-slide\"></div>");
	$('#spot-slide').wrap("<div id=\"spot-cont\"></div>");
	$('#spot-cont').wrap("<div id=\"spot-outer\"></div>");
	$('#spot-outer').prepend('<div id="spot-left" style="display: none;"></div>');
	$('#spot-outer').append('<div id="spot-right"></div>');

	$('#spot-left, #spot-right').click(function(){
		
		var curr = parseInt($('#spotlight-features').data('curr'));
		var count = parseInt($('#spotlight-features').data('count'));
		
		if ($(this).is('#spot-left')){

			var new_spot	= curr == 0 ? count - 1 : curr - 1;
		
		} else {
			
			var new_spot	= curr == count - 1 ? 0 : curr + 1;
		}

		$('#spot-nav a:eq(' + new_spot + ')').trigger('click');
	});



	
	// handles the originals / features browser	

	$('li.original-nav a').click(function(){
		
		index		= parseInt($(this).attr('rel'));
		switch_original(index);

		track_event('Videos Browser Widget', 'Browse', null, null);

		$(this).blur();
		return false;
	});

	$('ul.pager li.prev a').click(function(){
		
		index = index > 0 ? index - 1 : originals.length - 1;
		switch_original(index);
		track_event('Videos Browser Widget', 'Browse', null, null);
		$(this).blur();
		return false;
	});

	$('ul.pager li.next a').click(function(){
		
		index = index < originals.length - 1 ? index + 1 : 0;
		switch_original(index);
		track_event('Videos Browser Widget', 'Browse', null, null);
		$(this).blur();
		return false;
	});


	// handles the news / blogs tabs

	$('#home-news-tab td#leading-posts-head a').click(function(){
		
		var parent = $(this).closest('td');
		parent.find('a').removeClass('active');
		$(this).addClass('active');
		
		var grand_parent = $(this).closest('table');
		grand_parent.find('div.leading-posts').hide();
		grand_parent.find('div#' + $(this).attr('rel')).show();

		track_page_view("/home/blogs-tab-" + $(this).attr('rel'));

		$(this).blur();
		return false;
	});
});


function switch_original(index){
	
	new_item	= originals[index];
	$('#original-name').html('<a href="/features/detail/' + new_item.id + '/' + new_item.slug + '">' + new_item.widont + '</a>');
	$('#original-class').html(new_item.class_name);
	$('#original-img').attr('src', '/_user/images/250/' + new_item.image);

	o_links = $('a.original-link');
	o_links.attr('href', '/features/detail/' + new_item.id + '/' + new_item.slug);
	o_links.attr('title', new_item.name);

	$('li.original-nav').hide();
	curr = $('li.original-nav a[rel="' + index + '"]').parent();
	curr.show();
	curr.next().show();
	curr.next().next().show();
	curr.prev().show();
	curr.prev().prev().show();
	if (index == 1){ curr.next().next().next().show(); }
	
	$('#content div.text p').hide();
	$('p#original-' + new_item.id).show();
	
	$('li.original-nav').removeClass('active');
	curr.addClass('active');
}