function iOSloader() {
	this.minDistance=800;
	this.minPictures=5;
	this.target=false;
	this.pictures=Array();

	this.maxPosition=0;
	this.counter=0;
	
	// setter functions
	this.setTarget = function(target) {
	
		if(!target)
			return false;
			
		this.target = target;
		return true;
		
	}
	
	this.setPictures = function (aPictures) {
	
		if(aPictures.length>0) {
			this.pictures = aPictures;

			for(p=0; ((p<this.pictures.length) && (p<9999)); p++) {

				this.addImage(this.pictures[p]);
		
			}

			return true;
		}
		
		return false;
			
	}
	
	// other functions
	/*
	this.loadNext = function() {
		//alert('length: '+ this.pictures.length + ', childNodes: ' + $('kcaImages').childNodes.length);
		x = $('kcaImages').childNodes.length;
		if(x<this.pictures.length) {
			picture = this.pictures[x];
			picture = this.addImage(picture);
		
			addEvent(picture.newPicImg,'load',function() {
					window.iOSloader.checkDistance();
			});
			
			if(!ismobilesafari()) {
				window.iOSloader.loadNext();
			}
		} else {
			//alert('all images loaded');
		}
	}
	*/
/*
	this.checkDistance = function() {
	
		if(window.iOScroll.getScrollState) {
			state=window.iOScroll.getScrollState();
		
			if(state<this.minDistance) {
				//this.loadNext();
			}
		}
	}
	*/

	this.loadImages = function(x1,x2) {

		// loads all images within x1 to x2

		if(!this.target)
			return false;

		pics = this.target.childNodes;
		for(i=0; i<pics.length; i++) {
	
			if( (pics[i].posLeft >= x1) && (pics[i].posRight <= x2) ) { // check if matching to given window
			
				pics[i].loadImage();

			} 

		}
	}

	this.unloadImages = function(x1,x2) {

		// unloads all images outside of x1 to x2

		if(!this.target)
			return false;

		pics = this.target.childNodes;
		for(i=0; i<pics.length; i++) {
	
			if( (pics[i].posRight < x1) || (pics[i].posLeft > x2) ) { // check if matching to given window
			
				pics[i].unloadImage();

			} 

		}
	}

	this.autoLoadImages = function(scrollPos) {
		
		if(window.blockLoading)
			return false;

		loaderSize = 2000;
		loaderOffset = ((loaderSize-1000)/2);
		
		x1 = scrollPos-loaderOffset;
		x2 = scrollPos+loaderOffset+loaderSize;
	
		this.loadImages(x1,x2);
		this.unloadImages(x1,x2);
	}

	this.addImage = function(picture) {
	
		path=picture[0];
		width=picture[1];
		height=picture[2];
	
		newPic = document.createElement('div');
		newPic.className = 'kcaPic';
		newPic.style.width = width+'px';
		newPic.style.height = height+'px';
		newPic.loaded=false;
		
		newPicWrap = document.createElement('span');
		newPicWrap.className = 'kcaPicWrap';
		newPicWrap.style.width = width+'px';
		newPicWrap.style.height = height+'px';
		
		//newPicImg.style.display='none';
		newPic.loadImage = function() {
			
			if(!this.loaded) {
				//this.newPicImg.style.display='block';
				newPicImg = document.createElement('img');
				newPicImg.src = this.path;
				newPicImg.alt = '';
	
				this.loaded=true;
				this.wrap.appendChild(newPicImg);
				this.wrap.img = newPicImg;
			}
		}

		newPic.unloadImage = function() {
			//this.wrap.removeChild(this.wrap.img);
			this.wrap.innerHTML='';		
			this.loaded=false;	
			//this.newPicImg.style.display='none';
		}
		

		//addEvent(newPicImg,'click',function() { alert(this.src); });
		this.counter++;
		
		newPic.path = path;
		//newPicWrap.appendChild(newPicImg);
		newPic.appendChild(newPicWrap);

		newPic.wrap=newPicWrap;
		newPic.path = path;


		picPadding=10;
		newPic.posLeft=this.maxPosition;
		newPic.posRight=this.maxPosition+width+picPadding;
		
		
		if(this.target) {
			this.target.appendChild(newPic);

			this.maxPosition += width + picPadding;
			this.counter++;
			return newPic;
		}		
	
		return false;
		
	}
	
	// init
	//setInterval('window.iOSloader.checkDistance();',200);

}

