var currentClient;
var ctext;
var currentMovie, currentSWF;
var curImage;
var max, aStep, aCounter;
var scroll;
var scrollTH;

function init() {

	scroll = new Fx.Scroll('galleryscroll', {
		wait: false,
		duration: 1500,
		offset: {'x': 0, 'y': 0},
		transition: Fx.Transitions.Quad.easeInOut
	});

	$('btn_print').addEvent('click', function(e) {
		e = new Event(e).stop();
		$('leftmover').setStyle( 'visibility','visible' );
		$('rightmover').setStyle( 'visibility','visible' );
		$('topcontent').setStyle( 'background-image','none' ); // suck safari
		$('gallerycontainer').setStyle( 'visibility','visible');
		$('gallery').setStyle( 'width', 5235 );
		var url = "./_includes/print.html";
		sendRequest( url, 'gallery', true );
	});
	$('btn_ci').addEvent('click', function(e) {
		e = new Event(e).stop();
		$('leftmover').setStyle( 'visibility','visible' );
		$('rightmover').setStyle( 'visibility','visible' );
		$('topcontent').setStyle( 'background-image','none' ); // suck safari
		$('gallerycontainer').setStyle( 'visibility','visible');
		$('gallery').setStyle( 'width', 2520 );
		var url = "./_includes/corporate_identity.html";
		sendRequest( url, 'gallery', true );
	});
	$('btn_film').addEvent('click', function(e) {
		e = new Event(e).stop();
		$('leftmover').setStyle( 'visibility','hidden' );
		$('rightmover').setStyle( 'visibility','hidden' );
		$('topcontent').setStyle( 'background-image','none' ); // suck safari
		$('gallerycontainer').setStyle( 'visibility','visible');
		$('gallery').setStyle( 'width', 782 );
		var url = "./_includes/film.html";
		sendRequest( url, 'gallery' );
	});
	$('btn_ambient').addEvent('click', function(e) {
		e = new Event(e).stop();
		$('gallery').setStyle( 'width','100%' );
		$('leftmover').setStyle( 'visibility','visible' );
		$('rightmover').setStyle( 'visibility','visible' );
		$('topcontent').setStyle( 'background-image','none' ); // suck safari
		$('gallerycontainer').setStyle( 'visibility','visible');
		$('gallery').setStyle( 'width', 1223 );
		var url = "./_includes/ambient.html";
		sendRequest( url, 'gallery' );
	});
	$('btn_online').addEvent('click', function(e) {
		e = new Event(e).stop();
		$('leftmover').setStyle( 'visibility','visible' );
		$('rightmover').setStyle( 'visibility','visible' );
		$('topcontent').setStyle( 'background-image','none' ); // suck safari
		$('gallerycontainer').setStyle( 'visibility','visible');
		$('gallery').setStyles( {'width': 1743 } )
		var url = "./_includes/online.html";
		sendRequest( url, 'gallery', true );
	});
	$('btn_produkt').addEvent('click', function(e) {
		e = new Event(e).stop();
		$('leftmover').setStyle( 'visibility','hidden' );
		$('rightmover').setStyle( 'visibility','hidden' );
		$('topcontent').setStyle( 'background-image','none' ); // suck safari
		$('gallerycontainer').setStyle( 'visibility','visible');
		$('gallery').setStyle( 'width', 782 );
		var url = "./_includes/produkt.html";
		sendRequest( url, 'gallery' );
	});
}

function sendRequest( url, responseto, scroller ) {
	if( url == undefined || responseto == undefined ) return;

	var response = $( responseto );
	response.empty();

	new Ajax(url, {
			method: 'get',
			encoding: 'utf-8',
			update: response,
			onSuccess:function() {
				var fx = new Fx.Scroll('galleryscroll', {	wait: false, duration: 0	});
				scroll.toLeft();
				response.setStyles( { 'visibility':'visible'} );
				max = (response.getStyle('width').toInt() / $('galleryscroll').getStyle('width').toInt() ).toInt();
				aStep = 782;
				aCounter = 0;
				initMovers();
			}
	}).request();
}


function initMovers() {
	$('leftmover').removeEvents();
	$('rightmover').removeEvents();

	$('leftmover').addEvent('click', function(e) {
		e = new Event(e).stop();
		++aCounter;
			if( aCounter < 0 ) {
				aCounter = 0;
				return;
			}
			if( aCounter > max ) {
				aCounter = max;
				return;
			}
			var x = aCounter * aStep;
			scroll.scrollTo( x, 0 );
	});

	$('rightmover').addEvent('click', function(e) {
		e = new Event(e).stop();
		--aCounter;
			if( aCounter < 0 ) {
				aCounter = 0;
				return;
			}
			if( aCounter > max ) {
				aCounter = max;
				return;
			}
			var x = aCounter * aStep;
			scroll.scrollTo( x, 0 );
	});

}

var aCounterTH = 0;
var maxTH = 0;
var aStepTH = 260;

function initClientDetailMovers( clientname ) {

	if( ! $('btn_leftmover_th') ) return;

	$('btn_leftmover_th').removeEvents();
	$('btn_rightmover_th').removeEvents();
	maxTH = ($('gallery-th').getStyle('width').toInt() / $('galleryscroll-th').getStyle('width').toInt() ).toInt();
	aCounterTH = 0;

	$('btn_leftmover_th').addEvent('click', function(e) {
		e = new Event(e).stop();
		++aCounterTH;
			if( aCounterTH < 0 ) {
				aCounterTH = 0;
				return;
			}
			if( aCounterTH > maxTH ) {
				aCounterTH = maxTH;
				return;
			}
			var x = aCounterTH * aStepTH;
			scrollTH.scrollTo( x, 0 );
	});

	$('btn_rightmover_th').addEvent('click', function(e) {
		e = new Event(e).stop();
		--aCounterTH;
			if( aCounterTH < 0 ) {
				aCounterTH = 0;
				return;
			}
			if( aCounterTH > maxTH ) {
				aCounter = maxTH;
				return;
			}
			var x = aCounterTH * aStepTH;
			scrollTH.scrollTo( x, 0 );
	});
}

function showClientDetails( clientname ) {
	if( currentClient != clientname ) {
	var path = clientname.split('_');
	$('clientdetails').setStyles( { 'display':'block','opacity':0 } );
	$('clientdetails').empty();
	var url = './_includes/_popups/'+ path[0] + '/' + path[1] + '.html';
	//alert(url);
		new Ajax(url, {
			method: 'get',
			encoding:'UTF-8',
			update: $('clientdetails'),
			onSuccess:function() {
				if( path[0].indexOf( 'film' ) > -1 ) {
					showMovie( path[1] );
				} else {
					curImage = undefined;
					//alert( path[0] + '_' + path[1] + '_01.jpg' );
					changeClientImage( path[0] + '_' + path[1] + '_01.jpg' );
				}
				scrollTH = undefined;
				scrollTH = new Fx.Scroll('galleryscroll-th', {
					wait: false,
					duration: 2000,
					offset: {'x': 0, 'y': 0},
					transition: Fx.Transitions.Quad.easeInOut
				});
				initClientDetailMovers( clientname );
			}
		}).request();
		$('clientdetails').effect('opacity').start(1);
		currentClient = clientname;
	}
}

function closeClientDetails() {
	var c = $('clientdetails');
	if( currentClient.indexOf( 'film' ) > -1 ) {
		$('container-l').empty();
	}
	c.effect('opacity', {
			onComplete:function() {
				c.setStyle( 'display','none');
				c.empty();
			} } ).start(0);
	currentClient = undefined;
	ctext = undefined;
	currentMovie = undefined;
	currentSWF = undefined;
}

function changeClientImage( imagepath, hastext ) {
	var path = imagepath.split( '_' );
	var scope = path[0];
	path.splice( 0, 1 );
	path = path.join( "_" );
	var newimage = './images/ergebnisse/' + scope + '/popup/' + path;
	if( curImage == newimage ) return;
	curImage = newimage;
	currentSWF = undefined;

	var imgContainer = $( 'container-l' );
	imgContainer.empty();
	var loaded = [];
	imgContainer.setStyle( 'background-position','50% 50%' );
	var img = new Asset.images( [newimage],
			{	id: 'c-image',
				onProgress:function( i ) {
					loaded[i] = this;
					this.setStyle( 'opacity', 0);
				},
				onComplete:function() {
					imgContainer.setStyle( 'background-position','-500px -500px' );
					loaded.each( function( image, i ) {
						image.inject( imgContainer );
						image.effect('opacity').start( 1 );
					} );
				}
			}
		);

	if( hastext ) {
		var claim = path.split( '.' );
		claim = claim[0];
		if( ctext != claim ) {
			if( claim.indexOf( '02' ) > -1 ) {
				var tmp = claim.split( "_" );
				tmp = tmp[0] + "_01";
				$( tmp ).setStyle( 'display','none' );
			}
			$( claim ).setStyle( 'display','block' );
			if( ctext ) $( ctext ).setStyle( 'display','none' );
			ctext = claim;
		}
	}
}

function showMovie( movie, hastext ) {
	if( currentMovie == movie ) return;
	$('container-l').empty();
	var quicktime = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="320" height="250">' +
									'<param name="src" value="./media/ergebnisse/film/' + movie + '.mov" />' +
									'<param name="controller" value="true" />' +
									'<param name="autoplay" value="false" />' +
									'<embed src="./media/ergebnisse/film/' + movie + '.mov" width="320" height="256" autoplay="false" controller="true" loop="false" bgcolor="#000000" pluginspage="http://www.apple.com/quicktime/download/" />' +
									'</object>';
	$('container-l').setHTML( quicktime );
	currentMovie = movie;
}

function showFlash( flash, w,h, hastext ) {
	if( currentSWF == flash ) return;
	curImage = undefined;

	var path = flash.split( '_' );
	var scope = path[0];
	path.splice( 0, 1 );
	path = path.join( "_" );

	$('container-l').empty();

	var swf = new SWFObject( "media/ergebnisse/" + scope + "/" + path, "clip", w, h, 8, "#cccccc");
	swf.write( 'container-l' );
	currentSWF = flash;

	if( hastext ) {
		var claim = path.split( '.' );
		claim = claim[0].split( '_' );
		claim = claim[0] + "_" + claim[2];
		if( ctext != claim ) {
			if( claim.indexOf( '02' ) > -1 ) {
				var tmp = claim.split( "_" );
				tmp = tmp[0] + "_01";
				$( tmp ).setStyle( 'display','none' );
			}
			$( claim ).setStyle( 'display','block' );
			if( ctext ) $( ctext ).setStyle( 'display','none' );
			ctext = claim;
		}
	}


}


window.addEvent( 'domready', init );