function playerReady(o){
	var player = $("#"+o.id);
	player.each(function(){
		this.addControllerListener("PLAY","playerStart");
	});
//	console.log(o,'player ready');
}
function playerStart(o){
	if ( o.state ){
		$.slideshowStop();
		$("#"+o.id).parents("li").find("div.caption").fadeOut('fast');
	}else{
		$.slideshowStart();
	}
//	console.log(o,'player start');
}

$(document).ready(function() {
	
	$.slideshowStop = function(){
		$("#slideshow").children('ul').cycle("pause");
	};
	$.slideshowStart = function(){
		$("#slideshow").children('ul').cycle("resume");
	};
	if (navigator.userAgent.indexOf('MSIE') == -1) {
		$("#slideshow video").each(function(){
			this.addEventListener('play',function(){
				$.slideshowStop();
				$(this).parents("li").find("div.caption").fadeOut('fast');
			},false);
	
			this.addEventListener('pause',$.slideshowStart,false);
			this.addEventListener('ended',$.slideshowStart,false);
		});
	}
	if ($('li.on > ul').length == 0) {
		$('li.on').removeClass('on');
	}
	
	if (typeof DD_belatedPNG != 'undefined')
	{
		DD_belatedPNG.fix("#slideshow div.caption");
	}
	$('.breadcrumb a:last').addClass('last');
	// input initial value
	//$("input[type=text]").initval();
	if ( $("#slideshow ul li").length > 1 )
	{
		$("#slideshow ul").cycle({
			fx: 'fade',
			speed: 1000,
			timeout: 5000,
			before: function(){
				$(".caption",this).show();
				if ( $(this).is(".video") ){
					var c = $(".caption",this)[0];
					setTimeout(function(){$(c).fadeOut('medium');},2000);
				}
			}
		});
		$("#slideshow a.prev, #slideshow a.next").click(function(){
			var cmd = '';
			if ( $(this).is(".prev") ) cmd = 'prev';
			else cmd = 'next';
			$("#slideshow ul").cycle(cmd);
			return false;
		}).fadeTo(1,.3);
		/*$("#slideshow img").click(function(){
			$("#slideshow ul").cycle('next');
			return false;
		});*/

		$("#slideshow").hover(function(){
			$("a.next,a.prev",this).fadeTo('medium',1);
		}, function(){
			$("a.next,a.prev",this).fadeTo('medium',.3);
		});
	}else{
		$("#slideshow a.next, #slideshow a.prev").hide();
	}
	
	$("#navigator li.on").addClass("super-on").children('ul').show();
	if ( ! $("#navigator li.super-on").children("ul").length ){
		$("#navigator > ul").css("background","none");
	}

	$("#navigator li:has(ul)").not('.on').hover(function(){
//		$(this).addClass("on").siblings().removeClass('on');
		$("#navigator li > ul:visible").stop(true,true).fadeOut("medium");
		$(this).children("ul").fadeIn("medium");
	}, function(){
		if (!$(this).hasClass('con'))
			$(this).removeClass("on");
		$(this).children("ul").fadeOut('medium');
		$("#navigator li.super-on > ul").stop(true,true).fadeIn('medium');
//		$("#navigator li.super-on").addClass('on');
	});
	
	$("#navigator li:has(ul) a").addClass("with-sub");



	$("form a.submit").click(function(){
		$(this).parents('form').submit();
		return false;
	});

	if ( typeof jQuery.fn.altselect != 'undefined' )
	$("#browse select").altselect({debug: true});

	//add arrows
	$("a.arrow, #main-stories a").append(" <img src='/images/bullets/arrow-blue-r.gif' alt='' width='9' height='11' />");

	/*if ( $.fn.liveSearch )
		$("input.live-search").liveSearch({
			url: 'search-results.html?q=',
			id: 'live-results'
		});*/
	

	/*if ( $("form#browse").length ){
		$("form#browse").hide();
		$("#browse-sw").click(function(){
			$("form#browse").toggle("slow");
			return false;
		});
	}*/
	if ( $("div#browse").length ){
		$("div#browse").hide();
		$("#browse-sw").click(function(){
			$("div#browse").toggle("slow");
			return false;
		});
	}

	//archive search results
	$("ul.results a.more2").click(function(){
		if ( $(this).siblings(".hidden").length ){
			$(this).hide().siblings(".hidden").toggle("medium");
			return false;
		}
	});
	
	
	function split(val) {
		return val.split(/,\s*/);
	}
	function extractLast(term) {
		return split(term).pop();
	}
	$('#searchkwds,#i-keywords').click(function(){
		if ($(this).val() == 'Search') {
			$(this).val('');
		}
	});
	if ($("#searchkwds").length)
		$("#searchkwds").autocomplete({
			source: function(request, response) {
				$.getJSON("/base/cms/index/ajax", {
	//				term: extractLast(request.term)
					term: request.term
				}, response);
			},
			search: function() {
				// custom minLength
				var term = extractLast(this.value);
				if (term.length < 2) {
					return false;
				}
			},
			focus: function() {
				// prevent value inserted on focus
				return false;
			},
			select: function(event, ui) {
				/*var terms = split( this.value );
				// remove the current input
				terms.pop();
				// add the selected item
				terms.push( ui.item.value );
				// add placeholder to get the comma-and-space at the end
				terms.push("");
				this.value = terms.join(", ");
				return false;*/
			}
		});
	
});
