$(document).ready(function(){
	//	$("div#main-image").supersleight({shim: '/mmlib/js/supersleight/x.gif'});
	
	window.cyclecount = 0;
	
	$("input.placeholder").focus(function(){
		if($(this).hasClass("placeholder")){
			$(this).val("");
			$(this).removeClass("placeholder");
		}
	});
	
	// Change all movies links to use fragment version
	$('a[href^=/movies]').each( function() {
		if (!$(this).attr("id") == "nav-movies")
		{
			$(this).attr( 'href', $(this).attr( 'href' ).replace(/\/movies\/#/, '/movies/').replace(/\/movies\//, '/movies/#') );
		}
	});
	
	

	var fadeTime = 6000;
	var t = null;
	var isHovering = false;
	function fadeTo(sender, id)
	{
		var speed = 300;

		var select_id = "#unit-main-image-slideshow #movie_" + id;
		//if this is already the selected tab do nothing and break out
		if ($(select_id).hasClass("selected"))
		{
			return;
		}

		//select the right tab
		$("#unit-video-browser li").removeClass("selected");
		$(sender).addClass("selected");

		//fade out the selected tab content
		$("#unit-main-image-slideshow .selected").fadeTo(speed, 0, function(){
			//test to see if this tab has been reselected
			if (!$(this).hasClass("selected"))
			{
				$(this).addClass("hidden");
			}
		});

		$("#unit-main-image-slideshow .selected").removeClass("selected");
		$(select_id).addClass("selected");

		$(select_id).fadeTo(1000, 1, function(){
		}).removeClass("hidden");	

		clearTimeout(t);
		t = setTimeout(fadeCycle, fadeTime);
	}

	function fadeCycle()
	{
		//get the selected tab
		//get the next tab (or cycle round if it's the last one)
		//fadeTo() the next tab

		var next = $("#unit-video-browser li.selected").next();
		if (next.length < 1)
		{
			next = $("#unit-video-browser li:first");
		}

		var id = $(next[0]).attr("id");
		if( id ) {
			var parts = id.split("_");
			var index = parts[1];
			fadeTo(next[0], index);
		}
	}
	$(document).ready(function(){
		//set the default opacity values initially
		$("#unit-main-image-slideshow .hidden").css("opacity", 0);
		$("#unit-main-image-slideshow .selected").css("opacity", 1);

		$("#unit-video-browser li a").click(function(){
			//return false;
		});

		$("#unit-video-browser li").mouseover(function(){
			var id = $(this).attr("id");		
			var parts = id.split("_");
			var index = parts[1];
			
			fadeTo(this, index);
		});

		$("#unit-main-image-slideshow").mouseover(function(){
			isHovering = true;
			clearTimeout(t);
		});
		$("#unit-main-image-slideshow").mouseout(function(){
			isHovering = false;

			clearTimeout(t);
			t = setTimeout(fadeCycle, fadeTime);
		});

		t = setTimeout(fadeCycle, fadeTime);
	});



});
