function sortByLengthDesc(a, b) {
	if (a.length < b.length)
		return 1;

	if (a.length > b.length)
		return -1;

	return 0;
}


$(function(){
	$('#clients').carousel();
	$('#newest').slider();
	$('#announcements').twitter({
		username: 'curiosity360',
		tags: ['#award', '#launch', '#cur100', '#team', '#news', '#curiosity']
	});
	$('#content a, #toolbar .related a').qtip({
		position: {
			my: 'bottom center',
			at: 'top right',
			target: 'mouse',
			adjust: {
				y: -4
			}
		},
		style: {
			classes: 'ui-tooltip-orange ui-tooltip-shadow ui-tooltip-rounded'
		}
	});

	var curiosity, options, map, $map, marker;

	$map = document.getElementById('map');

	if ($map) {
		curiosity = new google.maps.LatLng(39.10052, -84.50865);

		options = {
			zoom: 16,
			center: curiosity,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			mapTypeControl: false
		};

		map = new google.maps.Map($map, options);

		setTimeout(function() {
			marker = new google.maps.Marker({
				map: map,
				draggable: false,
				animation: google.maps.Animation.DROP,
				position: curiosity,
				title: 'Curiosity Advertising'
			});

			google.maps.event.addListener(marker, 'click', goToGMaps);
		}, 1500);
	}

	$categories = $('.filter :checkbox');
	$tiles      = $('.tiled > li');

	$categories.click(function(e) {
		var selected          = [],
		    selected_medium   = [],
		    selected_client   = [],
		    selected_campaign = [],
		    $filters          = $('.filter :checkbox:checked'),
		    $mediums          = $('.filter.medium :checkbox:checked');
		    $clients          = $('.filter.client :checkbox:checked');
		    $campaigns        = $('.filter.campaign :checkbox:checked');

		$filters.each(function () {
		selected.push('.'+$(this).val());
		});
		$mediums.each(function () {
			selected_medium.push('.'+$(this).val());
		});
		$clients.each(function () {
			selected_client.push('.'+$(this).val());
		});
		$campaigns.each(function () {
			selected_campaign.push('.'+$(this).val());
		});

		selected = [];
		
		if ($mediums.length) {
			selected.push($mediums);
		}

		if ($clients.length) {
			selected.push($clients);
		}

		if ($campaigns.length) {
			selected.push($campaigns);
		}

		selected.sort(sortByLengthDesc); // sort descending by amount of selections

		var vals = [];
		
		// sl = Largest selected list
		// sm = Middle selected list
		// ss = Smallest selected list
		// Used for looping through, and creating the selectors
		
		if(selected[0])
		{
			$.each(selected[0], function(i, sl) {
				if(selected[1])
				{
					$.each(selected[1], function(j, sm) {
						if(selected[2])
						{
							$.each(selected[2], function(j, ss) {
								vals.push('.'+$(sl).val()+'.'+$(sm).val()+'.'+$(ss).val());
							});
						}
						else
						{
							vals.push('.'+$(sl).val()+'.'+$(sm).val());
						}
					});
				}
				else
				{
					vals.push('.'+$(sl).val());
				}
			});
		}

		if (selected.length) 
		{
		$tiles.filter(function() {
			return $(this).is(vals.join(','));
		}).fadeIn(200);

		$tiles.filter(function() {
			return ! $(this).is(vals.join(','));
		}).fadeOut(200);
		}
		else
		{
			$tiles.filter(':hidden').fadeIn(200);
		}

	});

	$('.reset').click(function(e) {
		$(':checkbox').each(function() {
			e.preventDefault();
			$categories.prop('checked', false);
			$tiles.fadeIn(200);
		})
	});
	
});

function goToGMaps() {
	window.location = 'http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=Curiosity+Advertising,+221+East+4th+Street,+Cincinnati,+OH&aq=0&sll=39.099826,-84.507265&sspn=0.010374,0.017102&ie=UTF8&hq=Curiosity+Advertising,&hnear=221+E+4th+St,+Cincinnati,+Ohio+45202&ll=39.100509,-84.508638&spn=0.010374,0.017102&z=16';
}

