var portfolio = {
	
	items: undefined,
	types: undefined,
	
	/**
	 * @function
	 * @description An abstraction layer for the portfolio items
	 * @param {function} callback The function that is fired with the new information
	 */
	getItems: function(callback){
		callback(portfolioItems.portfolio, portfolioItems.portfolioTypes);
	},
	
	/**
	 * @function
	 * @description Gets a random portfolio peice to show on the side
	 * @param {element} el The jquery DOM object to append to
	 */
	getRandom: function(el){
		
		portfolio.getItems(function(items, types){
			
			var randomNumber = Math.floor( items.length * Math.random() );
			el.html(
				'<a href="' + portfolio.getPath('portfolio/#?item=' + items[randomNumber].id) + '" title="' + items[randomNumber].title + '">' +
					'<img alt="' + items[randomNumber].title + '" src="' + items[randomNumber].thumb + '" />' +
				'</a>'
			);
			$('.portfolio-random a').click(function(){
				portfolio.getItem($('.portfolio-container'), String($(this).attr('href')).split('item=')[1]);
				portfolio.getRandom(el);
			});
			
		});
	},
	
	/**
	 * @function
	 * @description Gets the entire path to a link
	 * @param {string} path
	 */
	getPath: function(path){
		return 'http://tysonlloydcadenhead.com/' + path;
	},
	
	/**
	 * @function
	 * @param {element} el The jquery DOM object to append to
	 */
	getLists: function(el){
	
		portfolio.getItems(function(items, types){
		
			var portfolioTypes = [];
			for (var i = 0; i < types.length; i++) {
				portfolioTypes[i] = types[i].typename;
			}
			
			$('.storytitle').html('<a href="' + portfolio.getPath('portfolio') + '">Portfolio</a>');
			el.html('');
			
			for (var j = 0; j < portfolioTypes.length; j++) {
				var type = portfolioTypes[j];
				el.append('<div class="portfolio-group" id="portfolio-' + type + '" />');
				$('#portfolio-' + type).append('<h2>' + type + '</h2>');
				$('#portfolio-' + type).append('<div class="portfolio-group-thumbs" />');
				for (var i = 0; i < items.length; i++) {
					if (items[i].typename === type) {
						$('#portfolio-' + type + ' .portfolio-group-thumbs').append('<a rel="bookmark" href="#?item=' + items[i].id + '" title="' + items[i].title + '">' +
						'<img alt="' +
						items[i].title +
						'" src="' +
						items[i].thumb +
						'">' +
						'</a>');
					}
				}
			}
			$('.blog-next-previous').html('');
			
			$('.portfolio-group-thumbs a').click(function(){
				portfolio.getItem(el, $(this).attr('href').split('item=')[1]);
			});
			
		});
		
	},
	
	/**
	 * @function
	 * @description gets a single portfolio item
	 * @param {element} el The element to append to 
	 * @param {string} id The portfolio item ID
	 */
	getItem: function(el, id){
		window.scrollTo(0, 120);
		portfolio.getItems(function(items, types){
			var item, html = '';

			for (var i = 0; i < items.length; i++) {
				if (items[i].id === id) {
					item = items[i];
				}
			}

			$('.storytitle').html('<a href="' + portfolio.getPath('portfolio#?id=' + id) + '">' + item.title + '</a>');
			if (item.link) {
				html += '<a title="' + item.title + '" href="' + item.link + '" rel="bookmark">';
			}
				html += '<img src="' + item.image + '" alt="' + item.title + '">';
			if (item.link){
				html += '</a>';
			}
				html += '<div class="portfolio-content">' + item.description + '</div>';
			if (item.link){
				html += '<div class="portfolio-website">' +
					'<a class="read-more" title="' + item.title + '" href="' + item.link + '" rel="bookmark">See This Site</a>' +
				'</div>';
			}
			el.html(html);
			$('.blog-next-previous').html('<a title="Back" id="portfolio-back" href="#" class="back-button">Back</a>');
			$('#portfolio-back').click(function(){
				window.location = '#';
				portfolio.getLists(el);
			});
		});
	},
	
	init: function(){
		var el = $('.portfolio-container');

		if (String(window.location).indexOf('item=') !== -1) {
			portfolio.getItem(el, String(window.location).split('item=')[1]);
		}
		
		$('.portfolio-group-thumbs a').click(function(){
			portfolio.getItem(el, $(this).attr('href').split('item=')[1]);
		});	

	}
	
};

