jQuery(document).ready(function($) {

	var ie = $.browser.msie;
	var version = parseInt($.browser.version);
	
	if ( ie && version <= "8" ) { 
		$(window).load(function() {
			$('#fullscreenimage img').fadeIn(200);
		});
	}
	
	// Form validation
	$('form').validator();

	// Equals
	$(window).load(function() {
		$('.column', 'section').equals();
	});
	$('.clients .player img').load(function() {
		$('.player', '.clients').equals('both', true);	
	});
	$('.player', '.clients').equals('both', true);
	
	// Fullscreen plugin
   	$('#fullscreenimage').fullscreenimage({
		maxUpscale: 1.2,
		showRaster: false,
		vAlign: 'center'
	});
	
	$('section.gallery').cycle({
		'pause' : 1,
		'timeout': 4000,
		'speed' : 800
	});
	$('section.features').cycle({
		'pause' : 1,
		'timeout': 6000,
		'speed' : 1000
	});

	// FB Share popup
	$('.fb-share').click(function(e) {
		e.preventDefault();
		window.open(this.href,'_blank','height=520,width=570');
	});

	// Expanding Archive Nav
	$('li','.archive').click(function(e) {
		e.stopPropagation()
		$(this).find('> ul').slideToggle('slow');
	});
	
	// twitter feed
	$('#tweets').liveTwitter('pn_nz', {
		showAuthor: true,
		mode: 'user_timeline',
		rate : 60000,
		limit: 20
	}, tweetActions);
	// SEARCH TERM "porternovelli"
	// $('#tweets').liveTwitter('porternovelli', {
	// 	rate : 60000,
	// 	limit: 20,
	// 	lang : 'en'
	// }, tweetActions);

	function tweetActions() {
		
		// equal heights on the tweets
		var tweetHeight = 0,
			tweetWidth 	= 0,
			visible		= 3;
			
		$('#tweets li').each(function(i) {
			
			// height
			if ( tweetHeight < $(this).height() ) { 
				tweetHeight = $(this).height();
			}
			// width
			if ( tweetWidth < $(this).width() ) { 
				tweetWidth = $(this).width();
			}

		}).height(tweetHeight);
		
		// setup the width and container as well as the clone rotator
		var tweetCount = $('#tweets li').length,
			containerWidth = ( tweetWidth * tweetCount );
		
		$('#tweets').css({
			'height': tweetHeight,
			'width' : containerWidth,
			'position':'absolute'
		})
		.wrap('<div id="tweetlist" />')
		.parent().css({
			'overflow':'hidden',
			'position':'relative'
		});
		
		$('#tweetlist').jCarouselLite({
			auto: 4000,
			speed: 400,
			beforeStart: classy
		});
		
		/* 	add a class to remove the dotted separator line.
			since jCarousel adds 3 elements to the front of the queue the :eq is 5 (6th item) 
			to apply it to the 3rd visible one
		*/
		$('#tweets li:eq(5)').addClass('no-bg'); 
		function classy() {
			if ( $('#tweets li:last').hasClass('no-bg') ) { 
				 $('#tweets li').removeClass('no-bg').eq(6).addClass('no-bg');
			} else {
				$('#tweets').find('li.no-bg').removeClass('no-bg').next().addClass('no-bg');
			}
		};
		
		
	}
	
	// Hover colour animations.
	function colourOverlay() {
	var CO = {};
	
	$('.landing article').hover(function() {
		$(this).css('overlay', 'hidden');
		
		CO.tar = $(this).find('h2');
		CO.curW	= CO.tar.width();
		CO.curH	= CO.tar.height();
		
		CO.tar.animate({
			'height':'100%',
			'width':'100%'
		}, 400);
		
	}, function() {
		CO.tar.stop().animate({
			'height': CO.curH + 'px',
			'width':  CO.curW + 'px'
		}, 150);
	});
	}
	colourOverlay();
	
});





