function imageEngine() {
    this.loader_img			= new Image();		//Betöltő kép
	this.imgs				= new Array();		//Képek
	this.selected			= 1;				//Kiválasztott kép
	this.previous			= 1;				//Az előzőleg kiválasztott kép
	this.container			= '';				//Hova kerüljenek a képek
	this.title_container	= '';				//Hova kerüljön a képaláírás
	this.popup				= '';				//Popup script
	this.id_prefix			= 'img_';			//képek prefix-e
	this.partibility		= new Array();		//Oszthatósági szabályok
	this.pagerFunction		= true;				//Lapozás kezelése
	this.ItemsOnPage		= 5;				//Egy oldalon lévő képek száma
	this.Pages				= 1;				//Oldalak száma
	this.selectedPage		= 1;				//Kiválasztott oldal

	//Események
	this.onNoNext			= function() {};
	this.onNoPrev			= function() {};
	this.onNoNextPage		= function() {};
	this.onNoPrevPage		= function() {};
	this.onChange			= function() {};
	this.onPartibility		= function( id, dir ) {};
	this.onPageChange		= function( old_page, next_page ) {};

}


imageEngine.prototype = {

	//Képek száma -- Array.length nem jól működött
	getImagesCount: function() {
		var imagesC = 0;
		var kepek = new Array();
		kepek = this.imgs;
		for (var i in kepek) {
			imagesC++;
		}
		//this.imgs.forEach( function( element, index, arr ) { imagesC++ }  );
		return imagesC;		
	},

	//Onclick esemény beállítása
	setPopup: function( str ) {
		this.popup = str;
	},

	//Közepes kép tárolására való container beállítása
	setContainer: function( str ) {
		this.container = str;
	},

	//Class név beállítása
	setClass: function( str ) {
		this.className = str;
	},	

	//Betöltőkép beállítása
	setLoaderImage: function( str ) {
		this.loader_img.src = str;
	},

	//Megnevezések tárolására való container beállítása
	setTitleContainer: function( str ) {
		this.title_container = str;
	},

	//Új oszthatóségi szabály (Azonosító, Irány{prev,next,both}, osztó, maradék)
	addPartibility: function( id, dir, num, mod ) {
		this.partibility.push( new Array( num, mod, id, dir ) );
	},

	//Pager Beállítása
	setPagers: function( itemsOnPage ) {
		this.ItemsOnPage = itemsOnPage;
		this.Pages = Math.ceil( this.imgs.length / this.ItemsOnPage );
		imgEngine.addPartibility( '_pager_next', 'next', itemsOnPage, 1 );
		imgEngine.addPartibility( '_pager_prev', 'prev', itemsOnPage, 0 );

		if ( this.Pages == 1 ) {
		    this.onNoNextPage();
		}

	    this.onNoPrevPage();
	},

	//Új kép hozzáadása
	addImage: function( id, img_path, imgtitle, preload ) {
		this.imgs[ id ]			= new Array();
		this.imgs[ id ]['img']	= img_path;		
		this.imgs[ id ]['args']	= arguments;
		this.imgs[ id ]['title']	= imgtitle;
		if ( preload ) { //A negyedik paraméter jelzi, hogy előtöltsük e a fájlt
		    this.imgs[ id ]['preloaded'] = this.preLoad( img_path );
		}
		this.Pages = Math.ceil( this.getImagesCount() / this.ItemsOnPage );
	},


	//Kép előtöltése
	preLoad: function( img_url ) {
		var img_pre = new Image();
		img_pre.src = img_url;
		img_pre.onclick = function() {
			eval(this.popup);
		}
		img_pre.container_name = this.container;
		img_pre.className = this.className;
		return img_pre;
	},


	//Onclick esemény értelmezése
	preparePopup: function( from, args, type ) {
		if ( type == 2 ) {	//Előre definiált képek esetében 2vel el kell tolni az argumentum listát
			for (i = 0; i < args.length; i++) {
				from = from.replace( '%%p'+i+'%%', args[i+3] );
			}
		} else {
			for (i = 0; i < args.length; i++) {
				from = from.replace( '%%p'+i+'%%', args[i+1] );
			}
		}
		return from;
	},


	//Kép csere ID alapján
	changeImgById: function( id ) {
		//Tároló definiálása
		var container_obj = document.getElementById( this.container )

		//Konténer ürítése
		container_obj.innerHTML = '';

		//Képaláírás cserélése
		if ( this.title_container !== '' ) {
			var title_container = document.getElementById( this.title_container );
			title_container.innerHTML = this.imgs[id]['title'];
		}


		//Kép előtöltése
		img_pre = this.preLoad( this.imgs[id]['img'] );

		//Aktuális kép beállítása
		this.previous = this.selected;
		this.selected = id;

		//onClick esemény feldolgozása
		var popup_event = this.preparePopup(this.popup, this.imgs[id]['args'], 2);
		img_pre.popup = popup_event;

		if ( !img_pre.complete ) { //Ha nem cachből jön akkor előtöltés kell

			//Betöltőkép létrehozása
			container_obj.appendChild(this.loader_img);
		    
			//Betöltődéskor megjelenítés
			img_pre.onload = function() {
				container_obj = document.getElementById( this.container_name );
				container_obj.innerHTML = '';
				container_obj.appendChild(this);
			}

		} else { // ha cacheből jön akkor egyből meg kell jeleníteni			
		    container_obj.appendChild(img_pre);
		    
		}

		this.onChange();

		//Ha nem létezik akkor lefuttatja a megfelelő eseményt
		if (typeof this.imgs[id+1] == 'undefined')
			this.onNoNext();

		//Ha nem létezik akkor lefuttatja a megfelelő eseményt
		if (typeof this.imgs[id-1] == 'undefined')
			this.onNoPrev();
	},


	//Oszthatóság ellenőrzése (Lapozáshoz)
	checkPartibility: function( dir ) {
		//Végigellenőrzi az összes oszthatósági szabályt
		for (i = 0; i < this.partibility.length; i++) {
			partibility = this.partibility[i];
			//Ha nem illik az adot irányra a feltétel akkor továbblép
			if ( partibility[3] !== 'both' && partibility[3] !== dir )
				continue;

			if ( (this.selected%partibility[0] == partibility[1]) && (partibility[2] == '_pager_next') ) {
			    this.nextPage();

			} else if ( (this.selected%partibility[0] == partibility[1]) && (partibility[2] == '_pager_prev') ) {
			    this.prevPage();

			} else if ( this.selected%partibility[0] == partibility[1] ) {
		        this.onPartibility( partibility[2], dir );
		    }
		}
	},

	// Következő képre léptetés
	nextPic: function() {
		//Következő kép
		selected = this.selected+1;
		//Ha létezik a következő kép akkor arra vált
		if (typeof this.imgs[selected] !== 'undefined') {
			//onClick esemény kiváltása
			document.getElementById(this.id_prefix + selected).onclick();

		}

		//Oszthatóság ellenőrzése
		this.checkPartibility( 'next' );
	},

	// Előző képre léptetés
	prevPic: function() {
		//Előző kép
		selected = this.selected-1;

		//Ha létezik az előző kép akkor arra vált
		if (typeof this.imgs[selected] !== 'undefined') {
			//onClick esemény kiváltása
			document.getElementById(this.id_prefix + selected).onclick();

		}

		//Oszthatóság ellenőrzése
		this.checkPartibility( 'prev' );
	},


	//Egy adott oldalra ugrás
	jumpToPage: function( page, selected ) {
		if ( page < 1 || page > this.Pages ) {
		    return false;
		}

	    document.getElementById( 'page_'+this.selectedPage ).style.display = 'none';
		document.getElementById( 'page_'+page ).style.display = 'block';
		if ( selected == 'first' ) {
			document.getElementById(this.id_prefix + ((page-1)*this.ItemsOnPage+1)).onclick();
		} else {
			document.getElementById(this.id_prefix + (page*this.ItemsOnPage)).onclick();
		}
		this.onPageChange( this.selectedPage, page );
		this.selectedPage = page;
	},

	//Következő oldalara ugrás
	nextPage: function() {
		this.jumpToPage( this.selectedPage+1, 'first' );

		if ( this.selectedPage == this.Pages ) {
		    this.onNoNextPage();
		}
	},

	//Előző oldalra ugrás
	prevPage: function() {
	    this.jumpToPage( this.selectedPage-1, 'last' );

		if ( this.selectedPage == 1 ) {
		    this.onNoPrevPage();
		}
	}

}