jQuery(document).ready(function(){

// all carousel images are hidden by default, add class of first to show first image
jQuery('#homecarousel').find('div:eq(0)').addClass("first");

jQuery.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) { 
    jQuery(pager).find('a')
		.removeClass('activeSlider') 
        .filter('a:eq('+currSlideIndex+')')
		.addClass('activeSlider'); 
}; 
	
if(jQuery('#homecarousel').length)
{
	jQuery('#homecarousel').cycle({ 
	    fx:     'fade', 
	    speed:   800, 
	    timeout: 5000,
		activePagerClass: 'activeSlider',
		pager: '#carouselnav',
	        pagerAnchorBuilder: function(idx, slide) { 
	                return '<a class="navi nav' + idx + '"><h5>' + idx + '.</h5><p>' 
					+ jQuery(slide).children("h2").eq(0).text() 
					+ '<br /><span class="arial">' 
					+ jQuery(slide).children("img").attr("alt")
					+ '</span></p></a>';
    	    }
	}); 
};

jQuery('#slide-1').live('click',function(){
		startplayer();

});

jQuery('.navi:not(.nav0)').live('click',function(){
	//if the video area div exists
	if(jQuery('#videoarea').length) {
		resetplayer();
	};
});

jQuery('.nav0').live('click',function(){
	if(jQuery('#videoarea').length){
		//remove the video div from the carousel
		jwplayer("videoarea").stop();
		jwplayer('videoarea').remove();
		jQuery('#videoarea').remove();
	}
	startplayer();
});

function startplayer(){
		//insert a div in the fiest slide
		jQuery('#homecarousel').prepend('<div id="videoarea" style="display:none"></div>');
		//stop the carousel
		jQuery('#homecarousel').cycle('pause');
		//hide the other panels
		jQuery('.panel').hide();
		//load the video
		jwplayer("videoarea").setup({
        flashplayer: "wp-content/themes/sandbox/player/player.swf",
        streamer: 'rtmp://unpbf-content.cdn.ekouk.com/cfx/st/',
		file: "wp-content/uploads/videos/PBF_FINAL_23 Dec.flv",
        height: 303,
        width: 704,
		autostart: true,
        events: {
            onComplete: function() {
               // document.getElementById("status").innerHTML = "That's all folks!";
			   resetplayer();
            }
        }
    	});
}

function resetplayer(){
	//remove the video div from the carousel
	jwplayer("videoarea").stop();
	jwplayer('videoarea').remove();
	jQuery('#videoarea').remove();
	//show the other panels again
	jQuery('.panel').show();
	//reset the carousel
	jQuery('#homecarousel').cycle('resume');	
}

});
	

