var xmlhttp;
var currSet;
var nextSet;
var currImageNdx;
var setImages = [];
var setDescs = [];
var host = 'http://' + window.location.hostname + '/' + (window.location.hostname == '10.0.1.99' ? 'sandy-nicholson2/' : '') + currPortfolio + '/';

function getSetDetails(setName) {
	nextSet = setName;
	$.ajax({url:host + setName.id, type:'GET', dataType:'xml', success:function(xml) {stateChanged(xml);}});
}

function stateChanged(xmlDoc) {
	var xmlURL = xmlDoc.getElementsByTagName('url');
	var xmlDesc = xmlDoc.getElementsByTagName('desc');
				
	setImages = [];
	setDescs = [];
						
	for (i=0; i<xmlURL.length; i++) {
		setImages[i] = xmlURL[i].childNodes[0].nodeValue;
		
		try {
			setDescs[i] = xmlDesc[i].childNodes[0].nodeValue;
		} catch(err) {
			setDescs[i] = '';
		}
	}
	
	initSets();
	initBoxes();
	initPDF(xmlDoc.getElementsByTagName('set')[0].attributes.getNamedItem("pdf").nodeValue);
	initLoadImage();
}

function initSets() {
	if (currSet) {$(currSet).toggleClass('select',false);}
	$(nextSet).toggleClass('select',true);
	currSet = nextSet;
}

function changeSetState(elt, state) {
	if (elt.id != $(currSet).attr('id')) {$(elt).toggleClass('select', state);}
	$('#info').text(state ? descArr[elt.id] : '');
}

function changePageState(elt, state) {
	if (elt.id != $(currSet).attr('id')) {$(elt).toggleClass('select', state);}
}

function initBoxes() {
	var innerHTML = '';
	
	for (i=0; i<setImages.length; i++) {
		innerHTML += '<a href="" id="sub' + i + '" class="arrC"></a>';
	}

	$('#boxes').html(innerHTML);

	$('#boxes a').each(
		function(ndx) {
			$(this).removeAttr('href').click(function(){showImage(ndx);});
		}
	).slice(0, 1).toggleClass('boxSel', true);
	
	currImageNdx = 0;
}

function initPDF(link) {
	if (link != '') {
		$('#navPDF').css('display', 'block')
		$('#linkPDF').attr('href', link);
	} else {
		$('#navPDF').css('display', 'none');
		$('#linkPDF').attr('href', '');
	}
}

$.fn.image = function(src, f, e) {
	var i = new Image();
	var t = $(this);
	$(i).load(function(){t.empty().append($(this)); f(this.width, this.height);}).error(e).attr('src', src);
}

function initLoadImage() {
//	$('#main_content').click(function(){}); //.mouseover(function(){}).mouseout(function(){});
	$('#main_image').animate({opacity:0}, 300, "swing", loadImage);
}

function loadImage() {
	$('#main_content').toggleClass('loading', true);
	$('#main_image').image(setImages[currImageNdx], loadComplete, loadError);
}

function loadComplete(width, height) {
	$('#main_content').toggleClass('loading', false); //[.mouseover(function(){showImageDetails(true);}).mouseout(function(){showImageDetails(false);});
	$('#main_image').attr('src', $('#main_image').attr('src'));
	$('#main_image').width(width).height(height).css('border','0').center().animate({opacity:1}, 300, "swing");
}

function changeImage(obj) {
	var halfWidth = $(document).width() / 2;
	if (obj.pageX < halfWidth) {showImage('-');}
	else {showImage('+');}
}

function updateCursor(obj) {
	var halfWidth = $(document).width() / 2;
	if (obj.pageX < halfWidth) {$('#main_content').css('cursor', 'w-resize');}
	else {$('#main_content').css('cursor', 'e-resize');}
}

function loadError() {
	alert('An error occurred loading: ' + setImages[currImageNdx])
}

function showImageDetails(isOver) {
	if (setDescs[currImageNdx] != '') {
		if (isOver) {
			$('#image_info').html('<p><span>INFO: </span>' + setDescs[currImageNdx] + '</p>').css('display', 'block');
			$('#nav').css('display', 'none');
		} else {
			$('#image_info').html('').css('display', 'none');
			$('#nav').css('display', 'block');		
		}
	}
}

function showImage(posn) {
	$('#boxes #sub' + currImageNdx).toggleClass('boxSel', false);
	
	switch(posn) {
		case '-':
			currImageNdx = (currImageNdx == 0) ? setImages.length-1 : currImageNdx-1;
			break;
		case '+':
			currImageNdx = (currImageNdx == setImages.length-1) ? 0 : currImageNdx+1;
			break;
		default:
			currImageNdx = posn;
			break;
	}
	
	$('#boxes #sub' + currImageNdx).toggleClass('boxSel', true);
	initLoadImage();
}

function gotoPage(pageName) {
	var url = pageName.id;
	url = (currPortfolio == '2nd-book') ? currPortfolio + '/' + url : url;
	window.location = url;
}

$(document).ready(function () {
	if (currPortfolio == '2nd-book') {
		var setLinks = $('#setLinks span').slice(0, 1).css('display', 'none');

		$(setLinks).each(
			function() {
				$(this).bind(
					'click', function(){getSetDetails(this);}
				).bind(
					'mouseover', function(){changeSetState(this, true);}
				).bind(
					'mouseout', function(){changeSetState(this, false);}
				);
			}
		);
		
		var pageLinks = $('#setLinks span').slice(1);

		$(pageLinks).each(
			function() {
				$(this).bind(
					'click', function(){gotoPage(this);}
				).bind(
					'mouseover', function(){changePageState(this, true);}
				).bind(
					'mouseout', function(){changePageState(this, false);}
				);
			}
		);
	} else {
		var setLinks = $('#setLinks span');

		$(setLinks).each(
			function() {
				$(this).bind(
					'click', function(){getSetDetails(this);}
				).bind(
					'mouseover', function(){changeSetState(this, true);}
				).bind(
					'mouseout', function(){changeSetState(this, false);}
				);
			}
		);
	}
	
	var setNum = Math.floor(Math.random() * setLinks.length);

	if (currPortfolio != 'main') {$('#menu_'+currPortfolio).toggleClass('select', true);}

	currSet = $(setLinks).slice(setNum, setNum+1);	
	$(currSet).trigger('click');
	
	$('#main_content').click(function(e){changeImage(e);}).mousemove(function(e){updateCursor(e);});
	
	$('#navSubs a.arrL').removeAttr('href').click(function(){showImage('-');});
	$('#navSubs a.arrR').removeAttr('href').click(function(){showImage('+');});
	
	$('#navPDF').bind('mouseover', function() {$('#info').text('Download the Current Set as a PDF');}).bind('mouseout', function() {$('#info').text('');})
});