/*
	Title: jQuery Dialog Plugin
	Author: Scott Jehl, scott[at]filamentgroup.com
	Copyright (c) 2009 Filament Group 
	Licensed under MIT (filamentgroup.com/examples/mit-license.txt)
*/

if ($(window).width() > 480) {

 $.fn.dialog = function(settings){

	// Configurable options

	var o = $.extend({

	title: "h1, h2, h3, h4, h5, h6, legend, label, p", focus: "a, input, button, select, textarea, [tabindex]", buttons: false},settings);

	// Return jQuery	

	return $(this).each(function(){

	// Add body role

	if(!$("body").is("[role]") ){ $("body").attr("role", "application"); }

		// Close any existing dialogs

		$("#dialog").trigger("close");

		// Create screen

		var modalScreen = $("<div class=\"modal-screen\"><iframe src=\"javascript:false;\"></iframe></div>");

		// Reinforce some styles for IE :-(

		modalScreen.css("opacity", 0.7).height($(window).height()).width($(window).width()).children(0).css("opacity",0);

		// Dialog wrapper

		var dialog = $("<div id=\"dialog\" role=\"dialog\" aria-labelledby=\"dialog-title\"></div>");

		// Close link

		var close = $("<a href=\"#\" tabindex=\"2\" class=\"dialog-close\" role=\"button\" aria-controls=\"dialog\">Close</a>").appendTo(dialog);

		// Content div

		var dialogContent = $("<div class=\"dialog-content\"></div>").append(this).appendTo(dialog);

		// Specify title

		dialog.find(o.title).eq(0).attr("id", "dialog-title");

		// Buttons with events

		if(o.buttons){

			var dialogFooter = $("<div class=\"dialog-footer\"></div>");

			for(var name in o.buttons){

				$("<button>" + name + "</button>").click(o.buttons[name]).appendTo(dialogFooter);

			}

		dialogFooter.appendTo(dialog);

		}

		// ====== Events ======

		// Custom close event

		dialog.bind("close",function(){

			if (document.getElementById("highlight")) { // HACK: Kill video for good in IE 

				var video = document.getElementById("highlight");
				video.innerHTML = "";

			}

			modalScreen.remove();
			dialog.remove();
			$("body").removeClass("blocked");
			$(document).unbind("keydown.dialog focus.dialog");

		});

		// Close button click

		close.click(function(){ 

			dialog.trigger("close"); 
			return false;

		});

		// Close on esc key

		$(document).bind("keydown.dialog", function(ev){

			if(ev.which == 27){ 

				dialog.trigger("close"); 

			}

		});

		// Keep focus inside dialog while it's open

		$(document).bind("focus.dialog", function(ev){

			if(!$(ev.target).parents("#dialog").length){ 

				dialog.find("a, input, button, select, textarea, [tabindex]")[0].focus();  

			}

		});

		// Add blocked class to body, append modal and dialog	

		$("body").addClass("blocked").append(modalScreen).append( dialog );

		// Direct focus to title

		if(dialogContent.find(o.focus).length){

			dialogContent.find(o.focus)[0].focus();

		} else if(o.buttons){

			dialogFooter.find('button')[0].focus();

		} else {

			close[0].focus();

		}

	});

};


// Utility version, for url-based dialogs

$.dialog = function(url, settings){

	var o = $.extend({loadingText: "Loading...", complete: false}, settings);

	//Create temp loader

	$("<span class=\"ajax\" tabindex=\"-1\">" + o.loadingText + "</span>").dialog({title: "span", focus: "span"});

	// Differentiate url from selector 

	var splitUrl = url.split(' ');
	var justUrl = splitUrl[0];

	splitUrl.shift();

	if(splitUrl.length){ var subset = splitUrl.join(' '); }

	$.get(justUrl,function(response){
	
		var response = $("<div></div>").append(response);

		if(subset != ' '){ response = response.find(subset); }

		response.dialog(settings);

		if(o.complete){o.complete(response);}

	});

};

$("a.video").click(function() { // Transcript popup

	var link = $(this).attr("href")+ "&s=0";
	//alert(link);
	$.dialog( link + " #highlight"); // Pull in video
	return false;

});

}// End Window width
