HC.video = {
	
	init : function() {
		HC.accordion.add_on_change_start_callback(HC.video.accordion_change_start);
		HC.accordion.add_on_change_done_callback(HC.video.accordion_change_done)
	},
	
	embed : function(element_id) {
		HC.video.save_video_holder(element_id);
		HC.video.draw_player(element_id);
	},
	
	_video_holders : { },
	save_video_holder : function(element_id) {
		HC.video._video_holders[element_id] = {
			html : $('#'+element_id).html()
		}
	},
	
	accordion_change_start : function (e, ui) {
		// check if we're unloading one of our loaded videos.
		for (element_id in HC.video._video_holders) {
			if($(ui.oldContent).find('#'+element_id)){
				HC.video.revert_to_holder(element_id);
			}
		};
	},
	
	accordion_change_done : function (e, ui) {
		// check for auto play flags
		$(ui.newContent).find('.auto_play_video').each(function(){
			HC.video.embed($(this).attr('id'));
		});
	},
	
	revert_to_holder : function (element_id) {
		$('#'+element_id).html(HC.video._video_holders[element_id].html);
		delete HC.video._video_holders[element_id];
	},
	
	draw_player : function(element_id) {

		var params = {
			bgcolor: "#000000",
			allowFullScreen: "true",
			allowScriptAccess: "always",
			wmode: "transparent"
		};
		
		// create container for the small Brightcove player
		$('#'+element_id).append("<div id=\"bc_smallPlayer\"></div>");
		
		var flashvars = {
			playerID: "23955449001",
			initialVideoID: element_id,
			playerWidth: 480,
			playerHeight: 270
		};
		var atts = { id: "bcSmallPlayerObject" };
		swfobject.embedSWF(docrootURL + "swf/player_480x270.swf", 
				"bc_smallPlayer", "480", "270",
				"9.0.0", "expressInstall.swf", flashvars, params, atts);
		
		
	}
	
}

HC.app.add_on_dom_ready(HC.video.init);
