// JavaScript Document
var other = {
	init : function(){
		this.menu = $('aside');
		this.menuItems = this.menu.find('a');
		this.menuLi = this.menu.find('li');
		this.initMenu().initNews();
		
		if($.browser.msie) 
			$('a.toggle-link').live('click', function(){
				if($(this).hasClass('active'))
					$(this).removeClass('active').next('.toggle-block').height(0);
				else
					$(this).addClass('active').next('.toggle-block').height('auto');
			});
		else
			$('a.toggle-link').live('click', function(){
				var next = $(this).toggleClass('active').next('.toggle-block').get(0);
				if($(this).hasClass('active'))
					$(next).stop(true).animate({height: next.scrollHeight}, 500, function(){ $(this).height('auto') });
				else
					$(next).stop(true).animate({height: 0}, 500);
			});
			
		
	},
	
	initMenu : function(){
		this.menuItems.parent().css('z-index', function(i){
			return other.menuItems.length - i;
		});
		if(!$.cookie('menu')){
			other.menu.addClass('showed');
			other.menuItems.css('left', 0);
			$(document).one('click', function(){
				other.menuLi.mouseenter().mouseleave();
			})
		}else{
			 this.menu.addClass('closed');
			other.menuItems.css('left', function(){
				return -$(this.parentNode).width()
			});
		}
		
		var animated = false;
		this.menuLi.mouseenter(function(){
			if(animated) return;
			if(!other.menu.hasClass('showed')){
				animated = true;
				other.menuItems.each(function(){
					$(this).stop(true).animate({left: 0}, 500, 'easeInQuad', function(){ 
						other.menu.addClass('showed'); 
						animated = false;
						$(this).css('left', 0);
					});
				});
				other.menu.removeClass('closed');
			}
			$.cookie('menu', true, {expires:1});
		});
		this.menu.mouseleave(function(){
			animated = false;
			if(!other.menu.hasClass('closed')){
				other.menuItems.stop(true).delay(100).each(function(){
					$(this).animate({left: - $(this.parentNode).width()}, 500, 'easeInQuad', function(){ 
						other.menu.addClass('closed'); 
						other.menuItems.css('left', function(){ return - $(this.parentNode).width(); });
					});
				});
				other.menu.removeClass('showed');
			}
		});
		
		this.menu.removeClass('load');
		if($.browser.msie) this.menu.ieClearType();
		return this;
	},
	initNews : function(){
		this.newsBlock = $('#news-block');
		
		if(this.newsBlock.is('.complete')) return this;
		
		this.newsWrap = $('#news-wrapper');
		this.newsUl = $('#news-tabs');
		this.newsTabs = $('#news-tabs > li');
		this.newsClose = $('a.news-close', this.newsBlock);
		this.newsToggle = $('a#news-toggle', this.newsBlock);
		
		this.newsTabs.children('a').click(function(e){
			$(this.parentNode).active(true).siblings('.active').active(false);
			e.preventDefault();
			e.stopPropagation();
		});
		
		var timeout = 5e3, timer, 
		
		startTimer = function(){
			timer = setInterval(function(){
				if(!other.newsTabs.filter('.active').next().children('a').click().length) other.newsTabs.filter(':first').children('a').click();
			}, timeout);
		}, 
		
		stopTimer = function(){
			clearInterval(timer);
		};
		
		startTimer();
		this.newsBlock.hover(stopTimer, startTimer);
		
		//FOLDING
		other.newsWrap.css({
			height: function(){ return $(this.parentNode).height(); },
			width: function(){ return $(this.parentNode).width(); }
		});
		
		var foldS = {width:85, height:22}, duration = 500, w, h,
			fold = function(now){
					w = w || other.newsBlock.width();
					h = h || other.newsBlock.height();
				if(ie6 || (typeof now == 'boolean' && now)){
					other.newsWrap.add(other.newsBlock).stop(true).css(foldS);
					other.newsUl.stop(true).fadeTo(0, 0);
					other.newsToggle.stop(true).show().fadeTo(0, 1).removeClass('open').ieClearType();
				}else{
					other.newsWrap.add(other.newsBlock).stop(true).animate(foldS, duration);
					other.newsUl.stop(true).delay(duration).fadeTo(duration * 0, 0);
					other.newsBlock.css('overflow', 'visible');
					other.newsToggle.stop(true).delay(duration).queue(function(){ $(this).show().dequeue(); }).fadeTo(0, 1, function(){ $(this).removeClass('open').ieClearType(); });
				}
				$.cookie('foldnews', 'false');
			},
			unfold = function(){
				if(ie6){
					other.newsWrap.add(other.newsBlock).stop(true).css({width: w, height: h});
					other.newsUl.fadeTo(0, 1, function(){$(this).ieClearType();});
					other.newsToggle.fadeTo(0, 0).hide().addClass('open');
				}else{
					other.newsWrap.add(other.newsBlock).stop(true).animate({width: w, height: h}, duration);
					other.newsUl.stop(true).fadeTo(duration * 0, 1, function(){$(this).ieClearType();});
					other.newsBlock.css('overflow', 'visible');
					other.newsToggle.stop(true).fadeTo(duration * .5, 0, function(){ $(this).hide().addClass('open'); });
				}
				$.cookie('foldnews', 'true');
			}
		other.newsClose.click(fold);
		other.newsToggle.click(unfold);
		if($.cookie('foldnews') == "false") fold(true);
		this.newsBlock.addClass('complete');
		return this;
	},
	initFolding: function(){
		//as sample: folding tables
		var cls = 'folder-active';
		$('div.folder:not(.complete) > a.js + div.folded', window.context).prev().each(function(){
			$(this).next().height($(this).parent().hasClass(cls) ? 'auto' : 0);
			
			$(this).click(function(){
				App.autoscroll(true);
				if(ie6)
					$(this).next().toggle().parent().toggleClass(cls);
				else
					$(this).next().animate({height: $(this).parent().hasClass(cls) ? 0 : $(this).next().get(0).scrollHeight}, 500).parent().toggleClass(cls);
				setTimeout(function(){
					App.autoscroll(false);
					if(catalog.refreshPoints) catalog.refreshPoints();
				}, 1000); 
			});
			$(this.parentNode).addClass('complete');
		}).selectHover();
		
		
		if($.browser.msie)
			$('a.toggle-link', window.context).each(function(){
				if($(this).hasClass('active')) $(this).next().height('auto');
				else $(this).next().height(0);
			});
			
			
		
		$('a + div.hidden-text:visible, p:has(a) + div.hidden-text:visible').each(function(){
			var div = $(this).hide();
			$(this).prev().children().andSelf().filter('a').click(function(){
				div.show()
				$(this).parent('p').andSelf().remove();
			});
		});
		return this;
	},
	initTop : function(){
		var arrow = $('#scroll-top'), active = false, offset = 12;
		$(window).bind('resize scroll', function(){
			var top = $(window).scrollTop();
			if(top > offset && !active)
				arrow.addClass('fixed'),
				active = true;
			else if (top < offset && active)
				arrow.removeClass('fixed'),
				active = false;
		});
		$('#scroll-top').click(function(){
			catalog.scrolling(0, function(){
				$(document).trigger('backToPage');
			});
		});
	}
};
App.add(other.initFolding);
