// Option type is the type of lightbox to create
// Can be 'big' or 'small'
var lightboxUrl;

var Lightbox = function( type ) {
	// Private config object
	var lb = {
		window: false,
		arrow: false,
		overlay: false,
		triggerElement: false,
		url: false,
		cache: '',
		tplSmall: "<div id='lightbox-small' class='lightbox'><div class='pointer point_left'></div><div class='content'></div><div class='bottom'><a class='close'>Lukk <span>X</span></a></div></div>",
		tplBig: "<div id='overlay' /><div id='lightbox-big' class='lightbox'><div class='big'><a class='close'>Lukk <span>X</span></a><div class='content'></div><div class='bottom'></div></div></div>",
		tplAuto: "<div id='overlay' /><div id='lightbox-big' class='lightbox auto-wide front'><div class='big'><a class='close'>Lukk <span>X</span></a><div class='content'></div><div class='bottom'></div></div></div>",
		tplWait: "<div id='overlay' /><div id='lightbox-big' class='lightbox'><div class='big'><div class='content'></div><div class='bottom'></div></div></div>"
	}

	// Public variables
	this.timeout = false; // Use this for setTimeout and clearTimeout

	var root = this;

	// Public methods
	this.init = function() {
		if( type == 'big' ) {
			$("body").append( lb.tplBig );
			lb.id = 'lightbox-big',
			lb.window = $('#' + lb.id );
			lb.overlay = $('#overlay');
			lb.setPosition = positionCenter;
		}

		else if( type == 'auto' ) {
			$("body").append( lb.tplAuto );
			lb.id = 'lightbox-big',
			lb.window = $('#' + lb.id );
			lb.overlay = $('#overlay');
			lb.setPosition = positionCenter;
		}

		else if ( type == 'small' ) {
			$("body").append(lb.tplSmall);
			lb.id = 'lightbox-small',
			lb.window = $('#' + lb.id );
			lb.arrow = lb.window.find('.pointer');
			lb.setPosition = positionFromTrigger;
		}

		else if ( type == 'img' ) {
			$("body").append(lb.tplAuto);
			lb.id = 'lightbox-big',
			lb.window = $('#' + lb.id );
			lb.overlay = $('#overlay');
			lb.setPosition = positionCenter;
		}

		else if ( type == 'wait' ) {
			$("body").append(lb.tplWait);
			lb.id = 'lightbox-big',
			lb.window = $('#' + lb.id );
			lb.overlay = $('#overlay');
			lb.setPosition = positionCenter;
		}

		lb.window.find('a.close').click(function(){
			root.hide();
			return false;
		});

		$('#overlay').click(function() {
			root.hide();
		});
	
		$(window).resize(function(){
			lb.setPosition();
		});
	}

	this.hide = function() {
		lb.window.hide();
		$('#overlay').hide();
	}

	this.unhide = function() {
		lb.window.show();
		$('#overlay').show();
	}
	
	this.cache = function( inputUrl, data ) {
		if( lb.url != inputUrl ) {
			lb.url = inputUrl;
			lb.cache = data;
		}
	}
	
	this.show = function( inputUrl, inputTriggerElement ) {
		lb.triggerElement = inputTriggerElement;

		if ( !lb.window ) {
			root.init();
		}

		if( lb.url != inputUrl ) {
			lb.url = inputUrl;
			lightboxUrl = inputUrl;
			if (type == 'img') {
				var myImg = document.createElement('img');
				myImg.src = inputUrl;
				lb.window.find('.content').empty().append(myImg);
				lb.setPosition();
			} else {
				lb.window.find('.content').load( lb.url, lb.setPosition );
			}
		} else {
			if (lb.cache) lb.window.find('.content').html(lb.cache);
			lb.setPosition();
		}

		/*lb.window.bind('mouseenter', function(e){
			clearTimeout( lightbox.timeout );
		}).bind('mouseleave', function(e) {
			lightbox.timeout = setTimeout( function(){
				lightbox.hide();
			}, 500 );
		});*/
	}

	function positionCenter() {
		var de = document.documentElement;
		var w = self.innerWidth || ( de && de.clientWidth ) || document.body.clientWidth;
		var h = self.innerHeight || ( de && de.clientHeight ) || document.body.clientHeight;

		var lightboxX = ( w - lb.window.width() ) / 2;
		var lightboxY = ( h - lb.window.height() ) / 2;

		var scrollTop = de.scrollTop || document.body.scrollTop;
		var scrollLeft = de.scrollLeft || document.body.scrollLeft;

		lb.window.css({
			top: lightboxY + scrollTop + 'px',
			left: lightboxX + scrollLeft + 'px'
		});

		lb.overlay.css({
			height: de.scrollHeight + 'px'
		})

		root.unhide();

		// handle internal navigation
		$('#lightbox-big .navigation a').click(function(e) {
			$('#lightbox-big .navigation a').removeClass('active');
			$(this).addClass('active');
			var section = this.className.match(/navigation_(\w+)/);
			section = section[1];
			$('#lightbox-big .tab_panel').hide();
			$('#lightbox-big .panel_' + section).show();
			e.preventDefault();
		});

		// extra close button
		$('#lightbox-big .content .close_button').click(function(){
			root.hide();
			return false;
		});

		// handle form submission
		$('#contact_form').submit(function(e) {
			var params = {};
			$(this)
				.find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea")
				.filter(":enabled")
				.each(function(){
					params[this.name || this.id || this.parentNode.name || this.parentNode.id] = this.value;
				});
			$.post(this.getAttribute("action") + "?call=ajax", params, function(result){
				$('#lightbox-big .content').empty().append(result);
				$('#lightbox-big .content .close_button').click(function(){
					root.hide();
					return false;
				});
			});
			e.preventDefault();
		});
	}

	function positionFromTrigger() {
		var de = document.documentElement;
		var w = self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
		var h = self.innerHeight || (de && de.clientHeight) || document.body.clientHeight;

		var offset = lb.triggerElement.offset();
		var lightboxX = offset.left + lb.triggerElement.width();
		var lightboxY = offset.top + ( lb.triggerElement.height() / 2 ) - ( lb.window.height() / 2 );
		var arrowY = ( lb.window.height() / 2 ) - ( lb.triggerElement.height() / 2 );

		var scrollTop = de.scrollTop || document.body.scrollTop;
		var scrollLeft = de.scrollLeft || document.body.scrollLeft;

		// Lightbox is positioned too far to the top, above the browser area, move down
		if ( lightboxY < scrollTop ) {
			arrowY = arrowY - scrollTop + lightboxY;
			if ( arrowY < 10 ) {
				arrowY = 10;
			}
			lightboxY = scrollTop;
		}

		// Lightbox is positioned too far down, below the visible browser area, move up
		else if ( ( lightboxY + lb.window.height() ) > ( h + scrollTop ) ) {
			var newLightboxY = scrollTop + h - lb.window.height() - 20;
			arrowY = arrowY - newLightboxY + lightboxY;
			lightboxY = newLightboxY;

			if ( arrowY > lb.window.height() - 25 ) {
				arrowY = lb.window.height() - 35;
			}
		}

		if ( ( lightboxX + lb.window.width() ) > ( scrollLeft + w ) ) {
			lightboxX = offset.left - lb.window.width();
			lb.window.find('.point_left').removeClass('point_left').addClass('point_right');
		} else {
			lb.window.find('.point_right').removeClass('point_right').addClass('point_left');
		}

		lb.window.css({
			top: lightboxY + 'px',
			left: lightboxX + 'px'
		});

		lb.arrow.css({
			top: arrowY + 'px'
		});

		root.unhide();
	}
};