// JavaScript Document
var lightview_active;

function lightviewClass(options, content){	
	this.img_path = ptr + '/../global/movies/';
	this.class_name = '';
	this.style = {
		position: 			'fixed',
		zIndex: 			'160',
		padding: 			'0px',
		border:				'1px solid #ffffff',
		backgroundColor: 	'#000000'
	}
	this.close_type = 'img';	// none, img, text
	this.close_text = 'schließen';
	this.close_img = 'btn_close.png';
	this.set_content = content;
	this.message_type = 'none'; // none, alert, confirm, prompt, user-defined
	this.prompt_init_value = '';
	this.closeFunction = function() {};
	this.alertFunction = function() {};
	this.confirmFunction = function() {};
	this.promptFunction = function(input_value) {};
	this.cancelFunction = function() {};
	this.buttonClass = 'bar_bg_32';
	this.effect = 'unfold'; // none, unfold, appear
	this.area; // Element-ID
	this.overlay = true;
	this.overlay_background_color = '#000000';

	this.params;
	this.Layer;
	this.Overlay;
	var self = this;

	for(var opt_name in options) {
		if(opt_name == 'style') {
			for(var style_name in options.style) {
				this.style[style_name] = options.style[style_name]
			}
		} 
		else {
			this[opt_name] = options[opt_name];
		}
	}
	
	if (this.message_type != 'none') {
		this.style.position = 'fixed';
		this.close_type = 'none';
		if (this.effect != 'none') {
			this.effect = 'appear';
		}
	}

	this.showLightview = function() {
		this.Layer = document.createElement('div');
		this.Layer.id = 'lightview_layer';
		this.Layer.className = this.class_name;
		for(var style_name in this.style) {
			this.Layer.style[style_name] = this.style[style_name];
		}

		if (this.effect == 'appear') {
			this.Layer.style.opacity	= 0.0;
			this.Layer.style.filter	= 'Alpha(opacity=0)';
			var appear_eff = new Effect.Class({duration: 0.4, from: 0.3, to: 1.0, transition: 'linear'});
			appear_eff.transform = function(pos) {
				self.Layer.style.opacity = pos;
				self.Layer.style.filter = 'Alpha(opacity=' + pos * 100 + ')';
			}
			appear_eff.init();
		}

		document.getElementsByTagName('body')[0].appendChild(this.Layer);
		
		var ContentBox = document.createElement('div');
		ContentBox.id = 'lightview_content_box';
		ContentBox.style.overflow = 'hidden';
		ContentBox.innerHTML = this.set_content; //box_content;
		this.Layer.appendChild(ContentBox);

		if(this.message_type != 'none') {
			var BarTop = document.createElement('div');
			BarTop.className = 'bar_bg_32';
			BarTop.style.height = '32px';
			this.Layer.style.padding = '0px';
			ContentBox.style.padding = this.style.padding;
			this.Layer.insertBefore(BarTop, this.Layer.firstChild);
			
			var ControlsContainer = document.createElement('div');
			ControlsContainer.style.padding = '4px 0px';
			ControlsContainer.style.textAlign = 'center';
			if(this.message_type == 'prompt') {
				var InputField = document.createElement('input');
				InputField.type = 'text';
				InputField.style.display = 'block';
				InputField.style.width = '244px';
				InputField.style.margin = '0px auto 10px';
				InputField.value = this.prompt_init_value;
				ControlsContainer.appendChild(InputField);
			}
			if(this.message_type != 'user-defined') {
				var ButtonOK = document.createElement('input');
				ButtonOK.type = 'button';
				ButtonOK.value = 'ok';
				ButtonOK.className = this.buttonClass;
				ControlsContainer.appendChild(ButtonOK);
			}
			if(this.message_type == 'alert') {
				ButtonOK.onclick = function() {
					self.closeLightview();
					self.alertFunction(); 
				}
			} else if(this.message_type == 'confirm') {
				ButtonOK.onclick = function() {
					self.closeLightview();
					self.confirmFunction(); 
				}
			} else if(this.message_type == 'prompt') {
				ButtonOK.onclick = function() {
					self.closeLightview();
					self.promptFunction(InputField.value);
				}
			}
			if(this.message_type == 'confirm' || this.message_type == 'prompt') {
				var ButtonCancel = document.createElement('input');
				ButtonCancel.type = 'button';
				ButtonCancel.value = 'abbrechen';
				ButtonCancel.className = this.buttonClass;
				ButtonCancel.style.marginLeft = '4px';
				ButtonCancel.onclick = function() {
					self.cancelFunction();
					self.closeLightview();
				}
				ControlsContainer.appendChild(ButtonCancel);
				ContentBox.style.width = '250px';
			}
			ContentBox.appendChild(ControlsContainer);
			if(InputField) InputField.focus();
		}
		
		this.getParams(ContentBox);
		if(!this.style.width) {
			this.style.width = this.params.obj_width + 'px'; 
		}
		if(!this.style.height) {
			this.style.height = this.params.obj_height + 'px';
		} 
		if (! this.area) {
			if (! this.style.top) this.Layer.style.top = (this.params.view_height / 2 + (this.style.position != 'fixed' ? this.params.scrolled_y : 0) - this.Layer.offsetHeight / 2) + 'px';
			if (! this.style.left) this.Layer.style.left = (this.params.view_width / 2 + (this.style.position != 'fixed' ? this.params.scrolled_x : 0) - this.Layer.offsetWidth / 2) + 'px';
		} else {
			this.getParams(document.getElementById(this.area));
			if (! this.style.top) this.Layer.style.top = (this.params.y + this.params.obj_height / 2 - this.Layer.offsetHeight / 2) + 'px';
			if (! this.style.left) this.Layer.style.left = (this.params.x + this.params.obj_width / 2 - this.Layer.offsetWidth / 2) + 'px';
		}
		
		if(this.effect == 'unfold') {
			this.Layer.style.width	= '0px';
			this.Layer.style.height	= '0px';	
			ContentBox.style.width = '0px';
			ContentBox.style.height = '0px';
			var unfold_eff = new Effect.Class({duration:0.4});
			var target_width = parseInt(this.style.width);
			var target_height = parseInt(this.style.height);
			var orig_top = parseInt(this.Layer.style.top) + target_height / 2;
			var orig_left = parseInt(this.Layer.style.left) + target_width / 2;
			unfold_eff.transition = function(pos) {
				var new_width = target_width * pos;
				var new_height = target_height * pos;
				self.Layer.style.width = new_width + 'px';
				self.Layer.style.height = new_height + 'px';
				self.Layer.style.top = (orig_top - new_height / 2) + 'px';
				self.Layer.style.left = (orig_left - new_width / 2) + 'px';
				ContentBox.style.width = new_width + 'px';
				ContentBox.style.height = new_height + 'px';
			}
			unfold_eff.init();
		}
	
		if(this.close_type != 'none') {
			if(this.close_type == 'img') {
				/*
				var close_img_src = this.img_path + this.close_img;
				var close_img_obj = new Image();
				close_img_obj.src = close_img_src;
				*/
				var CloseImg = document.createElement('img');
				CloseImg.id = 'lightview_close_btn';
				CloseImg.src = this.img_path + this.close_img;
				CloseImg.alt = '';
				CloseImg.style.position = 'absolute';
				CloseImg.style.zIndex = '999';
				CloseImg.onclick = function() {self.closeLightview()};
				CloseImg.style.cursor = 'pointer';
				CloseImg.style.display = 'block';
				this.Layer.appendChild(CloseImg);
				CloseImg.style.width = CloseImg.offsetWidth + 'px';
				CloseImg.style.height = CloseImg.offsetHeight + 'px';
				CloseImg.style.top = (- CloseImg.offsetHeight / 2) + 'px';
				CloseImg.style.right = (- CloseImg.offsetWidth / 2) + 'px';
			} 
			else if(this.close_type == 'text') {
				var CloseText = document.createElement('a');
				CloseText.id = 'lightview_close_btn';
				CloseText.style.position = 'absolute';
				CloseText.style.zIndex = '180';
				CloseText.style.display = 'block';
				CloseText.style.cursor = 'pointer';
				CloseText.onclick = function() {self.closeLightview()};
				CloseText.appendChild(document.createTextNode(this.close_text));
				this.Layer.appendChild(CloseText);
				CloseText.style.top = '4px';
				CloseText.style.right = '8px';
			}
		}
		
		if (this.overlay) {
			this.setBackgroundOn();
		}
		
		window.onresize = function() {
			self.getParams();
			//self.Layer = document.getElementById('box_layer');
			self.Layer.style.top = (self.params.view_height / 2 - parseInt(self.style.height) / 2 + self.params.scrolled_y) + 'px';
			self.Layer.style.left = (self.params.view_width / 2 - parseInt(self.style.width) / 2 + self.params.scrolled_x) + 'px';
			/*
			//self.Overlay = document.getElementById('overlay');
			self.Overlay.style.width		= self.params.scrollable_x + 'px';
			self.Overlay.style.height	= self.params.scrollable_y + 'px';	
			*/
		}

		lightview_active = this;
		
	}
	
	this.setBackgroundOn = function() {	
		this.Overlay = document.createElement('div');
		this.Overlay.id = 'overlay';
		this.Overlay.style.zIndex = '150';
		this.Overlay.style.backgroundColor	= this.overlay_background_color;
		
		if(! this.area) {
			this.Overlay.style.position = 'fixed';
			this.Overlay.style.top 		= '0px';
			this.Overlay.style.left 		= '0px';
			this.Overlay.style.width		= '100%'; 
			this.Overlay.style.height	= '100%';	
		} else {
			this.getParams(document.getElementById(this.area));
			this.Overlay.style.position = 'absolute';
			this.Overlay.style.top 		= this.params.y + 'px';
			this.Overlay.style.left 		= this.params.x + 'px';
			this.Overlay.style.width		= this.params.obj_width + 'px'; 
			this.Overlay.style.height	= this.params.obj_height + 'px'; 
		}
		if (this.effect != 'none') {
			this.Overlay.style.opacity	= 0.0;
			this.Overlay.style.filter	= 'Alpha(opacity=0)';
		} else {
			this.Overlay.style.opacity	= 0.8;
			this.Overlay.style.filter	= 'Alpha(opacity=80)';
		}
		this.Overlay.style.display = 'none';
		document.getElementsByTagName('body')[0].appendChild(this.Overlay);
		this.Overlay.style.display = 'block';
	
		if (this.effect != 'none') {
			var appear_eff = new Effect.Class({duration: 0.4, from: 0.3, to: 0.8, transition: 'linear'});
			appear_eff.transform = function(pos) {
				self.Overlay.style.opacity = pos;
				self.Overlay.style.filter = 'Alpha(opacity=' + pos * 100 + ')';
			}
			appear_eff.init();
		}
	}
	
	this.setBackgroundOff = function() {		
		if (this.effect != 'none') {
			var vanish_eff = new Effect.Class({duration: 0.2, from: 0.2, tranition: 'linear'});
			vanish_eff.transition = function(pos) {
				//var Overlay = document.getElementById('overlay');
				self.Overlay.style.opacity = 1 - pos;
				self.Overlay.style.filter = 'Alpha(opacity=' + (100 - pos * 100) + ')';
				if (pos >= 1) document.getElementsByTagName('body')[0].removeChild(self.Overlay);
			}
			vanish_eff.init();
		}
		else {
			document.getElementsByTagName('body')[0].removeChild(self.Overlay);
		}
	}
	
	this.closeLightview = function() {
		document.getElementsByTagName('body')[0].removeChild(this.Layer);
		if (this.overlay) {
			this.setBackgroundOff();
		} 
		this.closeFunction();
		this.closeFunction = function() {};
		window.onresize = '';
	}
	
	this.getParams = function() {
		
		var orig_object = '';
	
		this.params = {x:0, y:0, obj_width:0, obj_height:0, view_width:0, view_height:0, scrolled_x:0, scrolled_y:0, scrollable:0, xrel:0, yrel:0};

		if (arguments.length == 1) {
			orig_object = arguments[0];
			this.params.obj_width = orig_object.offsetWidth;
			this.params.obj_height = orig_object.offsetHeight;
			do {
				this.params.x += orig_object.offsetLeft;
				this.params.y += orig_object.offsetTop;
			} while (orig_object = orig_object.offsetParent);
		}
	
		if (window.innerWidth) {
			this.params.view_width = window.innerWidth;
		}
		else if (document.documentElement.clientWidth) {
			this.params.view_width = document.documentElement.clientWidth;
		}
		else if (document.body.clientWidth) {
			this.params.view_width = document.body.clientWidth;
			
		}
	
		if (window.innerHeight) {
			this.params.view_height = window.innerHeight;
		}
		else if (document.documentElement.clientHeight) {
			this.params.view_height = document.documentElement.clientHeight;
		}
		else if (document.body.clientHeight) {
			this.params.view_height = document.body.clientHeight;	
		}
		
		if (window.pageXOffset) {
			this.params.scrolled_x = window.pageXOffset;
		}
		else if (document.documentElement.scrollLeft) {
			this.params.scrolled_x = document.documentElement.scrollLeft;
		}
		else if (document.body.scrollLeft) {
			this.params.scrolled_x = document.body.scrollLeft;
		}
		
		if (window.pageYOffset) {
			this.params.scrolled_y = window.pageYOffset;
		}
		else if (document.documentElement.scrollTop) {
			this.params.scrolled_y = document.documentElement.scrollTop;
		}
		else if (document.body.scrollTop) {
			this.params.scrolled_y = document.body.scrollTop;
		}
	
		if (document.documentElement.scrollWidth) {
			this.params.scrollable_x = document.documentElement.scrollWidth;
		}
		else if (document.body.scrollWidth) {
			this.params.scrollable_x = document.documentElement.scrollWidth;
		}
		if ((this.params.view_width + this.params.scrolled_x) > this.params.scrollable_x) {
			this.params.scrollable_x = this.params.view_width + this.params.scrolled_x;
		}
	
		if (document.documentElement.scrollHeight) {
			this.params.scrollable_y = document.documentElement.scrollHeight;
		}
		else if (document.body.scrollHeight) {
			this.params.scrollable_y = document.documentElement.scrollHeight;
		}
		if ((this.params.view_height + this.params.scrolled_y) > this.params.scrollable_y) {
			this.params.scrollable_y = this.params.view_height + this.params.scrolled_y;
		}
	}
	
	this.showLightview();
	
}

