/* =====================================================
// 	slide_projector.css
// ================================================== */


$(function(){
	slideProjector.init();
});


var slideProjector = {
	
	DEBUG				: 0,
	BACKDOOR			: 9,
	
	SLIDES_START_NUM 	: 1,
	SLIDES_END_NUM 		: 38,
	SLIDE_WIDTH			: 72,
	SLIDE_HEIGHT		: 52,
	OFFSET_TOP			: 10,
	OFFSET_RIGHT		: 15,
	OFFSET_BOTTOM		: 150,
	OFFSET_LEFT			: 10,
	IMG_OVERLAP			: 7,
	
	OCCUPIED			: new Array(),
	SLIDES 				: new Array(),
	SLIDES_DIR			: '/images/slides',
	USER_AGENT			: navigator.userAgent,
	
	
	
	/* ============================================================
	// init dimmer box 
	// ========================================================== */
	init : function() {
		slideProjector.pushSlidesIntoArr();
		slideProjector.projectSlides(document.getElementById('slides'));
	},
	
	
	/* init the slide array */
	pushSlidesIntoArr : function() {
		s = slideProjector.SLIDES_START_NUM || 0;
		e = slideProjector.SLIDES_END_NUM || 0;
		
		for(i=s; i<=e; i++) {
			var s = "";
			switch (i.toString().length) {
				case 1: s = "000"; break;
				case 2: s = "00"; break;
				case 3: s = "0"; break;
				default: break;
			}
			s = s+i;
			slideProjector.SLIDES.push(s);
		}
	},
	
	
	/* project the slides on the wall */
	projectSlides : function(e) {
		
		var pageSize = new slideProjector.getPageSize();
		pageSize.width	-= slideProjector.SLIDE_WIDTH;
		pageSize.width	-= slideProjector.OFFSET_RIGHT;
		pageSize.width	-= slideProjector.OFFSET_LEFT;
		pageSize.height	-= slideProjector.SLIDE_HEIGHT;
		pageSize.height	-= slideProjector.OFFSET_TOP;
		pageSize.height	-= slideProjector.OFFSET_BOTTOM;
		
		/* print page size */
		if(slideProjector.DEBUG==1) {
			var sh = '<p>'+pageSize.width+' x '+pageSize.height+'</p>';
			var sx = document.createElement("div");
			sx.setAttribute("id","psize");
			sx.innerHTML = sh;
			e.appendChild(sx);
		}
		
		var arr = slideProjector.SLIDES;
		for(var i=0; i<arr.length; i++) {
			var imgSrc = slideProjector.SLIDES_DIR + "/" + arr[i]+".jpg";
			
			var slide = document.createElement("img");
			slide.setAttribute("src",imgSrc);
			
			if(this.USER_AGENT.indexOf("MSIE ") != -1) {
				slide.setAttribute("className","slide");
			} else {
				slide.setAttribute("class","slide");
			}
			
			var k	= 1;
			var xy	= new slideProjector.getPos(pageSize);
			var posCheck = slideProjector.checkPos(xy,k);
			
			while (k<=slideProjector.BACKDOOR || posCheck==false) {
				
				// problem is: das neue xy wird nicht ans while gegeben 
				
				xy = new slideProjector.getPos(pageSize);
				posCheck = slideProjector.checkPos(xy,k);
				k++;
			}
			
			slide.style.left = xy.x+'px';
			slide.style.top  = xy.y+'px';
			
			e.appendChild(slide);
			
			/* add the occupied area */
			var x = new Array(xy.x,xy.y, xy.x+slideProjector.SLIDE_WIDTH,xy.y+slideProjector.SLIDE_HEIGHT);
			slideProjector.OCCUPIED.push(x);
		}
	},
	
	checkPos : function(xy,k) {
		//var m = "";
		var arr = slideProjector.OCCUPIED;
		for(var i=0; i<arr.length; i++) {
			var x1=arr[i][0] + slideProjector.IMG_OVERLAP;
			var y1=arr[i][1] + slideProjector.IMG_OVERLAP;
			var x2=arr[i][2] - slideProjector.IMG_OVERLAP;
			var y2=arr[i][3] - slideProjector.IMG_OVERLAP;
			//m += k+": "+xy.x+" x "+xy.y+"\n";
			
			if( (xy.x>x1 && xy.y>y1) && (xy.x<x2 && xy.y<y2) ) {
				//m += " -- FALSE --";
				//alert(m);
				return false;
			}
		}
		//alert(m);
		return true;
	},
	
	getPos : function(pageSize) {
		this.x = (parseInt(Math.random()*pageSize.width)+slideProjector.OFFSET_TOP) || slideProjector.OFFSET_TOP;
		this.y = (parseInt(Math.random()*pageSize.height)+slideProjector.OFFSET_LEFT) || slideProjector.OFFSET_LEFT;
	},
	
	getPageSize : function() {
		var docElem = document.documentElement;
		this.width = self.innerWidth || (docElem&&docElem.clientWidth) || document.body.clientWidth;
		this.height = self.innerHeight || (docElem&&docElem.clientHeight) || document.body.clientHeight;
	},
	
	getElementSize : function(e) {
		this.width = e.offsetWidth || e.style.pixelWidth;
		this.height = e.offsetHeight || e.style.pixelHeight;
	},
	
	getElement : function(eID) {
		return document.getElementById(eID);	
	},
	
	
	lafu : function() {}
}


/* eof ============================================== */

