//New Flash Widget
$(document).ready(function(){
	var hash;
	$('.flashWidget').each(function(i){
		eval('hash='+$(this).attr("rel"));
		$(this).flash(hash);	
	});
});	


// Configure a link to load colorbox and then run JWPlayer embed code on contents after load.
// Added by Brian 07/14/2011
var colorboxopencheckintervalID = -1;
jQuery.fn.colorboxWithJWPlayer = function (linkelement) {
	// we just call colorbox ... the callback events don't work on this version of colorbox
    jQuery(this).colorbox();

    // when we get a colorbox complete event, fire off our JWPlayer setup
    $(document).bind('cbox_complete', function() {
		colorboxopencheckintervalID = setInterval(configureColorBoxVideoIfOpen,150);
    });
    
    // when we get a colorbox cleanup event, fire off our JWPlayer destructor
    $(document).bind('cbox_cleanup', function() {
		var myplayer = jwplayer('colorboxembed');
		if ( myplayer != null ) {
			myplayer.remove();
		}
    });
};

// This function is called repeatedly via setInterval to check if the colorbox is open yet,
// we had to create and use it because the colorbox events don't seem to fire at the appropriate time.
// Added by Brian 07/14/2011
function configureColorBoxVideoIfOpen() {
	if ( $('div#colorboxembed').length > 0 ) {
		// clear the interval scheduler
		clearInterval(colorboxopencheckintervalID);
		configureJWPlayerInsideColorbox();
	}
}

// This function reads the HTML content that is loaded into the color box, parses the appropriate
// video configuration values from it, and then runs the JWPlayer scripts inside the colorbox itself.
// Added by Brian 07/14/2011
function configureJWPlayerInsideColorbox() {
	// read settings from the <img> tag on the HTML page
	var cblink = $("#colorboxembed a");
	var cbimage = $("#colorboxembed img");
	var imgfilename = cbimage.attr("src");
	var videofilename = cbimage.attr("alt");
	var videowidth = cbimage.attr("width");
	var videoheight = cbimage.attr("height");
	
//	alert( 'videofilename: ' + videofilename + "\nimgfilename: " + imgfilename );
	// call the JWPlayer setup routine on the container in the HTML page			
	jwplayer('colorboxembed').setup({
		'file': videofilename,
		'image': imgfilename,
		'width': videowidth,
		'height': videoheight,
		modes: [
			{type:'flash', src:'/js/jwplayer.swf'},
			{type:'html5'},
			{type:'download'}
		]
	});
	
}
