$(function() {
 	
	$('#video_slider li').removeClass('active');
	
	$('#video_slider li:first').addClass('active');
	
	$('div.gallery').cycle({ 
		fx:    'scrollHorz',
		timeout: 0,
		prev:   '.left_arrow',
		next:   '.right_arrow',
		after:   onAfterFung
	});
	
	
	$('div.gallery_details').cycle({ 
		fx:    'fade',
		timeout: 0,
		prev:   '.left_arrow',
		next:   '.right_arrow'
	});
	
		
	$('#video_slider li').click(function() {
			
		$('#video_slider li').removeClass('active');
		
		var curIndx = parseFloat($('a', $(this)).attr('rel'));
		
		
		if (curIndx > -1) {
			
			$('div.gallery').cycle(curIndx);
			$('div.gallery_details').cycle(curIndx);
			
			$(this).addClass('active');
		}
		
		return false;
		
	});
	
	$('#video_slider').cycle({ 
		fx:    'scrollHorz',
		timeout: 0,
		prev:   '#prev',
		next:   '#next',
		after:   onAfter
	});
	
	$('div.photo a').hover(function() {
		$(this).next().fadeIn('slow');				  
	},
	function() {
		$(this).next().fadeOut('slow');
	});
	
	$('div.photo').click(function() {
		
		var curRel = $('a', $(this)).attr('rel');
		
		var arrRel = curRel.split('_');
		
		$(this).hide();
		
		$('div.video_cont').each(function(i) {
			
			var curIndx = i + 1;
			
			$(this).html('<div id="playerdiv'+curIndx+'"></div>');
			
		 });
		
		loadNewVideo(arrRel[0], arrRel[1], arrRel[2]);
		
		$(this).next().show();
		
		var vimeoAPI = $('#' + arrRel[1]).get(0);
		
		setTimeout(function() {
		
			vimeoAPI.api_play();
			
		}, 10000);
		
		
	
	});
	
});

function onAfter(curr, next, opts) {

	var index = opts.currSlide;
	
	$('#totalSpan').html(opts.slideCount);
	$('#indexSpan').html(index+1);
	
	if (index == 0) {
		$('span.left_arrow1').hide();
		$('span.left_arrow1_inactive').show();
	}
	else {
		$('span.left_arrow1_inactive').hide();
		$('span.left_arrow1').show();
	}
	
	if (index == opts.slideCount - 1) {
		$('span.right_arrow1').hide();
		$('span.right_arrow1_inactive').show();
	}
	else {
		$('span.right_arrow1_inactive').hide();
		$('span.right_arrow1').show();
	}

}

function onAfterFung(curr, next, opts) {

	var index = opts.currSlide;
	
	
	$('#video_slider li').removeClass('active');
	$('#video_slider li').eq(index).addClass('active');
	
	if (index >= 8) {
		$('#video_slider').cycle(1);
	}
	else {
		$('#video_slider').cycle(0);
	}
	
	$('div.video_cont').each(function(i) {
			
		var curIndx = i + 1;
		
		$(this).html('<div id="playerdiv'+curIndx+'"></div>');
		
	 });
	
	$('div.photo').show();
	
	if (index == 0) {
		$('.left_arrow').hide();
		$('.left_arrow_inactive').show();
	}
	else {
		$('.left_arrow_inactive').hide();
		$('.left_arrow').show();
	}
	
	if (index == opts.slideCount - 1) {
		$('.right_arrow').hide();
		$('.right_arrow_inactive').show();
	}
	else {
		$('.right_arrow_inactive').hide();
		$('.right_arrow').show();
	}

}

function loadNewVideo(clipID, divID, divClass) {
	
	var flashvars = {
		
		'clip_id': clipID,
		'server': 'vimeo.com',
		'show_title': 1,
		'show_byline': 1,
		'show_portrait': 0,
		'fullscreen': 1,
		'js_api': 1
	
	}
		
	var parObj = {
		
		'swliveconnect':true,
		'fullscreen': 1,
		'allowscriptaccess': 'always',
		'allowfullscreen':true
		
	};
			
	var attObj = {}
	attObj.id = divID;
		
	swfobject.embedSWF('http://vimeo.com/moogaloop.swf', divClass, '378', '208', '9.0.28', 'expressInstall.swf', flashvars, parObj, attObj);
	
}
