/**
 * WAMU Main JavaScript
 * 
 */

/**
 * jQuery functions to test if an element exists.
 * .ifExists() can be chained in a jQuery call and .exists() can be tested for true/false.
 *  example: if (jQuery(selector).exists()) { }
 */
jQuery.fn.exists = function() { return (this.length > 0); };
jQuery.fn.ifExists = function() { if (this.length > 0) { return this; } };


 /**
  * jQuery pop up window
  * Call with: <a href="yourURL.html" rel="0" class="newWindow" >click me</a>
  */
var windowSizeArray = [ "width=770,height=395",
                        "width=600,height=400,scrollbars=yes" ];
jQuery(document).ready(function(){
  jQuery('.newWindow').click(function (event){

    var url = jQuery(this).attr("href");
    var windowName = "popUp";//$(this).attr("name");
    var windowSize = windowSizeArray[0]; // windowSizeArray[jQuery(this).attr("rel")];

    window.open(url, windowName, windowSize);

    event.preventDefault();

  });
});

/**
 * Show links in parent
 * 
 * This should be called on popup pages with links back to the parent.
 * These links should have the class "openInParent"
 */
function openInParent() {
	jQuery(document).ready(function(){
    jQuery("a.openInParent").click(function(event){ 
      window.opener.location = jQuery(this).attr('href');
			jQuery(window.opener).focus();
      event.preventDefault();
    }); 
	});
}


/**
 * Setup Station Selector / Now Playing 
 * Only run this if the on now section is being displayed
 */
jQuery(document).ready(function(){
	if (jQuery("#nowPlayingTabs").exists() && jQuery(".stationInfoNowOn").exists()) {
	  // Initialize jQuery UI Tabs
	  // See: http://jqueryui.com/demos/tabs
    jQuery("#nowPlayingTabs").tabs(); 

	  // Update Now Playing Play button & Station Selector list
	  jQuery("#stationSelector_885_1").click(function(){
	    jQuery("#nowPlaying_playButton").attr("href","/audio-player/885_1");
	  });
	  jQuery("#stationSelector_885_2").click(function(){
	    jQuery("#nowPlaying_playButton").attr("href","/audio-player/885_2");
	  });
	  jQuery("#stationSelector_885_3").click(function(){
	    jQuery("#nowPlaying_playButton").attr("href","/audio-player/885_3");
	  });
	  jQuery("#stationSelector_883_1").click(function(){
	    jQuery("#nowPlaying_playButton").attr("href","/audio-player/883_1");
	  });
	
	  // Actions for all Station Selector dropdown buttons
	  jQuery("a.stationSelector_link").click(function(){
	    // Change label of current station
	    jQuery("li.stationSelector_label").html("<p>" + jQuery(this).text() + "</p>");
	    // Show all stations
	    jQuery("li.stationSelector_names").css({
	      "display":"block",
	      "position":"relative",
	      "z-index":"1",
	      "visibility":"visible"
	    });
	    // hide this station
	    jQuery(this).parent("li").css({
	      "display":"none",
	      "position":"absolute",
	      "z-index":"0",
	      "visibility":"hidden"
	    });
	
	    // close ctools dropbutton
      jQuery(".nowPlayingHeader_stationSelector .ctools-twisty").click();
	  });		

    // Set default station
		jQuery("a.stationSelector_link.default").click();
    jQuery(".ctools-twisty, .nowPlayingHeader_stationSelector").click();
		// Display after ctools slide up timer
    setTimeout("jQuery(\"#nowPlayingTabs .hideFOUC\").fadeOut(300);",200); // slider set to 100
	} // END if exists #nowPlayingTabs
}); // END doc.ready for station selector

/**
 * jQuery clear and restore input field
 * see: http://bassistance.de/2007/01/23/unobtrusive-clear-searchfield-on-focus/
 * 
 * Usage: 
 *   Add a default value to the <input> field which will be displayed as the internal label
 *   Put the following line in $(document).ready or the footer or run after field has loaded: 
 *     jQuery(".IdentifierOfClearableFields").clearInput();
 */ 
jQuery.fn.clearInput = function() {
  return this.focus(function() {
    if( this.value == this.defaultValue ) {
      this.value = "";
    }
  }).blur(function() {
    if( !this.value.length ) {
      this.value = this.defaultValue;
    }
  });
};

;

