function imageEngine() {
    this.loader_img			= new Image();
	this.imgs				= new Array();
	this.selected			= 0;
	this.container			= '';
	this.title_container	= '';
	this.popup				= '';
	this.id_prefix			= 'img_';
	this.className			= '';
	this.partibility		= new Array();
	this.onNoNext			= function() {};
	this.onNoPrev			= function() {};
	this.onChange			= function() {};
	this.onPartibility		= function( id, dir ) {};
}


imageEngine.prototype = {

	//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 ) );
	},


	//Ú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 );
		}

	},


	//Kép előtöltése
	preLoad: function( img_url ) {
		var img_pre = new Image();
		img_pre.src = img_url;
		img_pre.style.border = '1px solid #d3a83c';
		img_pre.container_name = this.container;
		img_pre.className = this.className;
		return img_pre;
	},


	//Onclick esemény fordítása
	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 a paraméterek alapján
	changeImg: function( new_img, imgtitle ) {
		//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 = imgtitle;
		}

		//Kép előtöltése
		img_pre = this.preLoad( new_img );

		//onClick esemény feldolgozása
		var popup_event = this.preparePopup(this.popup, arguments);
		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);
		    
		}
	},


	//Kép csere ID alapján
	changeImgById: function( id ) {
		$('.xxxx').hide();
		$('#title_container_'+id).show();
		$('.imgbox').hide();
		$('#container_'+id).show();
		this.selected = id;
		this.onChange();
	},


	//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;

			//Ha megfelel az oszthatósági szabálynak akkor kiváltja az eseményt és paraméterben továbbítja a fő adatokat
		    if ( this.selected%partibility[0] == partibility[1] ) {
		        this.onPartibility( partibility[2], dir );
		    }
		}
	},

	// Következő képre léptetés
	nextPic: function(count) {
		if (!count) count = 1

		//Következő kép
		selected = this.selected+count;

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

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

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

	// Előző képre léptetés
	prevPic: function(count) {
		if (!count) count = 1

		//Előző kép
		selected = this.selected-count;

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

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

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

}