// JavaScript Document
	var global_photo_gallery = new Array();

	function mycarousel_itemLoadCallback(carousel, state)
	{
		if (state != 'init') return;
		
		jQuery.get('/cocoon/saii/multimedia/photo-day-'+languageCode+'.xml', null, function(data) {
			if (data) mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, data);
		});
	};
	
	function mycarousel_itemAddCallback(carousel, first, last, data)
	{
		var count = 0;
		// Simply add all items at once and set the size accordingly.
		var photos = data.getElementsByTagName('channel')[0].childNodes;
		if (photos.length > 0)
		{
			for (var i=0; i < photos.length; i++) {
				var photo = photos[i];
				if (photo.nodeName == 'item') {
					var image = photo.getElementsByTagName('image')[0];
					var source = '';
					
					if (image.firstChild && image.firstChild.text) source = image.firstChild.text;
					if (image.textContent) source = image.textContent;
					
					if (source != null && source.length > 0) {
						var desc_ele = photo.getElementsByTagName('description')[0];
						var description = '';
						
						if (desc_ele.firstChild && desc_ele.firstChild.text) description = desc_ele.firstChild.text;
						if (desc_ele.textContent) description = desc_ele.textContent;
						
						if (description.length == 0)
						{
							var title_ele = photo.getElementsByTagName('title')[0];
							if (title_ele.firstChild && title_ele.firstChild.text) description = title_ele.firstChild.text;
							if (title_ele.textContent) description = title_ele.textContent;
						}
						global_photo_gallery[count+1] = (new Array(source,description));
						if (count < 9) carousel.add(count, mycarousel_getItemHTML(count+1,source,description));
						//carousel.add(count, mycarousel_getItemHTML(count+1,source,description));
						count++;
					}
				}
				else if (photo.nodeName == 'errormessage') 
				{
					removeCarousel(carousel);
					count = 0;
					return(false);
				}
			}
		}
		carousel.size(count);
		if (count <= 8) {
			jQuery('#play').fadeOut();
			jQuery('#pause').fadeOut();
			jQuery('.jcarousel-prev').fadeOut();
			jQuery('.jcarousel-next').fadeOut();
			carousel.stopAuto();
			carousel.clip.hover(null);
		}
	};
	
	function mycarousel_getItemHTML(i,src,desc)
	{
		var img = jQuery(document.createElement('img'));
		src = '/cocoon/saii/'+src;
		var lg_src = src.replace('TN','HP');
		img.attr('src',lg_src);
		img.attr('thumb_src',src);
		img.attr('large_src',lg_src);
		img.attr('alt',desc);
		img.css('border','none');
		img.attr('id','img_'+i);
		
		if (i == 1) {
			if (desc.length > 0) jQuery('#caption').text(desc);
			else jQuery('#caption').fadeOut();
		}
		else img.css('display','none');
		
		img.appendTo('#multiHolder');
		
		return '<a href="#" onclick="showLarge(\'img_'+i+'\'); return(false);"><img src="'+src+'" border="none" alt="" width="35" height="30" /></a>';
	};

	function mycarousel_initCallback(carousel)
	{
		jQuery('#play').bind('click', function() {
			carousel.startAuto(14);
			return false;
		});
		jQuery('#pause').bind('click', function() {
			carousel.stopAuto();
			return false;
		});
		
		carousel.buttonNext.bind('click', function() {
			carousel.stopAuto();
		});
	
		carousel.buttonPrev.bind('click', function() {
			carousel.stopAuto(0);
		});
		
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto(14);
		});
	};
	
	function mycarousel_itemVisibleInCallback (carousel,li,index,state) {
		var next = index+1;
		if (!carousel.has(index) && global_photo_gallery[next]) {
			var src = global_photo_gallery[next][0];
			var thumb = src.replace('/lg/','/tn/');
			var desc = global_photo_gallery[next][1];
			carousel.add(index,mycarousel_getItemHTML(next,src,desc));
		}
	}
	
	function mycarousel_itemFirstInCallback(carousel,li,index,state) {
		if (index == 1) jQuery('.jcarousel-prev').fadeOut();
		else jQuery('.jcarousel-prev').fadeIn();
		showLarge('img_'+index);
	}
	
	function mycarousel_itemLastInCallback(carousel,li,index,state) {
		if (carousel.size() && (carousel.size()+1) == index) {
			jQuery('.jcarousel-next').fadeOut();
			jQuery('#play').fadeOut();
			jQuery('#pause').fadeOut();
			carousel.stopAuto();
		}
		else {
			jQuery('.jcarousel-next').fadeIn();
			jQuery('#play').fadeIn();
			jQuery('#pause').fadeIn();
		}
	}
	
	function showLarge(img) {
		// get all images in the multiHolder block
		var images = jQuery('#multiHolder').children('img');
		// for each image, if the display is not "none", fade it out, then fade in the current image.
		jQuery.each(images, function (e) {
			var i = jQuery(images[e]);
			if (i.css('display') != 'none') i.fadeOut('def', function() {
				jQuery('#'+img).fadeIn('def', function () {
					var desc = jQuery('#'+img).attr('alt');
					if (desc.length > 0) {
						jQuery('#caption').text(desc);
						jQuery('#caption').fadeIn();
					}
					else jQuery('#caption').fadeOut();
				});
			});
		});
	}
	
	jQuery(document).ready(function() {
		jQuery('#multiHolder').empty();
		jQuery('#caption').empty();
		jQuery('#thumbs').empty();
		jQuery('#thumbs').jcarousel({
			auto: 14,
			scroll: 1,
			/*wrap: 'both',*/
			itemFirstInCallback: mycarousel_itemFirstInCallback,
			itemLastInCallback: mycarousel_itemLastInCallback,
			initCallback: mycarousel_initCallback,
			itemLoadCallback: mycarousel_itemLoadCallback,
			itemVisibleInCallback: mycarousel_itemVisibleInCallback
		});
		
		jQuery('#region_68').tabs();
		jQuery('#navBar #pause').after('<a href="#" class="toggleCaption toggleCaption-active"></a>');
		jQuery(".toggleCaption").toggle(
			function () {
				jQuery("#caption").stop().animate({top: 413, height:60}, 250);
				jQuery(".toggleCaption").removeClass("toggleCaption-active");
			},
			function () {
				jQuery("#caption").stop().animate({top: 326, height:80}, 250);
				jQuery(".toggleCaption").addClass("toggleCaption-active");
				
			}
		); 		
	});