/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * 1. width=300
      Use this to define the width of the new window.

   2. height=200
      Use this to define the height of the new window.

   3. resizable=yes or no
      Use this to control whether or not you want the user to be able to resize the window.

   4. scrollbars=yes or no
      This lets you decide whether or not to have scrollbars on the window.

   5. toolbar=yes or no
      Whether or not the new window should have the browser navigation bar at the top (The back, foward, stop buttons..etc.).

   6. location=yes or no
      Whether or not you wish to show the location box with the current url (The place to type http://address).

   7. directories=yes or no
      Whether or not the window should show the extra buttons. (what's cool, personal buttons, etc...).

   8. status=yes or no
      Whether or not to show the window status bar at the bottom of the window.

   9. menubar=yes or no
      Whether or not to show the menus at the top of the window (File, Edit, etc...).

  10. copyhistory=yes or no
      Whether or not to copy the old browser window's history list to the new window.
  
  More Attributes
   1. screenX=number in pixels (screenX option used for both NS4+ and IE4+)
      Sets the position of the window in pixels from the left of the screen in Netscape 4+.
      3. left=number in pixels
         Sets the position of the window in pixels from the left of the screen in IE 4+.

   2. screenY=number in pixels (screenY option used for both NS4+ and IE4+)
      Sets the position of the window in pixels from the top of the screen in Netscape 4+.
      4. top=number in pixels
         Sets the position of the window in pixels from the top of the screen in IE 4+.
   

    
 *
 */


jQuery.fn.newWindow = function(o){
  $(this).click(function(e){
    e.preventDefault();
    o = o ? o : {};
    var windowUrl = $(this).attr('href');
    var windowName = $(this).attr('title').replace(' ', '_');
    var newWindowOptions = {};
    var newWindowAttrs = '';
    
    if (o.width){
      newWindowOptions.width = 'width='+o.width;
    }
    else if (o.maxWidth){
      if (o.maxWidth <= screen.availWidth){
        newWindowOptions.width = 'width='+o.maxWidth;
      }
      else {
        newWindowOptions.width = 'width='+screen.availWidth;
      }

    }
    else {
      newWindowOptions.width = 'width=640';
    }

    if (o.height){
      newWindowOptions.height = 'height='+o.height;
    }
    else if (o.maxHeight){
      if (o.maxHeight <= screen.availHeight){
        newWindowOptions.height = 'height='+o.maxHeight;
      }
      else {
        newWindowOptions.height = 'height='+screen.availHeight;
      }

    }
    else {
      newWindowOptions.height = 'height=480';
    }

    if (o.resizable){
      newWindowOptions.resizable = 'resizable='+o.resizable;
    }
    else {
      newWindowOptions.resizable = 'resizable=yes';
    }

    if (o.scrollbars){
      newWindowOptions.scrollbars = 'scrollbars='+o.scrollbars;
    }
    else {
      newWindowOptions.scrollbars = 'scrollbars=yes';
    }

    if (o.toolbar){
      newWindowOptions.toolbar = 'toolbar='+o.toolbar;
    }
    else {
      newWindowOptions.toolbar = 'toolbar=no';
    }

    if (o.location){
      newWindowOptions.location = 'location='+o.location;
    }
    else {
      newWindowOptions.location = 'location=no';
    }

    if (o.directories){
      newWindowOptions.directories = 'directories='+o.directories;
    }
    else {
      newWindowOptions.directories = 'directories=no';
    }

    if (o.status){
      newWindowOptions.status = 'status='+o.status;
    }
    else {
      newWindowOptions.status = 'status=no';
    }

    if (o.menubar){
      newWindowOptions.menubar = 'menubar='+o.menubar;
    }
    else {
      newWindowOptions.menubar = 'menubar=no';
    }

    if (o.copyhistory){
      newWindowOptions.copyhistory = 'copyhistory='+o.copyhistory;
    }
    else {
      newWindowOptions.copyhistory = 'copyhistory=no';
    }
    
    if (o.screenX){
      newWindowOptions.screenX = 'screenX='+o.screenX;
      newWindowOptions.left = 'left='+o.screenX;
    }
    else {
      newWindowOptions.screenX = 'screenX=15';
      newWindowOptions.left = 'left=15';
    }
    
    if (o.screenY){
      newWindowOptions.screenY = 'screenY='+o.screenY;
      newWindowOptions.top = 'top='+o.screenY;
    }
    else {
      newWindowOptions.screenY = 'screenY=15';
      newWindowOptions.top = 'top=15';
    }

    // put all the options into a single string
    $.each(newWindowOptions, function(){
      newWindowAttrs += this + ',';
    });

    newWindowAttrs = newWindowAttrs.substr(0, newWindowAttrs.length-1);

    window.open(windowUrl, windowName, newWindowAttrs);

  })
};