//*************************************************************************************************************//
//Filterable
//*************************************************************************************************************//

// http://ejohn.org/blog/javascript-array-remove/
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

$(function(){
	
	var activeCategories = [];
	var projects = $('#project-list');
	$('#project-tags a').click(function(e) {
		var activate = !$(this).hasClass('active');
		var category = $(this).attr('href').substring(1);
		
		if (activate)
			activeCategories.push(category);
		else if (activeCategories.length > 0)
			activeCategories.remove($.inArray(activeCategories, category)) ;
		
		projects.find('li').show(); 
		
		if (activeCategories.length > 0)
			projects.find('li:not(.' + activeCategories.join('.') + ')').hide();

		$(this).toggleClass('active');
		e.preventDefault();
		
		setTimeout(function() {
			projects.find('li:visible').removeClass('alt');
			projects.find('li:visible:even').addClass('alt');
			document.cookie = 'projects=' + activeCategories.join(',');
		}, 0)
		
	})
	
	//    $("#projects").filterprojects({
	//  animationSpeed: 0,
	//  animationPulse: 0,
	//  show: { width: "show" },
	//  hide: { width: "hide" },
	//  filterTagSelector: [ '#tags a' ]
	// });

	$('.campaign.with-link').live('click', function(e) {
		e.preventDefault();
		window.location.href = $(this).find('.campaign-link').attr('href');
	})
});




//Hent prosjekt-tagger
function get_project_tags() {
  var tags = new Array();
  var i = 0;
  $("#project-tags a").each(function() {
  	if ($(this).hasClass("active")) {
  		tags[i] = $(this).attr("href").substr(1);
   	 i++;
  	}
  });

	document.cookie = "projects=" + escape(tags);			
}

//Hent cookie
function get_cookie(cookie_name) {
  var results = document.cookie.match ('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
  if (results)
    return (unescape(results[2]));
	else
		return null;
}

