(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);

function slide(navigation_id, pad_out, pad_in, time, multiplier)
{
	// creates the target paths
	var list_elements = navigation_id + " li";
	var link_elements = list_elements + " a";

	// initiates the timer used for the sliding animation
	var timer = 0;

	// creates the slide animation for all list elements
	$(list_elements).each(function(i)
	{
		// margin left = - ([width of element] + [total vertical padding of element])
		$(this).css("margin-left","-180px");
		// updates timer
		timer = (timer*multiplier + time);
		$(this).animate({ marginLeft: "0" }, timer);
		$(this).animate({ marginLeft: "15px" }, timer);
		$(this).animate({ marginLeft: "0" }, timer);
	});

	// creates the hover-slide effect for all link elements
	$(link_elements).each(function(i)
	{
		$(this).hover(
		function()
		{
			$(this).animate({ paddingLeft: pad_out }, 150);
		},
		function()
		{
			$(this).animate({ paddingLeft: pad_in }, 150);
		});
	});
}



$(function() {

	initTooltips({
		timeout: 6000
	});

});

function initTooltips(o) {
//alert($.browser.safari)

	var showTip = function() {
		var el = $('.tt', this).css('display', 'block')[0];

		var ttHeight = $(el).height();
		var ttOffset =  el.offsetHeight;
		var ttTop = ttOffset + ttHeight;

		$('.tt', this)
			.stop()
			.css({
				'opacity': 0,
				'top': 15 - ttOffset
			})
			.animate({
				'opacity': 1,
				'top': 25 - ttOffset
			}, 250);
	};

	var hideTip = function() {

		var self = this;
		var el = $('.tt', this).css('display', 'block')[0];

		var ttHeight = $(el).height();
		var ttOffset =  el.offsetHeight;
		var ttTop = ttOffset + ttHeight;
	//	alert(label.height());
	//	el.hiding = true;
		$('.tt', this)
			.stop()
			.animate({
				'opacity': 0,
				'top': 10 - ttOffset
			}, 250, function() {
				el.hiding = false;
				$(this).css('display', 'none');
			});


	};

	$('.tip .tt').hover(
		function() { return false; },
		function() { return true; }
	);

	$('.tip').hover(
		function(){
			var self = this;
			showTip.apply(this);
			if (o.timeout) this.tttimeout = setTimeout(function() { hideTip.apply(self) } , o.timeout);
		},
		function() {
			clearTimeout(this.tttimeout);
			hideTip.apply(this);
		}
	);

}


$(function () {
  // IE6 doesn't handle the fade effect very well - so we'll stick with
  // the default non JavaScript version if that is the user's browser.
  //if ($.browser.msie && $.browser.version < 7) return;

  $('#nav li, #projects')

    // remove the 'highlight' class from the li therefore stripping
    // the :hover rule
    .removeClass('highlight')

    // within the context of the li element, find the a elements
    .find('a')

    // create our new span.hover and loop through anchor:
    .append('<span class="hover"></span>').each(function () {

      // cache a copy of the span, at the same time changing the opacity
      // to zero in preparation of the page being loaded
      var $span = $('> span.hover', this).css('opacity', 0);

      // when the user hovers in and out of the anchor
      $(this).hover(function () {
        // on hover

        // stop any animations currently running, and fade to opacity: 1
        $span.stop().fadeTo(222, 1);
      }, function () {
        // off hover

        // again, stop any animations currently running, and fade out
        $span.stop().fadeTo(444, 0);
      });
    });

   $('.post.play')
	.css( {backgroundPosition: "0 -91px"} )
	.mouseover(function(){
		$(this).stop().animate(
			{backgroundPosition:"(0 0)"},
			{duration:199})
		})
	.mouseout(function(){
		$(this).stop().animate(
			{backgroundPosition:"(0 -91)"},
			{duration:199})
		});
   slide("li.categories ul", 20, 0, 0, .8);
});

