(function($){
	//Resize image on ready or resize
	$.fn.supersize = function() {	
		//Invoke the resizenow() function on document ready
		$(document).ready(function() {
			$('#supersize').resizenow(); 
		});
		//Invoke the resizenow() function on browser resize
		$(window).bind("resize", function() {
    		$('#supersize').resizenow(); 
		});
	};
	//Adjust image size
	$.fn.resizenow = function() {
		//Define starting width and height values for the original image
		var startwidth = 1440;  
		var startheight = 956;
		//Define image ratio
		var ratio = startheight/startwidth;
		//Gather browser dimensions
		var browserwidth = $(window).width();
		var browserheight = $(window).height();
		//Resize image to proper ratio
		if ((browserheight/browserwidth) > ratio) {
		    $(this).height(browserheight);
		    $(this).width(browserheight / ratio);
		    $(this).children().height(browserheight);
		    $(this).children().width(browserheight / ratio);
		} else {
		    $(this).width(browserwidth);
		    $(this).height(browserwidth * ratio);
		    $(this).children().width(browserwidth);
		    $(this).children().height(browserwidth * ratio);
		}
		//Make sure the image stays center in the window
		$(this).children().css('left', (browserwidth - $(this).width())/2);
		$(this).children().css('top', (browserheight - $(this).height())/2);
	};
})(jQuery);

$(document).ready(function() {
	//Invoking the supersized function on the div with id - supersize. It is the div that contains the large background image
	$("div#supersize").supersize();

	// ----- begin movie carousel

	var movies = [
		'Xc3pnU2aU88',
		'CkY1Z4xPYZY',
		'wU4ty9c7rxs',
		'bGvUsgxuPQk',
		'qLxy0CCnP-g',
		'TBUNl9XyR3U',
		'ijoEgJnyqQk'
	];

	shuffle = function(o){ //v1.0
		for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
		return o;
	};
	
	movies = shuffle(movies);

	var $slider = $('#slider');

	$.each(movies, function( key, value ){
		$slider.append('<li><div class="'+value+'" /></li>');
		$('.'+value).tubeplayer({
			width: 934,
			height: 555,
			allowFullScreen: true,
			preferredQuality: "default",
			modestbranding: true,
			showControls: true,
			initialVideo: value,
		});
	});

	$slider.anythingSlider();
	$('.start-stop').remove();

	var $anchors = $('.anythingSlider').find('a');

	$anchors.click(function(){

		$this = $(this);
		
		// current page & current youtube id
		var currentPage = $('#slider').data('AnythingSlider').currentPage;				
		var x = currentPage - 1;
		var idNow = movies[x];

		var $parent = $this.parent();

		if($parent.is('li')) {
			var str = $(this).attr('class');
			var str = str.replace('panel','');
			var str = str.split(' ');
			var y = str[0] - 1;
		} else if ($parent.is('span') && $parent.hasClass('back')) {
			var y = x - 1;
		} else if ($parent.is('span') && $parent.hasClass('forward')) {
			var y = x + 1;
		}
		
		$('.' + movies[x] + ':eq(0)').tubeplayer('stop');
		$('.' + movies[y] + ':eq(0)').tubeplayer('stop');

	});
});
