/* 
 *  Scripts for VSA Client Sites
 *  Site by Eli Van Zoeren - http://elivz.com
 */

$(document).ready(function() {
    
  // Add classes to input elements reflecting their
  // type. Allows styling in IE6
  $("input[type='text'], input[type='password'], textarea").addClass('input-text');
  $("input[type='submit'], input[type='button']").addClass('input-button');
  
  // Select the first form element on the page
  $('#username').focus();
  
  // Open external links in a new window
  $('a[href^="http://"]').filter(function() { return this.hostname && this.hostname !== location.hostname; })
    .attr("target", "_blank")
    .addClass('external');
  
  // Clear textboxes on focus
  $('.autoclear').autoClear();
  
  // Set print links to open the browser print dialog
  $('.printPage').click(function() {
  	window.print();
  	return false;
  });
  
  // Setup "New Item" links
  //dialog.init('a.newItem');
	
});


// Open "New Item" links in a dialog box
var dialog = {
  
  $dialogContent: $('<div id="dialogContent" />'),
  
  init: function(linkSelector) {
    // Create a div to hold the dialog markup
    dialog.$dialogContent.insertAfter('#wrapper').hide();
    
    // Bind to the "add new item" links
    $(linkSelector).click(dialog.load);
  },
  
  load: function() {
    // Get the url of the dialog contents
    var target = $(this).attr('href');
    
    // Load the dialog contents
    dialog.$dialogContent.load(target, dialog.show);
    
    return false;    
  },
  
  show: function(data) {
    // Setup the tabbed section
    $('#tabs', dialog.$dialogContent).tabs();
    
    // Execute all the scripts inside of the newly-injected HTML
    $("script", dialog.$dialogContent).each(function() {
        alert( this.text || this.textContent || this.innerHTML || "" );
    });
    
    // Display the dialog
    dialog.$dialogContent.dialog();
  }
  
};

// Clears the default text when an input receives
// focus and reinstates it if it is left blank
(function($) {

  $.fn.autoClear = function() {	
    return this.each(function() {
      $(this).focus(function() {
        if( this.value == this.defaultValue ) {
          this.value = "";
        }
      })
      .blur(function() {
        if( !this.value.length ) {
          this.value = this.defaultValue;
        }
      });
    });
  };

})(jQuery);