var thumbnails;
var isBig = false;
var currentBig;
var isTween = false;
var currentEffect;

jQuery.noConflict();

window.addEvent('domready', function() {
	thumbnails = document.getElements('.ngg-gallery-thumbnail');
	if(thumbnails)
	{
	    thumbnails.each(function(item,index)
		{
			item.setStyle('opacity',0);
		});
	}
});

window.addEvent('load', function() {
	if(!thumbnails)
	{
		return 0;
	}

	if(!document.getElement('#galleryContainer'))
	{
		thumbnails.each(function(item,index)
		{
			setTimeout(function(){item.tween('opacity',1);},100*index);
			var img = item.getElement("img");
			var link = item.getElement("a");
			link.set("href",img.get("title"));
			img.set("title","");
		});
		return 0;
	}

	document.getElement('#galleryContainer').setStyle('height',document.getElement('.ngg-galleryoverview').getHeight())

    topLeftBottomRight(1);

    thumbnails.each(function(item,index){

    	item.addEvent("mouseenter",function(){
    		if(!isBig)
    		item.tween("opacity",.5);
    	});

    	item.addEvent("mouseleave",function(){
    		if(!isBig)
    		item.tween("opacity",1);
    	});

    	var aElement = item.getElement('a');

    	aElement.store('path',aElement.get("href"));

    	aElement.set('href','javascript:void(0)');

    	aElement.addEvent("click",function(event){

    		if(isBig || isTween)
    		{
    			return 0;
    		}

    		if(currentEffect)
    		{
    			currentEffect.cancel();
    		}

    		
    		isBig = true;

    		event.stop();



    		var image = new Asset.image(this.retrieve('path'), {
			    id: 'bigImage', 
			    onload: function(){

			    	if(currentBig)
			    	{
			    		currentBig.destroy();
			    		currentBig = null;
			    	}

			    	bottomRightTopLeft(0,bigCallBack);
			    	image.setStyle('opacity',0);
			        image.inject(document.getElement('#galleryContainer'),'bottom');
			        gallery = document.getElement('.ngg-galleryoverview');
			        image.set("width",image.get('width')*1.3);
			        image.set("height",image.get('height')*1.3);
			        var scale = gallery.getWidth()/image.get('width');
			        var resizeEffect = new Fx.Morph(image, {duration: 4000, transition: Fx.Transitions.Sine.easeOut});
 
					resizeEffect.start({
					    'height': image.get('height')*scale,
					    'width': gallery.getWidth(),
					    'opacity':[0,1]
					});

					currentEffect = resizeEffect;

					currentBig = image;

					image.addEvent('click',backToNormal);
			    }
			});

    	});

    });
});

function bigCallBack()
{
	document.getElement('.ngg-galleryoverview').setStyle('opacity',0);
}

function backToNormal()
{
	if(isTween)
	{
		return 0;
	}
	currentBig.set('tween', {duration: 400});
	currentBig.tween('opacity',0);
	document.getElement('.ngg-galleryoverview').setStyle('opacity',1);
	topLeftBottomRight(1);
	setTimeout(turnNormal,4);
}

function turnNormal()
{
	isBig = false;
}

function topLeftBottomRight(opacity,callBack)
{
	isTween = true;
	thumbnails.each(function(item,index)
	{
		setTimeout(function()
		{
			item.fade(opacity);
			if(index == thumbnails.length - 1)
			{
				setTimeout(function(){
						isTween = false;
						if(callBack != null)
						{
							callBack()
						}
				},700);
				
			}
		},50*index);
	});
}

function bottomRightTopLeft(opacity,callBack)
{
	isTween = true;
	thumbnails.reverse().each(function(item,index)
	{
		setTimeout(function()
		{
			item.fade(opacity);
			if(index == thumbnails.length - 1)
			{
				setTimeout(function(){
						isTween = false;
						if(callBack != null)
						{
							callBack()
						}
				},700);
			}
		},50*index);
	});
}
