function initSlide(){
	var _nodes = $('#sub-nav li:has("ul")');

	for(var i=0; i<_nodes.length; i++) {
		var cookiesValue = getCookie("menuActive"+i);
		if(cookiesValue != 'undefined' && cookiesValue != null && cookiesValue != -1) _nodes.eq(cookiesValue).addClass('active');
	} 

	_nodes.each(function(){
		var _hold = $(this);
		var _btn = _hold.children('a');
		var _box = _hold.children('ul');
		var _h = _box.height();

		if(_hold.hasClass('active')) _box.show();
		else _box.hide();
		
		_btn.click(function(){
				
			var today = new Date();
			var exp = new Date(today.getTime() + (365*24*60*60*1000));

			if(_hold.hasClass('active')){
				_hold.removeClass('active');
				_box.stop().animate({height:0}, 300, function(){
					$(this).css({display:'none', height: 'auto'});
					setCookie(("menuActive"+_nodes.index(_hold)), -1, exp.toGMTString(), "/");
				});
			}
			else{
				_hold.addClass('active');
				if(_box.is(':hidden')){
					_box.show();
					_h = _box.height();
					_box.height(0);
				}
				_box.stop().animate({height: _h}, 300, function(){
					$(this).height('auto');
					setCookie(("menuActive"+_nodes.index(_hold)), _nodes.index(_hold), exp.toGMTString(), "/");
				});
			}
			return false;
		});
	});
}
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

/*--- fade gall ---*/
function fadeGall(){
	var _d = 6000; //in ms
	$('#carusel').each(function(){
		var _this = $(this);
		var _list = $('ul.carusel-items li', _this);
		var _pause = $('a.pause',_this);
		var _btnPrev = $('a.prev-btn',_this);
		var _btnNext = $('a.next-btn',_this);
		var _cur = $('span.curent-element', _this);
		var _ElLength = $('span.elements-length', _this);
		_ElLength.text(_list.length);
		function rndNum(c) {return Math.floor(Math.random()*c+1)-1;}
		var _rndStart = rndNum(_list.length);
		_cur.text(_rndStart+1)
		_list.removeClass('active').eq(_rndStart).addClass('active');
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		var _t;
		_t = setTimeout(function(){
			if(_a < _list.length - 1) changeEl(_a + 1);
			else changeEl(0);
		}, _d);
		_pause.click(function(){
			if ($(this).hasClass('stop')){
				$(this).removeClass('stop');
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, _d);
			} else {
				$(this).addClass('stop');
				if(_t) clearTimeout(_t);
			}
			return false;
		})
		_btnPrev.click(function(){
			var _prevItem = 0;
			if (_a > 0) _prevItem = _a-1;
			else _prevItem = _list.length-1;
			changeEl(_prevItem);
			return false;
		})
		_btnNext.click(function(){
			var _nextItem = 0;
			if (_a < _list.length - 1) _nextItem = _a+1;
			else _nextItem = 0;
			changeEl(_nextItem);
			return false;
		})
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				 _list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:700});
				_list.eq(_ind).addClass('active').animate({opacity: 1}, {queue:false, duration:700});
				_a = _ind;
				_cur.text(_a+1)
			}
			_t = setTimeout(function(){
				if(_a < _list.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, _d);
		}
	});
}
$(document).ready(function(){
	initSlide();
	fadeGall();
});
