// JavaScript Document
$.fn.image = function(src, f){
  return this.each(function(){
    $("<img>").appendTo(this).src( src ).load( f );
  });
};

$.fn.image2 = function(src, f){
   return this.each(function(){
			$(".sgImage").remove();
			$("#waiting").addClass("show"); 
			var img_tag = "<img src=" + src + " class='sgImage' />"
     	$(img_tag).appendTo(this);
     	//$('.sgImage').src = src;
			return false;
   });
};

$.fn.image3 = function(src, f){
	 $(".sgImage").remove();
	 $("#waiting").addClass("show");
   return this.each(function(){
     $("<img />").appendTo(this).each(function(){
				$(this).addClass('sgImage');																			 
        this.src = src;
        this.onload = f;
     });
   });
} 
/**
 * Converts obfuscated email addresses into normal, working email addresses.
 *
 * @name defuscate
 * @param Boolean link If true, all defuscated email addresses will be turned into links, defaults to true (optional)
 * @param String find The regular expression used to search for obfuscated email addresses (optional)
 * @param String replace Replacement text for defuscating email addresses (optional)
 * @descr Converts obfuscated email addresses into normal, working email addresses
 */
jQuery.fn.defuscate = function( settings ) {
    settings = jQuery.extend({
        link: true,
        find: /\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})\b/gi,
        replace: '$1@$2'
    }, settings);
    return this.each(function() {
        if ( $(this).is('a[@href]') ) {
            $(this).attr('href', $(this).attr('href').replace(settings.find, settings.replace));
            var is_link = true;
        }
        $(this).html($(this).html().replace(settings.find, (settings.link && !is_link ? '<a href="mailto:' + settings.replace + '">' + settings.replace + '</a>' : settings.replace)));
    });
};

/**************************************************
* Pops up href location in a new centered window using the specified attributes.
***************************************************/
function popupwin(thehref, win, xwidth, yheight, attrib )
	{
	//figure out the center position
	var scrW = screen.availWidth;
	var scrH = screen.availHeight;
	if (xwidth > scrW)
		{
		pW = scrW - 10;
		}
	else
		{
		pW = xwidth;
		}
	if (yheight > scrH)
		{
		pH = scrH - 40;
		}
	else
		{
		pH = yheight;
		}
	scrX = (scrW - pW - 10) * .5;
	scrY = (scrH - pH - 30) * .5; 
	
	var windowatts = "width=" + xwidth + ",height=" + yheight + ",left=" + scrX + ",top=" + scrY + ",screenX=" + scrX + ",screenY=" + scrY;
  windowatts += "," + attrib;
	window.open( thehref, win, windowatts);
	return false;
	}
	
/**************************************************
*  Sitewide OnLoad (Ready) action, leave at bottom of page.
***************************************************/
$(document).ready(function()
	{
	 $(".email").defuscate();
	 	$("a.popup").click( function()
		{
			var address = this.href;
			popupwin(address, '', 900, 600, 'statusbar=1,status=1,toolbar=1,scrollbars=1,resizable=1,location=1,address=1,directories=1,menubar=1' );
			return false;
		});
	 	$("a.popup-big").click( function()
		{
			var address = this.href;
			popupwin(address, '', 960, 700, 'statusbar=1,status=1,toolbar=1,scrollbars=1,resizable=1,location=1,address=1,directories=1,menubar=1' );
			return false;
		});		
	});
