
/*
	klasa javascript
	autor: Marcin Łukasiewicz, m-l.pl

	aktualizacja 2010-09-22
	
	wymaga jquery 1.4
*/

var box = {

	isOpen:false,
	dv : null,
	dvImg : null,
	bg : null,
	loader : null ,

	className : "box_bg",

	zIndex : 100,

	loaderImg : "_img/loader.gif",

	startStyles : {position : "absolute", display : "block", margin : "auto", zIndex : 0},

	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	show : function(type, d, p1, p2)
	{
		this.startStyles.zIndex = this.zIndex;
	
		if(this.isOpen)
			return false;
		
		var that = this;

		switch(type)
		{
			case 'div':
				
				this.dv = this.$d(d);

				this.startuj();
			
				this.setPos();
			
			break;
			
			// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
			
			case 'url':
			
				this.dv = this.$d(p1);
				
				this.startuj();
				
				page.addLoader(p2 || p1);
				this.setPos();

				page.sendQuery(d, function(res){

					that.$d(p2 || p1).innerHTML = res;

					that.setPos();

				});

			break;	
			
			// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
			
			case 'img':
				
				if(this.dvImg)
					document.body.removeChild(this.dvImg);
				
				this.dvImg = this.addDiv();
				this.dvImg.style.zIndex = this.zIndex + 1;
				
				this.startuj(this.dvImg);
				
				this.wait();
				
				this.setPos(this.dvImg);			

				this.dvImg.innerHTML = '<a href="#" onclick="box.close(); return false;"><img src="'+d+'" alt="" style="border:none;" onload="box.imageLoadOk();"></a>';

			break;
		}
	},
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	startuj : function(div)
	{
		var d = div || this.dv;
	
		this.bgSet(true);

		for(var a in this.startStyles)
		{
			d.style[a] = this.startStyles[a];
		}

		$(d).css('display', 'none');
		$(d).fadeIn(500);
	},

	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	bgSet : function(p)
	{
		if(p)
		{
			var ps = this.pageSize();

			this.bg = this.addDiv(null, null, "box_bg");
			//this.bg.style.display = 'block';
			this.bg.style.position = 'absolute';
			this.bg.style.top = '0px';
			this.bg.style.left = '0px';
			this.bg.style.zIndex = this.zIndex-1;
 			this.bg.style.width = '100%';
			this.bg.style.height = '100%';
			this.bg.className = this.className;

			this.bg.style.width = ps.pageWidth;
			this.bg.style.height = ps.pageHeight;
		}
		else
			document.body.removeChild(this.bg);
	},

	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	imageLoadOk : function()
	{
		this.wait(-1);
		var that = this;
		this.setPos(this.dvImg);
	},
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	wait : function(x)
	{
		if(x == -1)
		{
			document.body.removeChild(this.loader);
			this.loader = null;
		}
		else
		{
			if(this.loader)
				return false;
			
			this.loader = this.addDiv(null, '<img src="'+this.loaderImg+'">');
			this.loader.style.position = "absolute";
			this.loader.style.zIndex = this.zIndex+2;
		
			this.setPos(this.loader);
		}
	},	
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	close : function(funct)
	{
		var that = this;
		
		var d = this.dvImg || this.dv;
		/*
		mint.fx.Style(dv, "opacity", 100, 0, 10, 200, null, function()
		{
			that.isOpen = false;
			that.bgSet(false);
			dv.style.display = "none";
			
			if(funct)
			{
				//window.execScript(funct);
				eval(funct);
			}
		}
		);
		*/

		$(d).fadeOut(500, function(){
			
			that.isOpen = false;
			that.bgSet(false);
			d.style.display = "none";
			
			if(funct)
				eval(funct);
		});
		
	},
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	addDiv : function(obj, str, cName)
	{
		var d = document.createElement('div');
		
		if(str)
			d.innerHTML = str;
		
		if(cName)
			d.className = cName;
		
		(obj || document.body).appendChild(d);
		
		return d;
	},
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	setPos : function(obj)
	{
		var x = 0; y = 0;
		//var size = GetSize(obj || this.dv);
		
		var d = obj || this.dv;
		
		var size = {width : $(d).width(), height : $(d).height()};
		var ps = this.pageSize();

		x = ps.windowWidth/2 - size.width/2 + this.scroll().left;
		y = ps.windowHeight/2 - size.height/2 + this.scroll().top;

		$(d).css('left', x);
		$(d).css('top', y);
		
		$(d).css('width', size.width);
		$(d).css('height', size.height);		

		//SetPos(obj || this.dv, x, y);
		//SetSize(obj || this.dv, size.width, size.height);		
	},
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	scroll : function()
	{
		var xScroll = $(document).scrollLeft();
		var yScroll = $(document).scrollTop();
	
		return {left : xScroll, top : yScroll};
	},
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	pageSize : function()
	{
		var pageWidth, pageHeight, windowWidth, windowHeight;
		
		pageWidth = $(document).width();
		pageHeight = $(document).height();
		windowWidth = $(window).width();
		windowHeight = $(window).height();
	
		return {pageWidth:pageWidth, pageHeight:pageHeight, windowWidth:windowWidth, windowHeight:windowHeight};
	},	
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	$d : function(x)	{
		return (typeof x == "object")? x : document.getElementById(x);
	},
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	//Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602		
	
	pause : function(numberMillis)
	{
		var now = new Date();
		var exitTime = now.getTime() + numberMillis;
		while (true)
		{
			now = new Date();
			if (now.getTime() > exitTime)
				return;
		}
	}	
};


