/**
 * Galileory jQ v0.3.2f
 * All-in-box jQuery image gallery
 * http://galileory.sourceforge.net/
 *
 * Copyright (c) 2010 Eugene Tyurin
 * Licensed under the LGPL Version 3 license.
 * http://www.gnu.org/licenses/lgpl.txt
 *
 * Date: June 14, 2010
 * 
 * Release details: 
 * - added flickr plugin
 **/
var view = 0;
var slideshowInterval;
(function($) {
//***************************
$.galileory = {
	options: {
		delay_fade: 300, 
		delay_rollover: 150, 
		delay_slide: 600, 
		thumb_ratio: 3/2, 
		screen_mode: 'window'
	}, 
	layers: {}
};

//***************************
$.fn.galileory = function(options) 
{
	var options = $.extend($.galileory.options, options);  
	
	return this.each(function(){
		options.element = this;
		
		if ($(this).find('.galileory-gallery').length == 0)
			new Gallery(options);
	});
};

//***************************
function Gallery(options)
{
	var gallery = this;
	var layer2;
	
	$.extend(gallery, options);
	
	var el_container = gallery.el_container = gallery.element;
	gallery.images = [];
	gallery.layers = {};
	gallery.midSize = {
		width: $(el_container).width(), 
		height: $(el_container).height()
	};
	
	gallery.processImages(el_container);
	
	// clear gallery canvas
	$(el_container).css('position', 'relative').html('');
	gallery.element = $('<div class="galileory-gallery ui-widget-content" style="border: none;"></div>')
		.width($(gallery.el_container).width())
		.height($(gallery.el_container).height())
		.appendTo(el_container)
		.get(0);
	
	var gallery_width = $(gallery.el_container).width();
	var loader_width = Math.floor(gallery_width / 3);
	gallery.el_loader = $('<div class="galileory-loader"></div>')
		.css({
			width: loader_width, 
			height: '20', 
			left: Math.floor((gallery_width - loader_width) / 2), 
			top: Math.floor($(gallery.el_container).height() / 2) - 10
		})
		.appendTo(gallery.element)
		.progressbar()
		.get(0);
	
	$(gallery).bind('images_loaded', function() {
		$(gallery.el_loader).hide();
		gallery.build();
	
		// start viewing
		$(gallery.element).find('.fullScreen').addClass('visible');
		gallery.showLayer('full');
		view = 0;
	});
};

//***************************
Gallery.prototype.processImages = function (element)
{
	var gallery = this;
	
	gallery.image_count = $(element).find('img').length;
	var loaded_images = 0;
	$.each($(element).find('img'), function(ind) {
		var image = new Image;
		
		image.src = $(this).attr('src');
		image.src2 = $(this).attr('src');
		image.srcLarge = $(this).attr('srcLarge');
		image.exif = $(this).attr('exif');
		image.onload = function() {
			loaded_images++;
			gallery.updateLoader(loaded_images);
			
			if (gallery.image_count == loaded_images) 
				$(gallery).trigger('images_loaded');
		};
		
		gallery.images.push(image);
	});
};

//***************************
Gallery.prototype.updateLoader = function(loaded_images_count)
{
	var gallery = this;
	var process_value = Math.floor(100 / gallery.image_count * loaded_images_count);
	
	$(gallery.el_loader).progressbar("value", process_value);
};

//***************************
Gallery.prototype.build = function ()
{
	var gallery = this;
	
	// add layers
	for (var i in $.galileory.layers)
		gallery.layers[i] = $.galileory.layers[i](gallery);
	
	// add control: full screen
	gallery.addControl({
		name: 'fullScreen', 
		label: 'Full screen', 
		icon: 'ui-icon-arrow-4-diag', 
		position: {
			right: '10px',
			top: '10px'
		}, 
		click: function() {
			$("html").css('overflow', "hidden");
			$(gallery.element)
				.appendTo(document.body)
				.width($(window).width())
				.height($(window).height())
				.css({position: 'fixed'});
			gallery.updateSizes(gallery);
			$(this).removeClass('visible').hide();
			$(gallery.element).find('.winScreen').addClass('visible').show();
			gallery.screen_mode = 'full';			
		}
	});
	
	// add control: link to image
	gallery.addControl({
		name: 'imgLink', 
		label: 'Link', 
		icon: 'ui-icon-image', 
		position: {
			right: '100px',
			top: '10px'
		}, 
		click: function() {			
			var ind = $(gallery.layers.full.element).find('.current').index();			
			var srcLarge = $('#img_'+ind).attr('srcLarge');
			window.open(srcLarge);			
		}
	});
	
	// add control: EXIF data
	gallery.addControl({
		name: 'exif', 
		label: 'EXIF', 
		icon: 'ui-icon-note', 
		position: {
			right: '130px',
			top: '10px'
		}, 
		click: function() {			
			var ind = $(gallery.layers.full.element).find('.current').index();						
			var exif = $('#img_'+ind).attr('exif');
			$.prompt(exif,{ 
				buttons:{},
				callback: function(){																		
				}
			});
		}
	});		
	
	// add control: slideshow
	gallery.addControl({
		name: 'slideshow', 
		label: 'Slideshow', 
		icon: 'ui-icon-play', 
		position: {
			right: '70px',
			top: '10px'
		}, 
		click: function() {
			$(gallery.element).find('.slideshow').removeClass('visible').hide();
			$(gallery.element).find('.slideshowPause').addClass('visible').show();
			layer2.showNext(gallery.delay_fade);
			slideshowInterval = setInterval(function() { layer2.showNext(gallery.delay_fade); },3000);	
		}
	});
	
	// add control: slideshow stop
	gallery.addControl({
		name: 'slideshowPause', 
		label: 'Pause slideshow', 
		icon: 'ui-icon-pause', 
		position: {
			right: '70px',
			top: '10px'
		}, 
		click: function() {
			$(gallery.element).find('.slideshowPause').removeClass('visible').hide();
			$(gallery.element).find('.slideshow').addClass('visible').show();			
			clearInterval(slideshowInterval);
		}
	});
	
	// add next image button
	gallery.addControl({
		name: 'imgNext', 
		label: 'Next', 
		icon: 'ui-icon-circle-triangle-e', 
		position: {
			right: '10px',
			top: Math.floor($(gallery.element).height() / 2) - 12 + 'px'
		}, 
		click: function() {
			layer2.showNext(gallery.delay_fade);
		}
	});	
	
	// add previous image button
	gallery.addControl({
		name: 'imgPrev', 
		label: 'Previous', 
		icon: 'ui-icon-circle-triangle-w', 
		position: {
			left: '10px',
			top: Math.floor($(gallery.element).height() / 2) - 12 + 'px'
		}, 
		click: function() {
			layer2.showPrev(gallery.delay_fade);					
		}
	});
	
	$(gallery.element).find('.imgNext').addClass('visible').show();
	$(gallery.element).find('.imgPrev').addClass('visible').show();
	$(gallery.element).find('.imgLink').addClass('visible').show();
	$(gallery.element).find('.exif').addClass('visible').show();
	$(gallery.element).find('.slideshow').addClass('visible').show();
	
	// add control: window screen
	gallery.addControl({
		name: 'winScreen', 
		label: 'Window screen', 
		icon: 'ui-icon-newwin', 
		position: {
			right: '10px',
			top: '10px'
		}, 
		click: function() {
			$("html").css('overflow', "visible");
			$(gallery.element)
				.appendTo(gallery.el_container)
				.width(gallery.midSize.width)
				.height(gallery.midSize.height)
				.css({position: 'absolute'});
			gallery.updateSizes(gallery);
			$(this).removeClass('visible').hide();
			$(gallery.element).find('.fullScreen').addClass('visible').show();
			gallery.screen_mode = 'window';
		}
	});
	
	// init control visibility
	gallery.event_helper = {};
	gallery.event_helper.mouseInbound = false;
	
	// controls visible
	$(gallery.element).mouseover(function() {
		gallery.event_helper.mouseInbound = true;
		$(gallery.element).find(".control.visible").fadeIn(gallery.delay_rollover);
	});
	
	// controls hidden
	$(gallery.element).mouseleave(function() {
		gallery.event_helper.mouseInbound = false;
		
		setTimeout(function() {
			if (!gallery.event_helper.mouseInbound) {
				$(gallery.element).find(".control.visible").fadeOut(gallery.delay_rollover);
			}
		}, 2000);
	});
	
	// window resize
	$(window).resize(function(){
		if (gallery.screen_mode == 'full') {
			$(gallery.element)
				.width($(window).width())
				.height($(window).height());
			gallery.updateSizes(gallery);
		}
	});
	
	$(document).keydown(function (e){
		if (e.keyCode == 37) { 
		   	if(view)
		   		gallery.layers.thumb.rollLeft(gallery);
			else
				layer2.showPrev(gallery.delay_fade);
			return false;
		}
		else if (e.keyCode == 39) { 
		   if(view)
		   		gallery.layers.thumb.rollRight(gallery);
			else
				layer2.showNext(gallery.delay_fade);
		   return false;
		}
	});
	
};

//***************************
Gallery.prototype.updateSizes = function ()
{
	var gallery = this;
	
	for (var i in gallery.layers) {
		if (typeof gallery.layers[i].updateSize == 'function')
			gallery.layers[i].updateSize(gallery);
			
		if (typeof gallery.layers[i].updateControlPosition == 'function')
			gallery.layers[i].updateControlPosition(gallery);
	}
};

//***************************
Gallery.prototype.addControl = function (options)
{
	var gallery = this;
	
	var jq_control = $('<button class="control"></button>')
	.addClass(options.name)
	.html(options.label)
	.appendTo(gallery.element)
	.button({
		icons: {primary: options.icon},
		text: false
	})
	.css({
		position: 'absolute', 
		zIndex: 100000, 
		display: 'none'
	})
	.click(options.click);
	
	for (var i in options.position)
		jq_control.css(i, options.position[i]);
};

//***************************
Gallery.prototype.showLayer = function(layer_name)
{
	var gallery = this;
	
	if (gallery.active_layer != undefined) 
		gallery.active_layer.hide();
		
	gallery.active_layer = gallery.layers[layer_name];
	gallery.active_layer.show();
	//$(gallery.element).find('.control').hide();
};

//************************ Full Layer *******************************
$.galileory.layers.full = function(gallery) {
	return new LayerFull(gallery);
};

//***************************
function LayerFull(gallery)
{
	var layer = this;
	layer2 = layer;
	layer.gallery = gallery;
	
	layer.element = $('<div class="galileory-layer full"></div>')
		.appendTo(gallery.element)
		.hide()
		.get(0);
	
	// add images
	var i = 0;
	$.each(gallery.images, function(ind) {
		$(layer.element).append('<div class="imageWrap"><img id="img_'+i+'" src="'+gallery.images[ind].src2+'" srcLarge="'+gallery.images[ind].srcLarge+'" exif="'+gallery.images[ind].exif+'" /></div>');
		i++;
	});
	
	
	layer.updateSize(gallery);
	
	$(layer.element).find('.imageWrap').click(function() {
		layer.showNext(gallery.delay_fade);
	});
	
	layer.setCurrent(0);
};

//***************************
LayerFull.prototype.show = function()
{
	var layer = this;
	var gallery = layer.gallery;
	
	$(layer.element).show().find('.imageWrap').hide();
	$(layer.element).find(".current").fadeIn(gallery.delay_fade);
	$(gallery.element).find('.control-viewFull').addClass('visible');
	
	return this;
};

//***************************
LayerFull.prototype.hide = function()
{
	var layer = this;
	var gallery = layer.gallery;
	
	//$(layer.element).fadeIn(gallery.delay_fade);
	$(layer.element).hide();
	$(gallery.element).find('.control-viewFull').removeClass('visible');
	
	return this;
};

//***************************
LayerFull.prototype.setCurrent = function(ind)
{
	var layer = this;
	var gallery = this.gallery;
	
	$(layer.element)
		.find(".imageWrap")
		.removeClass('current')
		.eq(ind)
		.addClass('current')
		.fadeIn(gallery.delay_fade);
	
	return this;
};

//***************************
LayerFull.prototype.showNext = function(delay)
{
	var el_layer = this.element;
	$(el_layer).find('*:animated').stop(false, true);
	if ($(el_layer).find('.imageWrap.current + .imageWrap').length) {
		$(el_layer).find('.imageWrap.current + .imageWrap').addClass('current').fadeIn(delay);
		$(el_layer).find('.imageWrap.current:first').removeClass('current').fadeOut(delay);
	} else {
		$(el_layer).find('.imageWrap:first').addClass('current').fadeIn(delay);
		$(el_layer).find('.imageWrap.current:last').removeClass('current').fadeOut(delay);
	}
};

//***************************
LayerFull.prototype.showPrev = function(delay)
{
	var el_layer = this.element;
	
	$(el_layer).find('*:animated').stop(false, true);

	var ind = $(el_layer).find('.imageWrap.current:first').index();

	$(el_layer).find('.imageWrap.current:first').removeClass('current').fadeOut(delay);
	layer2.setCurrent(ind - 1);

};

//***************************
LayerFull.prototype.updateSize = function ()
{
	var layer = this;
	var gallery = this.gallery;
	
	var full_width = $(gallery.element).width();
	var full_height = $(gallery.element).height();
	var full_ratio = full_width / full_height;
	
	$(layer.element)
		.width(full_width)
			.height(full_height);
		
	$.each($(layer.element).find('.imageWrap'), function(ind) {
		var el_image_wrap = this;
		
		var wrap_width = image_width = full_width;
		var wrap_height = image_height = full_height;
		var image_left = 0;
		var image_top = 0;
		var image_ratio = gallery.images[ind].width / gallery.images[ind].height;
		
		if (image_ratio > full_ratio) {
			image_height = Math.ceil(image_width / image_ratio);
			image_top = Math.floor((full_height - image_height) / 2);
		} else if (image_ratio < full_ratio) {
			image_width = Math.ceil(full_height * image_ratio);
			image_left = Math.floor((full_width - image_width) / 2);
		}
		
		$(el_image_wrap)
			.css({
				width: wrap_width+'px', 
				height: wrap_height+'px'
			})
				.find('img')
					.css({
						width: image_width+'px', 
						height: image_height+'px', 
						left: image_left+'px', 
						top: image_top+'px'
					});
		
		//$.updateImageLoad(gallery.images[ind]);
	});
};

//************************ List Layer *******************************
$.galileory.layers.thumb = function(gallery) {
	return new LayerThumb(gallery);
};

//***************************
function LayerThumb(gallery)
{
	var layer = this;
	layer.gallery = gallery;
	
	var el_layer_thumb = $('<div class="galileory-layer thumb"></div>')
		.appendTo(gallery.element)
			.hide()
				.get(0);
	layer.element = el_layer_thumb;
	
	// add background to Thumb layer
	layer.background = {};
	layer.background.element = $('<div class="bg"><img /><div class="shader"></div></div>')
		.appendTo(el_layer_thumb)
				.get(0);
	layer.background.image = gallery.images[0];
	
	// add Thumb wrap
	var el_roll_wrap = $('<div class="rollWrap"></div>')
		.appendTo(el_layer_thumb)
			.get(0);
			
	// add Thumb roll
	var el_roll_thumb = $('<div class="roll"></div>')
		.appendTo(el_roll_wrap)
			.get(0);
	
	// add images to roll
	$.each(gallery.images, function(ind) {
		$('<div class="imageWrap"><img src="'+this.src2+'" /><div class="shader"></div></div>')
			.appendTo(el_roll_thumb);
	});
	
	// clone roll for scrolling
	if (gallery.images.length > 21) {
		$(el_roll_thumb).clone().appendTo(el_roll_wrap);
		//$(el_roll_thumb).clone().appendTo(el_roll_wrap);
	}
	
	// update sizes
	layer.updateSize(gallery);
	
	// add list view button
	gallery.addControl({
		name: 'viewList', 
		label: 'List view', 
		icon: 'ui-icon-calculator', 
		position: {
			right: '40px',
			top: '10px'
		}, 
		click: function() {
			$(gallery.layers.full.element).stop(false, true);
			var ind = $(gallery.layers.full.element).find('.current').index();
			layer.updateBg(gallery.images[ind]);						
			
			gallery.showLayer('thumb');
			
			$(gallery.element).find('.imgNext').removeClass('visible').hide();	
			$(gallery.element).find('.imgPrev').removeClass('visible').hide();
			
			view = 1;
			
			$(gallery.element).find('.slideshowPause').removeClass('visible').hide();
			$(gallery.element).find('.slideshow').addClass('visible').show();			
			clearInterval(slideshowInterval);
		}
	});
	
	if (gallery.images.length > 21) {
		// add right scroll button
		gallery.addControl({
			name: 'rollRight', 
			label: 'Scroll right', 
			icon: 'ui-icon-circle-triangle-e', 
			position: {
				right: '10px',
				top: Math.floor($(gallery.element).height() / 2) - 12 + 'px'
			}, 
			click: function() {
				gallery.layers.thumb.rollRight(gallery);
			}
		});
		
		// add left scroll button
		gallery.addControl({
			name: 'rollLeft', 
			label: 'Scroll left', 
			icon: 'ui-icon-circle-triangle-w', 
			position: {
				left: '10px',
				top: Math.floor($(gallery.element).height() / 2) - 12 + 'px'
			}, 
			click: function() {
				gallery.layers.thumb.rollLeft(gallery);
			}
		});
	}
	
	$(gallery.element).find('.viewList').addClass('control-viewFull');
	$(gallery.element).find('.rollRight, .rollLeft').addClass('control-viewList');
	
	// handle events
	$(el_layer_thumb).find('.roll .imageWrap').click(function() {
		gallery.layers.full.setCurrent($(this).index());
		gallery.showLayer('full');
		$(gallery.element).find('.imgNext').addClass('visible').show();	
		$(gallery.element).find('.imgPrev').addClass('visible').show();
		view = 0;
	});
	
	$(el_layer_thumb).find('.bg').click(function() {
		$('.control').hide().filter('.forFull, .forAll').show();
		gallery.showLayer('full');
		$(gallery.element).find('.imgNext').addClass('visible').show();	
		$(gallery.element).find('.imgPrev').addClass('visible').show();		
		view = 0;
	});
	
	$(el_layer_thumb).find('.roll .imageWrap').hover(function() {
		$(this).find('.shader').stop(false, true).hide();
	}, function() {
		$(this).find('.shader').fadeIn(gallery.delay_rollover);
	});
};

//***************************
LayerThumb.prototype.show = function()
{
	var layer = this;
	var gallery = this.gallery;
	
	$(layer.element)
		.show()
		.find('.rollWrap')
		.fadeIn(gallery.delay_fade);
	$(gallery.element).find('.control-viewList').addClass('visible');
};

//***************************
LayerThumb.prototype.hide = function()
{
	var layer = this;
	var gallery = this.gallery;
	
	$(layer.element).hide();
	$(gallery.element).find('.control-viewList').removeClass('visible');
};

//***************************
LayerThumb.prototype.updateBg = function(image)
{
	this.background.image = image;
	$(this.background.element).find('img').attr('src', image.src2);
	this.updateBgSize();
};

//***************************
LayerThumb.prototype.updateBgSize = function()
{
	var layer = this;
	var image = layer.background.image;
	var el_layer = layer.element;
	
	var layer_ratio = $(el_layer).width() / $(el_layer).height();
	var image_ratio = image.width / image.height;
	
	var image_width = $(el_layer).width() * 2;
	var image_height = $(el_layer).height() * 2;
	var image_left = Math.ceil($(el_layer).width() / -2);
	var image_top = Math.ceil($(el_layer).height() / -2);
	
	if (image_ratio > layer_ratio) {
		image_width = Math.ceil(image_height * image_ratio);
		image_left = Math.ceil(($(el_layer).width() - image_width) / 2);
		
	} else if (image_ratio < layer_ratio) {
		image_height = Math.ceil(image_width / image_ratio);
		image_top = Math.ceil(($(el_layer).height() - image_height) / 2);
	}
	
	$(layer.background.element).find('img').css({
		width: image_width, 
		height: image_height, 
		left: image_left, 
		top: image_top, 
		position: 'absolute', 
		visibility: 'visible'
	});
	
	$(layer.background.element).find('.shader').css({
		width: $(el_layer).width(), 
		height: $(el_layer).height()
	});
};

//***************************
LayerThumb.prototype.updateSize = function (gallery)
{
	var layer = this;
	var el_gallery = gallery.element;
	var el_layer_thumb = layer.element;
	
	// update layer size
	$(layer.element)
		.width($(gallery.element).width())
			.height($(gallery.element).height());
	
	// update background size
	$(layer.background.element)
		.width($(gallery.element).width())
			.height($(gallery.element).height());
			
	layer.updateBgSize();
	
	// resize rolls
	var thumb_ratio = gallery.thumb_ratio;
	var wrap_width = Math.floor($(layer.element).width() / 6);
	var wrap_height = Math.floor(wrap_width / thumb_ratio);
	
	//set roll position
	//var roll_left = Math.floor(($(layer.element).width() - Math.ceil(gallery.images.length / 3) * wrap_width) / 2);
	var roll_left = wrap_width * (Math.floor((24 - gallery.images.length) / 3) / 2);
	if (gallery.images.length > 21) {
		roll_left = Math.floor(wrap_width / 2);
	}
	
	$(layer.element)
		.find('.rollWrap')
			.css({
				left: roll_left + 'px', 
				top: Math.floor(($(layer.element).height() - wrap_height * 3) / 2) + 'px'
			});
	
	$.each($(layer.element).find('.roll'), function(ind) {
		var el_roll = this;
		
		//set roll position
		if (ind == 0) 
			var roll_left = 0;
		else if (ind == 1) 
			var roll_left = wrap_width * (Math.ceil(gallery.images.length / 3) + 1);
		var roll_top = Math.floor(($(layer.element).height() - wrap_height * 3) / 2);
		$(el_roll).css({left: roll_left+'px', top: 0});
	});
	
	// resize images
	$.each($(layer.element).find(".roll .imageWrap"), function(ind) {
		ind = $(this).index();
		var el_image_wrap = this;
		var image = gallery.images[ind];
		
		var wrap_left = (wrap_width + 0) * Math.floor(ind / 3);
		var wrap_top = (wrap_height + 0) * (ind - (Math.floor(ind / 3) * 3));
		
		var image_ratio = image.width / image.height;
		var image_width = wrap_width;
		var image_height = wrap_height;
		var image_left = 0;
		var image_top = 0;
		
		if (image_ratio > thumb_ratio) {
			image_width = Math.ceil(image_height * image_ratio);
			image_left = Math.ceil((wrap_width - image_width) / 2);
			
		} else if (image_ratio < thumb_ratio) {
			image_height = Math.ceil(image_width / image_ratio);
			image_top = Math.ceil((wrap_height - image_height) / 2);
		}
		
		$(el_image_wrap)
			.css({
				width: wrap_width+'px', 
				height: wrap_height+'px', 
				left: wrap_left+'px', 
				top: wrap_top+'px'
			})
				.find('img')
					.css({
						width: image_width+'px', 
						height: image_height+'px', 
						left: image_left+'px', 
						top: image_top+'px'
					});
					
		$(el_image_wrap)
			.find('.shader')
				.css({
					width: wrap_width+'px', 
					height: wrap_height+'px' 
				});
	});
};

//***************************
LayerThumb.prototype.updateControlPosition = function(gallery)
{
	$(gallery.element).find('.rollRight, .rollLeft').css('top', Math.floor($(gallery.element).height()/2) - 12 + 'px');
};

//***************************
LayerThumb.prototype.rollRight = function(gallery)
{
	var el_layer = this.element;
	var el_roll_wrap = $(el_layer).find('.rollWrap').get(0);
	var thumb_width = $(el_layer).find('.imageWrap').width();
	
	$(el_layer).find('*:animated').stop(false, true);
	
	var new_left = $(el_roll_wrap).position().left - Math.floor($(el_layer).width() / 2);
	
	// put roll wrap to correct position
	if ((thumb_width * Math.ceil(gallery.images.length / 3)) < (new_left * -1)) {
		$(el_roll_wrap).css({
			left: $(el_roll_wrap).position().left + (thumb_width * Math.ceil(gallery.images.length / 3)) + 'px'
		});
		
		new_left = $(el_roll_wrap).position().left - Math.floor($(el_layer).width() / 2);
	}
	
	// animate roll wrap
	$(el_roll_wrap).animate({left: new_left + 'px'}, gallery.delay_slide, 'easeOutExpo');
};

//***************************
LayerThumb.prototype.rollLeft = function(gallery)
{
	var el_layer = this.element;
	var el_roll_wrap = $(el_layer).find('.rollWrap').get(0);
	var thumb_width = $(el_layer).find('.imageWrap').width();
	
	$(el_layer).find('*:animated').stop(false, true);
	
	var new_left = $(el_roll_wrap).position().left + Math.floor($(el_layer).width() / 2);
	
	// put roll wrap to correct position
	if (new_left > 0) {
		$(el_roll_wrap).css({
			left: $(el_roll_wrap).position().left - (thumb_width * Math.ceil(gallery.images.length / 3)) + 'px'
		});
		
		new_left = $(el_roll_wrap).position().left + Math.floor($(el_layer).width() / 2);
	}
	
	// animate roll wrap
	$(el_roll_wrap).animate({left: new_left + 'px'}, gallery.delay_slide, 'easeOutExpo');
};

//***************************
$(function(){$(".galileory").galileory();});

})(jQuery);




/**
 * Galileory Flickr v0.2.1
 * Galileory jQ integration to Flickr
 * http://galileory.sourceforge.net/
 *
 * Copyright (c) 2010 Eugene Tyurin
 * Licensed under the LGPL Version 3 license.
 * http://www.gnu.org/licenses/lgpl.txt
 *
 * Date: June 14, 2010
 **/

(function($) {
//***************************
$.galileoryFlickr = {
	options: {
		api_key: 'd13361c02d7fc83f9138a8e2f755693d',
		size: 'medium',
		limit: 100
	}, 
	elements: {}, 
	sizes: {
		square: 's',
		thumbnail: 't',
		small: 'm',
		large: 'b',
		original: 'o'
	},
	
	//***************************
	processPhotoset: function(param) {
		var images_urls = [];
		
		for (var i in param.photoset.photo) {
			var p = param.photoset.photo[i];
			var url_size = $.galileoryFlickr.sizes[$.galileoryFlickr.options.size] != undefined ? '_'+$.galileoryFlickr.sizes[$.galileoryFlickr.options.size] : '';
			images_urls.push('http://farm'+p.farm+'.static.flickr.com/'+p.server+'/'+p.id+'_'+p.secret+url_size+'.jpg');
		};
		
		$.galileoryFlickr.addImages(images_urls);
	}, 
	
	//***************************
	processGallery: function(param) {
		if (param.stat != 'ok')
			return;
			
		var options = $.galileoryFlickr.options;
		if (param.gallery != undefined) {
			$.galileoryFlickr.flickrRequest({
				method: 'flickr.galleries.getPhotos',
				api_key: $.galileoryFlickr.options.api_key,
				gallery_id: param.gallery.id,
				per_page: options.limit,
				format: 'json',
				jsoncallback: 'jQuery.galileoryFlickr.processGallery'
			});
			return;
		}
		
		var images_urls = [];
		for (var i in param.photos.photo) {
			var p = param.photos.photo[i];
			var url_size = $.galileoryFlickr.sizes[options.size] != undefined ? '_'+$.galileoryFlickr.sizes[options.size] : '';
			images_urls.push('http://farm'+p.farm+'.static.flickr.com/'+p.server+'/'+p.id+'_'+p.secret+url_size+'.jpg');
		};
		
		$.galileoryFlickr.addImages(images_urls);
	}, 
	
	//***************************
	processPhotostream: function(param) {
		if (param.stat != 'ok')
			return;
			
		var options = $.galileoryFlickr.options;
		if (param.user != undefined) {
			$.galileoryFlickr.flickrRequest({
				method: 'flickr.people.getPublicPhotos',
				api_key: options.api_key,
				user_id: param.user.id,
				per_page: options.limit,
				format: 'json',
				jsoncallback: 'jQuery.galileoryFlickr.processPhotostream'
			});
			return;
		}
		
		var images_urls = [];
		for (var i in param.photos.photo) {
			var p = param.photos.photo[i];
			var url_size = $.galileoryFlickr.sizes[options.size] != undefined ? '_'+$.galileoryFlickr.sizes[options.size] : '';
			images_urls.push('http://farm'+p.farm+'.static.flickr.com/'+p.server+'/'+p.id+'_'+p.secret+url_size+'.jpg');
		};
		
		$.galileoryFlickr.addImages(images_urls);
	},
	
	//***************************
	flickrRequest: function(request_data) {
		$.ajax({
			url: 'http://api.flickr.com/services/rest/',
			data: request_data,
			dataType: 'jsonp'
		});
	},
	
	//***************************
	addImages: function(images_urls) {
		var images_html = '<img src="'+images_urls.join('"/><img src="')+'"/>';
		$.galileoryFlickr.elements.each(function(){
			$(this).html(images_html).galileory();
		});
	}
};

//***************************
$.fn.galileoryFlickr = function(url, options) 
{
	$.galileoryFlickr.elements = this;
	var options = $.galileoryFlickr.options = $.extend($.galileoryFlickr.options, options);
	var request_data = {format: 'json'};
	
	// photoset
	if (/sets/.test(url) != false) {
		var param = url.split('/');
		var photoset_id = '';
		while(photoset_id.length == 0) photoset_id = param.pop();
		
		request_data.method = 'flickr.photosets.getPhotos';
		request_data.api_key = options.api_key;
		request_data.photoset_id = photoset_id;
		request_data.per_page = options.limit;
		request_data.jsoncallback = 'jQuery.galileoryFlickr.processPhotoset';
	
	// gallery
	} else if (/galleries/.test(url) != false) {
		request_data.method = 'flickr.urls.lookupGallery';
		request_data.api_key = options.api_key;
		request_data.url = url;
		request_data.jsoncallback = 'jQuery.galileoryFlickr.processGallery';
	
	// photostream
	} else {
		request_data.method = 'flickr.urls.lookupUser';
		request_data.api_key = options.api_key;
		request_data.url = url;
		request_data.jsoncallback = 'jQuery.galileoryFlickr.processPhotostream';
	};
	
	$.galileoryFlickr.flickrRequest(request_data);
	
	return this;
};

})(jQuery);

function testResponse(param) {$('#canvas').html(debug(param, 10));};
