
/**
 * Bereitet Daten zum Versand via Ajax auf
 * 
 * @param String action Aktion
 * @param Object parameters Parameter
 */
function ajax(action, parameters){
	return $H({
		action: action,
		parameters: parameters ? parameters : {}
	}).toJSON();
}

/**
 * Kamera-Klasse
 * 
 * @author Philip Taffner
 */
var Cam = Class.create({
	
	/**
	 * Name der Kamera
	 * 
	 * @var String
	 */
	_cam: '',
	
	/**
	 * aktuelle Position
	 * 
	 * @var String
	 */
	_currentPosition: 1,
	
	/**
	 * Verfügbare Positionen
	 * 
	 * @var Array
	 */
	_positions: {},
	
	/**
	 * LightBox
	 * 
	 * @var lightBox
	 */
	_lb: null,
	
	/**	
	 * Konstruktor
	 * 
	 * @param String cam Name der Kamera
	 * @param String currentPosition aktuelle Position
	 * @param Array positions verfügbare Positionen
	 */
	initialize: function(cam, currentPosition, positions){
		this._cam = cam;
		this._currentPosition = currentPosition;
		Object.extend(this._positions, positions || {});
	},
	
	/**
	 * Bewegt die Kamera zu einer Position
	 * 
	 * @param String position Die gewünschte Position
	 */
	moveToPosition: function(position){
		
		if(this._currentPosition == position){
			return;
		}
		
		this._lb = new lightBox();
		this._lb.open('<div class="lightBoxContent"><h1>Die Kamera wird neu ausgerichtet.</h1><p>Bitte haben Sie etwas Geduld. Die Kamera wird gerade für Sie neu positioniert.<br />Das kann ein paar Minuten in Anspruch nehmen.</p><img class="lightBoxLoad" src="gfx/lightbox.load.gif" alt="" /></div>');		
		
		new Ajax.Request(location.href,{
			method: "post",
			parameters: {
				ajax: ajax('moveToPosition', {
					cam: this._cam,
					position: position					
				})
			},
			onSuccess: function(t){				
				window.setTimeout("cam.setPosition('"+position+"')", 60000);
			}.bind(this),
			onFailure: function(t){
				if (t.responseText.match(/^LOCK:/)) {
					(new lightBox()).open('<img class="lightBoxClose" src="gfx/lightbox.close.gif" alt="Schließen" title="Schließen" /><div class="lightBoxContent"><h1>Zeitsperre aktiv.</h1><p>Bitte versuchen Sie es in ' + t.responseText.replace(/^LOCK:/,'') + ' Minuten nochmal.</p><p style="text-align: right;"><a class="close" href="javascript:;">Meldung schließen</a></p></div>');
				} else {
					(new errorLightBox()).open(t.responseText);
				}
			}
		});
		
	},
	
	/**
	 * Setzt eine Position
	 * 
	 * @param String position Name der Position
	 */
	setPosition: function(position){
		this._currentPosition = position;
		$('image').down('img').src = 'image.' + position + '.jpg?t=' + (new Date()).getTime();
		$('position_desc').update(this._positions[position].desc);
		$('position_title').down('span').update(this._positions[position].title);
		cam._lb.close();		
	}
	
}); 

/**
 * Setzt die Event-Handler für die Thumbnails in der Positionsauswahl
 * Setzt einen Intervall für die minütliche Aktualisierung des Bilds
 */
document.observe('dom:loaded', function(){
	
	$$('#positions li').invoke('observe','click',function(e){
		var li = e.findElement('li');
		cam.moveToPosition(li.id.replace(/cam_/,''));
	});
	
	window.setInterval(function(){
		var i = $('image').down('img');
		var pos = i.cumulativeOffset();
		
		var load = new Element('img',{
			src: "gfx/lightbox.load.gif",
			alt: "Bild wird geladen",
			title: "Bild wird geladen"
		}).setStyle({
			top: (i.getHeight() / 2 - 16) + "px"
		});
		
		var l = new Element('div',{
			id: "reloadOverlay"
		}).setStyle({
			top: pos.top + "px",
			left: pos.left + "px",
			height: i.getHeight() + "px"
		}).update(load);
		$(document.body).insert(l);
				
		var img = new Image();
		img.onload = function(){
			l.remove();
		}
		img.src = 'current.jpg?t=' + (new Date()).getTime();
		
		new Ajax.Request(location.href, {
			method: "post",
			parameters: {
				ajax: ajax('getCurrentPosition')
			},
			onSuccess: function(t){
				i.src = img.src;
				cam.setPosition(t.responseText);
			}
		});		
		
	}, 60000);
	
});

