if (typeof console != 'undefined') log = console.log; else log = function () {};

var ISW_projects = [];
var ISW_current = 0;
var ISW_zindex = 999;
var ISW_init = false;
var ISW_ie6 = false;

$(document).ready(function() {
	
	$(window).hashchange(function() {
		hashChanged(location.hash);
	});
	
	if ($.browser.msie && $.browser.version == '6.0')
		ISW_ie6 = true;
	
	// load our xml
	$.ajax({
		url : 'projectlist.xml',
		success : loadPage,
		error : errorHandler,
		dataType: 'xml' 
	});
	
	if (!ISW_ie6)
	{
		// create ajax loader
		var loader = new Image();
		loader.src = '/images/loader.gif';
		loader.id = 'loader';
		$('p.breadcrumbs').prepend(loader);
	}
	
	
	// add top nav and counter
	$('p.breadcrumbs').append('<span class="work_counter">&nbsp; | &nbsp;<span id="current_count"></span>/<span id="total_count"></span></span>');
	$('p.breadcrumbs').append('<span class="work_nav"><a href="#" id="previous_project_link">Previous</a> &nbsp;|&nbsp; <a href="#" id="next_project_link">Next</a></span>');

	// add nav actions
	$('#previous_project, #previous_project_link').click(previousProject);
	$('#next_project, #next_project_link').click(nextProject);
	
	$('.project_link a').click(function(e) {
		e.preventDefault();
		window.open($(this).attr('href'));
	});
	
});

loadPage = function(d, textStatus) {
	

	ISW_projects = $(d).find('project');
	
	$('#total_count').text(ISW_projects.length);
	
	if (location.hash == "")
	{
		displayProject(ISW_projects[0]);
	}
	else 
	{
		$(ISW_projects).find('uri').each(function(i,o) {

			var h = location.hash.replace('#/', '').replace('/', '');
			if (h == $(o).text())
			{
				ISW_current = i;
				displayProject($(o).parent()[0]);
			}
		});
	}
	ISW_init = true;
};

displayProject = function(p) {
	// dec our index
	ISW_zindex --; 
	
	// show loader
	$('#loader').show();
	
	// set hash if it isn't already there

	var h = location.hash.replace('#/', '').replace('/', '');	
	if ((h != $(p).find('uri').text() &&
	 	ISW_init) ||
		(location.hash != "")
	   ) 
	{
		location.hash = '#/'+$(p).find('uri').text()+'/';
	}
	else
	{
		//location.hash = ' ';
	}

	
	// set counter
	$('#current_count').text(ISW_current + 1);
	
	// mark old image for fade out and removal later
	$('#work_img img').addClass('remove-me');
	
	// hide the text and swap it
	
	$('#work_desc p').hide();
	$('#client_name').text($(p).find('client').text());
	$('#project_name').text($(p).find('title').text());
	if($(p).find('link').length > 0)
	{
		$('.project_link a').attr('href', $(p).find('link').text()).show();
	}
	else
	{
		$('.project_link a').hide();
	}
	
	// create new image
	var img = new Image();
	img.src = $(p).find('img').attr('src');
	img.alt = $(p).find('img').attr('alt');
	
	if (!ISW_ie6)
	{
		img.style.display = 'none';
		img.style.position = 'absolute';
		img.style.zIndex = ISW_zindex;
	}
	
	
	$(img).load(function() {
		// hide loader
		$('#loader').hide();
		
		// fade in new image
		$(this).show();
		
		// fade in text
		if (ISW_ie6)
		{
			$('#work_desc p').show();
		}
		else
		{
			$('#work_desc p').fadeIn();
		}
		
		
		// remove old image after fade out
		
		if (ISW_ie6)
		{
			$('img.remove-me').remove();
			// fade in nav
			$('#previous_project').show();
			$('#next_project').show();
		}
		else
		{
			$('img.remove-me').fadeOut('fast', function() {
				$(this).remove();
			});
			
			// fade in nav
			$('#previous_project').fadeIn('fast');
			$('#next_project').fadeIn('fast');
		}
		
		$('#work_img').append(img);
		
		
	});
	
	
};

errorHandler = function(request, status, error) {
	alert('Oh crap! We messed up and now there is nothing for you to view! We are trying to fix this right now!');
	log(request);
};

hashChanged = function(h) {

	if (ISW_init)
	{
		if (h == "") // no hash. Default to first item
		{
			ISW_current = 0;
			displayProject($(ISW_projects)[0]);
		}
		else
		{
				$(ISW_projects).find('uri').each(function(i,o) {

					h = h.replace('#/', '').replace('/', '');

					if (h == $(o).text())
					{
						ISW_current = i;
						displayProject($(o).parent()[0]);
					}
				});
		}
	}
	
};

nextProject = function(e) {
	
	e.preventDefault();
	if (ISW_current == ISW_projects.length - 1) 
	{
		ISW_current = 0;
	}
	else
	{
		ISW_current ++;
	}
	
	displayProject(ISW_projects[ISW_current]);
	
};

previousProject = function(e) {
	e.preventDefault();
	
	if (ISW_current == 0)
	{
		ISW_current = ISW_projects.length - 1;
	}
	else
	{
		ISW_current --;
	}
	
	displayProject(ISW_projects[ISW_current]);
};