// page init
$(document).observe("dom:loaded", function() {
	initSlideShow();
	initAccordion();
	hoverForIE6('#nav li');
	initPopup();
});

function hoverForIE6(_list) {
	if(navigator.appName.indexOf('Microsoft Internet Explorer') != -1){
		$$(_list).each(function(_el){
			_el.onmouseover = function(){
				this.addClassName('hover');
			}
			_el.onmouseout = function(){
				this.removeClassName('hover');
			}
		});
	}
}
function initAccordion() {
	var _slideDuration = 0.5;
	var _activeClass = 'open';
	var _accordions = $$('div.accordion');
	var _minHeight = 75;
	_accordions.each(function(obj, i){
		var _holder = obj;
		var _items = _holder.select('div.box');
		_items.each(function(_obj, _ind){
			var _curitem = _obj;
			var _opener = _curitem.getElementsBySelector('a.opener')[0];
			var _drop = _curitem.getElementsBySelector('div.slide')[0];
			if(_drop) {
				var _dropHeight = _drop.getDimensions().height;
				if(_dropHeight < _minHeight) _dropHeight = _minHeight;
				if(!_curitem.hasClassName(_activeClass)) {
					_drop.setStyle({'height':'0'});
				}
				_opener.onclick = function () {
					if(_curitem.hasClassName(_activeClass)) {
						_curitem.removeClassName(_activeClass);
						new Effect.Morph(_drop, {
							style: {height: (0+'px')},
							duration: _slideDuration
						});
					} else {
						var _drops = _holder.getElementsBySelector('div.slide');
						var _activeItems = [];
						_items.each(function(it){
							if(it.hasClassName('open')) _activeItems.push(it);
						});
						_drops.each(function(_dr, _ind){
							new Effect.Morph(_dr, {
								style: {height: (0+'px')},
								duration: _slideDuration,
								afterFinish: function(){
									_activeItems.each(function(el){
										el.removeClassName(_activeClass)
									});
								}
							});
							_dr.up();
						});
						_curitem.addClassName(_activeClass);
						new Effect.Morph(_drop, {
							style: {height: (_dropHeight+'px')},
							duration: _slideDuration
						});
					}
					return false;
				}
			}
		});
	});
}
// slideshow function
function initSlideShow() {
	var _duration = 0.65;
	var _switchTime = 4500;
	var _autoSlide = true;
	var _activeClass = 'active';
	var _slideshow = $$('#carousel');

	_slideshow.each(function(obj, ind){
		// gallery options
		var _holder = obj;
		var _thumbs = _holder.select('ul.swicher a');
		var _slides = _holder.select('ul.slideset li');
		var _currentIndex = 0;
		var _oldIndex = 0;
		var _timer;

		// gallery control
		_thumbs.each(function(obj, ind){
			obj.observe('click', function(event){
				event.preventDefault();
				if(ind != _currentIndex) {
					_oldIndex = _currentIndex;
					_currentIndex = ind;
					switchSlide();
				}
			});
		});
		_slides.each(function(obj, ind){
			if(ind!=_currentIndex) {
				obj.setStyle({
					'display':'none',
					'opacity':0
				});
			} else {
				obj.setStyle({
					'display':'block',
					'opacity':1
				});
			}
		});

		// gallery animation
		function switchSlide() {
			var _prevSlide = _slides[_oldIndex];
			var _nextSlide = _slides[_currentIndex];
			_thumbs[_oldIndex].removeClassName(_activeClass)
			_thumbs[_currentIndex].addClassName(_activeClass)

			// fadeOut slide
			_prevSlide.removeClassName(_activeClass);
			new Effect.Morph(_prevSlide, {
				style: {'opacity': '0'},
				afterFinish: function() {
					_prevSlide.setStyle({'display':'none'});
				},
				duration: _duration
			});

			// fadeIn slide
			_nextSlide.setStyle({'display':'block'})
			_nextSlide.addClassName(_activeClass);
			new Effect.Morph(_nextSlide, {
				style: {'opacity': '1'},
				duration: _duration
			})

			autoSlide()
		}

		// autorotation
		function autoSlide() {
			if(_autoSlide) {
				if(_timer) clearTimeout(_timer);
				_timer = setTimeout(function(){
					_oldIndex = _currentIndex;
					if(_currentIndex<_slides.length-1) _currentIndex++;
					else _currentIndex=0;
					switchSlide();
				},_switchTime);
			}
		}
		autoSlide()
	})
}
function initPopup(){
	var _hold = $$('#sign-block')[0];
	var _box = _hold.select('div.column');
	_box.each(function(obj, ind){
		var _link = obj.select('div.column-block');
		_link.each(function(objLink, indI){
			objLink.onmouseover = function(){
				var _pop = this.parentNode.select('div.sign-popup')[0];
				showPopups(_pop);
				this.addClassName('opened');
			}
			objLink.onmouseout = function(){
				hidePopups();
				return false;
			}			
/*
			objLink.onclick = function(){
				var _pop = this.parentNode.select('div.sign-popup')[0];
				if(this.hasClassName('opened')){
					this.removeClassName('opened');
					hidePopups();
				}
				else{
					showPopups(_pop);
					this.addClassName('opened');
				}
			}
*/
		})
	})
	var _boxes = _hold.select('div.sign-popup');
	
	_boxes.each(function(obj, ind){
		var _closer =  obj.select('a.close')[0];
		
		_closer.onclick = function(){
				hidePopups();
				return false;
			}
	});
	
	function showPopups(_item){
		var _allPops = _hold.select('div.sign-popup');
		_allPops.each(function(obj, ind){
			obj.setStyle({
					'display':'none'
				});
		});
		_item.setStyle({
					'display':'block'
				});
		
	};
	function hidePopups(){
		var _allPops = _hold.select('div.sign-popup');
		_allPops.each(function(obj, ind){
			obj.setStyle({
					'display':'none'
				});
		});
	};
}