var pageManager = 
{
	pages:new Array(),
	current:undefined,
	wrapper:undefined,
	built:false,
	loadedPages:0,
	pageNum:0,
	interval:undefined,
	last:undefined,
	ajaxSectionUrl:'page.php?section=',
	pageLoaded:function()
	{
		this.loadedPages++;
	},
	init:function(wrapperId)
	{
		this.wrapper=getElement(wrapperId);
	},
	page:function(xpos,ypos,centerX,centerY)
	{
		this.element=document.createElement('div');
		if(pageManager.pages[ypos]==undefined) pageManager.pages[ypos]= new Array();
		
		pageManager.pages[ypos][xpos]=this;
		this.loaded=false;
		this.w=0;
		this.h=0;
		this.x=0;
		this.y=0;
		this.gridX=xpos;
		this.gridY=ypos;
		this.centerX=centerX;
		this.centerY=centerY;
		
		this.init=function(backgroundSrc)
		{
			this.image=undefined;
			this.bgSrc=backgroundSrc;
			
			this.element.innerHTML="&nbsp;";
			this.element.style.position="absolute";
			this.element.style.top="0px";
			this.element.style.left="0px";
			this.element.style.padding="0px";
			this.element.style.margin="0px";
			this.element.style.backgroundColor="black";
			this.element.className="baseElement";
			this.loaded=false;
			pageManager.pageNum++;
			pageManager.loadQueue.add(backgroundSrc,this);
		}
		
		
		this.fromDomObject = function (objId)
		{
			var tempEl=getElement(objId);
			if(tempEl==undefined) return false;
			this.element.innerHTML=tempEl.innerHTML;
			tempEl.parentNode.removeChild(tempEl);
		}
		this.fromAjax=function(section)
		{
			this.getContent=function()//redefine the dummy getContent function for this page-object, getContent ins run when the background-image is loaded
			{
				var that=this;
				//jQuery(this.element).load(pageManager.ajaxSectionUrl + section,function(){that.onLoad();});
				jQuery.get(pageManager.ajaxSectionUrl + section,function(data)
				{
					that.element.innerHTML +=data;
					that.onLoad();
				}
				);
			}
		}
		this.goTo=function(mode)//Either scrolls or goes directly to
		{
			if(this.loaded)
			{
				pageManager.last=this;
				this.centerOnPoint(this.centerX,this.centerY,mode);
			}
		}
		this.display=function(where)
		{
			var el=getElement(where);
			el.innerHTML="";
			el.style.backgroundColor='black';
			var tempNode=this.element.cloneNode(true);
			el.appendChild(tempNode);
			el.style.display="block";
			el.style.position="fixed";
			var top=-(this.centerY- Math.ceil(getCanvasHeight()/2));
			var left=-(this.centerX - Math.ceil(getCanvasWidth()/2));
			
			el.style.top=top + "px";
			el.style.left=left + "px";
			
			pageManager.last=this;
		}
		this.centerOnPoint=function(x,y,mode)
		{
			var centerX=this.x +x;
			var centerY=this.y+y;
			var scroll_Top=centerY - Math.ceil(getCanvasHeight()/2);
			var scroll_Left=centerX - Math.ceil(getCanvasWidth()/2);
			if(scroll_Top<0) scroll_Top=0;
			if(scroll_Left<0) scroll_Left=0;
			if(mode==='fast') window.scroll(scroll_Left,scroll_Top)
			else pleppo.animatedScroll(scroll_Left,scroll_Top);
			
		}
		this.internalOnLoad = function()
		{
			//this.element.style.background="url(" + this.bgSrc  + ")";
			this.loaded=true; 
			this.element.style.width=this.image.width + "px";
			this.element.style.height=this.image.height + "px";
			this.w=this.image.width;
			this.h=this.image.height;
			
			if(document.all)
				this.element.style.background="url(" + this.image.src + ")";
			else
			{
				
				jQuery(this.image).css({position:"absolute",top:"0px",left:"0px"});
				this.element.appendChild(this.image);
			}
			
			
			
			this.getContent();
			
			pageManager.loadedPages++;
			this.loaded=true;
			
		}
		this.onLoad=function(){}//Dummy function
		this.getContent=function(){this.onLoad();}//Dummy function
		this.update=function(part,section)//Partially updates the page, part can be anything that jQuery recognises (ex #elementId, use with causion, the part must be available in the section
		{
			jQuery(this.element).find(part).load(pageManager.ajaxSectionUrl + section + " " + part);
		}
	},
	/**********************************************/
	buildMap:function(startX,startY)
	{
		this.wrapper.innerHTML="";
		var xpos=0;
		var ypos=0;
		var addToY=0;
		var tempNode="";
		for(var y=0;y<this.pages.length;y++)
		{
			
			xpos=0;
			for(var x=0;x<this.pages[y].length;x++)
			{
				this.pages[y][x].element.style.top=ypos + "px";
				this.pages[y][x].element.style.left=xpos + "px";
				this.pages[y][x].x=xpos;
				this.pages[y][x].y=ypos;
				xpos +=this.pages[y][x].w;
				tempNode=this.pages[y][x].element.cloneNode(true);
				if(x==startX && y==startY)
				{
					tempNode.innerHTML=jQuery('#startWrap .baseElement').html();
				}
				
				this.wrapper.appendChild(tempNode);
				this.pages[y][x].element=tempNode;
				addToY=this.pages[y][x].h;
			}
			ypos +=addToY;
			
		}
		
		this.built=true;
	},
	buildOnLoad: function()
	{
		this.interval=setInterval(pageManager.checkLoaded,100);
		
	},
	checkLoaded:function()
	{
		if(pageManager.pageNum==pageManager.loadedPages && Sdialog.visible==0)
		{	
			Sdialog.do_open=false;
			pageManager.interval=clearInterval(pageManager.interval);
			
			pageManager.buildMap(pageManager.last.gridX,pageManager.last.gridY);
			
			document.body.removeChild(getElement('preloader'));
			
			setTimeout(function()
			{
				pageManager.pages[pageManager.last.gridY][pageManager.last.gridX].goTo('fast');
				setTimeout(function(){pageManager.onLoad();},200);
			},300);
			return true;
		}
		else return false;
	},
	
	loadQueue: 
	{
		elements:new Array(),
		run:true,
		add:function(src,caller)
		{
			var temp=new Object();
			temp.src=src;
			temp.caller=caller;
			this.elements.push(temp);
		},
		load:function()
		{
			if(this.run==false)
			{
				setTimeout(function(){pageManager.loadQueue.load();},100);
				return;
			}
			var first=this.elements.shift();
			if(typeof(first)!="undefined")
			{
				var image=new Image();
				image.src=first.src;
				image.onload=function()
				{
					if(!first.caller.loaded)
					{
						var loadbar=getElement('loadBar');
						first.caller.image=this;
						getElement('preloader').appendChild(this);
						first.caller.internalOnLoad();
						
						pleppo.update_loadbar(Math.ceil((pageManager.loadedPages/12)*100));
						
						//pageManager.loadQueue.load();
						setTimeout(function(){pageManager.loadQueue.load();},100);
					}
				}
				if(image.complete) image.onload();
			}//end of if
		}
	},
	onLoad:function()
	{
		/*Dummy*/
	}
}//end of pageManager