// Image rollover script modified from:
// http://peps.ca/blog/easy-image-rollover-script-with-jquery/
//
// Popup code modified from:
// http://yensdesign.com/2008/09/how-to-create-a-stunning-and-smooth-popup-using-jquery/

SIMPLENOTES = {
	popupStatus: 0,
    popupId: "",
};

SIMPLENOTES.rollover =
{
   init: function()
   {
      this.preload();
     
      jQuery(".simple-notes-popup-img").hover(
         function () { jQuery(this).attr( 'src', SIMPLENOTES.rollover.newimage(jQuery(this).attr('src')) ); },
         function () { jQuery(this).attr( 'src', SIMPLENOTES.rollover.oldimage(jQuery(this).attr('src')) ); }
      );
   },

   preload: function()
   {
      jQuery(window).bind('load', function() {
         jQuery('.simple-notes-popup-img').each( function( key, elm ) { jQuery('<img>').attr( 'src', SIMPLENOTES.rollover.newimage( jQuery(this).attr('src') ) ); });
      });
   },
   
   newimage: function( src )
   {
      return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '-hover' + src.match(/(\.[a-z]+)$/)[0];
   },

   oldimage: function( src )
   {
      return src.replace(/-hover\./, '.');
   }
};

SIMPLENOTES.popup =
{
  init: function()
  {
	// Set up the visiblity of the popups.
    this.preload();
	//jQuery("#simpleNotesMenu-popup").hide();

    jQuery(".simple-notes-popup-link").click(
      function()
      {
        $id = jQuery(this).attr('id');
        simpleNotesCenterPopup($id);
	    simpleNotesLoadPopup($id);
	    return false;
      }
    );

    // Handle clicking on the "x" button.
    jQuery(".simple-notes-popup-close").click(function()
    {
      simpleNotesDisablePopup();
    });

    // Clicking outside of the popup on the background.
    jQuery("#simple-notes-popup-background").click(function()
    {
      simpleNotesDisablePopup();
    });

    // We also allow pressing the Escape to exit.
    jQuery(document).keypress(function(e){
      if(e.keyCode == 27 && SIMPLENOTES.popupStatus == 1)
      {
        simpleNotesDisablePopup();
      }
    });
  },

  preload: function()
  {
     jQuery(window).bind('load', function()
     {
       jQuery('.simple-notes-popup').each( function( key, elm )
       {
		 jQuery(elm).hide();
       });
      });
   }
};

// Load the popup for this menu.
function simpleNotesLoadPopup($id)
{
  //loads popup only if it is disabled
  if (SIMPLENOTES.popupStatus == 0)
  {
	// Figure out what ID to work with.
    $popupId = "#" + $id + "-popup";

	// Fade in the background color.
    jQuery("#simple-notes-popup-background").css({
      "opacity": "0.7"
    });
    jQuery("#simple-notes-popup-background").fadeIn("fast");

    // Fade in the popup itself.
    jQuery($popupId).fadeIn("fast");

    // Mark the flag so we know to hide it.
    SIMPLENOTES.popupStatus = 1;
    SIMPLENOTES.popupId = $popupId;
  }
}

//disabling popup with jQuery magic!
function simpleNotesDisablePopup()
{
  //disables popup only if it is enabled
  if (SIMPLENOTES.popupStatus == 1)
  {
	// Cause the background and popup to disappear.
    jQuery("#simple-notes-popup-background").fadeOut("fast");
    jQuery(SIMPLENOTES.popupId).fadeOut("fast");

    SIMPLENOTES.popupStatus = 0;
	SIMPLENOTES.popupId = "";
  }
}

//centering popup
function simpleNotesCenterPopup($id)
{
  $popupId = "#" + $id + "-popup";

  //request data for centering
  var windowWidth = document.documentElement.clientWidth;
  var windowHeight = document.documentElement.clientHeight;
  var popupHeight = jQuery($popupId).height();
  var popupWidth = jQuery($popupId).width();

  // Center the popup itself.
  jQuery($popupId).css({
	"top": windowHeight/2 - popupHeight/2,
	"left": windowWidth/2 - popupWidth/2
  });

  //only need force for IE6
  jQuery("#simple-notes-popup-background").css({
    "height": windowHeight
  });
}
