// JavaScript Document

jQuery.fn.hint = function() {
	return this.each(function(){
		var t = $(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();
		}
	})
}


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);
      })
    );
  });
}


jQuery.fn.clearForm = function() {
  return this.each(function() {
	var type = this.type, tag = this.tagName.toLowerCase();
	if (tag == 'form') {
		return $(':input',this).clearForm();
	}

	if (type == 'text' || type == 'password' || tag == 'textarea') {
		this.value = '';
	} else if (type == 'checkbox' || type == 'radio') {
		this.checked = false;
	} else if (tag == 'select') {
		this.selectedIndex = (this.className=='multi') ? -1 : 0;
	} else if (type == 'hidden') {
		this.name = 'blank';
		this.value = 0;
	}
  });
};

jQuery.fn.clearStyles = function() {
  $("form#search-research dd, form#search-research dt").each(function()
	{
		$(this).removeClass("selected");
		//set urls
	});

	$("#form-skills-performance").attr("href", "/search-for-artistes/?open_all=1&#show-skills");
	$("#form-skills-dancing").attr("href", "/search-for-artistes/?open_all=1&open=dancing#show-skills");
	$("#form-skills-language").attr("href", "/search-for-artistes/?open_all=1&open=languages#show-skills");
	$("#form-skills-military").attr("href", "/search-for-artistes/?open_all=1&open=military#show-skills");
	$("#form-skills-musical").attr("href", "/search-for-artistes/?open_all=1&open=musical#show-skills");
	$("#form-skills-sports").attr("href", "/search-for-artistes/?open_all=1&open=sports#show-skills");

};

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();
    });
};


// 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"), "");
			}
		}
	}
}


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=1000,height=600,scrollbars=1,resizable=1,toolbar=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);
