

// http://flowplayer.org/tools/demos/scrollable/index.html

var zvmgallery = new function () {
	
	var me = this;
	var _divs = {
		thumbs:false,
		single:false
	};
	
	var _objArr = [];
	var _cnt = 0;
	
	this.toString = function() {	return '[object zvmgallery]';	};

	
	// --------------------------------------------------------------------------
	// Grossansicht der Bilder
	
	this.register_single = function ( div ) {
		_divs.single = div;
		
		$(div + ' a.next').click( me.next_image );
		$(div + ' a.prev').click( me.prev_image );
		$(div + ' .single_image').click( me.next_image );
		
		$(div).mouseenter( function () {
			$(div + ' .back_next').fadeIn('fast');
		});

		$(div).mouseleave( function () {
			$(div + ' .back_next').fadeOut('fast');
		});

		$(div + ' a.fullscreen').click( me.fullscreen );
	}
	
	this.next_image = function () {
		_cnt = (_cnt+1)%_objArr.length;
		me.fade_and_swop_single_image( _objArr[_cnt] );
	}
	
	this.prev_image = function () {
		_cnt--;
		if (_cnt < 0) _cnt = _objArr.length-1;
		me.fade_and_swop_single_image( _objArr[_cnt] );
	}
	
	this.fullscreen = function () {
		//alert( _objArr[_cnt].full );
		me.fade_background();
	}
	
	this.update_navigation_buttons = function () {
		
	}
	
	// --------------------------------------------------------------------------
	// Thumbnails

	this.register_thumbs = function ( div ) {
	
		_divs.thumbs = div;
		_objArr = [];
		_cnt = 0;
		
		$( div ).scrollable( { 
			items: '.zvmgallery_items',
			mousewheel: true		
		}).navigator({
			navi: 'ul.zvmgallery_pagenavi'
		});
		
		$.each( $(div + ' .zvmgallery_items a.zoom'), function (key, ref) {
			var obj = {
						ref: 	ref,
						zoom: 	$(ref).attr('href'),
						img:	$(ref).find('img').attr('src'),
						full:	$(ref).next().attr('href'),
						cnt:	key
					};
					
			$(ref).data( obj );
			
			/*
			var str = "";
			for (var i in obj) str += i + " = " + obj[i] + "\n";
			alert( str );
			//*/
			_objArr.push( obj );
		});
		
		$( div + ' .zvmgallery_items a.zoom'  ).unbind();
		
		$( div + ' .zvmgallery_items a.zoom'  ).click( function() {
			me.fade_and_swop_single_image( $(this).data() );
			me.select_current_thumb();
			return false;
		});
		
		me.select_current_thumb();
	}
	
	this.select_current_thumb = function () {
		$(_divs.thumbs + ' .zvmgallery_items a.zoom').fadeTo( 'fast', 1 );
		$(_objArr[_cnt].ref).fadeTo( 'fast', 0.4 );
	}
	
	this.fade_and_swop_single_image = function ( obj ) {
		if (_cnt == obj.cnt) return;
		_cnt = obj.cnt;		
		if (_divs.single) {
			var img = $(_divs.single + ' .single_image img');
			img.fadeOut('fast', function () {
				img.attr('src', obj.zoom );
				img.load( function () {
					img.fadeIn( 'fast' );
				});
			});
		}
	}
	
	// --------------------------------------------------------------------------
	// Fullscreen

	this.inject_code = function () {
	
		_fcnt = _cnt;
		
		$('body').append('<div id="zvmg_overlay"><div class="zvmg_overlay_bg"></div><div class="zvgm_image"><img src="'+_objArr[_fcnt].full+'" /></div></div>');
		
		$('#zvmg_overlay').css({
			height:	$(document).height()
		}).hide().fadeIn( 'slow' );
		
		$('#zvmg_overlay').click( function (e) {
			e.stopPropagation();
			$('#zvmg_overlay').fadeOut( 'fast', function () {
				$('#zvmg_overlay').remove();
				$(window).unbind( 'resize', this.adjust_fullscreen_position );
				$(window).unbind( 'scroll', this.adjust_fullscreen_position );
			});
		});
		
		$('.zvgm_image').click( function ( e ) {
			e.stopPropagation();
			_fcnt++;
			me.fade_and_swop_fullscreen_image( _objArr[_fcnt] );
		});
		
		this.fade_and_swop_fullscreen_image( _objArr[_fcnt] );
		
		this.adjust_fullscreen_position();
		$(window).resize( this.adjust_fullscreen_position );
		$(window).scroll( this.adjust_fullscreen_position );

	}
	
	
	this.adjust_fullscreen_position = function () {
		var t = ($(window).height()-$('.zvgm_image img').height() )/2 + $(window).scrollTop();
		var l = ($(window).width()-$('.zvgm_image img').width() )/2 + $(window).scrollLeft();
		$('.zvgm_image').css({
			top: 	t,
			left:	l
		});
		
	}
	
	this.fade_and_swop_fullscreen_image = function ( obj ) {
		_cnt = obj.cnt;
		if (_divs.single) {
			var img = $('.zvgm_image img');
			img.fadeOut('fast', function () {
				img.attr('src', obj.full );
				img.load( function () {
					img.fadeIn( 'fast' );
					me.adjust_fullscreen_position();
				});
			});
		}
	}
	
	this.fade_background = function () {
		this.inject_code();
	}
	
	
};










