// initialize portfolio
$(function() {
	$('#arrow_prev').click(function() {
		portfolioSwap($(this).attr('name'));
	});
	$('#arrow_next').click(function() {
		portfolioSwap($(this).attr('name'));
	});

	portfolioSwap(1);
});

function portfolioSwap(index) {
	$.ajax({
		url: 		'/xml/portfolio.xml',
		dataType: 	'xml',
		cache:		false,
		success:	function(data) {
			var count = data.getElementsByTagName('project').length;
			var prev = parseInt(index) == 1 ? count : parseInt(index)-1;
			var next = parseInt(index) == count ? 1 : parseInt(index)+1;
			
			$('#arrow_prev').attr('name', prev);
			$('#arrow_next').attr('name', next);

			
			var img = $(data).find('#'+index+' photo').text();
			var name = $(data).find('#'+index+' name').text();
			var client = $(data).find('#'+index+' client').text();
			var location = $(data).find('#'+index+' location').text();
			var agency = $(data).find('#'+index+' agency').text();
			var rollover = $(data).find('#'+index+' rollover').text();
			var html_file = $(data).find('#'+index+' html').text();
			var html = '';
			
			// name, client, location required
			html += '<strong>Project</strong><br />';
			html += name + '<br /><br />';
			html += '<strong>Client</strong><br />';
			html += client + '<br />';
			html += location;
			if (agency) {
				html += '<br /><br />';
				html += '<strong>Agency</strong><br />';
				html += agency;
			}
			
			$('#portfolio_details').html(html);
			
			if (html_file) {
				$.ajax({
					url:		html_file,
					success:	function(data) {
						$('#portfolio_img').html(data);
					}
				});
			} else {
				var full_size = img.replace('.', '_full.');
				var img_tag = '<a href="/img/portfolio/'+full_size+'" class="thickbox"><img src="/img/portfolio/'+img+'" /></a>';
				$('#portfolio_img').html(img_tag);
				
				tb_init('a.thickbox');
			}
		}
	});
	
	return false;
}
