var playlist = null;

var timeout    = 200;
var closetimer = 0;
var ddmenuitem = 0;

$(document).ready(function() { // page load
	//debugger;
	$(function(){ // create carousel
		if($().scrollingCarousel != null) {
			$('#bbf-carousel-videos').scrollingCarousel();
		}
	});
	
	// attach carousel onclick style handler
	$(".scroll-content div").click(function(){
		video_id = this.id;	
		$('#player').children().remove();

		$("div.emph").removeClass("emph");  // remove previous clickedclass
		
		//$("." + this.className.substr(this.className.indexOf(video_id), this.className.length)).addClass("emph");
		$(this).addClass("emph");	
		
		vimeo_player_create();
		
		return false;                                           // prevent browser form following link when clicked
	});
		
	// attach carousel onclick logic
	$('div.navigation-item').each(function() { 
		$(this).bind('mouseup', { el: this }, function(e) { showDiv(e.data.el); }); /*console.debug(this);*/ 
	})
	
	// event handlers for ddl
	if (navigator.appVersion.indexOf("MSIE 6.0") == -1) {
		$('#nav > li').bind('mouseover', nav_open)
		$('#nav > li').bind('mouseout',  nav_timer)
		$('.bbf-dropdown').bind('mouseover', nav_canceltimer);
	}
	
	//create play list
	if($('div.emph').length !=0) {
		video_id = $('div.emph')[0].id;//playlist[0].id;
		vimeo_player_create();
	}
}); // end ready()

function vimeo_player_create() { 
	$('#player').flash({
		swf: 'http://vimeo.com/moogaloop.swf',
		width: 467,
		height: 266, 		
		flashvars:{
			clip_id: video_id,
			autoplay: true,
			show_portrait: 0,
			show_byline: 0,
			show_title: 0, 
			js_api: 1, // required in order to use the Javascript API
			js_onLoad: 'vimeo_player_loaded', // moogaloop will call this JS function when it's done loading (optional)
			js_swf_id: 'moogaloop' // this will be passed into all event methods so you can keep track of multiple moogaloops (optional)
		},
		params: {allowscriptaccess: 'always',allowfullscreen: 'true'}
	}, function(){"loaded"});
}

function vimeo_on_finish(swf_id) {
	if($('div.emph').next().length == 0) {//debugger;
		video_id = $('div.emph').siblings('div').first()[0].id;
		$("div.emph").removeClass("emph").each(function() {
			$(this).siblings('div').first().addClass("emph");
		});
	}
	else {			
		video_id = $('div.emph').next()[0].id;
		$("div.emph").removeClass("emph").each(function() {
			$(this).next().addClass("emph");
		});
	}
	$('#player').children().remove();
	
	  // remove previous clickedclass
	$("div#" + this.id).addClass("emph");

	vimeo_player_create();
}
function vimeo_player_loaded(swf_id){ 
	$('#player').flash(function(){
		this.api_addEventListener("onFinish", "vimeo_on_finish");
	});			
}
		
function nav_open()
{  nav_canceltimer();
   nav_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');
   ddmenuitem.css('display', 'block');
}

function nav_close()
{
	window.onmousemove = function(e){
		var posx = 0;
		var posy = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
	
		if((posx > 233 && posx < 1033) && (posy > 110 && posy < 270)){			
			nav_canceltimer();
		}
	}
	
	if(ddmenuitem) {
		if(closetimer) {		
			$('.bbf-dropdown').stop();
			$('.bbf-dropdown').fadeOut(500, function() {
				ddmenuitem.css('visibility', 'hidden');
			});
		}
		else {
			ddmenuitem.css('visibility', 'hidden');
		}
	}
}

function nav_timer()
{  
	closetimer = window.setTimeout(nav_close, timeout);
}

function nav_canceltimer()
{  
	
	if(closetimer) {  
		window.clearTimeout(closetimer);
	}
	$('.bbf-dropdown').stop();
	$('.bbf-dropdown').fadeIn(500, function() {
			$('.bbf-dropdown').css('opacity', '1');
	});
	closetimer = null;
	window.onmousemove = null;
	
} 
	
function showDiv(div) {
	$('div.content-item').hide();
	$('#content_' + $(div).attr("id")).show();
}

function hideDiv(div) {
	$(div).hide();
	$('#content_optionDefault').show();
}
