(function($) {

	$(function() {
		//scrollpane parts
		var scrollPane = $( ".videoslider .view" ),
			scrollContent = $( ".videoslider .view-content" ),
			boxes = $( ".videoslider .views-row" );

		//change overflow to hidden now that slider handles the scrolling
		scrollPane.css( "overflow", "hidden" );

		// box size in pixels
		var boxsize = 191;
		// Numbers of boxes we slide for each click
		var boxes_to_slide = 3;

		var slidesize = boxsize * boxes_to_slide;

		// How many rows/boxes do we have in our view?
		var boxcount = $( boxes ).length;

		//Debug code
//		$("#boxcount").text( boxcount );

		var maxslide = -boxsize * boxcount;

		//Debug code
//		$("#maxslide").text( maxslide );

		$( "#forward" ).click(function() {
			var slideforward = -slidesize;
			var anime = parseInt( scrollContent.css( "margin-left" ), 10 ) + slideforward;
			
			//Debug code
//			$("#px").text( anime );
			
			modulus = anime % slidesize;

			//Debug code
//			$("#modulus").text( modulus );
			
			if (modulus != 0){
				anime = anime - modulus;
				
				modulus2 = anime % slidesize;
			//Debug code
//				$("#modulus2").text( modulus2 );
			}
			
				if (anime > maxslide) {
					scrollContent.animate({ marginLeft: anime });
				}
			
			return false;
		});

		$( "#backward" ).click(function() {
			//var gap = slidesize;
			var anime = parseInt( scrollContent.css( "margin-left" ), 10 ) + slidesize;
			
			//Debug code
//			$("#px").text( anime );

			modulus = anime % slidesize;

			//Debug code
//			$("#modulus").text( modulus );
			
			if (modulus != 0){
				anime = anime - modulus;
				
				modulus2 = anime % slidesize;

			//Debug code
//				$("#modulus2").text( modulus2 );
			}
			
			if (anime <= 0) {
				scrollContent.animate({ marginLeft: anime });
			}
			return false;
		});
		
		
// AJAX loading

	$.ajaxSetup ({
		cache: false
	});

	var ajax_load = "<div style='height: 340px; text-align: center;'><img style='margin-top: 155px;' class='loading' src='/sites/all/themes/umcms/gfx/videoslider_ajax-loader.gif' alt='Indlæser video...' /></div>";

	$(".ajax_load_get").click(function(event){
		event.preventDefault();
		var loadUrl = $(this).attr("href") + "?ajax=1";

		// Make black border around activated video
		$(".videoslider .views-row").removeAttr("style");
		$(this).parents(".views-row").css("border", "2px solid #333333");
		$(this).parents(".views-row").css("margin", "0px");

		$("#videoslider-player")
			.html(ajax_load)
			.load(loadUrl);
	});

// Load latest video
		var loadUrl = $(".ajax_load_get:first").attr("href") + "?ajax=1";

		// Make black border around activated video
		$(".ajax_load_get:first").parents(".views-row").css("border", "2px solid #333333");
		$(".ajax_load_get:first").parents(".views-row").css("margin", "0px");

		$("#videoslider-player")
			.html(ajax_load)
			.load(loadUrl);
	});	

})(jQuery);;

