function initAll() {
	initSearch();
	initZoom();
}

function initSlider() {
	new Slider();
}

function initAuth(){
	this.f = $('auth_form');
	this.l = $('auth_login');
	this.p = $('auth_password');
	this.b = $('auth_enter');
	this.err = $('auth_error');

	if (this.f && this.l && this.p && this.b){
		this.submit = function(e){
			e.preventDefault();

			var msg = '';

			if (this.l.value=='логин' || this.l.value=='') {
				msg = 'Введите логин';
			} else if(this.p.value=='') {
				msg = 'Введите  пароль';
			} else if(this.p.value.length<6) {
				msg = 'Пароль не может быть короче 6 символов';
			}

			if (msg){
				this.err.update(msg);
				this.err.show();
				e.preventDefault();
				return false;
			} else {
				this.f.submit();
			}
		};

		this.l.observe('blur', function(){
			if (this.l.value=='')this.l.value='логин'
		}.bind(this));

		this.l.observe('focus', function(){
			if (this.l.value=='логин')this.l.value='';
			this.err.update('');
			this.err.hide();
		}.bind(this));

		this.p.observe('focus', function(){
			this.err.update('');
			this.err.hide();
		}.bind(this));

		this.b.observe('click', function(e){
			this.submit(e);
		}.bind(this));

		this.f.observe('submit', function(e){
			this.submit(e);
		}.bind(this));
	}
}

function initSearch(){
	var f = $('search_form');
	var s = $('search');
	var ef = $('search_error');

	if (s && f){
		s.observe('blur', function(){
			if (s.value=='')s.value='поиск'
			ef.update();
			ef.hide();
		});

		s.observe('focus', function(){
			if (s.value=='поиск')s.value='';
			ef.update();
			ef.hide();
		});

		s.observe('keypress', function(){
			ef.update();
			ef.hide();
		});

		f.observe('submit', function(e){
			var msg = '';

			if (s.value=='поиск' || s.value=='')
				msg = 'Введите фразу для поиска';

			if(s.value.length<3)
				msg = 'Поисковая фраза не может быть короче 3 символов';

			if (msg){
				ef.update(msg);
				ef.show();
				e.preventDefault();
				return false;
			}
		});
	}
}

function initZoom(){
	var z = $('zoom');
	var s = $('scheme');
	if (z && s){
		z.observe('click', function(e){
			e.preventDefault();

			if (z.hasClassName('zoom_in') && s.readAttribute('original_width')) {
				z.className = 'zoom_out';
				s.style.width = s.readAttribute('original_width')+'px';
			} else {
				z.className = 'zoom_in'
				s.style.width = '350px';
			}
		});
	}
}

var ProductSearch = Class.create({
	initialize: function(url) {
		this.url = url;
		this.pList = $('producer_list');
		this.aList = $('agregate_list');
		this.producerId = this.pList.value;
		this.agregateCache = new Array();

		this.pList.observe('change', function(e){
			this.producerId = this.pList.value;
			this.loadAgregateList();
		}.bind(this));
	},
	loadAgregateList: function() {
		if (this.producerId) {
			if (this.agregateCache[this.producerId]) {
				this.fillList(this.agregateCache[this.producerId]);
			} else {
				new Ajax.Request(this.url+'?ajax=1&producer='+this.producerId , {
					method: 'get',
					onSuccess: function(transport) {
						if (transport.responseText.length) {
							var data = transport.responseText.evalJSON();
							if (data.producer_id){
								this.agregateCache[data.producer_id] = data.list;
								this.fillList(data.list);
							}
						}
					}.bind(this)
				});
			}
		}
	},
	fillList: function(list){
		if(this.aList.options.length){
			while(this.aList.options.length)
				this.aList.options[0]=null;
		}

		if (list){
			this.aList.appendChild(new Element('option', { 'value':'' }).update('не выбрано'));


			$H(list).values().each(function(el){
				this.aList.appendChild(new Element('option', { 'value': el.id}).update(el.name));
			}.bind(this));
		}
	}
});

var Slider = Class.create({
	initialize: function() {
		this.box = $('slider');
		this.list = new Array();
		this.slide = null;
		this.backBtn = $('back');
		this.forwardBtn = $('forward');
		this.n = 0;
		this.ready=true;
		this.TID = null;
		this.delay = 4000;//ms

		if (!this.box || !this.forwardBtn || !this.backBtn)
			return;

		var sList = $$('div.effect');
		if (sList) {
			sList.each(function(el){
				el.n=this.n;
				el.content=el.firstDescendant();

				this.addEventStart(el, 'mouseout');
				this.addEventStop(el, 'mouseover');

				this.list[this.list.length]=el
				this.n++;
			}.bind(this));

			if (this.list.length){
				this.slide = this.list[0];
			}
		};

		this.forwardBtn.observe('click', function(e){
			e.preventDefault();
			if (this.ready)this.forward();
		}.bind(this));
		this.addEventStart(this.forwardBtn, 'mouseout');
		this.addEventStop(this.forwardBtn, 'mouseover');

		this.backBtn.observe('click', function(e){
			e.preventDefault();
			if (this.ready)this.back();
		}.bind(this));
		this.addEventStart(this.backBtn, 'mouseout');
		this.addEventStop(this.backBtn, 'mouseover');

		this.start();
	},

	addEventStart:function(el, action){
		el.observe(action, function(e){
			e.preventDefault();
			this.start();
		}.bind(this));
	},

	addEventStop:function(el, action){
		el.observe(action, function(e){
			e.preventDefault();
			this.stop();
		}.bind(this));
	},

	start:function() {
		if (!this.TID){
			this.TID = setInterval(function(){
				this.forward();
			}.bind(this), this.delay);
		}
	},

	stop:function() {
		if(this.TID)clearInterval(this.TID);
		this.TID=null;
	},

	forward:function() {
		var next = this.getNext();
		if (this.slide=='undefined' || next==null || this.n<=1 || this.ready==false)
			return;

		this.ready=false;
		this.box.style.backgroundPosition = 'center bottom';
		this.box.style.backgroundImage = next.content.style.backgroundImage;
		this.slide.style.cssFloat='right';

		new Effect.SlideUp(this.slide,
		{
			scaleX:true,
			scaleY:false,
			//scaleFromCenter:true,
			duration:0.4,
			afterFinish:function(){
				this.slide.style.cssFloat='none';
				next.show();
				this.slide = next;
				this.ready = true;
			}.bind(this)
		});
	},

	back:function() {
		var prev = this.getPrev();
		if (this.slide=='undefined' || prev==null || this.n<=1 || this.ready==false)
			return;

		this.ready=false;
		this.box.style.backgroundPosition = 'center bottom';
		this.box.style.backgroundImage = prev.content.style.backgroundImage;


		new Effect.SlideUp(this.slide,
		{
			scaleX:true,
			scaleY:false,
			//scaleFromCenter:true,
			duration:0.4,
			afterFinish:function(){
				prev.show();
				this.slide = prev;
				this.ready = true;
			}.bind(this)
		});
	},

	getNext: function() {
		if(this.list.length>1 && this.slide){
			if (this.slide.n+1>=this.n){
				return this.list[0];
			} else {
				return this.list[this.slide.n+1];
			}
		}
		return null;
	},

	getPrev: function() {
		if(this.list.length>1 && this.slide){
			if (this.slide.n-1<0){
				return this.list[this.n-1];
			} else {
				return this.list[this.slide.n-1];
			}
		}
		return null;
	}
});
