$(function(){

	$(".video:not(.locked):not(.add)").hover(function() {
		$(this).addClass("videoHover");
	}, function() {
		$(this).removeClass("videoHover");
	});
	
	$(".locked").hover(function() {
		$(this).addClass("lockedHover");
	}, function() {
		$(this).removeClass("lockedHover");
	});
	
	var video = $(".eep").parent("div:not(.locked)");	
	
	pathToEep = "eep/eep.php";
	
	video.click(function(e){
	
		$("#eepEdit").remove();																
	
		instance = $(this).children(".eep:first");
		element = "UL";

		clickX = e.pageX;
		clickY = e.pageY;					
	
		content = instance.html();

			//pathToEep + "#editor",
		
		$.post(
			pathToEep,
			{
				loadEditor: "true",
				type: element,
				content: content,
				instance: "../content/" + instance.attr("title")				
			},
			function(data) {

				$("body").prepend(data);
				
				$("body").prepend("<div id='overlay'></div>");
				
				$("#overlay").fadeIn(300, function() {
					$("#eepEdit").fadeIn(300);				
				});
				
				$("#overlay").click(function() {
					$(".eepCancel").click();
				});
				
				$(".eepCancel").click(function(){
					$("#eepEdit").fadeOut(300, function() {
						$(this).remove();																
						$("#overlay").fadeOut(300, function() {
							$(this).remove();																						
						});							
					});
					return false;
				});
				
				$(".eepSubmit").click(function(){
				
					$(".eepCancel").click();				
				
					content = $(this).siblings("textarea").val();
					linkText = $(this).siblings("#linkText").val();
					URI = $(this).siblings("#URI").val();					
					$.post(
						pathToEep,
						{
							content: content,
							URI: URI,
							linkText: linkText,
							instance: "../content/" + instance.attr("title"),
							loadEditor: "false",
							type: element
						},
						"html"
					);			
					instance.fadeTo(1200, 0, function() {
						$.post(
							pathToEep,
							{
								instance: "../content/" + instance.attr("title"),
								getContent: "true"
							},
							function(data) {
								instance.html(data);					
								instance.fadeTo(200, 1);							
							},
							"html"
						);			
					});
					
					return false;
				});							
			},
			"html"
		);
		
		return false;
		
	});
	
	$(".index a").click(function() {
		var hash = $(this).attr("href");
		var anchor = hash.toString();				
		anchor = $(anchor);
		var offset = anchor.offset();
		$("html, body").animate({scrollTop: offset.top}, 600);
		return false;		
	});
	
	var $videos = $(".video");
	var $searchBox = $("#search");
	
	$searchBox.keyup(
		function(e) {
			var term = $(this).val().toLowerCase();
			$videos.each(
				function() {
					if (term == "") {
						$(this).show();
					} else {
						var line = $(this).text();
						$(this).removeHighlight();
						if(-1 == line.toLowerCase().indexOf(term.toLowerCase())) {
							$(this).hide();
						} else {
							$(this).show();
							if(term.length > 1) {
								$(this).highlight(term);						
							}
						}
					}
				}
			);
		}
	);	

	$("input[type=text]").focus(function() {
		if ($(this).val() == $(this).attr("title")) {
			$(this).val("");
		}
	});	

});

/*

highlight v3

Highlights arbitrary terms.

<http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>

MIT license.

Johann Burkard
<http://johannburkard.de>
<mailto:jb@eaio.com>

*/

jQuery.fn.highlight = function(pat) {
 function innerHighlight(node, pat) {
  var skip = 0;
  if (node.nodeType == 3) {
   var pos = node.data.toUpperCase().indexOf(pat);
   if (pos >= 0) {
    var spannode = document.createElement('span');
    spannode.className = 'highlight';
    var middlebit = node.splitText(pos);
    var endbit = middlebit.splitText(pat.length);
    var middleclone = middlebit.cloneNode(true);
    spannode.appendChild(middleclone);
    middlebit.parentNode.replaceChild(spannode, middlebit);
    skip = 1;
   }
  }
  else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
   for (var i = 0; i < node.childNodes.length; ++i) {
    i += innerHighlight(node.childNodes[i], pat);
   }
  }
  return skip;
 }
 return this.each(function() {
  innerHighlight(this, pat.toUpperCase());
 });
};

jQuery.fn.removeHighlight = function() {
 return this.find("span.highlight").each(function() {
  this.parentNode.firstChild.nodeName;
  with (this.parentNode) {
   replaceChild(this.firstChild, this);
   normalize();
  }
 }).end();
};