var complete_image_time = 4000;
/**
 * MainEffect
 */
function MainEffect(playground) {
	Effect.call(this, playground);
	this.active_image = 0;
	this.images = load_images_list("obrazki.txt", false);
	this.plates_count = playground.width * playground.height;
	this.plates_list = Array.create(0, this.plates_count);
	this.menu_count = 0;
	this.qid = null;
	this.wait = true;
	this.loaded = 0;
}

MainEffect.prototype = new Effect();

MainEffect.prototype.initMenu = function(box) {
	var li = document.getElementById(box).getElementsByTagName("li");
	this.menu_count = li.length;
	var positions = Array.urandom(li.length, this.plates_count);
	
	for(var i=0; i<li.length; i++) {
		var plate = this.getPlayground().getPlate(positions[i]);
		plate.setOpacity(0);
		plate.setCurrent("gfx/static/" + i + ".jpg");
		plate.locked = true;
		var img = plate.getCurrent();
		img.alt = li[i].childNodes[0].firstChild.nodeValue;
		img.style.cursor = "pointer";
		this.myEffect(MainEffect.prototype.showPlate, [positions[i]], 50);
		Event.add(img, "click", MainEffect.prototype.setLocation, this, [li[i].childNodes[0].attributes["href"].nodeValue]);
	}
}

MainEffect.prototype.setLocation = function(new_location) {
	document.location.href = new_location;
}

MainEffect.prototype.initPlayground = function() {
	this.initMenu("navmenu");
	
	for(i=0; i<this.plates_count; i++) {
		var pid = this.plates_list[i];
		var plate = this.getPlayground().getPlate(pid);
		
		if (!plate.locked) {
			plate.setOpacity(0);
			plate.setCurrent(create_path(this.images[this.getActive()], pid));
			Event.add(plate.getCurrent(), "load", MainEffect.prototype.onLoad, this, [pid]);
		}
	}
}

MainEffect.prototype.onLoad = function(pid) {
	var plate = this.getPlayground().getPlate(pid);
	this.myEffect(MainEffect.prototype.showPlate, [pid], 50);
	plate.setNext(create_path(this.images[this.getNext()], pid));
	Event.remove(plate.getCurrent(), "load");
	
	if (++this.loaded == (this.plates_count - this.menu_count)) {
		this.myEffect(MainEffect.prototype.wait, [], complete_image_time);
		this.myEffect(MainEffect.prototype.newQueue, [], 10);
	}
}

MainEffect.prototype.newQueue = function() {
	this.qid = iq_create(MainEffect.prototype.changeImage, this, [], complete_image_time);
	this.nextImage();
	this.changeImage();
	return false;
}

MainEffect.prototype.init = function() {
	this.images = this.images.shuffle();
	this.plates_list = this.plates_list.shuffle();
	this.qid = iq_create();
	this.initPlayground();
}

MainEffect.prototype.wait = function() {
	this.wait = !this.wait;
	return !this.wait;
}

MainEffect.prototype.play = function() {
	this.init();
}

MainEffect.prototype.changeImage = function() {
	this.plates_list = this.plates_list.shuffle();
	var next = this.nextImage();
	
	for(i=0, k=this.plates_list.length; i<k; i++) {
		var pid = this.plates_list[i];
		
		if (!this.getPlayground().getPlate(pid).locked) {
			this.changePlate(pid, 40, 40, create_path(this.images[next], pid));
		}
	}
}

MainEffect.prototype.myEffect = function(effect, params, speed) {
	iq_add(this.qid, effect, this, params, speed);
}

MainEffect.prototype.showPlate = function(pid) {
	var plate = this.getPlayground().getPlate(pid);
	var opacity = plate.getOpacity();
	
	if (opacity < 1) {
		if (plate.getCurrent().complete)
			plate.setOpacity(opacity + 0.1);
			
		return true;
	}
	
	return false;
}

MainEffect.prototype.hidePlate = function(pid, change, new_src) {
	var plate = this.getPlayground().getPlate(pid);
	var opacity = plate.getOpacity();
	var change = change || false;
	
	if (opacity > 0) {
		plate.setOpacity(opacity - 0.1);
		return true;
	} else if (change) {
		plate.change(new_src);
	}
	
	return false;
}

MainEffect.prototype.changePlate = function(pid, hspeed, sspeed, new_src) {
	this.myEffect(MainEffect.prototype.hidePlate, [pid, true, new_src], hspeed);
	this.myEffect(MainEffect.prototype.showPlate, [pid], sspeed);
	// this.myEffect(MainEffect.prototype.wait, [], 1000);
}

MainEffect.prototype.getActive = function() {
	return this.active_image;
}

MainEffect.prototype.nextImage = function() {
	this.active_image += 1;
	
	if (this.active_image >= this.images.length)
		this.active_image = 0;
		
	return this.active_image;
}

MainEffect.prototype.getNext = function() {
	var next = this.getActive() + 1;
	
	if (next < this.images.length) {
		return next;
	}
	
	return 0;
}

