

function changePhoto(pid) {
   if (selectedImageId != '') {
   	$('#thumb'+selectedImageId).removeClass("currentThumb");
   }
   selectedImageId = pid;
   $('#thumb'+pid).addClass("currentThumb");
   loadNewPhoto(pid);
}

function loadNewPhoto(pid) {
   $('#photo').html('<div></div>');
   $('#photo').addClass("jcarousel-photo-placeholder");
   //alert('get photo ' + pid);
    jQuery.get(
        'jcarousel_ajax_photo.php',
        {
            photoid: pid
        },
        function(xml) {
            var item = jQuery('item', xml);
            var width = parseInt(jQuery('width', item).text());
            var height = parseInt(jQuery('height', item).text());
            var marginTop = (height >= 400 ? 0 : (201-Math.round(height/2)));
            s = '<div id="photoholder" style="height:' + height + 'px;width:' + width + 'px;';
            s += 'margin-top:' + marginTop + 'px;';
            s += 'margin-bottom:0; margin-left:auto; margin-right:auto; padding:0; border: 0;">';
            s += '<img src="' + jQuery('src', item).text() + '" width="' + width + '" height="' + height + '" class="photo">';
            s += '</div>';
            $('#photo').html(s);
            $('#photo').removeClass("jcarousel-photo-placeholder");
            $('#caption').html('<p class="caption">' + jQuery('caption', item).text() + '</p>');
        },
        'xml'
    );   
}

function changeMenus(aId, toplink, sublink) {
    if (sublink != '') {
	if (selectedSubAlbum != '') {
		$('#'+selectedSubAlbum).removeClass('currentSubAlbum');
	}
	selectedSubAlbum = sublink;
	$('#'+selectedSubAlbum).addClass('currentSubAlbum');
    }
    else {
	if (selectedSubAlbum != '') {
		$('#'+selectedSubAlbum).removeClass('currentSubAlbum');
	}
    }
    if (selectedTopAlbum != '') {
	$('#'+selectedTopAlbum).removeClass('currentAlbum');
    }
    selectedAlbumId = aId;
    selectedTopAlbum = toplink;
    $('#'+selectedTopAlbum).addClass('currentAlbum');
    if (isVillaAlbum(aId)) {
	$('#galleryFiltersVillas').show();
	$('#galleryFiltersBluefields').hide();
    }
    else if (isBluefieldsAlbum(aId)) {
	$('#galleryFiltersVillas').hide();
	$('#galleryFiltersBluefields').show();
    }
    else {
	$('#galleryFiltersVillas').hide();
	$('#galleryFiltersBluefields').hide();
    }
}

function changeAlbum(aId, toplink, sublink) {
    //alert('aId='+aId+',toplink='+toplink+',sublink='+sublink);
    changeMenus(aId, toplink, sublink);
    theCarousel.reset();
    getThumbnails();
}

function getThumbnails() {
    jQuery.get(
        'jcarousel_ajax_thumbnails.php',
        {
            albumid: selectedAlbumId
        },
        function(xml) {
            theCarousel.reset();
            addItems(theCarousel, xml);
            theCarousel.size(jQuery('item', xml).length);
        },
        'xml'
    );
}

function addItems(carousel, xml) {
    var items = jQuery('item', xml);
    carousel.size(items.length);
    items.each(function(i) {
        carousel.add(1 + i, getItemHTML(this, i+1));
    });
    carousel.size(items.length);
}

function jarousel_init(carousel,state) {
    theCarousel = carousel;
}

function getItemHTML(item, index) {
    var st = '<img '
    if (index == 1) {
       st = st + 'class="currentThumb"'
       selectedImageId = jQuery('id', item).text();
       changePhoto(selectedImageId);
    }
    return st + ' src="' + jQuery('src', item).text() + '" width="60" height="60" alt="' + jQuery('title', item).text() + '" title="' + jQuery('title', item).text() + '" onclick="changePhoto(' + jQuery('id', item).text() + ');" id="thumb' + jQuery('id', item).text() + '" />';
};
