var start_time = 5
function display(){
	if(start_time > 0){
	    start_time -= 1;
	    setTimeout("display()",100);
	}
}

var MRs = {
	duration: 0.3,
	current: 1,
	last_cache_index: 1,
	cache: {},
	get_data: true,
	init: function(){
		this.season = $('season');
		this.season_ch = $('season-ch');
		this.season_ch._expanded = null;
		this.color = $('color');
		this.color_ch = $('color-ch');
		this.color_ch._expanded = null;

		this.growth = $('growth');
		this.growth_ch = $('growth-ch');
		this.growth_ch._expanded = null;
		this.signs_place = $('signs-place');
		this.growth.observe('click', function(){
			if(this.growth_ch._expanded){
				Effect.BlindUp(this.growth_ch, { duration: this.duration });
				this.growth_ch._expanded = false;
			}else{
				if(this.season_ch._expanded){
					Effect.BlindUp(this.season_ch, { duration: this.duration });
					this.season_ch._expanded = null;
				}
				if(this.color_ch._expanded){
					Effect.BlindUp(this.color_ch, { duration: this.duration });
					this.color_ch._expanded = null;
				}
				Effect.BlindDown(this.growth_ch, { duration: this.duration });
				this.growth_ch._expanded = true;
			}
		}.bind(this));

		this.season.observe('click', function(){
			if(this.season_ch._expanded){
				Effect.BlindUp(this.season_ch, { duration: this.duration });
				this.season_ch._expanded = false;
			}else{
				if(this.growth_ch._expanded){
					Effect.BlindUp(this.growth_ch, { duration: this.duration });
					this.growth_ch._expanded = null;
				}
				if(this.color_ch._expanded){
					Effect.BlindUp(this.color_ch, { duration: this.duration });
					this.color_ch._expanded = null;
				}
				Effect.BlindDown(this.season_ch, { duration: this.duration });
				this.season_ch._expanded = true;
			}
		}.bind(this));

		this.color.observe('click', function(){
			if(this.color_ch._expanded){
				Effect.BlindUp(this.color_ch, { duration: this.duration });
				this.color_ch._expanded = false;
			}else{
				if(this.growth_ch._expanded){
					Effect.BlindUp(this.growth_ch, { duration: this.duration });
					this.growth_ch._expanded = null;
				}
				if(this.season_ch._expanded){
					Effect.BlindUp(this.season_ch, { duration: this.duration });
					this.season_ch._expanded = null;
				}
				Effect.BlindDown(this.color_ch, { duration: this.duration });
				this.color_ch._expanded = true;
			}
		}.bind(this));

		this.place_1 = $("place-1");
		this.place_2 = $("place-2");
		this.place_3 = $("place-3");
		this.place_4 = $("place-4");
		this.place_5 = $("place-5");
		this.place_6 = $("place-6");
		this.place_7 = $('place-7');
		this.mr_1 	 = $("mr-1");
		this.mr_2 	 = $("mr-2");
		this.mr_3 	 = $("mr-3");
		this.mr_4 	 = $("mr-4");
		this.mr_5 	 = $("mr-5");
		this.mr_6 	 = $("mr-6");
		this.mr_7 	 = $("mr-7");
		this.mr_1._j = 1;
		this.mr_2._j = 2;
		this.mr_3._j = 3;
		this.mr_4._j = 4;
		if(this.mr_4){
			this.mr_4.is_showed = true;
			var ch = this.mr_4.childNodes;
			for(var i=0; i < ch.length; i++){
				if(ch[i].className == "m-tip"){
					this.addTipListener(this.place_4, ch[i]);
				}
			}
		}
		if(this.mr_5){
			this.mr_5._j = 5;
			this.mr_5.is_showed = true;
			var ch = this.mr_5.childNodes;
			for(var i=0; i < ch.length; i++){
				if(ch[i].className == "m-tip"){
					this.addTipListener(this.place_5, ch[i]);
				}
			}
		}
		if(this.mr_6){
			this.mr_6._j = 6;
			this.mr_6.is_showed = true;
			var ch = this.mr_6.childNodes;
			for(var i=0; i < ch.length; i++){
				if(ch[i].className == "m-tip"){
					this.addTipListener(this.place_6, ch[i]);
				}
			}
		}
		if(this.mr_7){
			this.mr_7._j = 7;
			this.mr_7.is_showed = true;
			var ch = this.mr_7.childNodes;
			for(var i=0; i < ch.length; i++){
				if(ch[i].className == "m-tip"){
					this.addTipListener(this.place_7, ch[i]);
				}
			}
		}
		this.mushrooms = $('mushrooms');
		this.arrow_left = $("arrow-left");
		this.arrow_right = $("arrow-right");
		this.arrow_left.className = "arrow-left-disabled";
		this.arrow_right.className = "arrow-right-disabled";
		this.disableAll();
		this.addToCache(js_list);

		display();
		this.arrow_left.observe('click', function(){
			if(this.arrow_left.can_move){
				if(start_time <= 0){
					this.current--;
					this.mainRender('left');
					start_time = 12;
				}
				display();
			}
		}.bind(this));

		this.arrow_right.observe('click', function(){
			if(this.arrow_right.can_move){
				if(start_time <= 0){
					this.current++;
					this.mainRender('right');
					start_time = 12;
				}
				display();
			}
		}.bind(this));
		this.rechangeHref();
	},
	addTipListener: function(p, c){
		p = $(p);
		c = $(c);
		p.observe("mouseover", function(_p, _c){
			new Effect.Opacity(_c, {from: 0.0, to: 1.0, duration: 0.7 });
		}.bind(this, p, c));

		p.observe("mouseout", function(_p, _c){
			new Effect.Opacity(_c, {from: 1.0, to: 0.0, duration: 0.7 });
		}.bind(this, p, c));
	},
	mainRender: function(cmd){
		var mr1 = this.current-3;
		var mr2 = this.current-2;
		var mr3 = this.current-1;
		var mr4 = this.current;
		var mr5 = this.current+1;
		var mr6 = this.current+2;
		var mr7 = this.current+3;
		if(this.mr_1 && mr1 > 0 && this.cache[mr1]){
			this.renderMr(this.mr_1, this.cache[mr1], 'small');
			this.mr_1.is_showed = true;
		}else if (this.mr_1){
			this.mr_1.hide();
			this.mr_1.is_showed = false;
		}
		if(this.mr_2 && mr2 > 0 && this.cache[mr2]){
			this.renderMr(this.mr_2, this.cache[mr2], 'small');
			this.mr_2.is_showed = true;
		}else if(this.mr_2){
			this.place_2.writeAttribute({'href':"#"});
			this.mr_2.hide();
			this.mr_2.is_showed = false;
		}
		if(this.mr_3 && mr3 > 0 && this.cache[mr3]){
			this.renderMr(this.mr_3, this.cache[mr3], 'small');
			this.mr_3.is_showed = true;
		}else if(this.mr_3){
			this.place_3.writeAttribute({'href':"#"});
			this.mr_3.hide();
			this.mr_3.is_showed = false;
		}
		if(this.mr_4 && mr4 > 0 && this.cache[mr4]){
			this.renderMr(this.mr_4, this.cache[mr4], 'big');
			this.mr_4.is_showed = true;
			if(this.cache[mr4]['signs'].length == 0){
				this.signs_place.update('-- Нет --');
			}else{
				this.signs_place.update(this.cache[mr4]['signs']);
			}
		}else if(this.mr_4){
			this.disAllowRight();
		}
		if(cmd == 'right'){
			if(!this.cache[mr5]){
				this.disAllowRight();
			}
		}else{
			if(!this.cache[mr3]){
				this.disAllowLeft();
			}
		}
		if(this.mr_5 && this.cache[mr5]){
			this.renderMr(this.mr_5, this.cache[mr5], 'small');
			this.mr_5.is_showed = true;
		}else if(this.mr_5){
			this.place_5.writeAttribute({'href':"#"});
			this.mr_5.hide();
			this.mr_5.is_showed = false;
		}
		if(this.mr_6 && this.cache[mr6]){
			this.renderMr(this.mr_6, this.cache[mr6], 'small');
			this.mr_6.is_showed = true;
		}else if(this.mr_6){
			this.place_6.writeAttribute({'href':"#"});
			this.mr_6.hide();
			this.mr_6.is_showed = false;
		}
		if(this.mr_7 && this.cache[mr7]){
			this.renderMr(this.mr_7, this.cache[mr7], 'small');
			this.mr_7.is_showed = true;
		}else if(this.mr_7){
			if(this.get_data){
				params = {'cmd': 'get_data'};
				if(_place){
					params["place"] = _place;
				}
				if(_season){
					params["season"] = _season;
				}
				if(_color){
					params["color"] = _color;
				}
				params["limit"] = 7;
				params["start"] = this.last_cache_index;
				Main.request(js_url+'ajax_mushrooms.php', {
					parameters: params,
					onSuccess: function(js){
						if(js["list"]["total"] > 0){
							this.addToCache(js["list"]);
							if(this.cache[mr7]){
								this.renderMr(this.mr_7, this.cache[mr7], 'small');
							}else{
								this.place_7.writeAttribute({'href':"#"});
								this.mr_7.hide();
							}
						}else{
							this.get_data = null;
							this.place_7.writeAttribute({'href':"#"});
							this.mr_7.hide();
							this.mr_7.is_showed = false;
						}
					},
					scope: this
				});
			}else{
				this.mr_7.hide();
				this.mr_7.is_showed = false;
			}

		}
		if(cmd == 'right'){
			this.allowLeft();
		}else{
			this.allowRight();
		}
		this.rechangeHref();
	},
	_gi: 1,
	renderMr: function(obj, data, img){
		if(this._gi >= 7){
			this._gi = 1;
		}
		this._gi++;
		setTimeout(function(_o){
			//obj.hide();
			//document.write('hide='+obj.id);
			//alert('hide='+obj.id);
			_o.fade({duration: 0.2});

		}.bind(this, obj), (this._gi*90));
		setTimeout(function(_o, _d, _i){
			//alert('show='+_o.id);
			//document.write('show='+_o.id);
			_o.update("");
			_o.writeAttribute("_href", _d['file_name']+'.html');
			_o.insert(new Element("a", {"href": _d['file_name']+'.html'}).insert(new Element("img", {"src": _d[_i]})));
			//<div class="m-tip" style="opacity: 0"><p>{$v.name}<br />{$v.cat}</p></div>
			var tip = new Element("div", {"class": "m-tip", "style": "opacity: 0"}).update('<p>'+_d['name']+'<br />'+_d['cat']+'</p>');
			switch(_o._j){
				case 1:
					var pl = this.place_1;
				break;
				case 2:
					var pl = this.place_2;
				break;
				case 3:
					var pl = this.place_3;
				break;
				case 4:
					var pl = this.place_4;
				break;
				case 5:
					var pl = this.place_5;
				break;
				case 6:
					var pl = this.place_6;
				break;
				case 7:
					var pl = this.place_7;
				break;
			}
			_o.insert(tip);
			this.addTipListener(pl, tip);
			_o.appear({duration: 0.9});
		}.bind(this, obj, data, img), (this._gi*100)+201);
		/*obj.writeAttribute({'href': data['file_name']+'.html'});
		var ch = $(obj.childNodes[0]);
		ch.writeAttribute({'src' : data[img]});
		if(img == 'small'){
			obj.show();
		}else{
			obj.hide();
			Effect.toggle(obj ,'appear');
		}*/
	},
	addToCache: function(list){
		for(var i=0; i< list['total']; i++){
			var preload_images = new Image();
			this.cache[this.last_cache_index] = list['data'][i];
			this.last_cache_index++;
			preload_images.src = list['data'][i]['big'];
		}
		if(list["total"] > 0){
			this.allowRight();
		}
	},
	allowRight: function(){
		this.arrow_right.className = "arrow-right-allowed";
		this.arrow_right.can_move = true;
	},
	allowLeft: function(){
		this.arrow_left.className = "arrow-left-allowed";
		this.arrow_left.can_move = true;
	},
	disAllowRight: function(){
		this.arrow_right.can_move = null;
		this.arrow_right.className = "arrow-right-disabled";
	},
	disAllowLeft: function(){
		this.arrow_left.can_move = null;
		this.arrow_left.className = "arrow-left-disabled";
	},
	disableAll: function(){
		this.arrow_left.can_move = null;
		this.arrow_right.can_move = null;
		this.arrow_left.className = "arrow-left-disabled";
		this.arrow_right.className = "arrow-right-disabled";
	},
	rechangeHref: function(){
		setTimeout(function(){
			if(this.mr_1 && this.mr_1.is_showed){
				this.place_1.writeAttribute({'href': this.mr_1.readAttribute("_href")});
			}else{
				this.place_1.writeAttribute({'href':"#"});
			}
			if(this.mr_2 && this.mr_2.is_showed){
				this.place_2.writeAttribute({'href': this.mr_2.readAttribute("_href")});
			}else{
				this.place_2.writeAttribute({'href':"#"});
			}
			if(this.mr_3 && this.mr_3.is_showed){
				this.place_3.writeAttribute({'href': this.mr_3.readAttribute("_href")});
			}else{
				this.place_3.writeAttribute({'href':"#"});
			}
			if(this.mr_4 && this.mr_4.is_showed){
				this.place_4.writeAttribute({'href': this.mr_4.readAttribute("_href")});
			}else{
				this.place_4.writeAttribute({'href':"#"});
			}
			if(this.mr_5 && this.mr_5.is_showed){
				this.place_5.writeAttribute({'href': this.mr_5.readAttribute("_href")});
			}else{
				this.place_5.writeAttribute({'href':"#"});
			}
			if(this.mr_6 && this.mr_6.is_showed){
				this.place_6.writeAttribute({'href': this.mr_6.readAttribute("_href")});
			}else{
				this.place_6.writeAttribute({'href':"#"});
			}
			if(this.mr_7 && this.mr_7.is_showed){
				this.place_7.writeAttribute({'href': this.mr_7.readAttribute("_href")});
			}else{
				this.place_7.writeAttribute({'href':"#"});
			}
		}.bind(this), 1100);
	}

}
Main.onReady(MRs.init, MRs);