$(function() {
	
	var visible = false;
	
	$('#audio-embed-btn').click(function() {
		if (visible == false)
		{
			$('.audio-player-box').animate({height: 200}, 500, 'linear');
			$('#audio-embed-box').fadeIn(500);
			visible = true;
		}
		else
		{
			$('#audio-embed-box').fadeOut(500);
			$('.audio-player-box').animate({height: 50}, 500, 'linear');
			visible = false;
		}
	});
	
	$('#video-embed-btn').click(function() {
		if (visible == false)
		{
			$('.video-player-box').animate({height: "+=150px"}, 500, 'linear');
			$('#video-embed-box').fadeIn(500);
			visible = true;
		}
		else
		{
			$('#video-embed-box').fadeOut(500);
			$('.video-player-box').animate({height: "-=150px"}, 500, 'linear');
			visible = false;
		}
	});
	
	
	/****************  Start of Homepage Thumbs section ***************/
	
	var canChange = true;
	
	var currentSelection_list = '.thumb-items';
	var currentSelection_idx = 0;
	
	var lastSelection_list;
	var lastSelection_idx;
	
	// Start by selecting the first item
	var currentId = prepareId(currentSelection_list, currentSelection_idx);
	$(currentId).addClass('thumb-highlight');
	
	setPreview(
		$(currentId).find('.home-thumb-title').text(),
		$(currentId).find('.home-thumb-permalink').text(),
		$(currentId).find('.home-thumb-desc').text(),
		$(currentId + ' img').attr('src')
	);
	
	
	function setThumbHighlight(itemId, lastId)
	{
		$(itemId).addClass('thumb-highlight');
		$(lastId).removeClass('thumb-highlight');
	}
	
	function setPreview(title, permalink, description, imgSrc)
	{
		$('#preview-img').attr('src', imgSrc);
		$('#preview-img-caption-title').text(title);
		$('#preview-img-caption a').attr('href', permalink);
		$('#preview-img-caption-desc').text(description);
		$('#preview-img-caption-link').attr('href', permalink);
	}
	
	function prepareId(listName, index)
	{
		return (listName + ':eq(' + index + ')');
	}
	
	setInterval(function() {
		
		if (canChange == true)
		{
			lastSelection_list = currentSelection_list;
			lastSelection_idx = currentSelection_idx;
			
			currentSelection_list = '.thumb-items'; // Use this as the main list
			currentSelection_idx++;
			
			if (currentSelection_idx > 17)
				currentSelection_idx = 0; // Clamp to max length of 18
				
			var currentId = prepareId(currentSelection_list, currentSelection_idx);
			setThumbHighlight(currentId, prepareId(lastSelection_list, lastSelection_idx));
			
			setPreview(
				$(currentId).find('.home-thumb-title').text(),
				$(currentId).find('.home-thumb-permalink').text(),
				$(currentId).find('.home-thumb-desc').text(),
				$(currentId + ' img').attr('src')
			);
		}
		
	}, 8000);
	
	
	
	$('.thumb-items').click(function() {
		var permalink = $(prepareId(currentSelection_list, currentSelection_idx)).find('.home-thumb-permalink').text();
		window.location.replace(permalink);
	});
	
	$('.thumb-items').hover(function() {
		$(this).find('img').addClass('thumb-item-hover').css({'opacity' : 1});
		$(this).siblings('li').find('img').css({'opacity' : 0.4}); 
		$(this).siblings('li').removeClass('thumb-item-hover');
		
		lastSelection_list = currentSelection_list;
		lastSelection_idx = currentSelection_idx;
		$(prepareId(lastSelection_list, lastSelection_idx)).removeClass('thumb-highlight');
			
		currentSelection_list = '.thumb-items';
		currentSelection_idx = $(".thumb-items").index(this);
		
		var currentId = prepareId(currentSelection_list, currentSelection_idx);		
		setPreview(
			$(currentId).find('.home-thumb-title').text(),
			$(currentId).find('.home-thumb-permalink').text(),
			$(currentId).find('.home-thumb-desc').text(),
			$(currentId + ' img').attr('src')
		);
		
		canChange = false;
		
	}, function() {
		$(this).find('img').removeClass('thumb-item-hover');
		canChange = true;
	});
	
	$('.thumbs').bind('mouseleave',function(){  
        $(this).find('img').css('opacity', 1);  
    });
    
    
    
    $('.media-items').click(function() {
		var permalink = $(prepareId(currentSelection_list, currentSelection_idx)).find('.home-thumb-permalink').text();
		window.location.replace(permalink);
	});
	
	$('.media-items').hover(function() {
		$(this).find('img').addClass('thumb-item-hover').css({'opacity' : 1});
		$(this).siblings('li').find('img').css({'opacity' : 0.4}); 
		$(this).siblings('li').removeClass('thumb-item-hover');
		
		lastSelection_list = currentSelection_list;
		lastSelection_idx = currentSelection_idx;
		$(prepareId(lastSelection_list, lastSelection_idx)).removeClass('thumb-highlight');
			
		currentSelection_list = '.media-items';
		currentSelection_idx = $(".media-items").index(this);
		
		var currentId = prepareId(currentSelection_list, currentSelection_idx);		
		setPreview(
			$(currentId).find('.home-thumb-title').text(),
			$(currentId).find('.home-thumb-permalink').text(),
			$(currentId).find('.home-thumb-desc').text(),
			$(currentId + ' img').attr('src')
		);
		
		canChange = false;
		
	}, function() {
		$(this).find('img').removeClass('thumb-item-hover');
		canChange = true;
	});
	
	$('.media-thumbs').bind('mouseleave',function(){  
        $(this).find('img').css('opacity', 1);  
    });
	
	
	/****************  End of Homepage Thumbs section ****************/
	
    var pick_visible = false;
    $('#pickofweek-btn').click(function() {
		
		if (pick_visible == false)
		{
			$('#pick-of-week').slideDown(500);
			pick_visible = true;
		}
		else
		{
			$('#pick-of-week').slideUp(500);
			pick_visible = false;
		}
	});
	
	//Default Action
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content
	
	//On Click Event
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		$(activeTab).slideDown(); //Fade in the active content
		return false;
	});
	
	
	$('#time-remaining').each(function(i) {
		var endDate = $(this).text();
		$(this).text('');
		
		$(this).countdown({
		  image: 'http://localhost/sandbox/digits.png',
		  startTime: '01:12:12:00'
		});
	});
});

