var DaylifeHomepage = {}

$(function () {
	//only append slideshow to homepage
	if($('body#homepage').size() > 0)
	{
		DaylifeHomepage.superSized();
	}
	$('.daylife-button').append('<span class="arrow"></span>');
	$('#main-content .slide-content h1').each(function () {
		$(this).html($(this).html().replace(/Daylife/,'<em>Day</em>life'));
	});
});


DaylifeHomepage.superSized = function () {
	var controls_on = false;
	var videos = [];
	var controls_hover = false;
	var prev_slide;
	var check_usage;
	var slides_init = false;
	var slide_images = [];
	var playing = true;
	var no_video = $('.video-content').size() > 0 ? 0 : 1;
	$('.slide-content').each(function () {
		slide_images.push({image: $(this).find('span.slide-location').html().replace(/^http:\/\/(\<a href=.+\>)(.+)(\<\/a\>)(.+)$/, 'http://$2$4'), thumb : '', url : '' })
		$(this).find('.video-content').size() > 0 ? videos.push($(this).find('.video-content').html()) : videos.push("");
	});
	var arrow_markup = '<!--Thumbnail Navigation--><div id="prevthumb"></div><div id="nextthumb"></div><!--Arrow Navigation--><a id="prevslide" class="load-item"></a><a id="nextslide" class="load-item"></a>';
	var slideshow_markup = '<div id="thumb-tray" class="load-item"><div id="thumb-back"></div><div id="thumb-forward"></div></div><!--Time Bar--><div id="progress-back" class="load-item"><div id="progress-bar"></div></div><!--Control Bar--><div id="controls-wrapper" class="load-item"><div id="controls"><a id="play-button"><img id="pauseplay" src="img/pause.png"/></a><!--Slide counter--><div id="slidecounter"><span class="slidenumber"></span> / <span class="totalslides"></span></div><!--Slide captions displayed here--><div id="slidecaption"></div><!--Thumb Tray button--><a id="tray-button"><img id="tray-arrow" src="img/button-tray-up.png"/></a><!--Navigation--><ul id="slide-list"></ul></div></div>';
	$('body#homepage #slideshow-controller').before(arrow_markup).append(slideshow_markup);
	var supersizer = $.supersized({
		// Functionality
		slide_interval          :   6000,		// Length between transitions
		transition              :   1, 			// 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
		transition_speed		:	700,		// Speed of transition							   
		// Components
		slide_links				:	'blank',	// Individual links for each slide (Options: false, 'num', 'name', 'blank')
		slides 					:  	slide_images,
		autoplay				:   true
	});
	prev_slide = vars.current_slide;
	$(theme).bind('animation_started', function () {
		if(slides_init)
		{
			$('.slide-content:eq(' + prev_slide + ')').find('.video-content').size() > 0 ? $('.slide-content:eq(' + prev_slide + ')').find('.video-content').empty() : '';
			$('.slide-content:eq(' + prev_slide + ')').fadeOut(700);
			$('.slide-content:eq(' + vars.current_slide + ')').fadeIn(700, function () {
				prev_slide = vars.current_slide; 
				$('.slide-content:eq(' +  vars.current_slide + ')').find('.video-content').size() > 0 ? $('.slide-content:eq(' +  vars.current_slide + ')').find('.video-content').html(videos[vars.current_slide]) : '';
			});
		}
		else
		{
			slides_init = true;
		}
	});
	$('.video-content').mouseover(function () {
		if(playing) { 
			playing = false;
			$('.overlay-trigger').remove();
			api.playToggle();
		}
	});
	$('#main-content').mousemove(function (e) {
		if(controls_on == false)
		{
			$('#slide-next-prev').show();
			
			controls_on = true;
			controls_hover = false;
			
			if(e.pageX >= ($(window).width() / 2))
			{
				$('#nextslide').show();
				$('#prevslide').hide();
			}
			else
			{
				$('#prevslide').show();
				$('#nextslide').hide();
			}

			$('#nextslide').hover(function () {
				controls_hover = true;
			}, function () {
				controls_hover = false;
			});
			$('#prevslide').hover(function () {
				controls_hover = true;
			}, function () {
				controls_hover = false;
			});
			check_usage=setInterval(checkUsage,500);
		}
	});
	function checkUsage()
	{
		if(!controls_hover)
		{
			$('#prevslide').hide();
			$('#nextslide').hide();
			clearInterval(check_usage);
			controls_on = false;
		}
	}
}
