var video = {};
$(function(){
	video.playing = false;
	$("div#carousel").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        visible: 1,
        circular: true,
        auto: 5000,
        autoCallBacks: true,
        speed: 800,
        beforeStart: function(a, curr) {
    	  	if($('#swf'+curr).length > 0 && video.playing){
    			if(typeof $('#swf'+curr)[0].stopAndResetVideo == 'function')
    				$('#swf'+curr)[0].stopAndResetVideo();
    	  	}
        }
    });

	$('div#carousel ul li').each(function(index, obj){
		if($(this).hasClass('video')){
			(function(){
				// need to watch out for cloned videos and change their ids 
				var idIdx = parseInt($(obj).children(".videoContainer").children().eq(0).attr('id').charAt(12), '0');
				if(idIdx != index){
					$(obj).children(".videoContainer").children().eq(0).attr('id', 'videoContent' + index);
					$(obj).children(".videoContainer").children().eq(1).attr('id', 'accessibleVideoContent' + index);
				}
	            CB.videoPlayer.init({
	                idToSwapWithVideo: 'videoContent' + (index).toString(),
	                accessibleVideoId: 'accessibleVideoContent'  + (index).toString(),
	                idToAccessSwf: 'swf' +  (index).toString(),
	                methodToStopAndResetVideo: 'stopAndResetVideo',
	                tabIndex: index,
					showBigPlayBtn: 'true',
					videoStartFlashCall:"video['" + index.toString() + "']['start']",
	                videoStopFlashCall: "video['" + index.toString() + "']['stop']"
	            });
	            video[index.toString()] = function(){
	            	callbacks = {};
	            	callbacks.stop = function(){
						video.playing = false;
						$("div#carousel")[0].auto.start(); 
						$("div#carousel ul li").eq(index).children("div.overlayBg").addClass('active');
						$("div#carousel ul li").eq(index).children("div.overlayText").addClass('active');	            		
	            		console.log('stoping');
	            	};
	            	callbacks.start = function (){
						video.playing = true;
						$("div#carousel")[0].auto.stop(); 
						$("div#carousel ul li").eq(index).children("div.overlayBg").removeClass('active');
						$("div#carousel ul li").eq(index).children("div.overlayText").removeClass('active');
						$("div#buttons a").removeClass("display");
	            		console.log('starting');
	            	};
	            	return callbacks;
	            }();
			}());
		};	           
	});

	$('a.next, a.prev').click(function(){
		$("div#carousel")[0].auto.stop(); 
		return false;
	});
	$("div#carousel li, div#buttons").hover(function(){
		$("div#buttons a").addClass("display");
		$("div#carousel")[0].auto.stop(); 
	},function(){
		$("div#buttons a").removeClass("display");
		if(!video.playing)
			$("div#carousel")[0].auto.start(); 
	});
});

