$(document).ready(function() {
	//Children of .equalh block are going to be the same height
	$(".equalh").each(function(index) {
		var tallest = 0;
	        $(this).children('div').each(
	                function(index) {
	                        var height = $(this).height();
	                        if (height > tallest) { 
	                                tallest = height; 
	                        }
	                });
			$(this).children('div').height(tallest);
	 });
	
	//Clideshow on the home page
	$("#slides").cycle({
		fx: 'wipe',
		speed:  500, 
		timeout: 0,
		pager:  '#pager',
		clip:   'l2r',
		after:   onAfter
		
	});
	
	//Description for each slide
	function onAfter() { 
	    $('#home-slider .clearfix .work-title').html($(this).find('.description').text());
	}
	
	//Little animations to hide font replacement glitch
	$("#main-nav").fadeIn(300);
	$("h1").fadeIn(300);
	$("#tabs ul li").fadeIn(300);
	
	//Forcing every 3rd item to have .last class
	$("#case-studies a:nth-child(3n)").addClass("last");
	
	//Filtering portfolio
	$("#portfolio-filter li a").click(function(e){
		e.preventDefault();
		var filter = $(this).text();
		filter = filter.toLowerCase();
		filter = filter.replace(/(\s)/g, "-");
		filterPortfolio(filter);
		$("#portfolio-filter li a").removeClass("strong");
		$(this).addClass("strong");
	});
	
	function filterPortfolio (filter) {
		$(".portfolio-item").hide();
		$("."+filter).show();
	}
	
	//Accordionizing job positions
	$('.positions').accordion({autoHeight: false, collapsible: true, navigation: true, active: false});
	
	//Setting hash and scrolling to the top
	$('.positions h2').click(function(){
		window.location.hash = $(this).index() / 2 + 1;
		scrollToClass('positions');
	});
	
	//If there's hash, do something useful
	var hash = window.location.hash;
	
	//Scrolling to content block if accessing from footer menu
	if (hash && window.location.pathname == "/work/") {
		hash = hash.substr (1, hash.length);
		scrollToClass(hash);
	}
	
	//Opening the position from url
	if (hash && (window.location.pathname == "/contact/careers/" || window.location.pathname == "/contact/internship/")) {
		hash = hash.substr (1, hash.length);
		$('.positions').accordion("activate", hash-1);
	}
	
	function scrollToClass (className) {
		var scrollTo = $('.'+className).offset().top - 20;
		$('html, body').animate({scrollTop:scrollTo}); 
	}
	
	
	//Social block integration
	if ($('#social-feed').length) {
		$("#social-feed").soSoSocial();
	}
});





/*
	* Developed by J.P. Given
	* http://johnpatrickgiven.com
	
	******************************************************************************************************
	* NOTE: DON'T DELETE ANY VARIABLES FROM THIS PAGE.  IF YOU DON'T USE A SERVICE SIMPLY LEAVE IT BLANK.*
	******************************************************************************************************
	
	* This file is required for the plug in to work.  Do not erase any of the Javascript variables on this page.
	* If you don't use one of the services below simply leave the value blank ('').
	
	* NOTE: all RSS feed links need to be fully qualified URLs.  ie http://...
*/

// Twitter RSS.  My Twitter RSS feed is in here as an example.
var TWITTER_RSS = 'http://twitter.com/statuses/user_timeline/75092926.rss';
//var TWITTER_RSS = '';

//Custom RSS
var CUSTOM_RSS = '';

// LastFM RSS.
var LASTFM_RSS = '';

// Facebook RSS.
// How do you get it?  It's annoying but here's how.
// After logging in click the link to your profile.
// Scroll down the page and on the left hand side there is a section called "Links".
// Click "See All"
// On the right you'll see a link to the RSS FEED.  Click it.
// Grab that URL.
var FACEBOOK_RSS = '';

// Flickr RSS.
var FLICKR_RSS = '';

// Delicious RSS.
var DELICIOUS_RSS = '';

//Tumblr RSS.
var TUMBLR_RSS = '';

//Wordpress RSS.
var WORDPRESS_RSS = '';

// How many to display.  Recommend no more than 25 since RSS only goes back so far.  0 = show all.
var LIMIT = 3;








/******************************************************
	* jQuery plug-in
	* So So Social Activity Feed
	* Services accessed: Facebook, Delicious, Twitter, Last.FM, Flickr
	* Developed by J.P. Given (http://johnpatrickgiven.com)
	* Useage: anyone so long as credit is left alone
******************************************************/

var ACTIVITY_ARRAY = new Array();
var TWITTER_FINISHED = 0;
var FACEBOOK_FINISHED = 0;
var FLICKR_FINISHED = 0;
var LASTFM_FINISHED = 0;
var DELICIOUS_FINISHED = 0;
var TUMBLR_FINISHED = 0;
var WORDPRESS_FINISHED = 0;
var CUSTOM_FINISHED = 0;
var CONTAINER = null;
var COUNT = 0;

(function($) {
	$.fn.soSoSocial = function(callback) {
		
		CONTAINER = $(this);
		
		
		/************************************************************************************
			* Repeated code is not ideal
			* However in this case you want multiple jQuery get's
		************************************************************************************/
		
		
		// Twitter
		if (TWITTER_RSS != "") {
			$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%22"+encodeURIComponent(TWITTER_RSS)+"%22&format=json&callback=?", function(d) {
				//grab ever rss item from the json result request
				$(d.query.results.rss.channel.item).each(function() {
					//if set up to be infinite or the limit is not reached, keep grabbing items
					var title = this.title;
					var link = this.link;
					var description = this.description;
					var pubDate = this.pubDate;
					pubDate = pubDate.replace(/\,/g,'');
					
					var status = title.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
						return '<a href="'+url+'">'+url+'</a>';
					}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
						return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
					});
					
					//Remove twitter username from front of status.
					status = '@' + status;
					status = status.replace(/\B@([_a-z0-9]+):/ig, "");
					
					//append to the div
					ACTIVITY_ARRAY[COUNT] = new Array();
					ACTIVITY_ARRAY[COUNT][0] = '<li class="clearfix"><div class="social-thumb"><a href="' + link + '" target="_blank"><img src="http://suitcaseinteractive.com/public/images/big-icon-twitter.png" alt="" /></a></div><div class="social-status">' + status;
					ACTIVITY_ARRAY[COUNT][1] = relative_time(pubDate);
					ACTIVITY_ARRAY[COUNT][2] = get_delta(pubDate);
					COUNT++;
				});
				
				TWITTER_FINISHED = 1;
				
			});
			
		} else { //if not on twitter set it to done.
			TWITTER_FINISHED = 1;
		}
		
		//LastFM
		if (LASTFM_RSS != '') {
			$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%22"+encodeURIComponent(LASTFM_RSS)+"%22&format=json&callback=?", function(d) {
				//grab ever rss item from the json result request
				$(d.query.results.rss.channel.item).each(function() {
					//if set up to be infinite or the limit is not reached, keep grabbing items
					var title = this.title;
					var link = this.link;
					var description = this.description;
					var pubDate = this.pubDate;
					pubDate = pubDate.replace(/\,/g,'');
					
					//append to the div
					ACTIVITY_ARRAY[COUNT] = new Array();
					ACTIVITY_ARRAY[COUNT][0] = '<li style="background: url(http://farm5.static.flickr.com/4007/4495300744_5c8afb3149_o.png) no-repeat left center;">Listened to <a href="' + link + '" target="_blank">' + title + '</a>';
					ACTIVITY_ARRAY[COUNT][1] = relative_time(pubDate);
					ACTIVITY_ARRAY[COUNT][2] = get_delta(pubDate);
					COUNT++;
				});
				
				LASTFM_FINISHED = 1;/*
	jQuery OneFBLike v1.0 - http://onerutter.com/open-source/jquery-facebook-like-plugin.html
	Copyright (c) 2010 Jake Rutter
	This plugin available for use in all personal or commercial projects under both MIT and GPL licenses.
*/

(function($){  
    $.fn.extend({
        oneFBLike: function(options) {  
        
            //Set the default values, use comma to separate the settings 
            var defaults = {  
            	appID: '',
            	siteTitle: '',
            	siteName: '',
            	siteImage: '',
            	buttonWidth: 450,
            	buttonHeight: 80,
            	showfaces: true,
            	font: 'lucida grande',
            	layout: 'normal',
            	action: 'like',
            	colorscheme: 'light'
            	
            }  
            
            var options =  $.extend(defaults, options);  
                        
            return this.each(function() {  
                var o = options;  
                var obj = $(this);
                
                // Add Meta Tags for additional data - options
                jQuery('head').append('<meta property="og:title" content="'+o.siteTitle+'"/>');
                jQuery('head').append('<meta property="og:site_name" content="'+o.siteName+'"/>');
                jQuery('head').append('<meta property="og:image" content="'+o.siteImage+'"/>');
                
                // Add #fb-root div - mandatory - do not remove
                jQuery('body').append('<div id="fb-root"></div>');
                
                // setup FB Developers App Link - do not touch
                window.fbAsyncInit = function() {
				FB.init({appId: o.appID, status: true, cookie: true, xfbml: true});
				};
				(function() {
				var e = document.createElement('script'); e.async = true;
				e.src = document.location.protocol +
				'//connect.facebook.net/en_US/all.js';
				document.getElementById('fb-root').appendChild(e);
				}());


                // Grab the URL and assign it to a variable
                var dynUrl = document.location;
                var fbDIV = obj;
                                
                              
                // Apply the like button to an element on the page and include all available options
                // If no options are passed in from the page, the defaults will be applied            
                jQuery(fbDIV).html('<fb:like href="'+dynUrl+'" width="'+o.buttonWidth+'" height="'+o.buttonHeight+'" show_faces="'+o.showfaces+'" font="'+o.font+'" layout="'+o.layout+'" action="'+o.action+'" colorscheme="'+o.colorscheme+'"/>')

                              
            });  
        } 
    }); 
})(jQuery);
				
			});
		
		} else {
			LASTFM_FINISHED = 1;
		}
		
		if (FACEBOOK_RSS != '') {
			$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%22"+encodeURIComponent(FACEBOOK_RSS)+"%22&format=json&callback=?", function(d) {
				//grab ever rss item from the json result request
				
				$(d.query.results.rss.channel.item).each(function() {
					//if set up to be infinite or the limit is not reached, keep grabbing items
					var title = this.title;
					var link = this.link;
					//var description = this.description;
					var pubDate = this.pubDate;
					pubDate = pubDate.replace(/\,/g,'');
					
					//append to the div
					ACTIVITY_ARRAY[COUNT] = new Array();
					
					ACTIVITY_ARRAY[COUNT][0] = '<li class="clearfix"><div class="social-thumb"><a href="' + link + '" target="_blank"><img src="/staging/sites/all/themes/unitedway/images/big-icon-facebook.png" alt="" /></a></div><div class="social-status">' + 'Posted <a href="' + link + '" target="_blank">' + title + '</a>';
					
					ACTIVITY_ARRAY[COUNT][1] = relative_time(pubDate);
					ACTIVITY_ARRAY[COUNT][2] = get_delta(pubDate);
					COUNT++;
				});
				
				FACEBOOK_FINISHED = 1;
				
			});
		} else {
			FACEBOOK_FINISHED = 1;
		}
		
		//Flickr
		if (FLICKR_RSS != '') {
			$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%22"+encodeURIComponent(FLICKR_RSS)+"%22&format=json&callback=?", function(d) {
				//grab ever rss item from the json result request
				$(d.query.results.rss.channel.item).each(function() {
					//if set up to be infinite or the limit is not reached, keep grabbing items
					var title = this.title;
					var link = this.link;
					var description = this.description;
					var pubDate = this.pubDate;
					pubDate = pubDate.replace(/\,/g,'');
					title = title.toString();
					title = title.split(",");
					title = title[0];
					
					//append to the div
					ACTIVITY_ARRAY[COUNT] = new Array();
					ACTIVITY_ARRAY[COUNT][0] = '<li style="background: url(http://farm3.static.flickr.com/2727/4494661413_0228be5f32_o.png) no-repeat left center;">Uploaded <a href="' + link + '" target="_blank">' + title + '</a>';
					ACTIVITY_ARRAY[COUNT][1] = relative_time(pubDate);
					ACTIVITY_ARRAY[COUNT][2] = get_delta(pubDate);
					COUNT++;
				});
				
				FLICKR_FINISHED = 1;
				
			});
		} else {
			FLICKR_FINISHED = 1;
		}
		
		//Delicious
		if (DELICIOUS_RSS != '') {
			$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%22"+encodeURIComponent(DELICIOUS_RSS)+"%22&format=json&callback=?", function(d) {
				//grab ever rss item from the json result request
				$(d.query.results.rss.channel.item).each(function() {
					//if set up to be infinite or the limit is not reached, keep grabbing items
					var title = this.title;
					var link = this.link;
					var description = this.description;
					var pubDate = this.pubDate;
					pubDate = pubDate.replace(/\,/g,'');
					
					//append to the div
					ACTIVITY_ARRAY[COUNT] = new Array();
					ACTIVITY_ARRAY[COUNT][0] = '<li style="background: url(http://farm5.static.flickr.com/4064/4495300640_2a7cbbb922_o.png) no-repeat left center;">Saved <a href="' + link + '" target="_blank">' + title + '</a>.';
					ACTIVITY_ARRAY[COUNT][1] = relative_time(pubDate);
					ACTIVITY_ARRAY[COUNT][2] = get_delta(pubDate);
					COUNT++;
				});
				
				DELICIOUS_FINISHED = 1;
				
			});
		} else {
			DELICIOUS_FINISHED = 1;
		}
		
		print_array($(this));
		
		if (typeof callback == 'function') { // make sure the callback is a function
			callback.call(this); // brings the scope to the callback
		}
		
		//Tumblr
		if (TUMBLR_RSS != '') {
			$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%22"+encodeURIComponent(TUMBLR_RSS)+"%22&format=json&callback=?", function(d) {
				//grab ever rss item from the json result request
				$(d.query.results.rss.channel.item).each(function() {
					//if set up to be infinite or the limit is not reached, keep grabbing items
					var title = this.title;
					var link = this.link;
					var description = this.description;
					var pubDate = this.pubDate;
					pubDate = pubDate.replace(/\,/g,'');

					//append to the div
					ACTIVITY_ARRAY[COUNT] = new Array();
					ACTIVITY_ARRAY[COUNT][0] = '<li style="background: url(http://farm5.static.flickr.com/4022/4494661551_3d68321873_o.png) no-repeat left center;">Posted <a href="' + link + '" target="_blank">' + title + '</a>.';
					ACTIVITY_ARRAY[COUNT][1] = relative_time(pubDate);
					ACTIVITY_ARRAY[COUNT][2] = get_delta(pubDate);
					COUNT++;
				});

				TUMBLR_FINISHED = 1;

			});
		} else {
			TUMBLR_FINISHED = 1;
		}

			print_array($(this));

		if (typeof callback == 'function') { // make sure the callback is a function
			callback.call(this); // brings the scope to the callback
		}
		
		
		//Wordpress
		if (WORDPRESS_RSS != '') {
			$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%22"+encodeURIComponent(WORDPRESS_RSS)+"%22&format=json&callback=?", function(d) {
				//grab ever rss item from the json result request
				$(d.query.results.rss.channel.item).each(function() {
					//if set up to be infinite or the limit is not reached, keep grabbing items
					var title = this.title;
					var link = this.link;
					var description = this.description;
					var pubDate = this.pubDate;
					pubDate = pubDate.replace(/\,/g,'');

					//append to the div
					ACTIVITY_ARRAY[COUNT] = new Array();
					ACTIVITY_ARRAY[COUNT][0] = '<li style="background: url(http://farm5.static.flickr.com/4060/4495300842_3f39a6b514_o.png) no-repeat left center;">Posted <a href="' + link + '" target="_blank">' + title + '</a> on Tumblr.';
					ACTIVITY_ARRAY[COUNT][1] = relative_time(pubDate);
					ACTIVITY_ARRAY[COUNT][2] = get_delta(pubDate);
					COUNT++;
				});

				WORDPRESS_FINISHED = 1;

			});
		} else {
			WORDPRESS_FINISHED = 1;
		}

			print_array($(this));

		if (typeof callback == 'function') { // make sure the callback is a function
			callback.call(this); // brings the scope to the callback
		}
		
		//Custom RSS
		if (CUSTOM_RSS != '') {
			$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%22"+encodeURIComponent(CUSTOM_RSS)+"%22&format=json&callback=?", function(d) {
				//grab ever rss item from the json result request
				$(d.query.results.channel.item).each(function() {
					//if set up to be infinite or the limit is not reached, keep grabbing items
					var title = this.title;
					var link = this.link;
					//var description = this.description;
					var pubDate = this.pubDate;
					pubDate = pubDate.replace(/\,/g,'');

					//append to the div
					ACTIVITY_ARRAY[COUNT] = new Array();
					ACTIVITY_ARRAY[COUNT][0] = '<li style="background: url(http://farm5.static.flickr.com/4060/4495300842_3f39a6b514_o.png) no-repeat left center;">Posted <a href="' + link + '" target="_blank">' + title + '</a> on Tumblr.';
					ACTIVITY_ARRAY[COUNT][1] = relative_time(pubDate);
					ACTIVITY_ARRAY[COUNT][2] = get_delta(pubDate);
					COUNT++;
				});

				CUSTOM_FINISHED = 1;

			});
		} else {
			CUSTOM_FINISHED = 1;
		}

			print_array($(this));

		if (typeof callback == 'function') { // make sure the callback is a function
			callback.call(this); // brings the scope to the callback
		}
		
	};
})(jQuery);

// Print the array! 
function print_array(obj) {
	
	if (LIMIT == 0) {
		LIMIT = ACTIVITY_ARRAY.length;
	}
	
	if ((FLICKR_FINISHED == 1) && (TWITTER_FINISHED == 1) && (FACEBOOK_FINISHED == 1) && (LASTFM_FINISHED == 1) && (DELICIOUS_FINISHED == 1) && (TUMBLR_FINISHED == 1) && (WORDPRESS_FINISHED == 1) && (CUSTOM_FINISHED == 1)) {
		
		CONTAINER.html("");
		
		ACTIVITY_ARRAY.sort(by(2,1));
		var html = '<ul>';
		for (j = 0; j < LIMIT; j++) {
			html += ACTIVITY_ARRAY[j][0] + ' <span class="date">(' + ACTIVITY_ARRAY[j][1] + ')</span></div></li>';
		}
		html += '</ul>';
		CONTAINER.append(html);
	} else {
		setTimeout("print_array()", 1000);
	}
}

// pubDate delta function
function get_delta(time_value) {
	var values = time_value.split(" ");
	time_value = values[2] + " " + values[1] + ", " + values[3] + " " + values[4];
	var parsed_date = Date.parse(time_value);
	var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	delta = delta + (relative_to.getTimezoneOffset() * 60);
	
	return delta;
}

// Function to return the relative time based off of delta.
function relative_time(time_value) {
	
	var delta = get_delta(time_value);

	if (delta < 60) {
		return 'less than a minute ago';
	} else if(delta < 120) {
		return 'about a minute ago';
	} else if(delta < (60*60)) {
		return (parseInt(delta / 60)).toString() + ' minutes ago';
	} else if(delta < (120*60)) {
		return 'about an hour ago';
	} else if(delta < (24*60*60)) {
		return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
	} else if(delta < (48*60*60)) {
		return '1 day ago';
	} else {
		return (parseInt(delta / 86400)).toString() + ' days ago';
	}
}

// Multi-Dementional Array sort.
function by(i,dir) {
	return function(a,b){a = a[i];b = b[i];return a == b ? 0 : (a < b ? -1*dir : dir)}
}




