// JavaScript Document
var fix = {
	init : function(){},
	initFix : function(){
		var scrollTop, enable = false, backlink;
		var footer = $('footer');
		this.fix = function(callback, options){
			delete backlink;
			var options = $.extend({
				noLink : false
			}, options);
			
			if(enable) return this;
			$('body').addClass('fixed');
			App.autoscroll(true);
			enable = true;
			fix.showTint(function(){
				$('#area').css('top', -(scrollTop = $(window).scrollTop()) );
				
				$('#wrapper').addClass('cutter');
				$(window)._scrollable().scrollTop(0);
				(callback || $.noop)();
			});
			if(!options.noLink) backlink = (App.history.oldHash[App.history.oldHash.length - 2] || '').split('#')[1] || '';
			return this;
		};
		this.free = function(callback, force){ 
			if(!enable) return this;
			App.autoscroll(false);
			enable = false;
			fix.hideTint(function(){
				$('body').removeClass('fixed');
				$('#area').css('top', 0);
				$('#wrapper').removeClass('cutter');
				$(window)._scrollable().scrollTop(scrollTop);
				scrollTop = 0;
				(callback || $.noop)();
				
				if(typeof backlink != "undefined"){
					if(!App.history.restoreLink())
						App.history.jump(false, backlink || 'page', true);
					delete backlink;
				}
			}, force || false);
			
			$(document).unbind('.fix');
			
			return this;
		};
		
		$('#area').bind({
			fix : fix.fix,
			free : fix.free
		});
		
		//$(document).bind('clearAll', function(){ fix.free(); });
	},	
	addFixedContent : function(){
		var f;
		if((f = $('#fixed-content')).length ) f.remove();
		$('body').append(f = $('<div id="fixed-content" />'));
		if($.browser.opera) f.cursor('/content/images/cur/rosma_icon_close.png');
		return f;
	},
	showTint : function(callback){
		if($('#tint').length) $('#tint').remove();
		var tint;
		$('body').append(tint = $('<div id="tint" />'));
		tint.css('opacity', 0).stop(true, true).show().fadeTo(400, .8, callback || $.noop).dequeue();
	},
	hideTint : function(callback, force){
		if(!$('#tint').length) return;
		if($.browser.msie || force){
			$('#fixed-content, #tint').remove();
			(callback || $.noop)();
		}else{
			$('#fixed-content').stop(true).fadeOut(function(){ $(this).remove(); });
			$('#tint').fadeOut(function(){ $(this).remove(); (callback || $.noop)(); });
		}
	},
	goHeight : function(){
		fix.container.height(function(){
			var Ar = fix.news.map(function(){
				return $(this).outerHeight() + 100;
			}).get();
			return Math.max.apply(this, Ar);
		});
	},
	initNews : function(){

	
		this.container = $('#news-container').fadeIn('slow');
		this.newsnext = $('div.news-zone-next', this.container.parent());
		this.newsprev = $('div.news-zone-prev', this.container.parent());
		
		if($.browser.opera){
			this.container.cursor('/content/images/cur/rosma_icon_close.png');
			this.newsnext.cursor('/content/images/cur/rosma_icon_right.png');
			this.newsprev.cursor('/content/images/cur/rosma_icon_left.png');
		}
		
		this.news = this.container.children();
		this.tints = $();
		this.news.each(function(i){
			var tint;
			fix.tints = fix.tints.add(
					tint = $('<div class="news-tint" />').css({
						opacity: .8
					})
				)
			$(this).append(tint);
			//click
			$(this).click(function(e){ e.stopPropagation(); });
			//zIndex
			$(this).css('z-index', i);
		});
		//DEFS
		var active = this.news.filter('.active'),
			top = 50,
			ease = "Quad",
			aIndex,
			duration = 400,
			zIndex = function(){
				fix.news.css('z-index', 1);
				active.css({'z-index': 10})
			},
			goHeight = fix.goHeight,
			setDir = function(){
				active.prevAll().addClass('news-prev').removeClass('news-next');
				active.nextAll().addClass('news-next').removeClass('news-prev');
			},
			refreshLinks = function(){
				fix.newsnext[active.next().length ? 'show' : 'hide']();
				fix.newsprev[active.prev().length ? 'show' : 'hide']();
			};
		
		this.newsnext.click(function(e){
			active.next().find('div.news-tint').click();
			e.stopPropagation();
			refreshLinks();
		});
		this.newsprev.click(function(e){
			active.prev().find('div.news-tint').click();
			e.stopPropagation();
			refreshLinks();
		});
		
		if(!active.length) active = this.news.filter(':first').addClass('active');
		setDir();
		aIndex = this.news.index(active);
		
		active.css({left: '50%', 'z-index': 10})
				.find('div.news-tint').hide().end()
			.siblings().css('top', top);
		active.prev().css('left', '0').prevAll().css('left', '-100%');
		active.next().css('left', '100%').nextAll().css('left', '200%');
		goHeight();
		refreshLinks();
		
		this.tints.bind('click.nolink', function(e){
			goHeight();
			e.stopPropagation();
			var li = this.parentNode;
			if(li == active.get(0)) return false;
				
			//tints
			fix.news.stop(true,true)
				.find('div.news-tint:hidden').stop(true,true).fadeIn();
			$(li).find('div.news-tint').stop(true, true).fadeOut(duration);
			
			//active
			$(li).animate({left: '50%', top: 0}, {duration:duration, specialEasing: {left: 'easeInOut' + ease, top: 'easeOut' + ease}})
				
			//prev
			.prev().animate({left: '0%', top: top}, {duration: duration, specialEasing: {left: 'easeInOut' + ease, top: 'easeIn' + ease}})
			.prevAll().animate({left: '-100%', top: top*2}, duration, 'easeInOut'+ease).end().end()
			
			//next
			.next().animate({left: '100%', top: top}, {duration:duration, specialEasing: {left: 'easeInOut' + ease, top: 'easeOut' + ease}})				
			.nextAll().animate({left: '200%', top: top*2}, duration, 'easeInOut'+ease);
			
			active = $(li);
			
			
			active.active(true).siblings().active(false);
				
			aIndex = fix.news.index(active);
			zIndex();
			setDir();
			goHeight();
			refreshLinks();
		}).click(function(){
			var href = active.children('a').attr('href');
			App.history.storeLink(href.substr(href.indexOf('#')), true).jump(false);
		});
		$('#fixed-content').parent().one('click', function(){ fix.free() });
	},
	openNews : function(args, hash, callback){
		var callback = callback || $.noop;
		//Проверяем если новости уже открыты то прост переходим на нужный линк
		if($('#news-container').length)
			return $('a[href$="' + hash + '"]' ,this.news).siblings('.news-tint').triggerHandler('click.nolink');
			
		
		var box = this.fix(function(){
			fix.addFixedContent().load(W_AJAX + 'item/get_news/', function(){
				fix.initNews();
				fix.goHeight();
				if((lnk = $('a[href$="' + hash + '"]', this)).length)
					lnk.siblings('.news-tint').triggerHandler('click.nolink');
				callback();
				setInterval("fix.goHeight()", 300);
			});
		}), lnk;
	},
	lightBox : function(href, mode, callback, noLink){
		$(document).trigger('clearAll');
		var wrapper, lightbox, content, image, mode = mode || 'image', callback = callback || $.noop, closeHandler;
		fix.fix(function(){
			var delHandler;
			fix.addFixedContent().append(
				wrapper = $('<div class="lightbox-wrapper" />').append(
					lightbox = $('<div class="lightbox" />').click(function(e){ e.stopPropagation(); })
				)
			).click(closeHandler = function(e){
				fix.free($.noop(), typeof e == "boolean" && e);
				$(document).unbind('keydown', delHandler);
			});
			if($.browser.opera) wrapper.cursor('/content/images/cur/rosma_icon_close.png');
			switch(mode){
				case 'image':
					image = $('<img />').load(function(){ 
						lightbox.fadeIn(function(){
							callback();
						}); 
					}).attr('src', href);
					break;
				case 'html':
					image = href;
					break;
				case 'id':
					image = $('#'+href)
			}
			lightbox.append(
				content = $('<div class="lightbox-content lightbox-white" />').append(image)
			)
			
			if($.browser.msie) content.append('<em class="news-t"></em><em class="news-tr"></em><em class="news-r"></em><em class="news-br"></em><em class="news-b"></em><em class="news-bl"></em><em class="news-l"></em><em class="news-tl"></em>');
			$(document).bind('keydown', delHandler = function(e){
				if(e.keyCode != 27) return;
				closeHandler();
			});
			if(mode=="html") lightbox.fadeIn(function(){callback();});
			
			$(document).bind('returnBack', function(e){ 
				closeHandler(true); $(this).unbind(e);
			});
		}, {noLink: noLink || false});
	},
	lightBoxId : function(id, callback){
		var html = $('#'+id).html();
		if(html == null || !html.length) return callback();
		this.lightBox(html, 'html', callback)
	}
};
$('a.link-lightbox').live('click.lightbox', function(e){
	e.preventDefault();
	App.history.jump(false, 'lightbox:' + $(this).attr('href'), false);
});
App.add(fix);
