/*
	### Antje Bockeloh
	JS functions for mini-lightbox and gallery feats
*/

var baseurl = window.location.toString();
baseurl = baseurl.substring(0, baseurl.lastIndexOf('/'))+'/';
var ajaxurl = baseurl+'ajax/';

var theimages = {
	'current': null,
	'next': null,
	'prev': null
}


window.addEvent('domready', function() {

	// # Init the gallery-links
	$$('.portfolio_image').each(function(item, index){
		$(item).addEvent('click', function(){
			// now init the antjebox
			var id = this.get('id');
			init_antjebox(id);
		});
	});
	
	// Slideshow
	$('slideshow').addEvent('click', function(){
		// now init the antjebox
		init_antjebox('slideshow');
	});

	
});

function init_antjebox(image_ID) {
	// # show the box
	display = $(document.body).getScrollSize();
	
	// the basic box
	var antjebox = new Element('div', {'id' : 'antjebox'});
	antjebox.setStyles({
		width: display.x,
		height: display.y
	});
	antjebox.fade('hide');
	antjebox.inject($('ajax'));

	// the wrapper, image-container and controls
	var wrapper = new Element('div', {'id': 'wrapper'});
	wrapper.inject($('antjebox'));

	if(image_ID == 'slideshow') {
		// Quicktime Movie
		//var qt_html = '<object CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="480" height="375" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab"><param name="src" value="http://www.bockeloh.de/wp-content/uploads/slideshow.mov"><param name="autoplay" value="true"><param name="loop" value="false"><param name="controller" value="true"><embed src="http://www.bockeloh.de/wp-content/uploads/slideshow.mov" width="480" height="375" autoplay="true" loop="false" controller="true" pluginspage="http://www.apple.com/quicktime/"></embed></object>';
		
		var qt_html = '<div style="margin-bottom: 5px"><object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab#version=7,3,0,0" id="slideshow" height="375" width="480"><param name="src" value="http://www.bockeloh.de/wp-content/uploads/slideshow.mov"><object id="SlideshowInner" data="http://www.bockeloh.de/wp-content/uploads/slideshow.mov" type="video/quicktime" height="375" width="480"><param name="autoplay" value="true"><param name="controller" value="true"><param name="cache" value="true"><param name="showlogo" value="true"><param name="saveembedtags" value="true"></object><param name="autoplay" value="true"><param name="controller" value="true"><param name="cache" value="true"><param name="showlogo" value="true"><param name="saveembedtags" value="true"></object></div>';
				
		$('wrapper').set('html', qt_html);
		$('wrapper').setStyles({
			width: 480,
			height: 395
		});
		
		// Controls
		var control_close = new Element('a', {'id': 'close', 'html': '&nbsp;'});
		control_close.addEvent('click', function(){ close_antjebox(); });
		control_close.inject(wrapper);
		
		var control_download = new Element('a', {'id': 'download', 'html': '&nbsp;', 'href': 'http://www.bockeloh.de/wp-content/uploads/slideshow.m4v'});
		//control_download.addEvent('click', function(){ alert('download'); });
		control_download.inject(wrapper);

		// show the box
		$('antjebox').fade('in');

	} else {
		// default gallery
		var image_container = new Element('div', {'id' : 'image_container'});
		image_container.inject(wrapper);
		resizeFx = image_container.effects({duration: 1000, transition: Fx.Transitions.Quart.easeOut});
	
		var image = new Element('img', {'id': 'current_image'});
		image.inject(image_container);
		imageFx = $('current_image').effects({duration: 500, transition: Fx.Transitions.Quart.easeOut});
		
		var control_close = new Element('a', {'id': 'close', 'html': '&nbsp;'});
		control_close.addEvent('click', function(){ close_antjebox(); });
		control_close.inject(wrapper);
		
		var control_next = new Element('a', {'id': 'next', 'html': '&nbsp;'});
		control_next.addEvent('click', function(){ antjebox_showimage('next'); });
		control_next.inject(wrapper);
		
		var control_prev = new Element('a', {'id': 'prev', 'html': '&nbsp;'});
		control_prev.addEvent('click', function(){ antjebox_showimage('prev'); });
		control_prev.inject(wrapper);
		
		// # load initialy image
		var url = ajaxurl+'?json=image&ID='+image_ID;
		var init_image_data = new Request.JSON({url: url, method: 'get', onComplete: function(data){
			
			// set image
			data = antje_set_image(data);
	
		}}).send().chain(function() {
	
		// show the box
		$('antjebox').fade('in');
	
		});
	}
}

function close_antjebox() {
	if( $defined($('antjebox')) ) {
		$('antjebox').fade('out');
		$('antjebox').empty();
		//$('antjebox').destroy();												 
	}
}

function antjebox_showimage(direction) {

	// load next image
	if(direction == 'next') {
		ID = theimages.next;
	} else {
		ID = theimages.prev;
	}
	
	// fade image out
	$('current_image').fade(0);

	$('current_image').set('src', '');

var url = ajaxurl+'?json=image&ID=ID_'+ID;
	var new_image_data = new Request.JSON({url: url, method: 'get', onComplete: function(data){
		//alert(data.url);
		antje_set_image(data);
		// fade image back in
		$('current_image').fade(1);
	}}).send();
}

function antje_set_image(data) {

	// check if the height is too large (450px)
	if(data.h > 450) {
		nh = 450;
		nw = (nh * data.w) / data.h;
		nw.floor();
		data.h = nh;
		data.w = nw;
	}

	$('wrapper').tween('width', data.w);
	resizeFx.start({
		'width': data.w,
		'height': data.h
	}).chain(function() {

	// image src
	$('current_image').set('src', data.url);
	$('image_container').set('width', data.w);
	$('image_container').set('height', data.h);

	});

	theimages.current = data.ID;
	theimages.next = data.next;
	theimages.prev = data.prev;

	// adjust wrapper size
	$('wrapper').setStyles({
		width: data.w,
		height: data.h
	});

	return data;
	
}