	
	var img = {};
	img.init = function (){
		var self = this;
		this.obj = $("#images");
		this.bg = $("#back_images");
		this.loaded_count = 0;
		this.imgs = [];
		this.text = [];
		this.current = 0;
		this.clickd = 0;
		var oimg;
		var conf = this.obj.find(".config div");
		for (var i=0 ; i<conf.length ; i++){
			if (i%2==0) {
				oimg = $('<img src="'+conf[i].innerHTML+'">');
				oimg.css({left: "-10000px", top: "-10000px", position: 'absolute'});
				oimg[0].onload = function() {self.loaded()}
				$(document.body).append(oimg);
				this.imgs.push(oimg);
			
			}else{
				this.text.push(conf[i].innerHTML);
			}
		}
		this.current = parseInt(Math.random()*1000)%3;
		this.bg.append(this.imgs[this.current]);
	}
	img.init2 = function() {
		var self = this;
		this.informer = this.obj.find("#informer");
		this.informer_text = this.obj.find("#informer div");
		this.control = this.obj.find("#control");
		this.control_links = this.obj.find("#control a");
		this.slider = this.obj.find("#control .slider");
		this.informer_text.html(this.text[this.current]);
		this.informer.slideDown(200);
		//this.informer.css("display", "block");
		this.control.slideDown(200, function(){self.slider_go(self.current); self.control_links[self.current].className = "selected";});
		this.timer = false;
		
		$.each(this.control_links, function(i,v){v.i=i});
		this.control_links.click(function(){
			self.clickd = 1;
			self.set(this.i);
			return false;
		});
		this.control_links.mouseover(function(){
			//dbg("+"+this.i);
			var i = this.i;
			if(self.timer) clearTimeout(self.timer);
			self.timer = setTimeout(function(){self.slider_go(i)}, 50);
		});
		this.control_links.mouseout(function(){
			//dbg("-"+this.i);
			if(self.timer) clearTimeout(self.timer);
			self.timer = setTimeout(function(){self.slider_go(self.current)}, 50);
		});
		
		img.auto(1);
	}
	img.set = function(x) {
		if (this.current == x) return false;
		this.control_links[x].className = "selected";
		this.control_links[this.current].className = "";
		this.imgs[this.current].before(this.imgs[x]);
		this.imgs[x].css("opacity", "1");
		this.imgs[this.current].animate({opacity: 0}, "slow");
		
		this.current = x;
		this.slider_go(this.current);
		this.informer_reload();
	}
	img.informer_reload = function() {
		var self = this;
		this.informer.slideUp(200, function(){
			self.informer_text.html(self.text[self.current]);
			self.informer.slideDown(400);
		});
	}
	img.slider_go = function (x){
		if(this.slider.current == x) return false;
		this.slider.current = x;
		this.slider.stop();
		this.slider.animate({left: (x*166+32)+"px"}, 'slow');
	}
	img.loaded = function() {
		this.loaded_count++;
		if (this.loaded_count==this.imgs.length) this.init2();
	}
	img.auto = function (start) {
		if(img.clickd) return false;
		if (!start)	img.set((img.current+1)%3);
		setTimeout(img.auto, 30000);
	}

$(function(){
	img.init();
	$("#city_select").change(function(){
		document.location = "/projects/"+$(this).val()+"/";
	});	
})
