"use strict";
/*global Reflection */

if (typeof travi !== 'object') {
	var travi = {};
}

travi.gallery = (function () {
	var imagePath, thumbPath, previewPath,
		$preview, $previewContainer,
	
	$previewLoader = $("<img>").load(function () {
	    var $this = $(this);
			
        $preview.attr("src", $this.data('previewSrc')).show()
			.parent().attr('href', imagePath + $this.data('originalSrc'));
		Reflection.add($preview.get(0));
		
		$previewContainer.height('auto').height($previewContainer.height());
		
		$preview.trigger('previewChanged');
    }),
	
	initPreviewPane = function () {
		var $previewLink;
		
		$('div.section').append($('<div id="imageContainer"><a></a></div>'));
		$previewLink = $('#imageContainer > a');
		
		$('<img/>', {
			id: "preview_loading",
			src: "/resources/shared/img/progress/large-loading.gif"
		}).appendTo('#imageContainer');
		
		
		$('<img/>', {
			"class": "preview",
			alt: "preview",
			id: "preview_pos",
			load: function () {
				$('#preview_loading').hide();
			}
		}).appendTo($previewLink);
		$previewLink.lightBox();	
		
		$preview = $('#preview_pos');
		$previewContainer = $('#imageContainer');
	},

	formatThumbnail = function (thumb, preview, original) {
	    return '<a href="' + previewPath + 
						preview + 
						'?original=' + 
						original + '">' +
					'<img src="' + thumbPath + 
						thumb + 
						'" alt="" />' +
				'</a>';
	},

	switchPreviewImage = function () {
	    var imgSrcData = $(this).attr("href"),
			previewSrc = imgSrcData.substring(0, imgSrcData.indexOf('?')),
			originalSrc = imgSrcData.substring(imgSrcData.indexOf('original=') + 9);
			
		$previewContainer.height($previewContainer.height());
        Reflection.remove($preview.get(0));	
		$preview.hide();
		$('#preview_loading').show();
	
	    $previewLoader.attr("src", previewSrc)
			.data('previewSrc', previewSrc)
			.data('originalSrc', originalSrc);
	
	    return false;
	},

	addItems = function (carousel, first, last, json) {
	    // Set the size of the carousel
	    carousel.size(parseInt(json.total, 10));
	
	    $(json.thumbs).each(function (i) {
	        carousel.add(first + i, formatThumbnail(this.filename, this.preview, this.original));
			if (first + i === 1) {
				$('.jcarousel-item-1 a').load(switchPreviewImage).triggerHandler("load");
			}
	    });
	},		

	loadItems = function (carousel, state)
	{
	    // Check if the requested items already exist
	    if (carousel.has(carousel.first, carousel.last)) {
	        return;
	    }
		
		var options = {
			ajax: true,
	        start: carousel.first,
	        last: carousel.last
	    },	
		album = $.url.param("album");
		
		if (album) {
			options.album = album;
		}
	
	    $.getJSON(
	        '?action=getThumbnails',
	        options,
	        function (json) {
	            addItems(carousel, carousel.first, carousel.last, json);
				$('.jcarousel-container').trigger('thumbsAdded');
	        }
	    );
	},
	
	init = function (jQ, image, thumb, preview) {
		imagePath = image || '/gallery/images/';
		thumbPath = thumb || '/gallery/images/thumbs/';
		previewPath = preview || '/gallery/images/previews/';
		
		$('#thumb-carousel').jcarousel({
			visible: 7,
			scroll: 7,
			initCallback: initPreviewPane,
			itemLoadCallback: loadItems,
			itemLastInCallback: function () {
				$('#thumb-carousel').trigger('thumbScrollComplete');
			}
		})
		.delegate(".jcarousel-item a", 'click', switchPreviewImage);	
	};
	
	$(document).ready(init);
	
	return {
		init : init
	};
}());