var close_img_onclick				= false;
var close_overlay_onclick			= true;
var close_button					= true;
var display_speed					= 3;
var used_tags						= Array('a', 'img', 'area');
var img_border_px					= 0;
var ajax_loader_img					= '/gfx/lpopup/ajax-loader.gif';
var container_id					= 'container';
var navigation_img_onclick			= false;
var navigation_img_onclick_nohover	= false;
var nav_layer_expand				= 0;
var nav_layer_background			= 'transparent url(/gfx/lpopup/1x1.gif) no-repeat 0 0';
var nav_layer_background_prev		= 'transparent url(/gfx/lpopup/lpopup_prev.gif) no-repeat 0 50%';
var nav_layer_background_next		= 'transparent url(/gfx/lpopup/lpopup_next.gif) no-repeat 100% 50%';

function lpopup_Engine() {
	this.imgPreload = new Image();
	this.pageWidth	= 0;
	this.pageHeight = 0;
	this.body		= null;
	this.callbacks	= {};
	this.cOverlay	= null;
	this.cContainer = null;
	this.cImage		= null;
	this.cPreloader	= null;
	this.gallery	= {};
	this.is_gallery = false;	
	this.closed		= false;
	this.imgBorder	= img_border_px;
};

lpopup_Engine.prototype = {

	init: function( tag_name ) {

		if (!document.getElementsByTagName)
			return;
		var tags_c = 0;
        var csplit = new Array();
		var tags = document.getElementsByTagName( tag_name );
		var tags_l = tags.length;
		for (var i=0; i<tags_l; i++){
			var pop = tags[i].className.split(" ")
			if ( !this.hasClass(pop[0], 'popup') )
				continue;

			csplit = pop[0].split(":")
            this.register('click', function(){lpopup.show(this)}, tags[i]);
			tags_c++;
            cat = 'default';

            if (csplit[7]) cat = csplit[7];

            if (!this.gallery[cat]) this.gallery[cat] = [];

            if (csplit[4]) this.gallery[cat][csplit[4]] = tags[i];
		}
		this.body = document.getElementsByTagName("body").item(0);

		this.createOverlay();
	},

	getPageSize: function() {

		this.pageHeight = sizeCalc.getRealHeight();		
		this.pageWidth	= sizeCalc.getScrollWidth();
	},

	createOverlay: function() {
		this.cOverlay = document.createElement("div");
		this.cOverlay.setAttribute('id','lpopup-overlay');
		
		this.cOverlay.style.display		= 'none';
		this.cOverlay.style.height		= (this.pageHeight + 'px');

		this.body.insertBefore(this.cOverlay, this.body.firstChild);

		if (close_overlay_onclick) {
			this.register('click', function(){lpopup.hide(true)}, this.cOverlay);
			this.cOverlay.title = 'Kattintson a bezáráshoz!';
			this.cOverlay.style.cursor = 'pointer';
		}

	},

	hasClass: function(c, s) {
		var classList = c.split(" ");
		for (var j=0; j<classList.length; j++)
		{
			prop = classList[j].split(":");
			if (prop[0] == s)
			{
				return true;
			}
		}
		return false
	},

	register: function(evnt, func, o) {
		if ( typeof(func) != 'function' )
			return false;

		if ( typeof(o) == 'undefined' )
			o = window;

		if (o.addEventListener) {
			o.addEventListener(evnt, func, false);

		} else if (o.attachEvent) {
			if (!o._listeners)
				o._listeners = new Array();

			if (!o._listeners[evnt])
				o._listeners[evnt] = new Array();

			var workaroundFunc = function() {
				func.apply(o, new Array());
			}

			o._listeners[evnt][func] = workaroundFunc;

			o.attachEvent('on' + evnt, workaroundFunc);
		}
	},

	unregister : function(evnt, func, o) {
		if ( typeof(func) != 'function' )
			return false;

		if ( typeof(o) == 'undefined' )
			o = window;

		if (o.removeEventListener) {
			o.removeEventListener(evnt, func, false);

		} else if (o.detachEvent) {
			if (o._listeners && o._listeners[evnt] && o._listeners[evnt][func]) {
				o.detachEvent('on' + evnt, o._listeners[evnt][func]);
			}

		}
	},

	createContainer: function(width, height) {

		var containerTop	= (sizeCalc.getWindowHeight() - 35 - height) / 2;

		scrOfY = sizeCalc.getScrollTop();
		scrOfX = sizeCalc.getScrollLeft();	

		containerTop += scrOfY;
		var containerLeft	= (this.pageWidth - 20 - width) / 2;
		
		var containerWidth  = parseInt(width);
        var containerHeight = parseInt(height);

		if (this.cContainer == null) {
			this.cContainer = document.createElement("div");
			this.body.insertBefore(this.cContainer, this.body.firstChild);
		}
		if (this.is_gallery) {
			close_img_onclick = false;
			this.cContainer.setAttribute('id','lpopup-container-gallery');
		}
		else
			this.cContainer.setAttribute('id','lpopup-container');

		this.cContainer.style.position	= 'absolute';
		this.cContainer.style.zIndex	= '91';
		this.cContainer.style.top		= (containerTop < 0) ? "0px" : containerTop + "px";
		this.cContainer.style.left		= (containerLeft < 0) ? "0px" : containerLeft + "px";
		this.cContainer.style.height	= containerHeight + 'px';
		this.cContainer.style.width		= containerWidth + 'px';
		this.cContainer.style.display	= 'block';

    },

	createImage: function(width, height, url, title) {
		if (this.cImage == null) {
			this.cImage = document.createElement("img");
			this.cImage.setAttribute('id','lpopup-image');
			this.cContainer.appendChild(this.cImage);
		}

		if (this.cTitle != null){
			this.cContainer.removeChild(this.cTitle);
			this.cTitle = null;
		}

		if (title != 'undefined' && title != '') {
			this.cTitle = document.createElement("div");
			this.cTitle.setAttribute('id','lpopup-title');
			this.cTitle.setAttribute('class','lpopup-title');			
			this.cContainer.appendChild(this.cTitle);
			this.cTitle.innerHTML = title;
		}
	
		this.cImage.style.display = 'none';

		this.register('load', function(){lpopup.imgLoaded()}, this.cImage);

		this.cImage.setAttribute('width', width);
		this.cImage.setAttribute('height', height);
		this.cImage.setAttribute('src', url);
		this.setOpacity(this.cImage, 10);
		this.cImage.style.top = parseInt((parseInt(this.cContainer.style.height) - parseInt(this.cImage.height)) / 2) + 'px';
		this.cImage.style.left = parseInt((parseInt(this.cContainer.style.width) - parseInt(this.cImage.width)) / 2) +'px';
	},

	createPreloader: function() {

		var preloaderTop = parseInt(this.cContainer.style.top.substr(0,this.cContainer.style.top.length-2)) + Math.round(parseInt(this.cContainer.style.height.substr(0,this.cContainer.style.height.length-2)) / 2);
		var preloaderLeft	= (this.pageWidth - 35) / 2;

		if ( this.cPreloader == null ) {
			this.cPreloader = document.createElement("img");
			this.cPreloader.setAttribute('id','lpopup-preloader');
			this.cPreloader.setAttribute('src', ajax_loader_img);
			this.body.insertBefore(this.cPreloader, this.body.firstChild);
			this.cPreloader.style.display	= 'none';
		}

		this.cPreloader.style.top		= preloaderTop + "px";
		this.cPreloader.style.left		= preloaderLeft + "px";
		this.cPreloader.style.position	= 'absolute';
		this.cPreloader.style.zIndex	= '92';
	},
	
	toggleOverPrev: function() {
        this.cPrevLayer.style.background    = nav_layer_background_prev;
	},
	toggleOverNext: function() {		
        this.cNextLayer.style.background    = nav_layer_background_next; 
	},
	toggleOut: function(obj) {
		document.getElementById(obj).style.background	= nav_layer_background;
	},

	createCloseButton: function(width) {
		this.cClose = document.createElement("div");
		this.cClose.setAttribute('id','lpopup-close');
		this.cClose.setAttribute('class','lpopup-close');			
		this.cContainer.appendChild(this.cClose);
//		this.cClose.style.width			= this.cContainer.style.width;
		lpopup.register('click', function(){lpopup.hide(true)}, this.cClose);
	},

	createNavigation: function(iwidth, iheight, prev, next) {
		
		if (prev) {

			if (navigation_img_onclick)	{
				this.cPrevLayer = document.createElement("div");
				this.cPrevLayer.setAttribute('id','lpopup-prevlayer');
				this.cPrevLayer.setAttribute('class','lpopup-navlayer');
				this.cContainer.appendChild(this.cPrevLayer);
				this.cPrevLayer.style.position		= 'absolute';
				this.cPrevLayer.style.height		= iheight + 'px';
				this.cPrevLayer.style.width			= iwidth / 2 + nav_layer_expand +'px';
				this.cPrevLayer.style.zIndex		= 99;
		        this.cPrevLayer.style.background    = navigation_img_onclick_nohover ? nav_layer_background_prev : nav_layer_background;
				this.cPrevLayer.style.top			= this.cImage.style.top;
				this.cPrevLayer.style.left			= parseInt(this.cImage.style.left.substr(0,this.cImage.style.left.length-2)) - nav_layer_expand + 'px';
				this.cPrevLayer.style.cursor		= 'pointer';
				this.register('click', function(){lpopup.show(prev)}, this.cPrevLayer);
				if (!navigation_img_onclick_nohover)	{
					this.register('mouseover', function(){lpopup.toggleOverPrev()}, this.cPrevLayer);
					this.register('mouseout', function(){lpopup.toggleOut('lpopup-prevlayer')}, this.cPrevLayer);
				}
			} else {
				this.cPrev = document.createElement("div");
				this.cPrev.setAttribute('id','lpopup-prev');
				this.cPrev.setAttribute('class','lpopup-prev');			
				this.cContainer.appendChild(this.cPrev);
				this.register('click', function(){lpopup.show(prev)}, this.cPrev);			
			}

		} else {
			if (this.cPrev != null) {
				this.cContainer.removeChild(this.cPrev);
				this.cPrev = null;
			}
			if (this.cPrevLayer != null) {
				this.cContainer.removeChild(this.cPrevLayer);
				this.cPrevLayer = null;
			}
		}

		if (next) {

			if (navigation_img_onclick)	{
				this.cNextLayer = document.createElement("div");
				this.cNextLayer.setAttribute('id','lpopup-nextlayer');
				this.cNextLayer.setAttribute('class','lpopup-navlayer');
				this.cContainer.appendChild(this.cNextLayer);
				this.cNextLayer.style.position		= 'absolute';
				this.cNextLayer.style.height		= iheight + 'px';
				this.cNextLayer.style.width			= iwidth / 2 + nav_layer_expand + (this.imgBorder*2) +'px';
		        this.cNextLayer.style.background    = navigation_img_onclick_nohover ? nav_layer_background_next : nav_layer_background;
				this.cNextLayer.style.zIndex		= 99;
				this.cNextLayer.style.top			= this.cImage.style.top;
				this.cNextLayer.style.left			= parseInt(this.cImage.style.left.substr(0,this.cImage.style.left.length-2)) + (iwidth / 2) + 'px';
				this.cNextLayer.style.cursor		= 'pointer';
				this.register('click', function(){lpopup.show(next)}, this.cNextLayer);
				if (!navigation_img_onclick_nohover)	{
					this.register('mouseover', function(){lpopup.toggleOverNext()}, this.cNextLayer);
					this.register('mouseout', function(){lpopup.toggleOut('lpopup-nextlayer')}, this.cNextLayer);
				}
			} else {
				this.cNext = document.createElement("div");
				this.cNext.setAttribute('id','lpopup-next');
				this.cNext.setAttribute('class','lpopup-next');			
				this.cContainer.appendChild(this.cNext);
				this.register('click', function(){lpopup.show(next)}, this.cNext);			
			}


		} else {
			if (this.cNext != null){
				this.cContainer.removeChild(this.cNext);
				this.cNext = null;
			}
			if (this.cNextLayer != null) {
				this.cContainer.removeChild(this.cNextLayer);
				this.cNextLayer = null;
			}
		}
	
	},
	
	
	show: function(i) {

		var pop = i.className.split(" ")
		var csplit = pop[0].split(":")

		var imageURL	= csplit[1];
		var imageWidth  = csplit[2];
		var imageHeight	= csplit[3];
		var imageNum	= csplit[4];
		var popupWidth	= csplit[5];
		var popupHeight	= csplit[6];
		var cat	        = csplit[7];

        if (!cat) cat = 'default';

		var prev		= 0;
		var next		= 0;
		this.getPageSize();

		this.cOverlay.style.height	= this.pageHeight + 'px';
		this.cOverlay.style.display = 'block';

		this.setOpacity(this.cOverlay, 80);

		if (typeof(this.gallery[cat][parseInt(imageNum)-1]) == 'object') 
			prev = this.gallery[cat][parseInt(imageNum)-1];
		if (typeof(this.gallery[cat][parseInt(imageNum)+1]) == 'object')
			next = this.gallery[cat][parseInt(imageNum)+1];
		
		if (this.cImage)
			this.hide(false);
		
		if (imageNum)
			this.is_gallery = true;
		else
			this.is_gallery = false;

		this.createContainer(popupWidth?popupWidth:imageWidth, popupHeight?popupHeight:imageHeight);

		this.closed = false;
		this.createImage(imageWidth, imageHeight, imageURL, i.title);
		this.createNavigation(imageWidth, imageHeight, prev, next);

		if (close_button)
			this.createCloseButton(imageWidth);

		this.togglePreload();
        if ( typeof(this.callbacks['show']) != 'undefined' )
            this.callbacks['show']( this.cContainer );
        
	},

	hide: function(close) {
		if (close)
			this.cOverlay.style.display		= 'none';

		this.closed = true;
		this.cContainer.style.display	= 'none';
		this.cPreloader.style.display	= 'none';

		this.cContainer.removeChild(this.cImage);
		this.cImage = null;
		if (close_button) {
			this.cContainer.removeChild(this.cClose);
			this.cClose = null;
		}
		if (this.cPrev) {
			this.cContainer.removeChild(this.cPrev);
			this.cPrev = null;
		}
		if (this.cPrevLayer) {
			this.cContainer.removeChild(this.cPrevLayer);
			this.cPrevLayer = null;
		}
		if (this.cNextLayer) {
			this.cContainer.removeChild(this.cNextLayer);
			this.cNextLayer = null;
		}
		if (this.cNext) {
			this.cContainer.removeChild(this.cNext);
			this.cNext = null;
		}
        if ( typeof(this.callbacks['hide']) != 'undefined' )
            this.callbacks['hide'](this.cContainer);
	},

	togglePreload: function() {
		if (this.closed) return false;
		this.createPreloader();
		this.cPreloader.style.display = ( this.cPreloader.style.display == 'none' )?'block':'none'; 
		return true;
	},

	imgLoaded: function() {
		if ( !this.togglePreload()) return;

		this.cImage.style.position = 'absolute';
		this.cImage.style.display = 'block';
		this.cImage.style.cursor  = 'default';

		this.cImage.onload = function(){}; // wipe
		this.unregister('load', this.cImage);

		if (close_img_onclick) {
			this.register('click', function(){lpopup.hide(true)}, this.cImage);
			this.cImage.setAttribute('title','Kattintson a bezáráshoz!');
			this.cImage.style.cursor = 'pointer';
		}

		this.cImage.style.top = parseInt((parseInt(this.cContainer.style.height) - parseInt(this.cImage.height)) / 2) + 'px';
		this.cImage.style.left = parseInt((parseInt(this.cContainer.style.width) - parseInt(this.cImage.width)) / 2) +'px';
        this.cImage.style.left = parseInt((parseInt(this.cContainer.style.width) - parseInt(this.cImage.width)) / 2) +'px'; 
	 	 
		if (this.cNextLayer != null) { 
			this.cNextLayer.style.top	= this.cImage.style.top; 
			this.cNextLayer.style.left  = parseInt(this.cImage.style.left.substr(0,this.cImage.style.left.length-2)) + (this.cImage.width / 2) + 'px'; 
		} 
		if (this.cPrevLayer != null) { 
			this.cPrevLayer.style.top	= this.cImage.style.top; 
			this.cPrevLayer.style.left  = this.cImage.style.left; 
		} 

		setTimeout('lpopup.imgFade(display_speed)', 1);

	},

	imgFade: function(opacity) {

		if (!this.cImage) return false;

		this.setOpacity(this.cImage, opacity*opacity);

		if (opacity*opacity < 100) {
			opacity += 0.70;
        } else return;

		setTimeout('lpopup.imgFade('+opacity+')', 1);
	},

	setOpacity: function(o, opacity) {
		o.style.opacity = (opacity / 100);
		o.style.MozOpacity = (opacity / 100);
		o.style.KhtmlOpacity = (opacity / 100);
		o.style.filter = "alpha(opacity=" + opacity + ")";
	},

    attach: function(event, func) {
        this.callbacks[event] = func;
	}

}

var lpopup = new lpopup_Engine();
lpopup.register('load', function(){	for (i in used_tags){ lpopup.init(used_tags[i]) }});


sizeCalc = {

    getWindowWidth: function() {
	    return (document.layers||(document.getElementById&&!document.all)) ? window.outerWidth : (document.all ? document.body.clientWidth : 0);
	},

	getWindowHeight: function() {
	    return window.innerHeight ? window.innerHeight :(document.getBoxObjectFor ? Math.min(document.documentElement.clientHeight, document.body.clientHeight) : ((document.documentElement.clientHeight != 0) ? document.documentElement.clientHeight : (document.body ? document.body.clientHeight : 0)));
	},

	getScrollWidth: function() {
	return document.all ? Math.max(Math.max(document.documentElement.offsetWidth, document.documentElement.scrollWidth), document.body.scrollWidth) : (document.body ? document.body.scrollWidth : ((document.documentElement.scrollWidth != 0) ? document.documentElement.scrollWidth : 0));
	},

	getScrollHeight: function(){
		body_height = document.all ? Math.max(Math.max(document.documentElement.offsetHeight, document.documentElement.scrollHeight), Math.max(document.body.offsetHeight, document.body.scrollHeight)) : (document.body ? document.body.scrollHeight : ((document.documentElement.scrollHeight != 0) ? document.documentElement.scrollHeight : 0));
		container_height = document.getElementById(container_id).scrollHeight;
		if (body_height < container_height) return container_height;
		else return body_height;
	},

	getScrollLeft: function() {
		return document.all ? (!document.documentElement.scrollLeft ? document.body.scrollLeft : document.documentElement.scrollLeft) : ((window.pageXOffset != 0) ? window.pageXOffset : 0);
	},

	getScrollTop: function() {
		return document.all ? (!document.documentElement.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop) : ((window.pageYOffset != 0) ? window.pageYOffset : 0);
	},

	getRealHeight: function() {
		return (sizeCalc.getScrollHeight() < sizeCalc.getWindowHeight()) ?  sizeCalc.getWindowHeight() : sizeCalc.getScrollHeight();
	}

}

