	/**
	* 3ma gallery copyright 3ma media. All rights reserved.
	* Version 1.5, 10.01.2012
	* For information contact Michael Dold (m.dold@3ma.de)
	* Gallery needs allow_url_include = on (php.ini setting)
	*/
	
galery = {
	config: {
		namespace:'JSGAL_',
		pictures:[],
		thumb_level_1_height:41,
		thumb_level_1_width:50,
		thumb_level_2_height:49,
		thumb_level_2_width:160,
		thumb_level_3_height:61,
		thumb_level_3_width:75,
		mainpic_width:920,
		mainpic_height:750,
		space_between_thumbs:10,
		show_meta:false,
		showArrows:false,
		hoverMove:true,
		autoSwitch:true,
		autoScroll:true,
		scrollSpeed:10,
		autoScrollInterval:100,
		autoScrollSpeed:1,
		preloadImg:'/sys-data/3ma-gallery/jsgal/ajax-loader.gif',
		pathToReflect:'/sys-data/3ma-gallery/jsgal/reflect4.php',
		showAlt:true,
		showTitle:true,
		hideThumbs:false
	},
	cPos:0,
	cScrolling:0,
	nextStep:false,
	auto:true,
	preloader:[],
	
	/**
	* setting the picturearray from thumbnails
	* metatag = thumb.title
	* bigpicture = thumb.longdesc
	* thumbnail = thumb.src
	* @param string id of thumb-div-container
	* @param string id of largepic-div-container
	* @param string id of meta-div-container
	* @return nothing
	*/
	init: function( thumbid, largepicid, metaid, config ){
	
		if( config )
		{
			for( x in config )
			{
				galery.config[x] = config[x];
			}
		}
		galery.thumbid = thumbid;
		galery.largepicid = largepicid;
		galery.metaid = metaid;
//		p = window.location.protocol+'//'+window.location.host+window.location.pathname;
		p = '';
		for( x = 0; x < $('#'+thumbid+' > img').length; x++ )
		{
			galery.config.pictures[ galery.config.pictures.length ] = {
				'url':$('#'+thumbid+' > img')[x].src.replace(p,''),
				'meta':($('#'+thumbid+' > img')[x].title?$('#'+thumbid+' > img')[x].title:''),
				'alt':($('#'+thumbid+' > img')[x].alt?$('#'+thumbid+' > img')[x].alt:''),
				'bigurl':$('#'+thumbid+' > img')[x].getAttribute('longdesc'),
				'caption':$('#'+thumbid+' > img')[x].getAttribute('caption')
			};
			
		}
		
		$('#'+thumbid).html('');
		galery.completeInit();
		//galery.loadConfig();
		if(galery.config.hideThumbs == true)
		{
		 $('#'+galery.thumbid).css("visibility", "hidden");
		}
	},

	/**
	* sets listeners and scroller-properties and the css-properties
	* @param nothing
	* @return nothing
	*/
	completeInit:function(){
		$('#'+galery.largepicid).append( '<img src="'+galery.config.preloadImg+'" id="'+galery.largepicid+'_container" />' );
		if( galery.config.hoverMove ) galery.config.showArrows = false;
		galery.largepic = $('#'+galery.largepicid+'_container');
		galery.meta = $('#'+galery.metaid);
		galery.meta.hide();
		galery.largepic.css({
			'max-height': galery.config.mainpic_height+'px',
			'max-width': galery.config.mainpic_width+'px',
		});
	
		$('#'+galery.thumbid).css({
			'overflow': 'hidden',
			'white-space': 'nowrap'
		});
		$('#'+galery.thumbid).append( '<div id="'+galery.config.namespace+'fading"></div>' );
		$('#'+galery.config.namespace+'fading').bind( 'mousemove', galery.mousescroll );
		$('#'+galery.config.namespace+'fading').bind( 'mouseout', function(){galery.cScrolling=0;galery.auto=true;} );
		$('#'+galery.config.namespace+'fading').bind( 'mouseenter', function(){galery.auto=false;} );
		$('#'+galery.config.namespace+'fading').bind( 'click', galery.clickedPos );


		if( galery.config.showArrows )
		{
			$('#'+galery.thumbid).append('<div id="'+galery.config.namespace+'leftArrow"></div>');
			$('#'+galery.thumbid).append('<div id="'+galery.config.namespace+'rightArrow"></div>');
			$('#'+galery.config.namespace+'leftArrow').css({
				'position':'absolute',
				'top':'0px',
				'left':'10px',
				'width':'76px',
				'height':'81px',
				'background':'url(/sys-data/3ma-gallery/jsgal/arrow_left.png) left top no-repeat',
				'z-index':6
			});
			$('#'+galery.config.namespace+'leftArrow').bind( 'mousedown', galery.scrollLeft );
			$('#'+galery.config.namespace+'leftArrow').bind( 'mouseup', galery.stopScrolling );
			$('#'+galery.config.namespace+'rightArrow').css({
				'position':'absolute',
				'top':'0px',
				'right':'10px',
				'width':'76px',
				'height':'81px',
				'background':'url(/sys-data/3ma-gallery/jsgal/arrow_right.png) left top no-repeat',
				'z-index':6
			});
			$('#'+galery.config.namespace+'rightArrow').bind( 'mousedown', galery.scrollRight );
			$('#'+galery.config.namespace+'rightArrow').bind( 'mouseup', galery.stopScrolling );
		}
		$('#'+galery.thumbid).append('<div id="'+galery.config.namespace+'scroller"></div>');
		galery.thumbs = $('#'+galery.config.namespace+'scroller');
		
		isIE = ( navigator.userAgent.indexOf(' MSIE') >= 0 ? true : false );
		
		for( x = 0; x < galery.config.pictures.length; x++ )
		{
			galery.preloader[x*2] = new Image();
			galery.preloader[x*2].src = galery.config.pictures[x].url;
			galery.preloader[x*2].id = x*2;
			if( !isIE )
				galery.preloader[x*2].onload = function(){galery.imagesLoaded(this);};
			galery.preloader[x*2+1] = new Image();
			galery.preloader[x*2+1].src = galery.config.pictures[x].bigurl;
			galery.preloader[x*2+1].id = x*2+1;
			if( !isIE )
				galery.preloader[x*2+1].onload = function(){galery.imagesLoaded(this);};
			$('#'+galery.config.namespace+'scroller').append( '<img src="'+galery.config.pathToReflect+'?img='+galery.config.pictures[x].url+'&cache=1" id="'+galery.config.namespace+'pic_'+x+'" />' );
			$('#'+galery.config.namespace+'pic_'+x).css({
				'max-height': galery.config.thumb_level_1_height+'px',
				'max-width': galery.config.thumb_level_1_width+'px',
				'margin-right': galery.config.space_between_thumbs+'px'
			});
			$('#'+galery.config.namespace+'pic_'+x).bind( 'click', galery.click );
			
		}
		if( isIE )
		{
			galery.lasta = 0;
			galery.showPic( 0 );
			galery.scrollTo( 0 );
			galery.setFadingOut();	
			window.setInterval( "galery.scroll()",50 );
			window.setInterval( "galery.autoscroll()",galery.config.autoScrollInterval );
		}
	},
	
	imagesLoaded:function( pic )
	{
		galery.preloader[pic.id].preloaded = true;
		allLoaded = true;
		for( x = 0; x < galery.config.pictures.length; x++ )
		{
			if( !galery.preloader[x*2].preloaded || !galery.preloader[x*2+1].preloaded )
			{
				allLoaded = false;
				break;
			}
		}
		if( allLoaded )
		{
			galery.lasta = 0;
			galery.showPic( 0 );
			galery.scrollTo( 0 );
			galery.setFadingOut();	
			window.setInterval( "galery.scroll()",50 );
			window.setInterval( "galery.autoscroll()",galery.config.autoScrollInterval );
		}
	},
	
	autoscroll:function()
	{
		if( galery.auto && galery.config.autoScroll )
		{
			if( galery.cScrolling != galery.config.autoScrollSpeed && galery.cScrolling != -galery.config.autoScrollSpeed )
			{
				galery.cScrolling = galery.config.autoScrollSpeed;
			}
		}
	},
	/**
	* thumbnail clicked-event
	* @param object element
	* @return nothing
	*/
	click:function( el ){
		galery.auto=false;
		picid = el.target.id;
		picid = picid.replace( galery.config.namespace+'pic_', '' );
		galery.nextStep = function(){galery.scrollTo( picid );}
		galery.scrollTo( picid );
	},
	
	/**
	* resets the thumbnailsizes of the given picture and sets the big viewing of given picture-id
	* @param int id of picture in galery.config.pictures
	* @return nothing
	*/
	showPic:function( picid ){
		picid = parseInt( picid );
		if( picid >= this.config.pictures.length ) return;
		galery.meta.fadeOut( 'slow' );
		galery.largepic.fadeOut( 'slow', function(){
			galery.meta.fadeIn( 'slow' );
			galery.lasta = picid;
			galery.largepic.prop( 'src', galery.config.pictures[ picid ].bigurl );
			if( galery.config.showTitle )
				galery.largepic.prop( 'title', galery.config.pictures[ picid ].meta );
			if( galery.config.showAlt )
				galery.largepic.prop( 'alt', galery.config.pictures[ picid ].alt );
			galery.largepic.prop( 'caption', galery.config.pictures[ picid ].caption );
			for( x = 0; x < galery.config.pictures.length; x++ )
			{
				$('#'+galery.config.namespace+'pic_'+x).css({
					'max-height': galery.config.thumb_level_1_height+'px',
					'max-width': galery.config.thumb_level_1_width+'px',
				});
			}
			$('#'+galery.config.namespace+'pic_'+picid).css({
				'max-height': galery.config.thumb_level_3_height+'px',
				'max-width': galery.config.thumb_level_3_width+'px',
			});
			$('#'+galery.config.namespace+'pic_'+(picid-1)).css({
				'max-height': galery.config.thumb_level_2_height+'px',
				'max-width': galery.config.thumb_level_2_width+'px',
			});
			$('#'+galery.config.namespace+'pic_'+(picid+1)).css({
				'max-height': galery.config.thumb_level_2_height+'px',
				'max-width': galery.config.thumb_level_2_width+'px',
			});
			if( galery.nextStep ) 
			{
				galery.nextStep();
				galery.nextStep = false;
			}
			if( galery.config.show_meta )
			{
				galery.meta.html( galery.config.pictures[picid].meta );
			}
			galery.largepic.fadeIn( 'slow' );
		} );
},

	/**
	* scrollposition to left
	* @param nothing
	* @return nothing
	*/
	scrollLeft:function(){
		galery.auto=false;
		galery.cScrolling = galery.config.scrollSpeed;
	},

	/**
	* scrollposition to right
	* @param nothing
	* @return nothing
	*/
	scrollRight:function(){
		galery.auto=false;
		galery.cScrolling = -galery.config.scrollSpeed;
	},
	
	/**
	* sets the autoscrollspeed to 0
	* @param nothing
	* @return nothing
	*/
	stopScrolling:function(){
		galery.auto=true;
		galery.cScrolling = 0;
	},
	
	/**
	* autoscroll function setted via interval
	* @param nothing
	* @return nothing
	*/
	scroll:function(){
	
		galery.cPos += galery.cScrolling;
		a = galery.centerPic();
		if( a >= galery.config.pictures.length || a < 0 ){ 
			galery.cPos -= galery.cScrolling; 
			if( galery.auto && galery.config.autoScroll )
				galery.cScrolling = galery.cScrolling * -1;
			return; 
		}
		if( galery.lasta != a && galery.config.autoSwitch ) galery.showPic( a );
		galery.lasta = a;
		galery.thumbs.css({ 'margin-left':galery.cPos+'px' });
		galery.setFadingOut();
	},
	
	/**
	* calculating the hovereffects in autoscroll function of thumbnailcontainer
	* @param object mouseelement
	* @return nothing
	*/
	mousescroll:function( e )
	{
	  if(galery.config.hideThumbs == true) { galery.cSCrolling = 0; return; }
		galery.auto=false;
		if( typeof e.layerX == 'undefined' )
			e.layerX = e.offsetX;
		if( galery.config.hoverMove )
		{
			if( e.layerX < 200 )
			{
				galery.cScrolling = 20 - e.layerX / 10;
			}
			else if( e.layerX > $('#'+galery.thumbid).innerWidth() - 200 )
			{
				galery.cScrolling = -20+($('#'+galery.thumbid).innerWidth()-e.layerX)/10;
			}
			else
				galery.cSCrolling = 0;
		}
	},
	
	/**
	* calculating the whole width of thumbnailcontainer
	* @param nothing
	* @return int width
	*/
	scrollerWidth:function()
	{
		w = 0;
		z = $('#'+galery.config.namespace+'scroller > img');
		for( x = 0; x < z.length; x++ )
		{
			w+=z.width();
			w+=galery.config.space_between_thumbs;
		}
		return w;
	},
	
	/**
	* calculate & set the scrollposition of active thumbnail
	* @param int img
	* @return nothing
	*/
	scrollTo:function( img ){
		w = 0;
		z = $('#'+galery.config.namespace+'scroller > img');
		for( x = 0; x < z.length; x++ )
		{
			w+=z.width();
			w+=galery.config.space_between_thumbs;
			if( x == img )
			{
				galery.cPos = $('#'+galery.thumbid).innerWidth()/2-w;
				galery.thumbs.css({ 'margin-left':galery.cPos+'px' });
				break;
			}
		}
		
	},
	
	/**
	* calculates the current picture in center
	* @param nothing
	* @return int x-position of picture
	*/
	centerPic:function()
	{
		w = 0;
		if( galery.cPos > $('#'+galery.thumbid).innerWidth()/2 ) return -1;
		center = galery.cPos*-1 + $('#'+galery.thumbid).innerWidth()/2;
		z = $('#'+galery.config.namespace+'scroller > img');
		for( x = 0; x < z.length; x++ )
		{
			w+=z.width();
			w+=galery.config.space_between_thumbs;
			if( w >= center ) return x;
		}
		return galery.config.pictures.length;
	},
	
	/**
	* calculates the current picture in center
	* @param nothing
	* @return int x-position of picture
	*/
	clickedPos:function( e )
	{
	 if(galery.config.hideThumbs == true) return; 
			galery.auto=false;
			cp = 0;
			w = 0;
			if( typeof e.layerX == 'undefined' )
				e.layerX = e.offsetX;
			e.layerX += $('#'+galery.config.namespace+'fading')[0].offsetLeft;
			z = $('#'+galery.config.namespace+'scroller > img');
			for( x = 0; x < z.length; x++ )
			{
				w = $(z[x]).position();
				if( x + 1 < z.length )
				{
					n = $(z[x+1]).position();
					if( e.layerX > w.left && e.layerX < n.left - galery.config.space_between_thumbs )
					{
						cp = x;
						break;
					}
				}
				else if( e.layerX > w.left )
				{	
					cp = x;
					break;
				}
			}
			if( cp == 0 ) cp = galery.config.pictures.length;
			if( cp >= 0 && cp < galery.config.pictures.length )
			{
				galery.nextStep = function(){galery.scrollTo( cp );}
				galery.showPic( cp );
			}
	},
	
	
	/**
	* calculates the first picture in thumbnailcontainer
	* @param nothing
	* @return int x-position of first picture
	*/
	firstPic:function()
	{
		w = 0;
		center = galery.cPos*-1;
		z = $('#'+galery.config.namespace+'scroller > img');
		for( x = 0; x < z.length; x++ )
		{
			w+=z.width();
			w+=galery.config.space_between_thumbs;
			if( w >= center ) return x;
		}
		return galery.config.pictures.length;
	},
	
	/**
	* calculates the last picture in thumbnailcontainer
	* @param nothing
	* @return int x-position of last picture
	*/
	lastPic:function()
	{
		w = 0;
		center = galery.cPos*-1 + $('#'+galery.thumbid).innerWidth();
		z = $('#'+galery.config.namespace+'scroller > img');
		for( x = 0; x < z.length; x++ )
		{
			w+=z.width();
			w+=galery.config.space_between_thumbs;
			if( w >= center ) return x;
		}
		return galery.config.pictures.length;
	},
	
	/**
	* sets the first and the last picture in thumbnailbar opaque
	* @param nothing
	* @return nothing
	*/
	setFadingOut:function(){
		return;
		for( x = 0; x < galery.config.pictures.length; x++ )
		{
			$('#'+galery.config.namespace+'pic_'+x).css({ opacity: 1 });
		}
		first = galery.firstPic();
		if( first < 0 ) first = 0;
		last = galery.lastPic();
		if( last >= galery.config.pictures.length ) last = galery.config.pictures.length - 1;
		$('#'+galery.config.namespace+'pic_'+first).css({ opacity: 0.2 });
		$('#'+galery.config.namespace+'pic_'+last).css({ opacity: 0.2 });
	
	}

};


