$(document).ready(function() {
	initFonts();
	initLayout();
	initHeader();
	initWork();
	initRows();
	initScrollTo();
	initContact();
	initInfinity();
	initProject();
	$(window).bind('resize', function() {
		initLayout();
	});	
	initTracking();
});

function initFonts()
{
	Cufon.replace('h2, h3 span, a.prev span, a.next span, #quotes li, #footer a', {
		hover: true
	});
}

function initLayout()
{
	var windowHeight = $(window).height();
	var navHeight = $('#nav').height();
	var navPosition = $('#wrapper').position();	
	var navPositionTop = (windowHeight / 2) - (navHeight + 70 / 2);	
	$('#nav').css({
		'position' : 'fixed',
		'top' : navPositionTop
	});
	// position nav then make visible for grandma
	if ($.browser.msie) {
		$('#nav').css({
			'left' : navPosition.left,
			'visibility' : 'visible'
		});
	};
	$('#content').css('min-height', windowHeight);	
}

function initHeader()
{
	animatedcollapse.addDiv('header', 'fade=1');
	animatedcollapse.init();
	animatedcollapse.ontoggle=function($, divobj, state){
		if (divobj.id == 'header'){
			if (state == 'block'){
				$('#wedidthat').text('Close');
			} else {
				$('#wedidthat').text('We Did That');
			}
		}
	}
	$('#wedidthat').css('visibility', 'visible');
}

function initProject()
{
	$('#jcarousel-gallery').jcarousel({
		scroll: 0,
	    initCallback:initCarousel,
	    buttonNextHTML:null,
	    buttonPrevHTML:null
	});	
	fancyLoad.load($('#jcarousel-gallery ul li'));
}

function initCarousel(carousel)
{
	$('.jcarousel-dots').show();
	$('.jcarousel-dots a:first').addClass('active');
    $('.jcarousel-dots a').bind('click', function() {
		$('.jcarousel-dots a').removeClass('active');
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).index() + 1));
		$(this).addClass('active');
        return false;
    });
};

function initRows()
{
	$('#items li:first').addClass('first');
	$('#items li:last').addClass('last');
}

var fancyLoad =
{
	load: function(element) {
		$loading = element.find('.mask')
		$img = element.find('img')
		$loading.css('height',$img.attr('height'))
		$loading.css('width',$img.attr('width'))
		$loading.show();
		
		element.each(function(index,elem) {
			$(elem).data('src',$(elem).find('img').attr('src'))
		});
		$img.attr('src','#')
		
		element.each(function(index,elem) {
			$e = $(elem)
			$i = $e.find('img')
			$i.bind('load readystatechange',function(e) {
				if (this.complete || (this.readyState == 'complete' && e.type =='readystatechange')) {
					$(e.target).siblings('.mask').fadeOut(400)
				}
			})
			$i.attr('src',$e.data('src'))
			
		}) 		
	}
}

function initWork()
{
	fancyLoad.load($('ul#project li'));
	fancyLoad.load($('ul#items li'));
	$('#summary ul li, #share ul li').prepend('- ');
	$('#share ul li:eq(1)').css('margin-bottom', '10px');
}

function initContact()
{
	$('#enquiry').validate();
}

function initScrollTo()
{
	$('#scrollUp').click(function(event) {
		$.scrollTo('#wedidthat', 450);
		event.PreventDefault;
		return false;
	});
}

function initInfinity()
{
	$('body#news #items').infinitescroll({
		navSelector  : "li#pagination",
		nextSelector : "a#more:last",
		itemSelector : "#items li",
		loadingText  : "Loading more posts.",
		donetext	 : "End of news archive.",
		loadingImg   : "loader.gif",
		debug        : true
	}, function(){
		window.callbackcontext = this;
		window.console && console.log('callbackcontext',this,$(this).find('p'));
		Cufon.replace('#items li h3 span');
		$('body#news #items li').removeClass('last');
		$('body#news #items li:last').addClass('last');
	});
}

function initTracking()
{
	$.ga.load("UA-676104-17");
}

// eof
