PEPS = {};

PEPS.rollover =
{
   init: function(scope)
   {
      this.preload();

      $("img.auto-rollover, input[type=image].auto-rollover", scope).hover(
         function () { $(this, scope).attr( 'src', PEPS.rollover.newimage($(this, scope).attr('src')) ); },
         function () { $(this, scope).attr( 'src', PEPS.rollover.oldimage($(this, scope).attr('src')) ); }
      );
/*      $("input.auto-rollover", scope).hover(
	  
         function () { alert(PEPS.rollover.newimage($(this, scope).css('background-image')));  $(this, scope).css( 'background-image', PEPS.rollover.newimage($(this, scope).css('background-image')) ); },
         function () { $(this, scope).css( 'background-image', PEPS.rollover.oldimage($(this, scope).css('background-image')) ); }
      );*/
   },

   preload: function(scope)
   {
      $(window, scope).bind('load', function() {
         $('img.auto-rollover, input[type=image].auto-rollover').each( function( key, elm ) { $('<img>', scope).attr( 'src', PEPS.rollover.newimage( $(this).attr('src') ) ); });
// Not working yet         $('img.auto-rollover, input[type=image].auto-rollover').each( function( key, elm ) { $('<img>', scope).attr( 'src', PEPS.rollover.newimage( $(this).css('background-image') ) ); });
      });
   },
   
   newimage: function( src )
   {
      return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '-o' + src.match(/(\.[a-z]+)$/)[0];
   },

   oldimage: function( src )
   {
      return src.replace(/-o\./, '.');
   }

};

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function autorollover(scope)
{
	PEPS.rollover.init(scope);
}

function weblancet_jquery_init(scope){
	
	$('.auto-clear', scope).each(function(e) {
        $(this).data('default-value', $(this).val());
	});
	
	// Set focus/blur options
    $('.auto-clear', scope).focus(function() {
		var current = $(this).val();
        if (!$(this).data('default-value') || current == $(this).data('default-value'))
        {
            $(this).attr('value', '');
            $(this).html('&nbsp;');
        }
    }).blur(function() {
		var current = $(this).val();
        var def = $(this).data('default-value');
        if (current == '' || current == '&nbsp;')
        {
            $(this).attr('value', def);
            $(this).html(def);
        }
    });
    $('input.auto-password-clear', scope).focus(function() {
		$(this).hide().parent().find('input.auto-password')
			.show()
			.focus()
			.blur(function() {
				if (!$('input.auto-password').attr('value'))
					$(this).hide().blur(function(){}).parent().find('input.auto-password-clear').show();
			});
    });

	 $('a.needs-confirmation', scope).click(function(e) {
	 	e.preventDefault();
	 	 $('#confirmation-dialog')
		 	.data('forward-url', $(this).attr('href'))
			.html($(this).attr('title'))
			.dialog('open');
	 });

    autorollover($("body", scope));

}

$(document).ready(function() {
	$('.ui-state-default').hover(
					function() { $(this).addClass('ui-state-hover'); }, 
					function() { $(this).removeClass('ui-state-hover'); }
	);
	
	weblancet_jquery_init(document);	 

	$('#confirmation-dialog').dialog({
	            autoOpen: false,
	            width: 400,
	            modal: true,
	            resizable: false,
	            buttons: {
	                "Confirm": function() {
	                    window.location = $(this).data('forward-url');
	                },
	                "Cancel": function() {
	                    $(this).dialog("close");
	                }
	            }
	        });	 
    
    $(".item").hover(
	function(e) {
    	    $(e.target).addClass('itemHover');
	},
	function(e) {
    	    $(e.target).removeClass('itemHover');
	}
    );
						       
    $(".itemActive").hover(
	function(e) {
    	    $(e.target).addClass('itemActiveHover');
	},
	function(e) {
    	    $(e.target).removeClass('itemActiveHover');
	}
    );
	
	$('tr.RowSelectable').click(function(e){
		row = this;
		checkbox = $(row).find('#Check_' + this.id).get(0);
		if (checkbox && row) {
			if (!(e.target instanceof HTMLInputElement)) // if caller is not checkbox
				checkbox.checked = !checkbox.checked;
			if (checkbox.checked) 
				$(row).addClass('rowSelected');
			else 
				$(row).removeClass('rowSelected');
		}
	});
	
	if (typeof($(document).pngFix) == "function")					       
		$(document).pngFix(); // png transparency fix    
   // Modal image zoom for popups
//   $('').lightBox();

//   $('a[rel=zoom],a[rel=lightbox]').lightBox();
//	$.localScroll();
});



