/**
 * @author lucianpacurar and jasongill
 */

$(document).ready(function() {
	$("#keynote-speakers #content .page p.speaker").each(function() {
		var speakerNode = $(this);
		speakerNode.data("height", speakerNode.height());
		speakerNode.data("content", speakerNode.html());
		speakerNode.html(speakerNode.html().substr(0,400) + "&hellip; <a class='readmore' href='#' onClick='return false;'>Read&nbsp;more &raquo;</a>");
		speakerNode.data("new-height", speakerNode.height());
	});
	$("#keynote-speakers #content a.readmore").live('click', function() {
		if($(this).parent().get(0).tagName=="EM"||$(this).parent().get(0).tagName=="STRONG"){
			//if the end of 400 characters is within an unclosed em or strong tag, it will break the Read More link
			//just remove the unclosed tag until the text gets expanded. (can't "close" it, browser already tried).
			$(this).unwrap();
		}
		speakerNode = $(this).parent();
		if (speakerNode.height() == speakerNode.data("new-height")) {
			speakerNode.html(speakerNode.data("content") + "&nbsp;<a class='readmore' href='#' onClick='return false;'>Read&nbsp;less &raquo;</a>");
		} else {
			speakerNode.html(speakerNode.html().substr(0,400) + "&hellip; <a class='readmore' href='#' onClick='return false;'>Read&nbsp;more &raquo;</a>");
		}
	});
	$(window.location.hash).effect('highlight',null,2500);
	$("#keynote-speakers .trigger").tooltip({relative:'true', position: 'bottom right', offset: [-50, 60]});
	$("#agenda .trigger").each(function() {
		$(this).tooltip({relative:'true', position: 'bottom right', offset: [-$(this).height()-30, 0]});
	});
	$('a[href^="http://www.youtube.com/watch"]').removeAttr("target").each(function(){ 
		this.href = this.href.replace(new RegExp("watch\\?v=", "i"), 'embed/');
	});
	if($(".cform").length!=0){
		$(".cform select option").each(function(){
			if($(this).val() == "-"){
				$(this).attr("disabled","disabled");
			}
			if($(this).val() == "groupstart"){
				var label = $(this).text();
				$(this).nextUntil('option[value|="groupend"]').wrapAll("<optgroup label='"+label+"' />");
				$(this).detach();
			}
		});
		$('option[value|="groupend"]').detach();
	}
});
