/**
participants code
*/




$(document).ready(function() {
		
	//if(!$.browser.msie)
	//{
		/*
		$('a.collectionLink').live("click",function(event){
			event.preventDefault();
			clickCollection(this,event);
			return false;
		});
	
		$('a.pieceLink').live("click",function(event){
			event.preventDefault();
			clickPiece(this,event);
			return false;
		});
		*/
		$('a.collectionLink').click(function(e){
			//e.preventDefault();
			clickCollection(this,e);
			return false;
		});
	
		$('a.collectionPiecesLink').click(function(e){
			//e.preventDefault();
			clickCollection(this,e);
			return false;
		});
	
		$('a.pieceLink').click(function(e){
			//e.preventDefault();
			clickPiece(this,e);
			return false;
		});
		
	//}

});


function clickCollection(obj,e)
{
	
	if(typeof( e.preventDefault) != 'undefined')
	{
		e.preventDefault();
	}
	obj = $(obj);
	href = obj.attr('href');
	temp = href.split(split_dir);
	hash = temp[1];
	hash = hash.replace('.html','');
	uri = split_dir+'#'+hash;
	debug(uri);
	if(hash.length > 0)
	{
		document.location.href = split_dir +'#'+hash;
	}
	return false;
}
function clickPiece(obj,e)
{
	if(typeof( e.preventDefault) != 'undefined')
	{
		e.preventDefault();
	}
	obj = $(obj);
	href = obj.attr('href');
	temp = href.split(split_dir);
	hash = temp[1];
	hash = hash.replace('.html','');
	uri = split_dir+'#'+hash;
	debug(uri);
	if(hash.length > 0)
	{
		document.location.href = split_dir +'#'+hash;
	}
	return false;
}

function parseHash(hash)
{
	//alert(hash);
	if(hash.length > 0)
	{
		hash = hash.substr(1,hash.length);
		args = hash.split('/');
		//alert(args[0]+' '+args[1])
		if(isNaN(args[1]*1))
		{
			debug('COLLECTION collection:'+args[1]+' id:'+ args[0]+' lang:'+language);
			args = { collection: args[1], id: args[0], lang: language};
			var rtnMethod = parseCollection;
			var dataUrl = "/ajax/collection/";
		}
		else
		{
			debug('PIECE collection:'+args[2]+' piece_id:'+args[1]+' id:'+ args[0]+' lang:'+language);
			args = { collection: args[2],piece_id:args[1], id: args[0], lang: language};
			var rtnMethod = parsePiece;
			var dataUrl = "/ajax/piece/";
		}

		$.ajax({
			url: dataUrl,
			type: "GET",
			dataType: "xml",
			data: args,
			success: function(xml){rtnMethod(xml);},
			error: function(){badRequest(hash);}
		});	
	}
	else
	{
		badRequest(hash);	
	}
}
function parseCollection(xml)
{
	//alert('XML '+ $(xml).find('info').text());
	//return;
	document.title = $(xml).find('title').text();
	cat_colour = $(xml).find('colour').text();
	pieces = $(xml).find('pieces').text();
	id = $(xml).find('id').text();
	zoom = $(xml).find('zoom').text();
	$('#collectionsMainImage').width('475px');
	$('#collectionsMiddleStrip').width('80px');
	$('#collectionsMiddleStrip').css('background-image','none');
	$('#collectionsMiddleStrip').css('background',cat_colour);
	$('#collectionsMiddleStrip').html(pieces); 

	$('a.collectionLink img').removeClass('activeCollection');
	$('a.collectionLink img').addClass('inActiveCollection');
	obj = $('a#collection'+id+' img');
	obj.removeClass('inActiveCollection');
	obj.addClass('activeCollection');	
	debug('COLLECTION IMAGE ' + zoom);
	debug('CURRENT PANEL ' + currentPanelID);
	debug('COLLECTION PANEL ' + panelsData[id][0]);
	itemPanelID = panelsData[id][0];
	if(currentPanelID != itemPanelID )
	{
		$('#collection'+currentPanelID+'panel').hide();
		$('#collection'+itemPanelID+'panel').show();
		currentPanelID = itemPanelID;
		//alert(pageTxt + '&nbsp;' + currentPanelID + "&nbsp;/&nbsp;" + maxPanels);
		$('span#pagination').html(pageTxt + '&nbsp;' + currentPanelID + "&nbsp;/&nbsp;" + maxPanels);
		processImages(currentPanelID);
	}
	//alert(cat_colour);
	var so = new SWFObject("/swf/zoom-pik-"+language+"-1.swf", "movie", "475", "475", "8.0.23", cat_colour);   
	so.addParam("wmode", "transparent");
	so.addVariable('imgfile', zoom);
	so.useExpressInstall('/js/expressinstall.swf');
	so.write("collectionsMainImage");	
	
	$('#collectionsInfoPanel').html($(xml).find('info').text());
	//$('#participantsInfoCollections').html($(xml).find('collections').text());
	
	
	//now must add events to the pieces links (for IE)
	$('#collectionsMiddleStrip a.collectionLink').click(function(e){
		//e.preventDefault();
		clickCollection(this,e);
		return false;
	});

	$('#collectionsMiddleStrip a.pieceLink').click(function(e){
		//e.preventDefault();
		clickPiece(this,e);
		return false;
	});		
	
}
function parsePiece(xml)
{
	
	document.title = $(xml).find('title').text();
	cat_colour = $(xml).find('colour').text();	
	
	//alert($(xml).find('info').text());	
	info = $(xml).find('info').text();
	collection_id = $(xml).find('collection_id').text();
	
	debug('PIECE IMAGE' + $(xml).find('zoom').text());
	if(!$('#piecesList').length)
	{
		debug('FILLING IN THE PIECES LIST');
		$('#collectionsMainImage').width('475px');
		$('#collectionsMiddleStrip').width('80px');
		$('#collectionsMiddleStrip').css('background-image','none');
		$('#collectionsMiddleStrip').css('background',cat_colour);
		$('#collectionsMiddleStrip').html($(xml).find('pieces').text());	
		
		debug('CURRENT PANEL ' + currentPanelID);
		debug('COLLECTION PANEL ' + panelsData[collection_id][0]);
		itemPanelID = panelsData[collection_id][0];
		if(currentPanelID != itemPanelID )
		{
			debug('SWITCHING PANEL ' + currentPanelID);
			$('#collection'+currentPanelID+'panel').hide();
			$('#collection'+itemPanelID+'panel').show();
			currentPanelID = itemPanelID;
			//alert(pageTxt + '&nbsp;' + currentPanelID + "&nbsp;/&nbsp;" + maxPanels);
			$('span#pagination').html(pageTxt + '&nbsp;' + currentPanelID + "&nbsp;/&nbsp;" + maxPanels);
			processImages(currentPanelID);
		}

	}

	$('a.pieceLink img').removeClass('activeThumb');
	$('a.pieceLink img').addClass('inActiveThumb');
	$('a.collectionPiecesLink img').removeClass('activeThumb');
	$('a.collectionPiecesLink img').addClass('inActiveThumb');
	
	obj = $('a#collection'+collection_id+' img');
	obj.removeClass('inActiveCollection');
	obj.addClass('activeCollection');	
	
	obj = $('a#piece'+$(xml).find('id').text()+' img');
	obj.removeClass('inActiveThumb');
	obj.addClass('activeThumb');	

	var so = new SWFObject("/swf/zoom-pik-"+language+"-1.swf", "movie", "475", "475", "8.0.23", cat_colour, true);
	so.addVariable('imgfile', $(xml).find('zoom').text());
	so.addParam("wmode", "transparent");	
	so.useExpressInstall('/js/expressinstall.swf');
	so.write("collectionsMainImage");		
		
	
	$('#collectionsInfoPanel').html(info);
	
	//now must add events to the pieces links (for IE)
	$('#collectionsMiddleStrip a.collectionPiecesLink').click(function(e){
		//e.preventDefault();
		clickCollection(this,e);
		return false;
	});

	$('#collectionsMiddleStrip a.pieceLink').click(function(e){
		//e.preventDefault();
		clickPiece(this,e);
		return false;
	});		
	
}
function badRequest(hash)
{
	// abort abort
	// just make up the old url if the request fails.
	debug('badRequest: '+hash);
	return;
	if(hash.length > 0)
	{
		document.location.href = split_dir + hash + '.html';
	}
	else
	{
		document.location.href = split_dir;	
	}
}