
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;

};



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++;

		}



		if (!tags_c)

			return;



		this.body = document.getElementsByTagName("body").item(0);



		this.createOverlay();

	},



	getPageSize: function() {

		var xScroll, yScroll;

		

		if (window.innerHeight && window.scrollMaxY) {	

			xScroll = document.body.scrollWidth;

			yScroll = window.innerHeight + window.scrollMaxY;



		} else if (document.body.scrollHeight > document.body.offsetHeight){

			xScroll = document.body.scrollWidth;

			yScroll = document.body.scrollHeight;



		} else {

			xScroll = document.body.offsetWidth;

			yScroll = document.body.offsetHeight;



		}

		

		var windowWidth, windowHeight;

		if (self.innerHeight) {

			windowWidth = self.innerWidth;

			windowHeight = self.innerHeight;



		} else if (document.documentElement && document.documentElement.clientHeight) {

			windowWidth = document.documentElement.clientWidth;

			windowHeight = document.documentElement.clientHeight;



		} else if (document.body) {

			windowWidth = document.body.clientWidth;

			windowHeight = document.body.clientHeight;

		}	

		

		if (yScroll < windowHeight){

			this.pageHeight = windowHeight;

		} else { 

			this.pageHeight = yScroll;

		}



		if (xScroll < windowWidth){	

			this.pageWidth = windowWidth;

		} else {

			this.pageWidth = xScroll;

		}



	},



	createOverlay: function() {

		this.cOverlay = document.createElement("div");

		this.cOverlay.setAttribute('id','lpopup-overlay');

		

		this.cOverlay.style.display		= 'none';

		this.cOverlay.style.position	= 'absolute';

		this.cOverlay.style.top			= '0px';

		this.cOverlay.style.left		= '0px';

		this.cOverlay.style.zIndex		= '90';

		this.cOverlay.style.width		= '100%';

		this.cOverlay.style.height = (this.pageHeight + 'px');



		this.body.insertBefore(this.cOverlay, this.body.firstChild);

	},



	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) {



		if( typeof( window.innerWidth ) == 'number' ) {

			// FireFox

			pageHeight = window.innerHeight;

		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {

			// IE 6+ in 'standards compliant mode'

			pageHeight = document.documentElement.clientHeight;

		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {

			// IE 4 compatible

			pageHeight = document.body.clientHeight;

		}



		var containerTop	= (pageHeight - 35 - height) / 2;



		var scrOfX = 0, scrOfY = 0;

		if( typeof( window.pageYOffset ) == 'number' ) {

			// FireFox

			scrOfY = window.pageYOffset;

			scrOfX = window.pageXOffset;

		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {

			// IE 6+ in 'standards compliant mode'

			scrOfY = document.body.scrollTop;

			scrOfX = document.body.scrollLeft;

		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {

			// IE 4 compatible

			scrOfY = document.documentElement.scrollTop;

			scrOfX = document.documentElement.scrollLeft;

		}

		

		containerTop += scrOfY;

		var containerLeft	= (this.pageWidth - 20 - width) / 2;



		var containerWidth  = width;

		var containerHeight = parseInt(height);



		if (this.cContainer == null) {

			this.cContainer = document.createElement("div");

			this.cContainer.setAttribute('id','lpopup-container');

			this.body.insertBefore(this.cContainer, this.body.firstChild);

		}



		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) {

		if (this.cImage == null) {

			this.cImage = document.createElement("img");

			this.cImage.setAttribute('id','lpopup-image');

			this.cContainer.appendChild(this.cImage);

		}



		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);

	},



	createPreloader: function() {



		if (window.innerHeight && window.scrollMaxY) {	

			var pageCorrection =  window.innerHeight / 2;

			var preloaderTop	= (this.pageHeight - 35 - pageCorrection) / 2;



		} else if (document.body.scrollHeight > document.body.offsetHeight){

			var pageCorrection = document.body.scrollHeight / 4;

			var preloaderTop	= (this.pageHeight - 35 - pageCorrection) / 2;



		} else {

			var pageCorrection = document.body.scrollHeight / 4;

			var preloaderTop	= (this.pageHeight - 35 - pageCorrection) / 2;



		}



		var scrOfX = 0, scrOfY = 0;

		if( typeof( window.pageYOffset ) == 'number' ) {

			scrOfY = window.pageYOffset;

			scrOfX = window.pageXOffset;



		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {

			scrOfY = document.body.scrollTop;

			scrOfX = document.body.scrollLeft;



		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {

			scrOfY = document.documentElement.scrollTop;

			scrOfX = document.documentElement.scrollLeft;

		}

		

		preloaderTop += scrOfY;



		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', 'images/loading.gif');



			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';

	},



	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];



		this.getPageSize();



		this.cOverlay.style.height	= this.pageHeight + 'px';

		this.cOverlay.style.display = 'block';



		this.setOpacity(this.cOverlay, 40);



		this.createContainer(imageWidth, imageHeight);

		this.createImage(imageWidth, imageHeight, imageURL);



		this.togglePreload();

        if ( typeof(this.callbacks['show']) != 'undefined' )

            this.callbacks['show']( this.cContainer );

        

	},



	hide: function() {

		this.cOverlay.style.display		= 'none';

		this.cContainer.style.display	= 'none';

		this.cPreloader.style.display	= 'none';



		this.cContainer.removeChild(this.cImage);

		this.cImage = null;



        if ( typeof(this.callbacks['hide']) != 'undefined' )

            this.callbacks['hide'](this.cContainer);



	},



	togglePreload: function() {

		this.createPreloader();

		this.cPreloader.style.display = ( this.cPreloader.style.display == 'none' )?'block':'none';

	},



	imgLoaded: function() {

		this.togglePreload();



		this.cImage.style.display = 'block';



		this.cImage.onload = function(){}; // wipe

		this.unregister('load', this.cImage);



		this.register('click', function(){lpopup.hide()}, this.cImage);



		setTimeout('lpopup.imgFade(3)', 1);

	},



	imgFade: function(opacity) {



		if (!this.cImage)

			return false;



		this.setOpacity(this.cImage, opacity*opacity);



		if (opacity*opacity < 100) {

			opacity += 0.45;

        } 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(){lpopup.init('a')});

lpopup.register('load', function(){lpopup.init('img')});



function setPopupTitle( cContainer ) {



    spectitle = document.createElement("div");

    spectitle.setAttribute('id','lpopup-close');

    spectitle.style.height  = '-15';

    spectitle.style.width   = cContainer.style.width;



    cContainer.insertBefore(spectitle, cContainer.firstChild);



    lpopup.register('click', function(){lpopup.hide()}, spectitle);

}

lpopup.attach('show', setPopupTitle);



function removePopupTitle( cContainer ) {

    cContainer.removeChild(cContainer.firstChild);

}

lpopup.attach('hide', removePopupTitle);


