var Homepage = {
	
	mobileDevice: false,
	
	successOpen: false,
	successArrow: null,
	successCount: null,
	
	currentVideo: 1,
	playingVideo: 0,
	animateID: null,
	animateDelay: 20000,
	
	currentRecentPage: 0,
	
	videos: [
		'<iframe src="http://player.vimeo.com/video/26145008?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=1" width="700" height="394" frameborder="0"></iframe>',
		'<iframe src="http://player.vimeo.com/video/26275232?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=1" width="700" height="394" frameborder="0"></iframe>',
		'<iframe src="http://player.vimeo.com/video/26405422?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=1" width="700" height="394" frameborder="0"></iframe>',
		'<iframe src="http://player.vimeo.com/video/26742234?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=1" width="700" height="394" frameborder="0"></iframe>'
	],

	init: function()
	{
		this.successArrow = $('.homepage .success-stories .arrow');
		this.successCount = $('.homepage .success-stories ul > li').length;
		
		// $('.homepage .fullscreen img.page:nth-child(1)').fadeIn(500);
		
		this.currentVideo = Math.floor(Math.random() * $('.homepage .features .feature .video-slides img').length) + 1;
		$('.homepage .features .feature .video-slides img:nth-child(' + this.currentVideo + ')').fadeIn(500);
		this.animateID = window.setInterval(Homepage.animateVideos, this.animateDelay);
		
		this.animateScreen();
		
		$('.ticker .ticker-content p span:nth-child(1)').clone().appendTo('.ticker .ticker-content p');
		window.setTimeout(function() {
			Homepage.animateRecents();
		}, 2000);
//		this.animateRecents();
	},
	
	animateScreen: function()
	{
		$('.homepage .topBox .fullscreen .screen').delay(2000).animate({'width': '427px', 'height': '242px'}, 2000);
		$('.homepage .topBox .fullscreen .mouse').delay(2000).animate({'marginLeft': '395px', 'marginTop': '405px'}, 2000);
		$('.homepage .topBox .fullscreen .pointer').delay(2000).animate({'marginLeft': '422px', 'marginTop': '237px'}, 2000, function() {
			$(this).hide(0);
			
			$('.homepage .topBox .fullscreen .screen .contents img:nth-child(1)').delay(2000).hide(0, function() {
				$('.homepage .topBox .fullscreen .screen .contents img:nth-child(2)').delay(3000).hide(0, function() {
					$('.homepage .topBox .fullscreen .screen').one('click', function() {
						$('.homepage .topBox .fullscreen .screen').css('width', '220px').css('height', '132px');
						$('.homepage .topBox .fullscreen .mouse').css('marginTop', '385px').css('marginLeft', '330px');
						$('.homepage .topBox .fullscreen .pointer').css('marginTop', '128px').css('marginLeft', '216px').show(0);
						$('.homepage .topBox .fullscreen .screen .contents img').show(0);
						
						Homepage.animateScreen();
					});
				});
			});
		});
	},
	
	animateVideos: function()
	{
		$('.homepage .features .feature .video-slides img:nth-child(' + Homepage.currentVideo + ')').fadeOut(500);
		
		Homepage.currentVideo++;
		if (Homepage.currentVideo > $('.homepage .features .feature .video-slides img').length) {
			Homepage.currentVideo = 1;
		}
		
		$('.homepage .features .feature .video-slides img:nth-child(' + Homepage.currentVideo + ')').fadeIn(500);
	},
	
	animateRecents: function()
	{
		$('.ticker .ticker-content p').animate({
			'left': '-' + $('.ticker .ticker-content p span:nth-child(1)').css('width')
		}, 40000, 'linear', function() {
			$('.ticker .ticker-content p').css('left', '0');
			Homepage.animateRecents();
		});
	},
	
	openPopup: function(id)
	{
		$('#overlay').addClass('show');
		$('#' + id).show(0);
		
		if (id == 'video-popup') {
			this.changeVideo(this.currentVideo);
		}
	},
	
	
	closePopup: function()
	{
		$('#overlay').removeClass('show');
		$('#video-popup').hide(0);
		$('#recent-popup').hide(0);
		
		this.changeVideo(0);
	},
	
	changeVideo: function(nr)
	{
		if (nr == 0) {
			$('#video-popup .popup-box #video').html('');
			Homepage.playingVideo = 0;
		}
		else {
			if (nr != Homepage.playingVideo) {
				$('#video-popup .popup-box #video').html('');
				$('#video-popup .popup-box #video').html(Homepage.videos[nr - 1]);
				Homepage.playingVideo = nr;
			}
		}
	},
	
	changeRecentPage: function(nr)
	{
		len = $('#recent-popup .popup-box .pages a.dot').length;
		
		$('#recent-popup .popup-box .pages a.dot:nth-child(' + (this.currentRecentPage + 1) + ')').removeClass('selected');
		
		if (nr == 'prev') {
			this.currentRecentPage = (this.currentRecentPage - 1 + len) % len;
		} else if (nr == 'next') {
			this.currentRecentPage = (this.currentRecentPage + 1) % len;
		} else {
			this.currentRecentPage = nr;
		}
		
		$('#recent-popup .popup-box .examples .pages').animate({
			'marginLeft': '-' + (Homepage.currentRecentPage * 700) + 'px'
		}, 300)
		$('#recent-popup .popup-box .pages a.dot:nth-child(' + (this.currentRecentPage + 1) + ')').addClass('selected');
	}

}


$(document).ready(function() 
{
	Homepage.init();
});
