/*
*	CMedium Image Lighbox
*
*	extra für boxoffice
*	- keine thumbs
*	- bild zentriert darstellen
*/

require('jsfTween.js');


function LightBox(){
	
	this.album_id = 0;
	this.image_id = 0;
	
	this.blend_mode = 'alpha';	// simple | alpha | top,left,right,bottom (fade) |  
	this.thumb_alpha = 60;
	
	this.body = document.getElementsByTagName('body')[0];
	
}

/*
*	die lightbox öffnen und ein album laden
*/
LightBox.prototype.open = function(album_id, image_id){

	this.album_id = album_id;
	this.image_id = image_id;
	
	$('image_box').style.display = 'none';
	
	this.fade_in();

}

/*
*	den background einfaden
*/
LightBox.prototype.fade_in = function(){

	this.bg = document.createElement('div');
	this.bg.id = 'cm_lightbox_bg';
	jsf_setAlpha(this.bg, 0);
	this.body.appendChild(this.bg);
	
	var _this = this;
	var bg_alpha = 0;
	var in_timer = window.setInterval(function(){
		jsf_setAlpha(_this.bg, bg_alpha);
		bg_alpha += 5;
		if(bg_alpha > 90){
			window.clearTimeout(in_timer);
			_this.create_box();
		}
	}, 20);
	

}


/*
*	die lightbox einrichten
*/
LightBox.prototype.create_box = function(){
	
	var _this = this;
	
	// center table
	this.center = document.createElement('table');
	this.center.id = 'cm_lightbox_center';
	this.body.appendChild(this.center);
	
	this.center_tbody = document.createElement('tbody'); 
	this.center.appendChild(this.center_tbody);
	
	this.center_tr = document.createElement('tr'); 
	this.center_tbody.appendChild(this.center_tr);

	this.prev_td = document.createElement('td'); 
	this.prev_td.valign = 'middle';
	this.prev_td.align = 'center';
	this.prev_td.style.width = '50px';
	this.prev_td.style.cursor = 'pointer';
	this.prev_td.innerHTML = '<img src="templates/boxoffice/img/prev.png" />';
	this.prev_td.onmousedown = function()
	{
			_this.prev();
	}
	this.center_tr.appendChild(this.prev_td);
	this.prev_td.firstChild.style.display = 'none';
	
	this.center_td = document.createElement('td'); 
	this.center_td.valign = 'middle';
	this.center_td.align = 'center';
	this.center_tr.appendChild(this.center_td);

	this.next_td = document.createElement('td'); 
	this.next_td.valign = 'middle';
	this.next_td.align = 'center';
	this.next_td.style.width = '50px';
	this.next_td.style.cursor = 'pointer';
	this.next_td.innerHTML = '<img src="templates/boxoffice/img/next.png" />';
	this.next_td.onmousedown = function()
	{
			_this.next();
	}
	this.center_tr.appendChild(this.next_td);
	this.next_td.firstChild.style.display = 'none';
	
	// mainframe
	this.frame = document.createElement('div');
	this.frame.id = 'cm_lightbox_frame';
	this.center_td.appendChild(this.frame);
	
	// close btn
	this.close_btn = document.createElement('a');
	this.close_btn.innerHTML = 'close';
	this.close_btn.id = 'cm_lightbox_close_btn';
	this.close_btn.onmousedown = function(){ _this.close(); };
	this.frame.appendChild(this.close_btn);
	
	// image container
	this.image_box = document.createElement('div'); 
	//this.image_box.id = 'cm_lightbox_image_box';
	this.image_box.style.background = 'url(templates/boxoffice/img/loading.png) center center no-repeat #666';
	this.center_td.appendChild(this.image_box);

	this.load_album();

}

/*
*	das album laden
*/
LightBox.prototype.load_album = function(){

	var _this = this;
	var ajax = new Ajax();
	if(!ajax) alert('error - no ajax');
    //ajax.url = 'inc/lightbox.src.php';
	ajax.url = 'inc/' + gallery_src;
	ajax.params = 'album_id=' + this.album_id;
	ajax.method = 'POST';
	ajax.doRequest(function(text, xml){ 
		
		_this.album = eval( "(" + text + ")" );
		_this.init_album();
		
	});

}

/*
*	das album initialisieren
*/
LightBox.prototype.init_album = function(){

	this.active_image = 0;
	
	if(this.album.data.count == 1)
	{
		this.prev_td.firstChild.style.display = 'none';
		this.next_td.firstChild.style.display = 'none';
	}
	else
	{
		this.prev_td.firstChild.style.display = 'inline';
		this.next_td.firstChild.style.display = 'inline';
	}
	
	this.main_image = document.createElement('img');
	this.main_image.style.margin = '10px';
	this.main_image.src = gallery_path + this.album_id + '/' + this.album.data[this.active_image].filename;
	this.image_box.appendChild(this.main_image);

	this.image_box.style.width = (this.album.data[this.active_image].info.width * 1 + 20) + 'px';
	this.image_box.style.height = (this.album.data[this.active_image].info.height * 1 + 20) + 'px';
}

/*
*	vorheriges bild
*/
LightBox.prototype.prev = function()
{
	if(this.album.data.count == 1 || this.tweening)
		return;
	
	var prev = this.active_image > 0 ?  this.active_image - 1 : this.album.data.count - 1;
	this.show(prev);
}

/*
*	nächstes bild
*/
LightBox.prototype.next = function()
{
	if(this.album.data.count == 1 || this.tweening)
		return;

	var next = this.active_image < this.album.data.count - 1 ?  this.active_image + 1 : 0;
	this.show(next);
}

/*
*	bildwechsel
*/
LightBox.prototype.show = function(pos)
{
	var w = 0;
	var h = 0;
	var key1 = new jsfKeyFrame();
	var key2 = new jsfKeyFrame();
	if(this.album.data[this.active_image].info.width != this.album.data[pos].info.width)
	{
		key1.set('width', (this.album.data[this.active_image].info.width * 1 + 20) + 'px');
		key2.set('width', (this.album.data[pos].info.width * 1 + 20) + 'px');
		w = 1;
	}
	if(this.album.data[this.active_image].info.height != this.album.data[pos].info.height)
	{
		key1.set('height', (this.album.data[this.active_image].info.height * 1 + 20) + 'px');
		key2.set('height', (this.album.data[pos].info.height * 1 + 20) + 'px');
		h = 1;
	}
	
	if(w || h)
	{
		var _this = this;
		this.image_box.removeChild(this.main_image);
		this.main_image = document.createElement('img');
		this.main_image.style.margin = '10px';
		this.tweening = 1;
		jsfTween(this.image_box, key1, key2, function(){ 
			_this.active_image = pos;
			_this.main_image.src = gallery_path + _this.album_id + '/' + _this.album.data[_this.active_image].filename;
			_this.main_image.width = _this.album.data[_this.active_image].info.width;
			_this.main_image.height = _this.album.data[_this.active_image].info.height;
			_this.image_box.appendChild(_this.main_image);
			_this.tweening = 0;
		});
	}
	else
	{
		this.active_image = pos;
		this.main_image.src = gallery_path + this.album_id + '/' + this.album.data[this.active_image].filename;
	}
}

/*
*	die lightbox schließen
*/
LightBox.prototype.close = function(onComplete){

	var _this = this;
	this.body.removeChild(this.center);

	var alpha = 100;
	var timer = window.setInterval(function(){
		jsf_setAlpha(_this.bg, alpha);
		alpha -= 5;
		if(alpha < 0){
			
			window.clearTimeout(timer);
			_this.body.removeChild(_this.bg);
			$('image_box').style.display = 'block';
			
			if(onComplete) onComplete();
			
		}
	}, 20);

}

//=====================================================================

/*
*	alpha setzen, alle browser die es unterstützen
*	val (0..100)
*/
function jsf_setAlpha(elm, val){
	
	elm.style.filter = 'alpha(opacity=' + val + ')';			
	elm.style.opacity = (val/100);
	elm.style.MozOpacity = (val/100);
	elm.style.KhtmlOpacity = (val/100);
	
}

