$(document).ready(function(){
	initNav("#nav li");
	/*initToolbar();
	initToolbarDrop()*/
	initSlider();
	initNav();
	
})

/*Menu*/
function initNav(h_list, h_class){
	if(!h_class) var h_class = 'hover';
	$(h_list).mouseenter(function(){
		$(this).addClass(h_class);
		$(this).find("ul").css({"z-index":10});
	}).mouseleave(function(){
		$(this).removeClass(h_class);
		$(this).find("ul").css({"z-index":0});
	});
}

function initNav(){ 
	var _duration = 50;
	$('#nav > li').each(function(){
		var _hold = $(this);
		var _box = _hold.find('ul').eq(0);
		var _btn = _hold.find('a.opener');
		var _h = _box.height();
		_box.hide();
		
		_hold.hover(function(){
			if(_box.is(':hidden')){
				_box.show();
				_h = _box.height();
				_box.height(0);
			}
			_box.stop().animate({height: 15}, _duration, function(){ 
				$(this).height('auto');
			});
			_box.css('zIndex', 15);
		}, function(){
			_box.stop().animate({height:0}, _duration, function(){
				$(this).css({display:'none', height:'auto'});
			});
			_box.css('zIndex', 5);
		});
	});
}

/*Personal Toolbar*/
function initToolbar(){
	var _holder = $(".toolbar-holder") 
	var _opener = _holder.find(".opener")
	var _closer = _holder.find(".closer")
	var _toolbar = _holder.find(".toolbar");
	_opener.click(function(){
		_toolbar.css({"display":"block"}).animate({"top": 0 }, 400);
		return false;
	})
	_closer.click(function(){
		_toolbar.animate({"top": -34 }, 400,function(){
			$(this).css({"display":"none"});
		});
		return false;
	})
}

function initToolbarDrop(){
	var _list = $(".toolbar-list")
	var _opener = _list.find("a").not(".drop a");
	var _closer = _list.find(".btn-close");
	_opener.click(function(){
		var _li = $(this).parent();
		_ifdrop = _li.find(".drop").hasClass("drop")
		if (_ifdrop) {
			_li.addClass("active");
			_li.find(".drop").css({"display":"block"});
			return false;
		}
	})
	_closer.click(function(){
		$(this).parents("li").removeClass("active").find(".drop").css({"display":"none"});
		return false;
	})
}

/*Homepage Sliders*/
function initSlider(){
	var wait_time = 5000;
	var change_speed = 800; //in ms
	var _t;
	var _f = true;
	$('div.gallery-holder').each(function(){
		var _btn = $(this).find('.switcher a');
		var list_h = $(this).find('ul.gallery');
		var _list = list_h.children('li');
		var _step = list_h.parent().width();
		var _a = _btn.index(_btn.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		
		_btn.removeClass('active').eq(_a).addClass('active');
		_btn.click(function(){
			if (_btn.eq(_btn.index(this)).hasClass('active')) {
				window.open($(this).attr('href').val());
			} else {
				moveList(_btn.index(this));
			}
			return false;
		});
		
		list_h.mouseenter(function(){
			_f = false;
			if(_t) clearTimeout(_t);
		}).mouseleave(function(){
			_f = true;
			if(_t) clearTimeout(_t);
			if(_f && wait_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) moveList(_a + 1);
					else moveList(0);
				}, wait_time);
			}
		});
		if(_f && wait_time){
			_t = setTimeout(function(){
				if(_a < _list.length - 1) moveList(_a + 1);
				else moveList(0);
			}, wait_time);
		}
		
		
		function moveList(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_btn.eq(_a).animate({"width": 20 + "px" }, 300, function(){
					_btn.eq(_a).removeClass('active');
					_btn.filter('.active').removeClass('active');
				})
				_btn.eq(_ind).animate({"width": 100 + "%" }, 400, function(){
					_btn.eq(_ind).addClass('active');
				})
				list_h.animate({marginLeft: -_ind*_step}, {queue:false, duration:change_speed});
				_a = _ind;
				if(_f && wait_time){
					_t = setTimeout(function(){
						if(_a < _list.length - 1) moveList(_a + 1);
						else moveList(0);
					}, wait_time);
				}
			}
		}
	});
}

