﻿/// <reference path="jquery/jquery-1.3.2-vsdoc2.js"/>

Cufon.replace('#navigation li a, #date', { fontFamily: 'Trade Gothic LT Std', fontWeight:'bold', separate: 'none', hover: true });
Cufon.replace('#main h1', { fontFamily: 'Trade Gothic LT Std' });

document.createElement("abbr");

$(document).ready(function()
{
	$(document).externalLinks();

	$('.labeltovalue').labelToValue();

	// Colorbox
	$("a[class*='colorbox']").colorbox({ iframe: true, width: "80%", height: "80%" });

	// Collapse / Expand
	$('.client-list h3 a').append('<span class="icon collapse"/>').click(function(e)
	{
		e.preventDefault();

		$(this).find('.icon').toggleClass('collapse')
		.end().parent().next('ul').fadeSlideToggle({ speed: 300 });

	}).click();

	// Project List
	$('.project-list img, .case-studies img').each(function(i)
	{
		var img = $(this);
		var imgHeight = img.height();

		$(this).load(function(e)
		{
			img = $(this);
			imgHeight = Math.max(imgHeight, img.height());

			if (imgHeight > img.parent().height())
			{
				attachBehaviour(img, imgHeight);
			}
		});

		if (imgHeight > img.parent().height())
		{
			attachBehaviour(img, imgHeight);
		}

		function attachBehaviour(img, imgHeight)
		{
			if (!img.data('hover'))
			{
				img.data('hover', true);

				img.mouseover(function(e)
				{
					$(this).stop().animate({
						top: img.parent().height() - imgHeight + 'px'
					}, 200, 'swing');
				})
				.mouseout(function(e)
				{
					$(this).stop().animate({
						top: '0px'
					}, 200, 'swing');
				});
			}
		};
	});

	// IE6 + IE7 fix for incorrect clearing behaviour on DD
	if ($.browser.msie && $.browser.version <= 8)
	{
		$('dl.vcard dd').each(function(i)
		{
			$('<div class="clear"/>').insertAfter($(this));
		});
	}
});

$.fn.delay = function(time, callback)
{
    jQuery.fx.step.delay = function() { };
    // Return meaningless animation, (will be added to queue)
    return this.animate({ delay: 1 }, time, callback);
};

$.fn.initAddThis = function()
{
    var self = $(this);
    var len = self.length - 1;
    
    self.each(function(i)
    {
        var addthis = $('<ul/>');

        var at_email = $('<li class="alpha email"><a class="addthis_button_email" href="#">Email</a></li>');
        var at_share = $('<li class="omega share"><a class="addthis_button" href="#">Share</a></li>');

        addthis.append(at_email).append(at_share);

        $(this).append($('<div class="social" />').append(addthis));

        if (i == len && $('head script[src*=addthis_widget.js]').length == 0)
        {
            $('head').append($('<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js?pub=tallulahlove"><\/script>'));
        }
    });

    return self;
}

$.assetsRoot = function()
{
    var scr = $('script[src*="/global.js"]');
    return (scr.length) ? scr.attr('src').replace(/\/js\/global\.js.*?$/gi, '') : '';
}

$.fn.externalLinks = function()
{
    return this.find('a[href^=http]:not([href*=' + location.hostname + ']), a:[rel=external]').attr('target', '_blank')/*.append('<em class="ext"> [this link opens in a new window] </em>')*/.end();
};

$.fn.bigLink = function(/**String*/selector)
{
    var self = this;
    var selector = selector || 'a[@href]:eq(0)';

    this.click(function(e)
    {
        var link = $(this).find(selector);

        // the first link within the supplied element
        var prevent_default = false;

        //
        var target_is_link = (e.target.nodeName == 'A');

        // check through all click events for preventDefault command (so we know whether to redirect or not)
        var all_events = link.data('events');
        var click_events;

        if (all_events && all_events.click)
        {
            click_events = all_events.click;

            for (var k in click_events)
            {
                if (/\.preventDefault\(\)/.test(click_events[k].toString()))
                {
                    prevent_default = true;
                }
            }
        }

        // trigger all jquery assigned events
        link.triggerHandler('click');

        // if preventDefault has never been assigned to this link then it is safe to do a standard redirect
        if (!prevent_default)
        {
            //document.location = link.attr('href');
            return !window.open(link.attr('href'), link.attr('target') || '_self');
        }
    })
	.hover(function()
	{
	    $(this).addClass('hover');
	},
	function()
	{
	    $(this).removeClass('hover');
	})
	.css('cursor', 'pointer');

    return self;
}

$.fn.labelToValue = function()
{
    var self = this;

    self.focus(function()
    {
        var field = $(this);

        if (this.id)
        {
            var label = $.trim($('label[for="' + this.id + '"]:last').hide().text());
        }
        else
        {
            return;
        }

        if (field.val() == label)
        {
            field
			.val('')
			.removeClass('label');
        }
        else
        {
            field
			.removeClass('label');
        }
    })
	.blur(function()
	{
	    var field = $(this);

	    if (this.id)
	    {
	        var label = $.trim($('label[for="' + this.id + '"]:last').hide().text());
	    }
	    else
	    {
	        return;
	    }

	    if (field.val() == label || field.val() == '')
	    {
	        field
			.val(label)
			.addClass('label');
	    }
	})
	.blur()
	.parents('form').submit(function(e)
	{
	    var current = $(':focus');
	    var scrollY = $(document).scrollTop();

	    // trigger the focus event to remove label text from field
	    self.focus();
	    // remove all bound events
	    self.unbind();

	    current.get(0).focus();

	    $(document).scrollTop(scrollY);
	});

    return self;
}

/*
$(this).next().fadesliderToggle({ speed:300, easing:"swing" });
*/
$.fn.fadeSlideToggle = function(settings)
{
	var self = $(this);
	var settings = $.extend({ speed: 500, easing: "swing", callback: function() { } }, settings);

	if (self.css("display") == "none")
	{
		self.animate({
			opacity: 1,
			height: 'toggle'
		}, settings.speed, settings.easing, settings.callback);
	}
	else
	{
		self.animate({
			opacity: 0,
			height: 'toggle'
		}, settings.speed, settings.easing, settings.callback);
	}
};