//<![CDATA[

FunctionHandler.register( '*', function(){

	// FIX FOR SEARCH BOX PLACEHOLDER IN OLD IE

	oldie_set_placeholder(".search_input", "Search ARDMS");
	
	// SET TARGET _blank ON EXTERNAL LINKS
	
	$("a").each(function(){
	
		if ( !this.target && (this.href.search(/ardms.org/i) < 0) && (this.href.search(/mailto/i) < 0) ) {
			this.target = "_blank";
		}
	
	});
	
	// PODCAST PLAYER

	$('a.audio_player').each(function(index){
		
		var this_href = $(this).attr("href");
		var this_title = $(this).attr("title");
		
		if ( this_href.indexOf(".mp3") > 1 ) {
			
			$(this).wrap(function() {
		 		return '<div id="jplayer_container_' + index + '" class="jplayer_container" data-playersrc="' + this_href + '" data-playertitle="' + this_title + '" data-playerindex="' + index + '" />';
			});	
		
		}
			
	});
	
	$('.jplayer_container a.audio_player').live("click", function(e){
	
		e.preventDefault();
	
		var this_player_div = $(this).parent(".jplayer_container");
		
		var this_playersrc = this_player_div.data("playersrc");
		var this_playertitle = this_player_div.data("playertitle");	
		var index = this_player_div.data("playerindex");
		
		this_player_div.load('/audio_player/jPlayer', function(){
		
			// window.log("Activating player: jp_jplayer_" + index);
			
			$("#jp_interface_X").attr("id", ("jp_interface_"+index));
			$("#jp_playlist_X").attr("id", ("jp_playlist_"+index));
			$("#jp_close_X").attr("id", ("jp_close_"+index)).data("playerindex",index);
			
			$("#jp_close_"+index+" a").live("click", function(){
				var newlink = $("<a>"+this_playertitle+"</a>").addClass("audio_player").attr("href", this_playersrc);
				$("#jplayer_container_"+index).empty().append(newlink);
				
			});
						
			if (this_playertitle) {$("#jp_playlist_"+index).children("ul").empty().append('<li>'+this_playertitle+'</li>')} else {
				$("#jp_playlist_"+index).children("ul").empty().append('<li>(podcast player)</li>')
			}

			$("#jp_jplayer_X").attr("id", ("jp_jplayer_"+index)).jPlayer({
				ready: function () {
					// alert("player ready");
					$(this).jPlayer("setMedia", {
						mp3: this_playersrc
					}).jPlayer("play");
				},
				swfPath: "/assets/scripts/jPlayer/",
				supplied: "mp3",
				// solution: "flash",
				cssSelectorAncestor: ("#jp_interface_"+index)
			}).bind($.jPlayer.event.play, function(){$(this).jPlayer("pauseOthers");});
		
		});
	
	});

});

FunctionHandler.register( 'contact_ardms', function(){

	//ARDMS Contact Us Template (template with all subjects listed)

	$(function(){
		var id = hashSt('id');
		if (id != '') {
		var obj = document.getElementById('contactUs');
			if (obj != null) {
				obj.src = obj.src + '#?id=' + id;
			}
		}
	});
	
	window.log("Contact ARDMS listing scripts run.");
	window.log("hash: " +  window.location.hash);
	window.log("id: " + hashSt('id'));

});


FunctionHandler.register( 'contact_us', function(){

	//Contact Us Template (once they select a subject and hit Contact Us)

	$(function(){
		var s = hashSt('sub');
		var id = hashSt('id');
		var src = hashSt('src');
		if (s != '') {
			
			window.log("s: "+s);
			var obj = $("iframe#ContactUs");
			var new_src = "http://online.ardms.org/contactardms/contactus.aspx?s=" + s;
			
			if (id != '') {
				new_src = new_src + '&id=' + id;
			}
			
			window.log("new_src: "+new_src);
			obj.attr("src", new_src);
			
			$("#ContactUs").remove();
			var newFrame = $('<iframe src="' + new_src + '" id="ContactUs" frameborder="0" height="600" width="100%"></iframe>');
			$("#ContactUsContainer").append(newFrame);
		}
	});
	
	window.log("Contact Us form scripts run.");
	window.log("hash: " + window.location.hash );
	window.log("sub :" + hashSt('sub'));
	

});


//]]>
