
/*
	klasa javascript
	autor: Marcin Łukasiewicz, m-l.pl

	aktualizacja 2010-09-22
	
	wymaga jquery 1.4
*/

var box = {

	isOpen:false,
	dv : null,
	newDV : null,
	newDvId : "djj43sgbn_ghyyBOX",
	bg : null,
	loader : null ,

	className : "box_bg",
	classMsg : "box_msg",

	zIndex : 100,

	loaderImg : "_img/loader.gif",
	logoImg : "_img/logo_small.gif",

	startStyles : {position : "absolute", display : "block", margin : "auto", zIndex : 100},

	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	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 = "div#"+d;
				
				this.bgSet(true);

				this.startuj();
			
				this.setPos();
			
			break;
			
			// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
			
			case 'url':
			
				this.dv = "div#"+p1;
				
				this.bgSet(true);
				
				this.startuj();
				
				page.addLoader(p2 || p1);
				this.setPos();

				page.sendQuery(d, function(res){

					(p2)? $("#"+p2).html(res) : $("#"+p1).html(res)
					
					that.setPos();

				});

			break;	
			
			// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
			
			case 'img':
				
				if(this.newDV)
					document.body.removeChild(this.newDV);
				
				this.newDV = this.addDiv();
				this.newDV.style.zIndex = this.zIndex + 1;
				
				this.bgSet(true);
				
				this.startuj(this.newDV);
				
				this.wait();
				
				this.setPos(this.newDV);			

				this.newDV.innerHTML = '<a href="#" onclick="box.close(); return false;"><img src="'+d+'" alt="" style="border:none;" onload="box.imageLoadOk();"></a>';

			break;
			
			// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
			
			case 'msg':
			case 'alert':

				var html = '<img src="'+this.logoImg+'" alt="">';
				
				html += '<p class="a">'+d+'</p>';
				
				if(p1)
					html += '<p class="b">'+p1+'</p>';
				
				html += '<div align="center" style="margin-top:20px;"><input type="Button" onclick="box.close();" value="OK" style="width:80px;"></div>';
				
				this.newDV = this.addDiv(null, html, this.classMsg);
				
				this.bgSet(true);
				
				this.startuj(this.newDV);
				
				this.setPos(this.newDV);

			break;
		}
	},
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	startuj : function(div)
	{
		var d = div || this.dv;
	
		for(var a in this.startStyles)
			$(d).css(a, this.startStyles[a]);

		$(d).css('display', 'none');
		$(d).fadeIn(500);
		
		//$(d).css('display', 'block');
	},

	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	bgSet : function(p)
	{
		if(p)
		{
			var ps = this.pageSize();

			this.bg = this.addDiv(null, null, this.className);
			
			$(this.bg).css({
				position : 'absolute',
				top : '0px',
				left : '0px',
				zIndex : this.zIndex-1,
 				width : ps.pageWidth,
				height : ps.pageHeight
			});
		}
		else
		{
			$(this.bg).remove();
		}
	},

	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	imageLoadOk : function()
	{
		this.wait(-1);
		var that = this;
		this.setPos(this.newDV);
	},
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	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+1;
		
			this.setPos(this.loader);
		}
	},	
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	close : function(funct)
	{
		var that = this;
		
		var d = this.newDV || this.dv;

		$(d).fadeOut(300, function(){
			
			that.isOpen = false;
			that.bgSet(false);
			
			$(d).css("display", "none")
			//d.style.display = "none";
			//$(d).remove();
			
			if(funct)
				eval(funct);
		});
		
	},
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	addDiv : function(obj, str, cName)
	{
		var idn = ("BOX_"+Math.random()).replace(".", "");

		$(obj || document.body).append('<div id="'+idn+'">&nbsp;</div>');
		
		if(str)
			$("div#"+idn).html(str);
			
		if(cName)
			$("div#"+idn).addClass(cName);
		
		return "div#"+idn;
	},
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	setPos : function(obj)
	{
		var x = 0; y = 0;
		
		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);		
	},
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	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;
		}
	}	
};


