function selectVideo(id,carousel,autostart)
{	


	$('.videolink img').fadeTo("fast",0.5);
	$("#video-"+id+"-thumb img").fadeTo("fast",1.0);

	//selectedVideo = videoArray[Math.min(0, id-1,videoArray.length-1)];
	
	if (id) {
		selectedVideo = videoArray[id-1];
	} else {
		selectedVideo = videoArray[0];
	}
	

	if (selectedVideo)
	{
		if (document.getElementById('player'))
		{
			document.getElementById('player').sendEvent('LOAD', {file:'/store/'+selectedVideo.video, image:'/store/previews/'+selectedVideo.preview});
			if (autostart!=true)
			{
				document.getElementById('player').sendEvent('STOP');
			}			
		}
	}

	$(".video-description").hide();
	$("#video-"+id+"-desc").show();	

	$("#videos .videolink").removeClass('selected');
	$("#video-"+id+"-link").addClass('selected');

	setButtonStates(carousel);
}

function setButtonStates(carousel)
{
	if (n == undefined || n == null)
	{
		var n = !carousel.locked && carousel.options.size !== 0 && ((carousel.options.wrap && carousel.options.wrap != 'first') || carousel.options.size == null || carousel.last < carousel.options.size);
		if (!carousel.locked && (!carousel.options.wrap || carousel.options.wrap == 'first') && carousel.options.size != null && carousel.last >= carousel.options.size)
			n = carousel.tail != null && !carousel.inTail;
	}
	if (p == undefined || p == null)
	{
		var p = !carousel.locked && carousel.options.size !== 0 && ((carousel.options.wrap && carousel.options.wrap != 'last') || carousel.first > 1);
		if (!carousel.locked && (!carousel.options.wrap || carousel.options.wrap == 'last') && carousel.options.size != null && carousel.first == 1)
			p = carousel.tail != null && carousel.inTail;
	}

	n ? $('#mycarousel-next').removeClass('disabled') : $('#mycarousel-next').addClass('disabled').removeClass('mycarousel-next-hover');
	p ? $('#mycarousel-prev').removeClass('disabled') : $('#mycarousel-prev').addClass('disabled').removeClass('mycarousel-prev-hover');

}

function mycarousel_initCallback(carousel)
{	

    jQuery('.videolink').bind('click', function()
	{
		setButtonStates(carousel);
		
		id = $(this).attr('id').split('-')[1];		
		selectVideo(id,carousel);

        return false;
    });
   
    jQuery('#mycarousel-next').bind('click', function()
	{
        carousel.next();
		setButtonStates(carousel);	

        return false;
    });

    jQuery('#mycarousel-prev').bind('click', function()
	{
        carousel.prev();
		setButtonStates(carousel);	

        return false;
    });

    if (defaultVideoID == 0)
    {
    	defaultVideoID = videoArray.length < 5 ? 1 : 1;
    }
	
	if($_GET.length > 0 && typeof($_GET['video']))
	{
		videoLinkInUrl = $("a[@href^=#"+$_GET['video']+"]").attr('id').split('-')[1];
		if(videoLinkInUrl.length)
		{
			START1=(videoLinkInUrl-0);
		}
	}
	else{
		setTimeout( function(){	selectVideo(defaultVideoID,carousel,autostart); },1500);
	}
	test = carousel;

}

function getVideoIDFromOriginalID(originalID)
{
	videoID = 0;
	
	for (i in videoArray)
	{
		if (videoArray[i].originalID == originalID)
		{
			videoID = parseInt(i)+1; 
		}
	}
	
	return videoID;
}

var $_GET = {}; 
 
document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () { 
    function decode(s) { 
        return decodeURIComponent(s.split("+").join(" ")); 
    } 
 
    $_GET[decode(arguments[1])] = decode(arguments[2]); 
}); 

$(document).ready(function()
{
	var ss1 = new SWFObject("/flash/player.swf","player","697","412","9","#FFFFFF");
	ss1.addParam("allowfullscreen","true");
	ss1.addVariable('fullscreen','true');
	ss1.addParam("flashvars","allowfullscreen=true&fullscreen=true&autostart=true");
	ss1.write("vidcontainer");	

	$(".video-description").hide();
	$("#thumbs img").fadeTo("slow",0.5);

	START1 = 1;
	videoID = -1;
	
	if(typeof($_GET['video']) == "string")
	{
		getID = $("a[@href^=#"+$_GET['video']+"]").attr('id');
		videoID = getID.split('-')[1];
		if(videoID != -1)
		{
			START1=(videoID-0);
		}
	}


	$("#mycarousel").jcarousel({
		scroll: 4,
		start:START1,
		initCallback: mycarousel_initCallback,
		
		buttonNextHTML: null,
		buttonPrevHTML: null,
		easing: 'swing',
		animation: 500
	});

	$('.transcript_toggle').click(function(){

		if ($(this).attr('value') == 'show transcript')
		{
			$(this).attr('value','hide transcript');
		}		
		else
		{
			$(this).attr('value','show transcript');
		}
		;//[*="'+CURRENT_VIDEO+'"]
		$('#transcript_'+$(this).attr('tabindex')).toggle();
	});

	$("#mycarousel-prev").hover(
		function(){
			if (!$(this).hasClass('disabled'))
			{			
				$(this).addClass('mycarousel-prev-hover')
			}
		},
		function(){
			$(this).removeClass('mycarousel-prev-hover')
		}
	);
	
	$("#mycarousel-next").hover(
		function(){
			if (!$(this).hasClass('disabled'))
			{
				$(this).addClass('mycarousel-next-hover')
			}
		},
		function(){
			$(this).removeClass('mycarousel-next-hover')
		}
	);
	

});
