var carouselObj;
var carButtonTextFR = new Array ('Fermer', 'Ouvrir');
var carButtonTextIN = new Array ('Tutup', 'Buka');
var carURIPrefix 	= 'http://www.ccf-bandung.org/gallery_photos/';
var carJSONPath		= 'http://www.ccf-bandung.org/gallery.js';

jQuery(document).ready(function() {
	var carButtonText = (carLanguage=='FR') ? carButtonTextFR : carButtonTextIN;
	var togglerText = carButtonText[0];
	$('#gallery_popup_toggler_button').text(togglerText);
	$('#gallery_popup_toggler_button').click(function() {
		$('.jcarousel-container').toggle();
		if (togglerText == carButtonText[0]) {
			togglerText = carButtonText[1];
			carouselObj.stopAuto();
			carouselObj.lock();
		} else {
			togglerText = carButtonText[0];
			carouselObj.unlock();
			carouselObj.startAuto();
		}
		$('#gallery_popup_toggler_button').text(togglerText);
		return;
	});
	$('#gallery_thumbnails').jcarousel({
		scroll: 1,
		auto: 3,
		wrap: "last",
		itemLoadCallback: galleryCarousel_itemLoadCallback
	});
	$('#gallery_popup_container').show();
});

function galleryCarousel_itemLoadCallback(carousel, state) {
	if(state != 'init')
		return;
		
	$.getJSON(carJSONPath, function(data) {

		var images = new Array(), titles = new Array(), descriptions = new Array();
		$.each(data.items, function (i, item) {
			images.push		 	= item.image;
			titles.push		 	= item.title;
			descriptions.push	= item.date;
		});
		
		$.each(data.items, function (i, item) {
			var gImage_thumb = carURIPrefix + URLEncode(item.image);
			var gImage_large = carURIPrefix + URLEncode(item.image);
			var gImage_title = item.title;
			var gImage_date = item.date;
			var gImage_html  = '<a rel="prettyPhoto[ccf]" title="' + gImage_title + '" href="' + gImage_large + '"><img class="carousel_item_image" alt="' + gImage_date + '" src="' + gImage_thumb + '" /></a>';
			var gImage_item  = $(gImage_html).get(0);

			carousel.add(i+1, gImage_item);
			carousel.size(i+1);
		});
		$("a[rel^='prettyPhoto']").prettyPhoto();
		
		$('.carousel_item_image').each(function() {
			var new_height;
			var new_width;
			
			$(this).height((40 / $(this).width()) * $(this).height());
			$(this).width(40);
			
			new_height			= $(this).height();
			new_width			= $(this).width();
			
			while ($(this).width() < 52 || $(this).height() < 52) {
				if ($(this).width() >= $(this).height()) {
					new_height++;
					new_width 	= (new_height / $(this).height()) * $(this).width();
				} else {
					new_width++;
					new_height	= (new_width / $(this).width()) * $(this).height();
				};
				
				$(this).height(new_height);
				$(this).width(new_width);
			};
			
			$(this).css('left', (($(this).width() - 48) / 2) * -1);
			$(this).css('top', (($(this).height() - 48) / 2) * -1);
		});
	});
	carouselObj = carousel;
}

function URLEncode(clearString) {
	var output = '';
	var x = 0;
	clearString = clearString.toString();
	var regex = /(^[a-zA-Z0-9_.]*)/;
	while (x < clearString.length) {
		var match = regex.exec(clearString.substr(x));
		if (match != null && match.length > 1 && match[1] != '') {
			output += match[1];
			x += match[1].length;
		} else {
			if (clearString[x] == ' ') {
				output += '%20';
			} else {
				var charCode = clearString.charCodeAt(x);
				var hexVal = charCode.toString(16);
				output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
			}
			x++;
		}
	}
	return output;
}