$(document).ready(function () {
	
	// Lonely bindings ...
	
	$('#project_lower_content :header, #client_copy :header').each(function (i, e) {
		var p = $(this).prevAll('p:not(:has(img))');
		$(p).each(function (x, y) {
			if (!$(y).hasClass('clear') && !$(y).is(':empty')) {
				$(y).css({
					'margin-bottom' : '2.5em'
				});
				return false;
			}
		});
	});
		
	// Global
	$(document).bind('keydown', function (e) {
		if (e.keyCode == '40') { 
			$(window)._scrollable().stop();
			$.scrollTo('+=300', 500, 'easeOutExpo');
			return false;
		}
		if (e.keyCode == '38') { 
			$(window)._scrollable().stop();
			$.scrollTo('-=300', 500, 'easeOutExpo');
			return false;
		}
	});
	
	$(document).hash_override();
	
	// Home
	$('#home_marquee').homey();
	
	// Exposure
	$('#exposure_content').exposure();
		
	// About
	$('#insidemc').about();
	
	// Collaborate
	$('#collaborate').collaborate();
	
	
	// Work
	$('#work').work();
		
	// Case
	$('#project_content').case_study();
	
	// IE6 ... fun!!
	if ($.browser.msie && $.browser.version == '6.0') {		
		$('.home_featured_module_image:first').css('left', '940px');
		$('#map_wrap, #togglemap').remove();
	}
	
});


(function($){
	
	// Homepage features ========================================================
	
	$.fn.featured = function() {

		function change(e) {			
			
			var _next = index[e.data.idx];
			var _curr = curr_index;
			
			if (_next.idx == _curr.idx) {
				return false;
			}
						
			unbind_arrows();
			unbind_keys();
			$(loading).show();
		
			$(_curr.anchor).removeClass('active');
			$(_next.anchor).addClass('active');
	
			var active_pos = $(_next.anchor).position().left+83;
		
			var pre_locate = (_next.idx > _curr.idx) ? 1880 : 0;
			var post_locate = (_next.idx > _curr.idx) ? 0 : 1880;
			var pre_next_locate = (_next.idx > _curr.idx) ? 1880 : 0;
			var post_next_locate = (_next.idx > _curr.idx) ? 0 : 1880;
			
			// Handle proximal arrows
			if (e.data.click == "arrow" && _next.idx === 0 && _curr.idx == (index.length-1)) {
				pre_locate = 1880;
				post_locate = 0;
				pre_next_locate = 1880;
				post_next_locate = 0;
				$('#active_caret').css('left', '-50px').animate(
					{
						left : active_pos
					}, speed/2, 'easeOutExpo'
				);
			} else if (e.data.click == "arrow" && _next.idx == (index.length-1) && _curr.idx === 0) {
				pre_locate = 0;
				post_locate = 1880;
				pre_next_locate = 0;
				post_next_locate = 1880;
				$('#active_caret').css('left', '1880px').animate(
					{
						left : active_pos
					}, speed/2, 'easeOutExpo'
				);
			} else {
				$('#active_caret').animate(
					{
						left: active_pos
					}, speed, 'easeOutExpo'
				);
			}
					
			$(_next.image).css({ 'left' : pre_locate + 'px' });
			$(_next.desc).css({ 'left' : pre_next_locate + 'px' });
					
			setTimeout(function() {
			
				// Animate images

					$(_curr.image).animate(
						{
							left : post_locate
						}, speed, 'easeOutExpo'
					);

					$(_next.image).animate(
						{
							left : 940
						}, speed, 'easeOutExpo'
					);	
					
				// Animate dscriptions	
					
					$(_curr.desc).animate(
						{
							left : post_locate
						}, speed+25, 'easeOutExpo'
					);

					$(_next.desc).animate(
						{
							left : 940
						}, speed+25, 'easeOutExpo',
						function () {
							curr_index = index[e.data.idx];
							$(loading).hide();
							bind_arrows();
							bind_keys();
						}
					);
				
			}, speed/6);
												
			return false;
			
		}
		
		function bind_keys() {
			$(document).bind('keyup', handle_key);
		}
		
		function unbind_keys() {
			$(document).unbind('keyup', handle_key);
		}
		
		function handle_key(e) {
			if (e.keyCode == 37) { 
				 prev();
				 kill_timer();
				 return false;
			}
			if (e.keyCode == 39) { 
				 next();
				kill_timer(); 
				return false;
			}
		}
		
		function bind_arrows() {
			$('#featured_left').bind('click', prev);
			$('#featured_right').bind('click', next);
			$('#featured_left, #featured_right').bind('click', kill_timer);
		}
		
		function unbind_arrows() {
			$('#featured_left').unbind('click', prev);
			$('#featured_right').unbind('click', next);
		}
	
		function next() {
			var e = {
				data : {
					idx : false,
					click : 'arrow'
				}
			};			
			e.data.idx = (curr_index.idx == (index.length-1)) ? 0 : curr_index.idx+1; 
			change(e);
			return false;
		}
	
		function prev() {
			var e = {
				data : {
					idx : false,
					click : 'arrow'
				}
			};
			e.data.idx = (curr_index.idx === 0) ? index.length-1 : curr_index.idx-1;
			change(e);
			return false;
		}
		
		function set_timer() {
			interval = setInterval(next, 5000);
		}
		
		function kill_timer() {
			clearInterval(interval);
		}
		
		function init() {
			$('a', anchors).each(function (i, e) {
				index[i] = {
					idx : i,
					image : $('#image_'+i),
					desc : $('#desc_'+i),
					anchor : $(e)
				};
				$(e).data('index', index[i]);
				$(e).bind('click', kill_timer);
				$(e).bind('click', {idx : i, click : 'anchor'}, change);
				if (i === 0) {
					curr_index = index[i];
				}
			});
		
			bind_keys();
			bind_arrows();
			set_timer();
						
		}
		
		if ($(this).size() > 0) {

			var anchors = $('#home_featured_module_anchors');
			var slider = $('#home_featured_module_images_slider');
			var loading = $('#loading_block');
			var speed = 800;
			var easing = 'easeOutExpo';
			var index = [];
			var curr_index;
			var interval;
			init();
		}
	
	};
	
	// faScroll
	
	$.fn.freeScroll = function (options) {
		
		function scroll(_index, movement) {
						
			if (typeof index[_index] == "undefined") {
				return false;
			}
			
			unbind_keys();
			unbind_dir();
			
			if (movement) {
				
				var old_base = $(base); // move the slider into another variable
				var new_left;
				var old_left;
				
				if (movement == "frontload" && _index == total_anchors-1) { // Load the last group to the front
					base = $(base).clone().css('left', -items_width).prependTo($(settings.holder)); // clone the slider content and prepend it to the holder
					old_left = holder_width;
					new_left = -items_width + holder_width;
				}
				
				if (movement == "backload" && _index === 0) {
					base = $(base).clone().css('left', holder_width).appendTo($(settings.holder)); // clone the slider content and prepend it to the holder
					old_left = -items_width;
					new_left = 0;
				}
				
				$(old_base).stop().animate( // animate the slider away from the view
					{
						left : old_left // just enough so we can't see it
					}, 500, 'easeOutExpo',
					function () {
						$(this).remove(); // now kill it
						rebuild(); // remap the index to the new base variable
					}
				);
				
				$(base).stop().animate( // animate the slider
					{
						left : new_left
					}, 500, 'easeOutExpo',
					function () {
						bind_keys();
						bind_dir();
					}
				);				
			
			} else {
			
				var pos = $(index[_index].target).position(); // get the item's positio

				$(base).stop().animate( // animate the slider
					{
						left : -pos.left
					}, 500, 'easeOutExpo',
					function () {
						bind_keys();
						bind_dir();
					}
				);
			
			}
			
			curr = _index;
			
			if (index[_index].anchor) {
				$('a', settings.pager).removeClass('active');
				$(index[_index].anchor).addClass('active');
			}
			
		}
		
		function click_anchor() {
			$('a', settings.pager).removeClass('active'); // remove the current active class
			$(this).addClass('active'); // add the active class to this acnchor
			var _index = $(this).data('index'); // get the index
			scroll(_index, null);
		}
				
		function handle_key(e) {
			if (e.keyCode == 37) { 
				 prev();
				 return false;
			}
			if (e.keyCode == 39) { 
				 next();
				 return false;
			}
		}
		
		function bind_keys() {
			$(document).bind('keyup', handle_key);
		}
		
		function unbind_keys() {
			$(document).unbind('keyup', handle_key);
		}
		
		function bind_dir() {
			if ($(settings.next)) {
				$(settings.next).bind('click', next);
			}				
			if ($(settings.prev)) {
				$(settings.prev).bind('click', prev);
			}
		}
		
		function unbind_dir() {
			if ($(settings.next)) {
				$(settings.next).unbind('click', next);
			}				
			if ($(settings.prev)) {
				$(settings.prev).unbind('click', prev);
			}
		}
		
		function next() {
			var _index = ( curr == (total_anchors-1) ) ? 0 : curr+1;
			var movement = ( curr == (total_anchors-1) ) ? 'backload' : null;			
			scroll(_index, movement);
			return false;
		}
	
		function prev() {
			var _index = (curr === 0) ? total_anchors-1 : curr-1;
			var movement = (curr === 0) ? 'frontload' : null;
			scroll(_index, movement);
			return false;
		}
		
		function rebuild() {
			settings.children = $(base).children(); // reload the children
			// loop through the anchors and reset the targets (because the base slider was most likely cloned)
			for (var i=0; i < total_anchors; i++) {
				var target;
				target = (i*settings.scrollBy > total_items-settings.scrollBy) ? $(settings.children).eq(total_items-settings.scrollBy) : $(settings.children).eq(i*settings.scrollBy);				
				var _class, a;
				index[i].target = target;
			}			
		}
		
		function build() {
			
			for (var i=0; i < total_anchors; i++) {
				var target;
				target = (i*settings.scrollBy > total_items-settings.scrollBy) ? $(settings.children).eq(total_items-settings.scrollBy) : $(settings.children).eq(i*settings.scrollBy);				
				var _class, a;
				if (settings.pager) {
					var text = (settings.pagerLabel) ? i+1 : '';
					_class = i === 0 ? 'active' : null; // set the first item as active
					a = $('<a></a>')
						.data('index', i) // store the offset
						.attr('class', _class)
						.text(text)
						.bind('click', click_anchor)
						.appendTo($(settings.pager)); 
				}
				index[i] = {
					target : target,
					anchor : (typeof a !== "undefined") ? a : false
				};
			}
			
			bind_keys();
			bind_dir();
		
		}
		
		function init() {
			
			settings.width = $(settings.holder).innerWidth();
			$.extend(settings, options);
			
			total_items = $(settings.children).size();
			total_anchors = Math.ceil(total_items/settings.scrollBy);
			
			if (total_anchors < 2) {
				if (settings.prev) {
					$(settings.prev).hide();
				}
				if (settings.next) {
					$(settings.next).hide();
				}
				return false;
				
			}
			
			$(settings.children).each(function (i,e) {
				items_width += $(e).outerWidth(true);
			});
			
			items_width = items_width - parseInt($(settings.children).last().css('margin-right'), 10); // trim off the last bit of right margin
			items_width = items_width - parseInt($(settings.children).first().css('margin-left'), 10); // trim off the first bit of left margin
			
			build();
			
		}
		
		var base = $(this),
			settings = {
				holder : $(base).parent(),
				children : $(base).children(),
				scrollBy : 4,
				pager : false,
				next : false,
				prev : false,
				byWidth : false,
				pagerLabel : false
			},
			total_items,
			items_width = 0,
			index = [],
			curr = 0,
			holder_width = $(settings.holder).innerWidth();
		
		if ($(this).size() > 0) {
			init();
		}

	};
	
	$.fn.lazyRedux = function () {
		
		function init() {
			var wrapper = $('#project_screenshots');
			var adjust_height = $(wrapper).offset().top;
			var first = true;
			for (var i in lazy) {
				if (typeof lazy[i] !== "undefined") {
					
					var alpha = (lazy[i].alpha) ? ' alpha ' : '';
					var div = $('<div class="projects_screenshot"></div>')
						.attr({
							'class' : 'projects_screenshot' + alpha
						})
						.height(lazy[i].height).appendTo($(wrapper));
						lazy[i].item_off = adjust_height;
						lazy[i].spot = div;
						lazy[i].loaded = false;
					if (lazy[i].item_off < ($(window).height() + $(window).scrollTop() + threshold) || first) {
						showit(i);					
					} 
					first = false;
					adjust_height += lazy[i].height + 40;
				}
			}
			$(window).bind("scroll", function(event) {
				for (var i in lazy) {
					if (!lazy[i].loaded && is_below(i)) {
						showit(i);
					}
				}
			});
		}
		
		function showit(i) {
			if (!lazy[i].loaded) {
				lazy[i].loaded = true;
				var img = new Image(); // privately load the image
				img.src = lazy[i].file;
				img.onload = function () {
					$(this).appendTo($(lazy[i].spot)).fadeIn(500);
				};
			}
		}
		
		function is_below(i) {
			var fold = $(window).scrollTop() + $(window).height() + threshold;
			var offset = lazy[i].item_off;
			return (offset < fold) ? true : false;
		}
		
		if (typeof lazy !== "undefined") {
			var threshold = 200;
			init();
		}
		
	};
	
	// Scroll Baby Scroll ========================================================
	
	$.fn.scroll_baby_scroll = function() {
			
		function scroll(event) {
			var scrolls = $(window).scrollTop();
			if (scrolls > scroll_bound) {
				destroy_scroll();
				return false;
			}
		}

		function destroy_scroll() {
			$(window).unbind("scroll", scroll);
			$(icon).fadeOut(function () {
				$(this).remove();
			});
		}
		
		if ($(this).size() > 0 && lazy) {
			var icon = $(this);
			var container = $(this).parent();
			var second_item = $('.projects_screenshot').eq(1);
			var icon_bound = lazy[0].height + lazy[1].height/2;
			var scroll_bound = $(container).offset().top + icon_bound;
			$(icon).css({top : icon_bound});
			$(window).bind("scroll", scroll);				
		}
		
	};
	
	// Google Location ===========================================================
	
	$.fn.google_location = function() {
				
		function init() {
			
			var map, marker, icon,
			fa = new google.maps.LatLng(33.874662, 35.517398),
			options = {
				zoom: 14,
				center : new google.maps.LatLng(33.876698, 35.510718),
				mapTypeId: google.maps.MapTypeId.ROADMAP,
			  draggable: true,
				navigationControl: true,
				navigationControlOptions : {
					style: google.maps.NavigationControlStyle.SMALL
				},
				streetViewControl : false,
				scrollwheel: false
			},
			_icon = $(base).attr('data-icon');

			if (_icon) {
				icon = new google.maps.MarkerImage(
					_icon,
					new google.maps.Size(92, 78),
					new google.maps.Point(0,0),
					new google.maps.Point(33,77)
				);
			} else {
				icon = false;
			}
			
			$('#togglemap').toggle(
				function () {
					$(this).removeClass('maptoggled').text('Show Map');
					$('.togglemap_wrap').removeClass('togglemap_wrap_toggled');
					$('#map_wrap').animate(
						{
							height : 0
						}, 500, 'easeOutExpo'
					);
					if ( $.browser.msie && $.browser.version < 8.0 ) {
						$('#map').animate(
							{
								height : 0
							}, 500, 'easeOutExpo'
						);
					}
					return false;
				},
				function () {
					$(this).addClass('maptoggled').text('Hide Map');
					$('.togglemap_wrap').addClass('togglemap_wrap_toggled');
					$('#map_wrap').animate(
						{
							height : 380
						}, 500, 'easeOutExpo'
					);
					if ( $.browser.msie && $.browser.version < 8.0 ) {
						$('#map').animate(
							{
								height : 380
							}, 500, 'easeOutExpo'
						);
					}
					return false;
				}
			);
			map = new google.maps.Map(document.getElementById('map'), options);
			marker = new google.maps.Marker({
				map: map, 
				position: fa,
				icon: icon,
				title : "MediaConseil"
			});
			
		}
		
		if ($(this).size() > 0) {
			var base = $(this);
			init();
		}
		
	};
	
	// LAST.FM ===================================================================
	
	$.fn.last_fm = function() {
		
		function run() {
			check();
			setInterval(check, 60000);
		}
		
		function check() {
			$.ajax({
				url : "http://ws.audioscrobbler.com/2.0/",
				dataType : 'jsonp',
				jsonpCallbackString : '?',
				data : {
					method : 'user.getrecenttracks',
					user : 'MediaConseil',
					api_key : 'a7e0676c20f8cfc430d3c41df12cc788',
					format : 'json',
					limit : '10'
				}, 
				success : function (obj) {			
					if (obj) {
						var i;
						var count = 0;
						
						for (i in obj.recenttracks.track) {
							var scrobbled_at = new Date(obj.recenttracks.track[i].date['#text']);
							if (scrobbled_at > todays_date) {
								count++;
							}
						}
						
						if (count) {
							count = (count < 99) ? count : 99;
							$('#mc_by_the_numbers_tracks_today').html(count);
						}
						
						var tune = $('<a></a>')
							.attr({
								title : obj.recenttracks.track[0].name + ' by ' + obj.recenttracks.track[0].artist['#text'],
								href : obj.recenttracks.track[0].url,
								'class' : 'blue'
							})
							.html(obj.recenttracks.track[0].name);
						var artist = $('<span></span>')
							.attr({
								'class' : 'artist'
							})
							.html(obj.recenttracks.track[0].artist['#text']);
						$('span', last_fm)
							.html('')
							.append('Currently listening to ')
							.append(tune)
							.append(' by ')
							.append(artist);
					}
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					$('#last_fm').hide();
				}
			});
		}
		
		if ($(this).size() > 0) {
			var last_fm = $(this);
			var todays_date = new Date();
			var todays_date = new Date(todays_date.getFullYear(), todays_date.getMonth(), todays_date.getDate());
			
			run();
		}
				
	};
	
	// Home Tweets ===================================================================
	
	$.fn.homeTweets = function() {
		
		function wipe(obj) {
			$(base).html(obj);
			looping = false;
		}
		
		function fade_in() {
			$('.tweet').fadeIn(300, function () {
				since = $('.tweet', base).attr('id');
			});
		}
		
		function base_append(obj) {
			$(base).append(obj);
			fade_in();			
		}
		
		function prepend(obj) {
			if (looping) {
				$('.tweet:last', base).fadeOut(300, function () {
					$(this).remove();
					$(base).prepend(obj);
					fade_in();
				});
			} 
		}
		
		function init() {
			fetch(3,since);
			loop();
		}
		
		function tweet(obj) {

			var id = obj.id_str;
			var user = obj.user.name;
			var handle = obj.user.screen_name;
			var time = obj.created_at;
			var text = obj.text;
			var img = obj.user.profile_image_url;

			var wrap = $('<div></div>')
				.attr({
					'class' : 'tweet',
					'id' : id
				});
				
			var left = $('<div></div>')
				.attr('class', 'tweet_left');
			var right = $('<div></div>')
				.attr('class', 'tweet_right');
			var clear = $('<div></div>')
				.attr('class', 'clear');
				
			var avatar = new Image();
				avatar.src = img;
			var _avatar = $('<a></a>')
				.attr('href', 'http://www.twitter.com/' + handle)
				.html(avatar);
				
			var via = $('<p></p>');
			var _via = $('<a></a>')
				.attr('href', 'http://www.twitter.com/' + handle)
				.html('@'+handle)
				.appendTo($(via));
				
			var _tweet = $('<p></p>')
				.html(linkify(text));
				
			// Shuffle things around
			
			$(left).append($(_avatar));
			$(right).append($(_tweet)).append($(via));
			
			$(wrap).append($(left)).append($(right)).append($(clear));
			
			return wrap;
			
		}
		
		function fetch(limit, since) {
			var r;
			var data = {};
			//data.per_page = limit;
			if (since) {
				data.since_id = since;
			}
			$.ajax({
				url : 'http://api.twitter.com/1/media_conseil/lists/' + twitter_list + '/statuses.json',
				type : 'GET',
				dataType : 'jsonp',
				data : data,
				success: function(obj) {					
					if (obj.error) {
						var img = new Image();
							img.src = theme_url + '/img/ico_fail_bird.png';
							wipe(img);
						return false;
					} else {
						var count = 1;
						for (var i in obj) {
							if (!obj[i].in_reply_to_status_id_str) {
								if (looping) {
									prepend(tweet(obj[i]));
								} else {
									base_append(tweet(obj[i]));		
								}
								if (count >= limit) {									
									return false;
								} else {
									count++;
								}								
							}
						}
						looping = true;
					}
				},
				error : function (obj) {
					var img = new Image();
						img.src = theme_url + '/img/ico_fail_bird.png';
						wipe(img);
					return false;
				}
			});
		}
		
		function loop() {
			setInterval(function () {
				if (looping) {
					fetch(1, since);
				}
			}, 60000);
		}
		
		if ($(this).size() > 0) {
			var base = $(this);
			var since = false;
			var looping = false;
			init();
		}
				
	};
	
	// Free Tweets ===================================================================
	
	$.fn.freeTweets = function() {
		
		function wipe(obj) {
			$(base).fadeOut();
			looping = false;
		}
		
		function swap(obj) {
			$(base).fadeOut(300, function () {
				$(base).html(obj);
				$(base).fadeIn(300, function () {
					since = $('.free_life_tweet:first', base).attr('id');
				});
			});
		}
		
		function init() {
			fetch(1,since);
			looping = true;
			loop();
		}
		
		function tweet(obj) {
			
			var id = obj.id_str;
			var user = obj.user.name;
			var handle = obj.user.screen_name;
			var time = obj.created_at;
			var text = obj.text;
			var img = obj.user.profile_image_url;

			var wrap = $('<div></div>')
				.attr({
					'class' : 'free_life_tweet',
					'id' : id
				});
				
			var _text = $('<div></div>')
				.attr('class', 'free_life_tweet_text')
				.html(linkify(text));
			var _via = $('<a></a>')
				.attr({ 
					'href' : 'http://www.twitter.com/' + handle,
					'class' : 'blue' 
					})
				.html('@' + handle);
			var _author = $('<div></div>')
				.attr('class', 'free_life_tweet_author')
				.append('via ')
				.append($(_via));
			var timespamp = $('<span></span>')
				.attr('class', 'timestamp')
				.html('')
				.appendTo($(_author));
								
			// Shuffle things around
			
			$(wrap).append($(_text)).append($(_author)).append($(timespamp));
			
			return wrap;
			
		}
		
		function fetch(limit, since) {
			var data = {};
			if (since) {
				data.since_id = since;
			}
			$.ajax({
				url : 'http://api.twitter.com/1/media_conseil/lists/' + twitter_list + '/statuses.json',
				type : 'GET',
				dataType : 'jsonp',
				data : data,
				success: function(obj) {					
					
					if (!obj.error) {
						var count = 1;
						var today = 0;
						
						for (var i in obj) {
							
							// Calculate recent tweets
							var tweet_created_at = new Date(obj[i].created_at);
							
							if (tweet_created_at > todays_date) {
								today++;
							}
							
							if (!obj[i].in_reply_to_status_id_str && count <= limit) {
								swap(tweet(obj[0]));
							}
							
							count++;
							
						}
						
						if (today) {
							$('#mc_by_the_numbers_tweets_today').html(today);
						}
						
					} else {
						wipe();
					}
				},
				error : function (obj) {
					wipe();
				}
			});
			
		}
		
		function loop() {
			setInterval(function () {
				if (looping) {
					fetch(1, since);
				}
			}, 60000);
		}
		
		if ($(this).size() > 0) {
			var base = $(this);
			var since = false;
			var looping = false;
			var todays_date = new Date();			
			var todays_date = new Date(todays_date.getFullYear(), todays_date.getMonth(), todays_date.getDate());
			init();
		}
				
	};
	
	// Case Aggregate Panels =====================================================
	
	$.fn.case_panels = function() {
		
		if ($(this).size() > 0) {
		
			$('.panel_toggle a').toggle(
				function () {
					var panel = $(this).parent().parent().find('.lower_panel');
					$(this).addClass('arrow_hide').html('<span>View Less</span>');
					$(panel).animate(
						{
							height : 240
						}, 400, 'easeOutExpo', function () {}
					);
					return false;
				},
				function () {
					var panel = $(this).parent().parent().find('.lower_panel');
					$(this).removeClass('arrow_hide').html('<span>View More</span>');
					$(panel).animate(
						{
							height : 0
						}, 400, 'easeOutExpo', function () {}
					);
					return false;
				}
			);
			
		}
				
	};
		
	// Home ===========================================================
	
	$.fn.homey = function() {
		
		function init() {
			$('#home_featured_module').featured();
			$('#home_tweets').homeTweets();
		}
		
		if ($(this).size() > 0) {
			init();
		}
		
	};
	
	// Work ============================================================
	
	$.fn.work = function() {
		
		function init() {
			$('#client_module_slider').freeScroll({
				holder : $('#client_module'),
				scrollBy : 3,
				next : $('#featured_right'),
				prev : $('#featured_left'),
				pager : $('#client_module_paginator')
			});
			//$('#category_panels').case_panels();
		}
		
		if ($(this).size() > 0) {
			init();
		}
		
	};
	
	// Case/Client ============================================================
	
	$.fn.case_study = function() {
		
		function scroll_to_case(e) {
			$.scrollTo(690, 550, {
				'easing' : 'easeOutExpo'
			});
		}
		
		function init() {
			$().lazyRedux();
			$('#scroll_baby_scroll').scroll_baby_scroll();
			$('#project_scroller').bind('click', scroll_to_case);
		}
		
		if ($(this).size() > 0) {
			init();
		}
		
	};
	
	// About ============================================================
	
	$.fn.about = function() {
		
		function init() {
			$('#free_life_tweets').freeTweets();
			$('#last_fm').last_fm();
			$('#mc_by_the_numbers_wrap ul').freeScroll({
				holder : $('#mc_by_the_numbers_wrap'),
				scrollBy : 1,
				next : $('#num_right'),
				prev : $('#num_left')
			});
			$('#mc_flickr_feed a').fancybox({
				centerOnScroll : true,
				overlayOpacity : 0.8,
				overlayColor : '#1A1107',
				titleShow : false,
				transitionIn : 'elastic',
				transitionOut : 'elastic'
			});
		}
		
		if ($(this).size() > 0) {
			init();
		}
		
	};
	
	// Collaborate =====================================================
	
	$.fn.collaborate = function() {
		
		function init() {
			
			$('#map').google_location();
			
			$('#collaborate_take_a_peek').mouseenter(function () {
				$('#collaborate_take_a_peek_box').stop().animate(
					{
						top : 0,
						opacity : 1
					}, 450, 'easeOutExpo', function () {}
				);
			}).mouseleave(function () {
				$('#collaborate_take_a_peek_box').stop().animate(
					{
						top : '-100%',
						opacity : 0
					}, 350, 'easeInExpo', function () {}
				);
			});

			$('.collaborate_job h4').bind('click', function () {
				var desc = $(this).parent().find('.collaborate_job_description');
				if ($(this).hasClass('toggled')) {
					$(this).removeClass('toggled');
					$(desc).slideUp(400, 'linear');
				} else {
					var loc = this;
					$(desc).slideDown(400, 'linear', function () {
						$.scrollTo($(loc).offset().top - 60, 400);
					});
					$(this).addClass('toggled');
					$('.collaborate_job h4').not(this).removeClass('toggled');
					$('.collaborate_job_description').not(desc).slideUp(400, 'linear');
				}
			});
			
		}
		
		if ($(this).size() > 0) {
			init();
		}
				
	};
	
	// Exposure =====================================================
	
	$.fn.exposure = function() {
		
		function init() {

			// Remove borders on links with images and prep lightbox
			$('.blog_content a, a[rel=lightbox]').has('img').css({
				border : 'none !important'
			}).fancybox({
				centerOnScroll : true,
				overlayOpacity : 0.8,
				overlayColor : '#1A1107',
				titleShow : false,
				transitionIn : 'elastic',
				transitionOut : 'elastic',
				showNavArrows : true
			});
			
			if ($('#fb_like').size() > 0) {
				$.ajax({
					url : "http://connect.facebook.net/en_US/all.js",
					dataType: 'script',
					success: function() {
						$('#fb_like').html('<fb:like layout="button_count"></fb:like>');
						FB.XFBML.parse(document.getElementById('fb_like'),
							function () {

							}
						);
					}
				});
			}
			
			$('.commentmetadata a').attr('href', '').click(function () { return false; });
			var commentform = document.getElementById("commentform");
			if (commentform) {
				H5F.setup(commentform, {
					validClass: "valid",
					invalidClass: "invalid",
					requiredClass: "required"
				});
				$(commentform).submit(function () {
					var valid = H5F.checkValidity(commentform);
					return valid;
				});
			}
			
		}
		
		if ($(this).size() > 0) {
			init();
		}
				
	};
	
	// Hash Override =====================================================
	
	$.fn.hash_override = function() {
		
		function init() {
			if (document.location.hash) {
				var hash = document.location.hash;
				document.location.hash = hash +  '_';
				$.scrollTo($(''+hash+''), 400);
			}
		}
		
		if ($(this).size() > 0) {
			init();
		}
				
	};
	
})(jQuery);
