/**
 *
 * ADXXL Gallery v0.4
 * Copyright (c) 2009 Andreas Levers
 * AF-FIX Werbegesellschaft mbH
 * Verwendet prototype.js und script.aculo.us
 * Inspiriert von Lokesh Dhakar's lightbox.js
 *
 * Gestestet mit MSIE 7, MSIE 6, Firefox 2, 
 * Layoutprobleme im MSIE 5.5
 **/

var ADXXLGallery = Class.create();
var initSlideshowTimeout = null;

ADXXLGallery.prototype = {

	initialize: function () {
		activeImage 	= 0;
		$A($('thumblist').getElementsByTagName('A')).each( function ( thumbLink ) {
			preloaded = (imageArray.length == 0);
			imageArray.push(
				new Array(
					thumbLink.getAttribute('href'),
					thumbLink.getAttribute('title'),
					preloaded
				)
			);
			Event.observe(thumbLink, 'click', function ( thumbClick ) {
				linkClicked = Event.findElement(thumbClick, 'A');
				gallery.show(linkClicked);
				Event.stop(thumbClick);
				return false;
			}, true);
		});
		Event.observe($('tbprev'),  'click', function( theClick ) { gallery.loadImage(-1); gallery.stopSlideshow(); }, true);
		Event.observe($('imgprev'), 'click', function( theClick ) { gallery.loadImage(-1); gallery.stopSlideshow(); }, true);
		Event.observe($('tbnext'),  'click', function( theClick ) { gallery.loadImage( 1); gallery.stopSlideshow(); }, true);
		Event.observe($('imgnext'), 'click', function( theClick ) { gallery.loadImage( 1); gallery.stopSlideshow(); }, true);
		Event.observe($('tbshow'),  'click', function( theClick ) { gallery.toggleSlideshow( theClick ); }, true);
		Event.observe($('tbsave'),  'click', function( theClick ) { gallery.stopSlideshow(); gallery.saveCurrent( theClick ); }, true);
		Event.observe($('tbsend'),  'click', function( theClick ) { gallery.stopSlideshow(); gallery.showRecommend( theClick ); }, true);
		Event.observe($('tblink'),  'click', function( theClick ) { gallery.stopSlideshow(); gallery.showCopylink( theClick ); },	true);
		Event.observe($('closerecommend'), 'click', function( theClick ) { gallery.hideRecommend( theClick ); }, true);
		Event.observe($('closecopylink'), 'click', function( theClick ) { gallery.hideCopylink( theClick ); }, true);
		Event.observe($('largelink'),  'focus', function () { $('largelink').select(); }, true);
		Event.observe($('mediumlink'), 'focus', function () { $('mediumlink').select(); }, true);
		Event.observe($('smalllink'),  'focus', function () { $('smalllink').select(); }, true);
		
		Event.observe($('toggleimage'),  'click', function( theClick ) { gallery.showImages( theClick ); }, true);
		Event.observe($('togglevideo'),  'click', function( theClick ) { gallery.showVideo( theClick ); }, true);
				
		Element.hide('thumbsloading');
		if (!$('notfound')) {
			Element.show('toolbar');
			Element.show('thumbs');
		}
		
		this.navPosition();
		Element.show('imgprev');
		Element.show('imgnext');
		Event.observe(window, 'resize', function ( theResize ) { gallery.navPosition(); }, true);
		if (document.getElementsByClassName('videoenabled').length == 0) {
			initSlideshowTimeout = window.setTimeout('gallery.toggleSlideshow()', 5000);
		}
		return this;
	},
	
	navPosition: function () {
		this.imgPosition();
		zw = Math.floor(zoomimgWidth/2);
		with ($('imgprev').style) {
			top 		= zoomimgPos[1] + 'px';
			left 		= zoomimgPos[0] + 'px';
			height  = zoomimgHeight + 'px'; 
			width = zw + 'px';
		}
		with ($('imgnext').style) {
			top 		= zoomimgPos[1] + 'px';
			left 		= zoomimgPos[0] + zw + 'px';
			height  = zoomimgHeight +  'px'; 
			width = zw + 'px';
		}
	},
	
	showImages: function (ev) {
		Element.hide('movieplayer');
		Element.show('images');
		Element.hide('toggleimage');
		Element.show('togglevideo');
		$('page').removeClassName('videoenabled');
	},
	
	showVideo: function (ev) {
		Element.hide('images');
		Element.show('movieplayer');
		Element.hide('togglevideo');
		Element.show('toggleimage');
		$('page').addClassName('videoenabled');
	},
	
	isSafari: function () {
		return (navigator.userAgent.toLowerCase().indexOf('safari') != -1);
	},
	
	
	show: function(linkClicked) {	
		for (var i = 0; i < imageArray.length; i++) {
			if (imageArray[i][0] == linkClicked.href) {
				activeImage = i;
				break;
			}
		}
		gallery.stopSlideshow();
		gallery.loadImage(0);
	},
	
	
	loadImage: function (imageOffset) {
		gallery.hideCopylink();
		if (imageOffset != 0) {
			if (imageOffset < 0) {
				activeImage--;
				if (activeImage < 0) {
					activeImage = imageArray.length - 1;
				}
			} else {
				activeImage++;
				if (activeImage + imageOffset > imageArray.length) {
					activeImage = 0;
					if (typeof(slideshowActive) == 'number') {
						gallery.stopSlideshow();
						gallery.showRecommend();
					}

				}
			}
		}
		$A(document.getElementsByClassName('active')).each ( function ( activeThumb ) {
			Element.removeClassName(activeThumb, 'active');
		});
		Element.addClassName('thumb-' + activeImage, 'active');
		if (!this.isSafari()) {
			imgPreloader = new Image();
			Event.observe(imgPreloader, 'load', function ( loadedEvent ) {
				if (!imageArray[activeImage][2]) {
					Effect.Fade('imageloading', {duration:0.3, from: 0.75});
				}
				
				$('zoomimg').src = imageArray[activeImage][0];
			    gallery.navPosition();
				imageArray[activeImage][2] = true;
			}, true);
			imgPreloader.src = imageArray[activeImage][0];
		} else {
			$('zoomimg').src = imageArray[activeImage][0];
		    gallery.navPosition();
		}
		$('activeId').innerHTML = (activeImage+1).toString();
		if (!imageArray[activeImage][2]) {
			this.zoomPosition();
			if (!this.isSafari()) {
				with ($('imageloading').style) {
					top 		= (zoomPos[1]+5) + 'px';
					left 		= (zoomPos[0]+5) + 'px';
					width 	= (zoomWidth-10)  + 'px';
				}
				Effect.Appear('imageloading', {duration:0.1, to: 0.75});
			}
		}
	},
	
	
	zoomPosition: function() {
		zoomWidth  = $('zoom').offsetWidth;
		zoomHeight = $('zoom').offsetHeight;
		zoomPos 	 = Position.cumulativeOffset($('zoom'));
	},
	
	imgPosition: function() {
		zoomimgWidth  = $('zoomimg').offsetWidth;
		zoomimgHeight = $('zoomimg').offsetHeight;
		zoomimgPos 	 = Position.cumulativeOffset($('zoomimg'));
	},
	
	toggleSlideshow: function ( theClick ) {
		if (typeof(slideshowActive) != 'number') {
			gallery.hideCopylink();
			gallery.hideRecommend();
			gallery.loadImage(1);
			slideshowActive = window.setInterval('gallery.loadImage(1)', 7500);
			Element.addClassName('tbshow', 'on');
		} else {
			gallery.stopSlideshow();
		}
	},
	
	
	stopSlideshow: function () {
	    window.clearTimeout(initSlideshowTimeout);
		window.clearInterval(slideshowActive);
		slideshowActive = false;
		Element.removeClassName('tbshow', 'on');
	},
	
	
	saveCurrent: function ( theClick ) {
	    prefix = (document.location.href.split('/').pop().match(/^[a-z0-9\-_]+/));
		currentUrl = imageArray[activeImage][0];
		currentUrl = currentUrl.substring(0, currentUrl.lastIndexOf('/'));
		downloadName = prefix + '_' + (activeImage + 1) + '_' + currentUrl.substring(currentUrl.lastIndexOf('/')+1, currentUrl.length);
		downloadUrl = 'http://www.af-fix.de/img/download/' + downloadName;
		location.href = 'http://www.af-fix.de/img/download/' + downloadName;
	},
	
	
	darkenImage: function() {
		this.zoomPosition();
		with ($('imagedarker').style) { top = (zoomPos[1]) + 'px'; left = (zoomPos[0]) + 'px'; width = zoomWidth + 'px'; height = zoomHeight + 'px';  }
		Effect.Appear('imagedarker', {to: 0.85, duration: 0.3, queue: {position:'end', scope: 'recommendscope'}});
	},
	
	
	lightenImage: function() {
		Effect.Fade('imagedarker', {from: 0.85, duration: 0.3, queue: {position:'end', scope: 'recommendscope'}});
	},
	
	
	showCopylink: function ( theClick ) {
		gallery.hideRecommend();
		if (!Element.hasClassName('tblink', 'on')) {
			Element.addClassName('tblink', 'on');
			this.zoomPosition();
			with ($('copylink').style) {
				top 	 = (zoomPos[1]) + 'px';
				left 	 = (zoomPos[0]) + 'px';
				width  = zoomWidth + 'px';
				height = zoomHeight-100 + 'px'; 
			}
			
			baseUrl = imageArray[activeImage][0];
			baseUrl = baseUrl.substring(0, baseUrl.lastIndexOf('/'));
			imgTagStart = '<a href="' + $('pagelink').value + '"><img src="';
			imgTagStop = '" alt="ADXXL&reg;" border="0"/></a>';
			$('largelink').value 	= imgTagStart + baseUrl + '/800' + imgTagStop;
			$('mediumlink').value = imgTagStart + baseUrl + '/400' + imgTagStop;
			$('smalllink').value 	= imgTagStart + baseUrl + '/200' + imgTagStop;
			this.darkenImage();
			Effect.Appear('copylink', {duration: 0.2, queue: {position:'end', scope: 'recommendscope'}});
		} else {
			gallery.hideCopylink();
		}
	},
	
	
	hideCopylink: function ( theClick ) {
		Element.removeClassName('tblink', 'on');
		if (Element.visible('copylink')) {
			Effect.Fade('copylink', {duration: 0.2, queue: {position:'end', scope: 'recommendscope'}});
			this.lightenImage();
		}
	},
	
	
	showRecommend: function ( theClick ) {
		gallery.hideCopylink();
		if (!Element.hasClassName('tbsend', 'on')) {
			Element.addClassName('tbsend', 'on');
			this.zoomPosition();
			with ($('recommend').style) {
				top 	 = (zoomPos[1]) + 'px';
				left 	 = (zoomPos[0]) + 'px';
				width  = zoomWidth + 'px';
				height = zoomHeight-100 + 'px'; 
			}
			this.darkenImage();
			Effect.Appear('recommend', {duration: 0.2, queue: {position:'end', scope: 'recommendscope'}});
			window.setTimeout('$("recipientmail").focus();', 1000);
		} else {
			gallery.hideRecommend();
		}
	},
	
	
	hideRecommend: function ( theClick ) {
		Element.removeClassName('tbsend', 'on');
		if (Element.visible('status')) {
			Effect.SlideUp('status', 			{duration: 0.6, queue: {position:'end', scope: 'recommendscope'}});
		}
		if (Element.visible('recommend')) {
			Effect.Fade('recommend', 			{duration: 0.2, queue: {position:'end', scope: 'recommendscope'}});
			this.lightenImage();
		}
	},
	
	
	recommendForm: function ( recommendationForm ) {
		$('imageurl').value = imageArray[activeImage][0];;
    $('statustext').innerHTML = 'Bitte warten...';
    var myAjax = new Ajax.Updater(
    	'statustext', recommendationForm.action,
      {
      	method: 'post',
      	parameters: Form.serialize(recommendationForm) + '&current_session=' + $('sid').innerHTML,
      	onComplete: function ( transport ) {
      		window.setTimeout ('gallery.hideRecommend()', 3000);
      	}
      }
    );
		Effect.SlideDown('status', {duration: 0.4, queue: {position:'end', scope: 'recommendscope'}});
		return false;
	}
}
var gallery;
var imageArray = new Array;
var activeImage;
var slideshowActive ;
var zoomWidth;
var zoomHeight;
var zoomPos;

function initADXXLGallery () {
	gallery = new ADXXLGallery();
}


function resizeGallery () {
}

Event.observe(window, 'load', initADXXLGallery, false);

function playerReady(thePlayer) {
	player = window.document[thePlayer.id];
	adxxlvideo_addVideoListeners();
}

function adxxlvideo_addVideoListeners() {
	if (player) { 
		player.addModelListener("STATE", "adxxlvideo_stateListener");
	} else {
		setTimeout("ts_addVideoListeners()",100);
	}
}

function adxxlvideo_stateListener(obj) {
   if (obj.newstate == 'COMPLETED') {
      window.setTimeout('gallery.showImages()', 1000);
      initSlideshowTimeout = window.setTimeout('gallery.toggleSlideshow()', 7000);
   }
}
