$(document).ready(function(){
	
	src = $('#videos-nav a:first').attr('rel');
	
	$('#video .video').html('<iframe width="640" height="345" src="' + src + '" frameborder="0" allowfullscreen></iframe>');
	
	$('#videos-nav a').click(function() {
		$('#videos-nav a').removeClass('activeSlide');
		$(this).addClass('activeSlide');
		$('#video .video').html('<iframe width="640" height="345" src="' + $(this).attr('rel') + '" frameborder="0" allowfullscreen></iframe>');
	});
	
	$('#slideshow-nav').jcarousel({
		scroll: 1,
		wrap: 'circular',
		buttonNextHTML: null,
		buttonPrevHTML: null,
		initCallback: slideshow_initCallback
	});
	
	var curSlide = 1;
	function slideshow_initCallback(carousel) {
		jQuery('#slideshow-next').bind('click', function() {
				$('#slideshow-images').cycle('next');
				if(curSlide == 10)
						carousel.next();
				else
						curSlide++;
				return false;
		});
		jQuery('#slideshow-prev').bind('click', function() {
				$('#slideshow-images').cycle('prev');
				if (curSlide == 10)
						carousel.prev();
				else
						curSlide--;
		
				return false;
		});
	};
	
	$('#videos-nav').jcarousel({
		scroll: 1,
		wrap: 'circular',
		buttonNextHTML: null,
		buttonPrevHTML: null,
		initCallback: videos_initCallback
	});
	
	var curVideo = 1;
	function videos_initCallback(carousel) {
		
		
		jQuery('#slideshow-next').bind('click', function() {
				pos = $('#videos-nav li a.activeSlide').parent().attr('jcarouselindex');
				len = $('#videos-nav li a').length;
				$('#videos-nav li a').removeClass('activeSlide');
				
				if (pos == len) {
					pos = 0;
				}
				
				$('#videos-nav li a').eq(pos).addClass('activeSlide');
				$('#video .video').html('<iframe width="640" height="345" src="' + $('#videos-nav li a').eq(pos).attr('rel') + '" frameborder="0" allowfullscreen></iframe>');
				
				
				if(curVideo == 10)
						carousel.next();
				else
						curVideo++;
				return false;
		});
		jQuery('#slideshow-prev').bind('click', function() {
				pos = $('#videos-nav li a.activeSlide').parent().attr('jcarouselindex');
				len = $('#videos-nav li a').length;
				$('#videos-nav li a').removeClass('activeSlide');
			
				pos -=2;
				if (pos == -1) {
					pos = len - 1;
				}
				
				$('#videos-nav li a').eq(pos).addClass('activeSlide');
				$('#video .video').html('<iframe width="640" height="345" src="' + $('#videos-nav li a').eq(pos).attr('rel') + '" frameborder="0" allowfullscreen></iframe>');
				
				if (curVideo == 10)
						carousel.prev();
				else
						curVideo--;
		
				return false;
		});
	};
	
	$('#slideshow-images').cycle({
		fx:       'fade',
		speed:    1000,
		timeout:   10000,
		pager:    '#slideshow-nav',
		startingSlide: 0,
		pagerAnchorBuilder: function(idx, slide) {
				return '#slideshow-nav li:eq(' + idx + ') a';
		}
	});
	
});
