;(function($) {
	var loadingTimer, loadingFrame = 1, loading, imgPreloader = new Image(),imgRegExp = /\.(jpg|gif|png|bmp|jpeg)(.*)?$/i, tmp, content, wrap, start_pos, final_pos, overlay, currentIndex = 0,selectedIndex = 0, selectedOpts = {}, selectedArray = [], currentIndex = 0, currentOpts = {}, currentArray = [],busy = false,fx = $.extend($('<div/>')[0], { prop: 0 }), interval;
	_abort = function() {
			loading.hide();

			imgPreloader.onerror = imgPreloader.onload = null;

			tmp.empty();
		},
	_start = function() {
		var obj = selectedArray[ selectedIndex ],
			href, 
			type, 
			title,
			str,
			emb,
			ret;
		_abort();
		selectedOpts = $.extend({}, $.fn.galerie.defaults, (typeof $(obj).data('galerie') == 'undefined' ? selectedOpts : $(obj).data('galerie')));
		if(interval){
			clearInterval(interval)
		}
		title = selectedOpts.title || (obj.nodeName ? $(obj).attr('title') : obj.title) || '';
		
		href = selectedOpts.href || (obj.nodeName ? $(obj).attr('href') : obj.href) || null;
		busy = false;

		$.galerie.showActivity();

		imgPreloader = new Image();

		imgPreloader.onerror = function() {
			//_error();
		};

		imgPreloader.onload = function() {
			busy = true;

			imgPreloader.onerror = imgPreloader.onload = null;

			_process_image();
		};
		imgPreloader.src = href;
	},
	
	_process_image = function() {
			selectedOpts.width = imgPreloader.width;
			selectedOpts.height = imgPreloader.height;

			$("<img />").attr({
				'id' : 'big-img',
				'src' : imgPreloader.src,
				'alt' : selectedOpts.title,
				'style':'opacity:0;filter:alpha(opacity=0)'
			}).appendTo( tmp );

			_show();
		},
	_show = function() {
			var pos, equal;

			loading.hide();

			busy = true;
			currentArray = selectedArray;
			currentIndex = selectedIndex;
			currentOpts = selectedOpts;

			content.children('img').fadeTo(currentOpts.changeFade, 0, function() {
				content.html(tmp.contents()).children('img').fadeTo(currentOpts.changeFade, 1, _finish);
				$('.photo-active').removeClass('photo-active');
				$(currentArray[currentIndex]).addClass('photo-active')
			});
			//$('#header').fadeTo('slow', 0)
			//content.animate({filter:'alpha(opacity=25)'},800);

				
		},
		_finish = function () {
			if (!$.support.opacity) {
				content.get(0).style.removeAttribute('filter');
				wrap.get(0).style.removeAttribute('filter');
			}

			busy = false;

			_preload_images();
			if(interval){
				interval=setInterval($.galerie.next, selectedOpts.interval);
			}
			
		},

		_preload_images = function() {
			var href, 
				objNext;

			if ((currentArray.length -1) > currentIndex) {
				href = currentArray[ currentIndex + 1 ].href;

				if (typeof href !== 'undefined' && href.match(imgRegExp)) {
					objNext = new Image();
					objNext.src = href;
				}
			}

			if (currentIndex > 0) {
				href = currentArray[ currentIndex - 1 ].href;

				if (typeof href !== 'undefined' && href.match(imgRegExp)) {
					objNext = new Image();
					objNext.src = href;
				}
			}
		},
	_animate_loading = function() {
		if (!loading.is(':visible')){
			clearInterval(loadingTimer);
			return;
		}

		$('div', loading).css('top', (loadingFrame * -40) + 'px');

		loadingFrame = (loadingFrame + 1) % 12;
	};
	
	$.fn.galerie = function(options) {
		
		if (!$(this).length) {
			return this;
		}
		if(options){
			if(options.auto==true){
				$(this).each(function(){
					selectedArray.push(this);	
				})
				
				interval=setInterval($.galerie.next, options.interval);
			}
			if(options.col!=""){
				prev=options.col-1
				$("#thumb-photo a:nth-child("+options.col+"n)").css({'margin':'0 0 7px 0'})
				$("#thumb-photo a:nth-child("+(options.col)+"n+1)").css({'clear':'left'});
			}
			$("#thumb-photo a:nth-child(1)").addClass("photo-active");
			$("#thumb-photo").fadeTo('fast',1);
		}
		$(this)
			.data('galerie', $.extend({}, options, ($.metadata ? $(this).metadata() : {})))
			.unbind('click.fb')
			.bind('click.fb', function(e) {
				e.preventDefault();

				if (busy) {
					return;
				}

				busy = true;

				$(this).blur();

				selectedArray = [];
				selectedIndex = 0;

				var rel = $(this).attr('rel') || '';

				if (!rel || rel == '' || rel === 'nofollow') {
					selectedArray.push(this);

				} else {
					selectedArray = $("a[rel=" + rel + "], area[rel=" + rel + "]");
					selectedIndex = selectedArray.index( this );
				}
				_start();
				return;
			});
		return this;
	};
	$.galerie = function(obj) {
		var opts;
		if (busy) {
			return;
		}

		busy = true;
		opts = typeof arguments[1] !== 'undefined' ? arguments[1] : {};

		selectedArray = [];
		selectedIndex = parseInt(opts.index, 10) || 0;

		if ($.isArray(obj)) {
			for (var i = 0, j = obj.length; i < j; i++) {
				if (typeof obj[i] == 'object') {
					$(obj[i]).data('galerie', $.extend({}, opts, obj[i]));
				} else {
					obj[i] = $({}).data('galerie', $.extend({content : obj[i]}, opts));
				}
			}

			selectedArray = jQuery.merge(selectedArray, obj);

		} else {
			if (typeof obj == 'object') {
				$(obj).data('galerie', $.extend({}, opts, obj));
			} else {
				obj = $({}).data('galerie', $.extend({content : obj}, opts));
			}

			selectedArray.push(obj);
		}

		if (selectedIndex > selectedArray.length || selectedIndex < 0) {
			selectedIndex = 0;
		}
		_start();
	};
	
	$.galerie.showActivity = function() {
		clearInterval(loadingTimer);

		loading.show();
		loadingTimer = setInterval(_animate_loading, 66);
	};
	$.galerie.hideActivity = function() {
		loading.hide();
	};
	
	
	$.galerie.next = function() {
		return $.galerie.pos( currentIndex + 1);
	};

	$.galerie.prev = function() {
		return $.galerie.pos( currentIndex - 1);
	};

	$.galerie.pos = function(pos) {
		if (busy) {
			return;
		}

		pos = parseInt(pos);
		currentArray = selectedArray;
		selectedArray = currentArray;
		//console.log(selectedArray)

		if (pos > -1 && pos < currentArray.length) {
			selectedIndex = pos;
			_start();

		} else if (currentOpts.cyclic && currentArray.length > 1) {
			selectedIndex = pos >= currentArray.length ? 0 : currentArray.length - 1;
			_start();
		}else{
			selectedIndex = 0;
			_start();
		}

		return;
	};	

	
	$.galerie.init = function() {
		if ($("#galerie-wrap").length) {
			return;
		}

		$('#big-photo').append(
			tmp	= $('<div id="galerie-tmp"></div>'),
			loading	= $('<div id="galerie-loading"><div></div></div>'));
		content = $('#galerie-content');
		wrap = $('#big-photo');
	}
	$.fn.galerie.defaults = {
		auto:false,
		interval:5000,
		changeFade : 'slow'
	}
	$(document).ready(function() {
		$.galerie.init();
	})
})(jQuery);
