var dialogTimer;
$(document).ready(function(){ 
	$("#searchtext a").click(function() {
    	$("form[name='audsearch']").attr("action",$(this).attr("href")).attr("method",$(this).attr("rel"));
		$("input#searchbox").attr("name",$(this).attr("name"));
		$("a.active").removeClass("active");
		$(this).addClass("active");
		return false;
    });
	$("#imagerotator").infiniteRotator();
	$("ul.sf-menu").superfish({delay:1000,animation:{opacity:"show",height:"show"},speed:"fast",autoArrows:false,dropShadows:false});
	$("ul.sf-menu li li a").css("background-color","#322354").fadeTo(500,0.9);
	$("ul.sf-menu li li a").hover(function(){$(this).fadeTo(1,1.0);},function(){$(this).fadeTo(1,0.9)});
	if ($("#modal > *").length !== 0) {
	  document.getElementById("wrapper").scrollIntoView(true);
	  $("#modal").dialog({
		  modal: true,
		  autoOpen: false,
		  open: (ytPlayEmbed()),
		  //focus: (overlayClicker()),
		  draggable: false, 
		  hide: "fadeOut('fast')", 
		  resizable: false, 
		  overlay: {"background-color": "#000", "opacity": "0.75", "-moz-opacity": "0.75"},
		  position: ["50%", 133],
		  width: 780,
		  height: 570
	  });
	  //dialogTimer = setTimeout(closeDialog, 10000);
	  $("#modalCall").click(function() {
		  $("#modal").dialog('open');
		  return false;
	  });
	};
});
/*function overlayClicker() {
	if ($("#modal").dialog('isOpen') == true) {
		console.log('Saying it\'s open');
	  	$(document).bind("click", function(e){
		  if (!$(e.target).parents().filter('.ui-dialog').length) {
			  closeDialog();
		  }
	  	});
	} else if ($("#modal").dialog('isOpen') == false) {
		console.log('Saying it\'s closed');
		$(document).unbind("click", function(e){
			if (!$(e.target).parents().filter('.ui-dialog').length) {
				closeDialog();
			}
		});
	}
}*/
function closeDialog() {
	$("#modal").dialog('close');
	clearTimeout(dialogTimer);
};
function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("player");
  ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
  //console.log('tried embeding')
}
function onytplayerStateChange(newState) {
	if (newState === 1) {
		clearTimeout(dialogTimer)
		//console.log("I cleared the timer", newState)
	} else if (newState === 0){
		dialogTimer = setTimeout(closeDialog, 10000);
		//console.log("Set to close again", newState, dialogTimer)	
	} else {
		//console.log("Some other state has been called", newState, dialogTimer)	
	}
}
function ytPlayEmbed() {
	var params = { allowScriptAccess: "always" };
    //var atts = { id: "myytplayer" };
    swfobject.embedSWF("http://www.youtube.com/v/WRdOzZOpZEM?enablejsapi=1&playerapiid=ytplayer", 
                       "player", "320", "185", "8", null, null, params);//, atts
}
function recordOutboundLink(link, category, action) {
  try {
    var pageTracker=_gat._getTracker("UA-1408456-1");
    pageTracker._trackEvent(category, action);
    setTimeout('document.location = "' + link.href + '"', 100)
  }catch(err){}
}