var Barty = {
	setProjectsList: function (items, itemsByTags) {
		// Looping though all the li items:
		
		items.each(function(i){
			var elem = $(this),
				tags = elem.data('tags').split(',');
			
			// Adding a data-id attribute. Required by the Quicksand plugin:
			elem.attr('data-id',i);
			
			$.each(tags,function(key,value){
				
				// Removing extra whitespace:
				value = $.trim(value);
				
				if(!(value in itemsByTags)){
					// Create an empty array to hold this item:
					itemsByTags[value] = [];
				}
				
				// Each item is added to one array per tag:
				itemsByTags[value].push(elem);
			});
			
		});
	},
	createList: function (text,items) {
		// This is a helper function that takes the
		// text of a menu button and array of li items
		
		// Creating an empty unordered list:
		var ul = $('<ul>',{'class':'hidden'});
		
		$.each(items, function(){
			// Creating a copy of each li item
			// and adding it to the list:
			
			$(this).clone().appendTo(ul);
		});

		ul.appendTo('#projects');

		// Creating a menu item. The unordered list is added
		// as a data parameter (available via .data('list'):
		
		var a = $('<a>',{
			html: text,
			href:'#',
			data: {list:ul}
		}).appendTo('#menu ul li:last');
	},
	createMenu: function(items, itemsByTags) {
		$('<ul>').appendTo('#menu');
		$('<li>').appendTo('#menu ul');
		// Creating the "Everything" option in the menu:
		Barty.createList('Wszystko',items);
	
		// Looping though the arrays in itemsByTags:
		$.each(itemsByTags, function(k,v) {
			$('<li>').appendTo('#menu ul');
			Barty.createList(k,v);
		});
	},
	addMenuEvents: function () {
		$('#menu ul li a').live('click',function(e){
			var link = $(this);
			
			$('#menu ul li').removeClass('active');
			link.parent().addClass('active');
			Cufon.refresh();
			
			// Using the Quicksand plugin to animate the li items.
			// It uses data('list') defined by our createList function:
			
			$('#stage').quicksand(link.data('list').find('li'));
			e.preventDefault();
		});
	},
	setProjectsInfo: function (el, name) {
		var details = $(el).find('.details');		
		var www = $(el).find('.www');
		var gallery = details.find(".gallery");
		
		if ($(details).css('display') == 'none') {
			Barty.showDetails(details, www);
		} else {
			Barty.hideDetails(details, www);
		}
		
		Barty.loadGallery(gallery, name);
//		gallery.addClass(name);
//		gallery.load("projects/" + name + ".html");
		
		Barty.setWorkList(el, details);
		if (www.length != 0) {
			Barty.setWorkLink(www);
		}
	},
	showDetails: function (details, www) {
		details.fadeIn();
		www.fadeIn();
	},
	hideDetails: function (details, www) {
		details.fadeOut();
		www.fadeOut();
	},
	setWorkList: function (el, details) {
		var data_test = $(el).attr('data-tags').split(',');
		var list = '<ul>';
		
		$.each(data_test, function() {
			list = list + '<li>' + this + '</li>';
		});
		details.find('p').html(list);
	},
	setWorkLink: function (www) {
		if (!(www.find('a').length) && (www.html().length) > 0) {
			www.html('<a href="http://' + www.html() + '" target="_blank">' + www.html() + '</a>');
		}
	},
	loadProjectPage: function(el) {
		var value = $(el).html();
		$("#link").html('<a href="http://' + value + '">test</a>');
		$('#link').click(function() {
			return true;
		}).click();
	},
	loadGallery: function (gallery, name) {
		gallery.addClass(name);
		gallery.load("projects/" + name + ".html");
	},
	loadProjectInfo: function (el) {
		var data = $(el).attr('data-tags');
		
		$(el).load("html-css.html");
		
		//alert($(el+":first-child").attr('alt'));
	},
	activeMe: function (id) {
		var el = $(id);
			
		//$('#about-me').load("o-mnie.html", function() { Cufon.refresh(); });
		$(id + ' .box').toggle("slow");
		if (el.hasClass('text')) {
			el.removeClass('text');
			$(id + ' a.close').css({ display: 'none' });
		} else {
			el.addClass('text');
			$(id + ' a.close').css({ display: 'block' });
		}
	},
	setCuffon: function () {
		Cufon.replace('#left h2');
		Cufon.replace('#right .box h2');
        Cufon.replace('#left a', { hover: { color: '#2e9bda'} });
        Cufon.now();
    }
}

$(document).ready(function(){
	var items = $('#stage li'),
		itemsByTags = {};
	var counter = 0;	
	// Set Projects List
	Barty.setProjectsList(items, itemsByTags);

	// Create Menu
	Barty.createMenu(items, itemsByTags);
	
	// Add Menu Events
	Barty.addMenuEvents();
	
	// Start Portfolio
	$('#menu a:first').click();
		
	// Cufon Settings
	Barty.setCuffon();
});
