jQuery.fn.delegate = function(eventType, rules) {
  return this.bind(eventType, function(e) {
    var target = $(e.target);
    for(var selector in rules)
      if(target.is(selector)) 
        return rules[selector].apply(this, arguments);
  })
};

jQuery.fn.realOffset = function() {
	var that = this[0];
	var x = 0;
	var y = 0;
	do {
		x += that.offsetLeft;
		y += that.offsetTop;
	} while (that = that.offsetParent);
	return {left: x, top: y};
}

$(function() {
	$("a[href=#]").click(function(e) {
		e.preventDefault();
	});
});
function setupAjaxForm(form, url){
	var timer = false;
    $(form).ajaxForm({
        url: url,
		target: form,
        success: function(response){
			if (timer) {
				clearTimeout(timer);
				timer = false;
			}
            setupAjaxForm(form, url);
        },
		beforeSubmit: function() {
			if (timer) {
				return false;
			}
			timer = setTimeout("$('" + form + " button').addClass('button-loading')", 300);
		}
    });
}


/* top nav */

$('#header a').hover(
	function () {
		navMove($(this));
	},
	function () {
		
	}
);

$('#header').hover(
	function () {},
	function () {
		navMove($('#header .active a'));
	}
);

$('#header').ready(function () {
    el = $('#header .active a');
	$('#selector').css('width', el.outerWidth());
	$('#selector').css('left', el.position().left + 'px');
	$('#selector').css('background-color', '#00B5CC');

});

function navMove(el)
{
	var vel = 250; // in pixels per 100ms
	var dis = Math.abs(el.position().left - $('#selector').position().left);
	var time = Math.round((dis / vel) * 100);
	
	$('#selector').animate({
		width: el.outerWidth(),
		left: el.position().left+'px'
	}, time, 'swing', false, false);
}

/* project listings */
// to deal with IEs opacity idiocy
$(function() {
	$('.project.hovers .text').css({opacity: 0}).show();
	$('.project.hovers').css({cursor: 'pointer'}).click(function() {
		location.href = $('h3 > a', this).attr('href');
	});
});
$('.project.hovers').hover(
	function () {
		$('.text',this).stop().fadeTo('100',1);
		$(this).stop().animate({borderTopColor: '#E9E9E9', borderRightColor: '#E9E9E9', borderBottomColor: '#E9E9E9', borderLeftColor: '#E9E9E9', backgroundColor: '#EEE' }, 100)
	},
	function () {
		$('.text',this).stop().fadeTo(1000,0);
		$(this).stop().animate({borderTopColor: '#FFF', borderRightColor: '#FFF', borderBottomColor: '#FFF', borderLeftColor: '#FFF', backgroundColor: '#FFF' }, 1000)
		
	}
);

