wta.portfolio = function(){
	// Private vars & functions
	var pf = {};
	pf.id = null;
	pf.container = '#pf_container';
	pf.wrapper = '#portfolio_content';
	pf.xml = "http://www.buildit.it/index.php/portfolio/portfolio"
	pf.items = [];
	
	var mOffset = null;
	
	function _init(){
		
		mOffset = $('#moon').width() / 2;
		
		_placeEventsNav();
		$.get(pf.xml, _parseXML);
		_observeMouse();
		_setupEvents();
		
	}
	
	function _placeEventsNav(){
		var ev = $('#events');
		ev.css({
			'position':'absolute',
			'top': '50%',
			'height': ev.height(),
			'margin-top': -(ev.height() / 2)
		});
	}
	function _observeMouse(){
		//$('#portfolio').mousemove(_scroll);
		$(pf.wrapper).mousemove(_scroll);
	}
	
	function _scroll(e){
		if( $(pf.container).width() > ((mOffset*2) - 20)){	
			var left = ( (e.clientX - mOffset) / ($('#viewport').width() - mOffset) ) * ( ( ($(pf.container).width() + mOffset) - $('#viewport').width() - mOffset ) * -1 );
			$(pf.container).css({'left':left});
		}
	}
	
	function _setupEvents(){
		$('a.event').click(function(){
			$('a.event').removeClass('activeEvent');
			$(this).addClass('activeEvent');
			_loadXML(this.href);
			return false;
		});
	}
	
	function _loadXML(url){
		$(pf.container).html('');
		pf.items = [];
		$.get(url, _parseXML);
	}
	
	function _parseXML(xml){
		// Process details
		if($(xml).find('portfolio title')){
			var p = {};
			p.title = $(xml).find('portfolio > title').text();
			p.what = $(xml).find('portfolio what').text();
			p.how = $(xml).find('portfolio how').text();
			p.when = $(xml).find('portfolio when').text();
			$('#portfolio_detail').html('<h3>' + p.title + '</h3>' + '<p>' + p.when + '<br />' + p.what + '<br />' + p.how + '</p>');
		}
			
		// Process images	
		$(xml).find('portfolio images').each(function(){
			var it = {};
			it.title = $(this).find('title').text();
			it.thumb = $(this).find('image_thumb_url').text();
			it.thumb_width = parseInt($(this).find('image_thumb_url').attr('width'));
			it.medium = $(this).find('image_full_url').text();
			it.caption = $(this).find('captio').text();
			pf.items.push(it);
		});
		_createHTML();
	}
	
	function _createHTML(){
		var tw = 0;
		var offset = 20;
		var u = document.createElement('ul');
		
		// Create a filler element
		var fl = document.createElement('li')
		var fi = document.createElement('img');
		$(fi).attr({
			'src': 'http://www.buildit.it/assets/images/filler.gif',
			'width': mOffset,
			'height': 180,
			'alt': 'Inizio galleria'
		});
		$(fl).addClass('filler').append(fi);
		$(u).append(fl);
		
		$(pf.items).each(function(){
			// create the li
			var l = document.createElement('li');
			// create the link
			var a = document.createElement('a');
			$(a).attr({'href': this.medium, 'title': this.title}).addClass('pf_image');
			$(a).fancybox({ 'hideOnContentClick': true, 'overlayOpacity': 0.6, 'overlayShow': true }); 
						
			// create the image
			var i = document.createElement('img');
			$(i).attr({'alt': this.title, 'src': this.thumb, 'height': 180});
			tw += (this.thumb_width + offset);
				
			// wrap up
			$(a).html(i);
			$(l).html(a);
			$(u).append(l);
			
		});
		//console.log(tw);
		$(pf.container).css('width', (tw + mOffset + offset)).append(u);
	}
	
	// Public vars & functions
	return {
		init: function(){
			$(document).ready(function(){
				_init();
			});
		},
		loadPortfolio: function(url){
			_loadXML(url);
		}
	}
}();
wta.portfolio.init();