var _IE = (navigator.appName.indexOf('Microsoft Internet Explorer') != -1) ? true : false;
/*--- IE hover ---*/
function HoverEl(h_list){
	if(_IE){
		$$(h_list).each(function(_el){
			_el.onmouseover = function(){
				this.addClassName('hover');
			}
			_el.onmouseout = function(){
				this.removeClassName('hover');
			}
		});
	}
}
/*---- clear inputs function ---*/
function clearInputs(){
	$$('input[type="text"], input[type="password"], textarea').each(function(_el){
		if(!_el.val) _el.val = _el.value;
		_el.onfocus = function(){
			if(this.value == this.val) this.value = '';
		}
		_el.onblur = function(){
			if(this.value == '') this.value = this.val;
		}
	});
}
/*--- slide box ---*/
function topPopup(){
	var _speed = 500; //in ms
	$$('div.top-image li').each(function(_hold){
		var _box = _hold.select('div.popup')[0];
		var _btn = _hold.select('a')[0];
		if(_box && _btn){
			_box.style.display = 'none';
			if(!_IE) _box.setOpacity(0);
			_btn.onclick = function(){
				_box.style.display = 'block';
				if(!_IE) new Effect.Opacity(_box, { to: 1, duration: _speed/1000});
				return false;
			}
			var btn_close = _box.select('a.close')[0];
			if(btn_close){
				btn_close.onclick = function(){
					if(_IE) _box.style.display = 'none';
					else new Effect.Opacity(_box, { to: 0, duration: _speed/1000, 'afterFinish': function(){ _box.style.display = 'none';}});
					return false;
				}
			}
		}
	});
}
/*--- gallery function ---*/
function initGallery(){
	var stay_time = 5000; //in ms
	var change_speed = 600; //in ms
	var move_speed = 800; //in ms
	var _hold = $('gallery-hold');
	if(_hold){
		var _f1 = true;
		var _f2 = true;
		var _t;
		var box_list = _hold.select('div.images li');
		var list_hold = _hold.select('div.gallery-scroll div.holder ul')[0];
		var _list = list_hold.childElements();
		var list_w = _list.length * _list[0].offsetWidth;
		var hold_w = list_hold.parentNode.offsetWidth;
		var _step = hold_w;
		var _a = -1;
		_hold.select('div.gallery-scroll')[0].innerHTML += '<a href="#" class="pause">play/pause</a><div class="pagging"></div>';

		_hold.select('div.pagging')[0].innerHTML += '<div class="p-l"><div class="p-r"><a href="#" class="prev">prev</a><span></span><a href="#" class="next">next</a></div></div>';
		var btn_prev = _hold.select('div.gallery-scroll div.pagging a.prev')[0];
		var btn_next = _hold.select('div.gallery-scroll div.pagging a.next')[0];
		var _info = _hold.select('div.gallery-scroll div.pagging span')[0];
		_info.innerHTML = '1 - ' + ((5 > _list.length)?(_list.length):(5)) + ' of ' + _list.length;
		var step_k = 0;
		list_hold.style.left = -step_k*_step+'px';
		if(list_w <= hold_w){
			btn_prev.style.display = 'none';
			btn_next.style.display = 'none';
		}
		btn_prev.onclick = function(){
			if(step_k > 0) moveList(step_k - 1);
			else moveList(Math.ceil((list_w - hold_w)/_step));
			return false;
		}
		btn_next.onclick = function(){
			if(step_k < Math.ceil((list_w - hold_w)/_step)) moveList(step_k + 1);
			else moveList(0);
			return false;
		}
		var btn_pause = _hold.select('div.gallery-scroll a.pause')[0];
		btn_pause.onclick = function(){
			if(btn_pause.hasClassName('play')){
				btn_pause.removeClassName('play');
				_f2 = true;
				if(_t) clearTimeout(_t);
				if(_f1 && _f2 && stay_time){
					_t = setTimeout(function(){
						if(_a < _list.length - 1) changeEl(_a + 1);
						else changeEl(0);
					}, stay_time);
				}
			}
			else{
				btn_pause.addClassName('play');
				_f2 = false;
				if(_t) clearTimeout(_t);
			}
			return false;
		}
		list_hold = _hold.select('div.gallery-scroll div.holder ul')[0];
		_list = list_hold.childElements();
		_hold.onmouseover = function(){
			_f1 = false;
			if(_t) clearTimeout(_t);
		}
		_hold.onmouseout = function(){
			_f1 = true;
			if(_t) clearTimeout(_t);
			if(_f1 && _f2 && stay_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time);
			}
		}
		for(var i = 0; i < _list.length; i++){
			if(_a == -1 && _list[i].hasClassName('active')) _a = i;
			_list[i].removeClassName('active');
			box_list[i].removeClassName('active');
			if(_IE) box_list[i].style.display = 'none';
			else box_list[i].setOpacity(0);
			_list[i]._i = i;
			_list[i].onmouseover = function(){
				changeEl(this._i);
				return false;
			}
		}
		
		if(_a == -1) _a = 0;
		_list[_a].addClassName('active');
		box_list[_a].addClassName('active');
		if(_IE) box_list[_a].style.display = 'block';
		else box_list[_a].setOpacity(1);
		if(_f1 && _f2 && stay_time){
			_t = setTimeout(function(){
				if(_a < _list.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, stay_time);
		}
		
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_a != _ind){
				moveList(Math.ceil((_ind+1)/5) - 1);
				_list[_a].removeClassName('active');
				_list[_ind].addClassName('active');
				box_list[_a].removeClassName('active');
				box_list[_ind].addClassName('active');
				if(_IE){
					box_list[_a].style.display = 'none';
					box_list[_ind].style.display = 'block';
				}
				else{
					new Effect.Opacity(box_list[_a], {to: 0, duration: change_speed/1000});
					new Effect.Opacity(box_list[_ind], {to: 1, duration: change_speed/1000});
				}
				_a = _ind;
			}
			if(_f1 && _f2 && stay_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time+change_speed);
			}
		}
		function moveList(_ind){
			if(_info) _info.innerHTML = (_ind*5+1) + ' - ' + ((_ind*5+5 > _list.length)?(_list.length):(_ind*5+5)) + ' of ' + _list.length;
			new Effect.Morph(list_hold, {
				style: {'left': -_ind*_step+ 'px'},
				duration: move_speed/1000
			});
			step_k = _ind;
		}
	}
}
/*--- slide blocks ---*/
function slideBlocks(){
	$$('div.add-slide').each(function(_hold){
		var _btn = _hold.select('span.add-link a')[0];
		var _box = _hold.select('div.slide-hold')[0];
		if(_btn && _box){
			_box.style.display = 'block';
			if(_hold.hasClassName('opened')){
				new Effect.SlideDown(_box, { duration: 0});
				_btn.innerHTML = _btn.innerHTML.replace('+ Expand', '- Collapse');
			}
			else{
				new Effect.SlideUp(_box, { duration: 0});
				_btn.innerHTML = _btn.innerHTML.replace('- Collapse', '+ Expand');
			}
			_btn.onclick = function(){
				if(_hold.hasClassName('opened')){
					_hold.removeClassName('opened');
					_btn.innerHTML = _btn.innerHTML.replace('- Collapse', '+ Expand');
					new Effect.SlideUp(_box, { duration: 0.4});
				}
				else{
					_hold.addClassName('opened');
					_btn.innerHTML = _btn.innerHTML.replace('+ Expand', '- Collapse');
					new Effect.SlideDown(_box, { duration: 0.4});
				}
				return false;
			}
		}
	});
}
document.observe("dom:loaded", function(){
	HoverEl('#nav li, .gallery .gallery-scroll li');
	clearInputs();
	topPopup();
	initGallery();
	slideBlocks();
});
