// JavaScript Document

var $j = jQuery.noConflict();

jQuery.fn.hint = function() {
	return this.each(function(){
		var t = $j(this); // get jQuery version of 'this'
		var title = t.attr('title'); // get it once since it won't change

		if (title) { // only apply logic if the element has the attribute

			// on focus, set value to blank if current value matches title attr
			t.focus(function(){
				if (t.val() == title) {
				  t.val('');
				  t.removeClass('blur');
				}
			})

			// on blur, set value to title attr if text is blank
			t.blur(function(){
				if (t.val() == '') {
				  t.val(title);
				  t.addClass('blur');
				}
			})

			// clear the pre-defined text when form is submitted
			t.parents('form:first()').submit(function(){
				if (t.val() == title) {
					t.val('');
					t.removeClass('blur');
				}
			});

			// now change all inputs to title
			t.blur();
		}
	})
}

$j(function(){
	$j('input:text, input:password').hint();
})


jQuery.fn.resizehandle = function() {
  return this.each(function() {
    var me = jQuery(this);
    me.after(
      jQuery('<div class="resizehandle"></div>')
      .bind('mousedown', function(e) {
        var h = me.height();
        var y = e.clientY;
        var moveHandler = function(e) {
          me
          .height(Math.max(20, e.clientY + h - y));
        };
        var upHandler = function(e) {
          jQuery('html')
          .unbind('mousemove',moveHandler)
          .unbind('mouseup',upHandler);
        };
        jQuery('html')
        .bind('mousemove', moveHandler)
        .bind('mouseup', upHandler);
      })
    );
  });
}


// add the class "ie6menu" to li tags to make ie6 mirror :hover
ie6menu = function() {
	if (document.getElementById("menu")) {
		var sfEls = document.getElementById("menu").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" ie6menu";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" ie6menu\\b"), "");
			}
		}
	}
}


jQuery.fn.mailme = function() {
    var at = / at /;
    var dot = / dot /g;
    this.each( function() {
        var addr = jQuery(this).text().replace(at,"@").replace(dot,".");
        var title = jQuery(this).attr('title')
        $(this)
            .after('<a href="mailto:'+addr+'" title="'+title+'">'+ addr +'</a>')
            .remove();
    });
};


function applyPopups()
{
  a = document.getElementsByTagName("a");

  for(i=0; i<a.length; i++)
  {
    if(a[i].getAttribute("target") && a[i].getAttribute("target") == "_blank")
    {
      a[i].onclick = function()
      {
        url = this.getAttribute("href");
        window.open(url,'popup','width=650,height=600,scrollbars=1,resizable=1');
        return false;
      }
    }
  }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(applyPopups);
addLoadEvent(ie6menu);
