
var Popup = new Class({

	Implements: [Events, Options],

	html: '<b class="tl"></b><b class="tr"></b><b class="bl"></b><b class="br"></b>',
	parkPosition: {left: -9999, top: -9999},

	options: {
		className: 'popup',
		zIndex: 10,
		width: 380,
		height: 'auto',
		duration: 360,
		opacity: true,
		contentTemplate: '{content}',
		iframeShim: false,
		persistent: false,
		instantInit: false,
		liveResize: false,
		overlay: false,
		getPosition: function(width, height, start) {
			var windowSize = window.getSize();
			var windowScroll = window.getScroll();
			return {
				x: windowScroll.x + (windowSize.x >> 1) - (width >> 1),
				y: windowScroll.y + (windowSize.y >> 1) - (height >> 1)
			}
		}
	},

	initialize: function(options) {
		this.setOptions(options);
		this.options.iframeShim = (this.options.iframeShim && (Browser.Engine.trident4 || (Browser.Engine.gecko && !Browser.Engine.gecko19 && Browser.Platform.mac)));
		this.state = 'new';
		this.width = this.options.width;
		this.height = this.options.height;
		this.fxbound = {
			start: this.fxStart.bind(this),
			complete: this.fxComplete.bind(this)
		};
		if (this.options.instantInit)
			this.lazyInit();
	},

	lazyInit: function() {
		this.element = new Element('div', {
			'class': this.options.className,
			html: this.html,
			styles: {
				position: 'absolute',
				zIndex: this.options.zIndex,
				left: this.parkPosition.left,
				top: this.parkPosition.top
			}
		}).inject(document.body);
		this.content = new Element('div', {
			styles: {
				position: 'absolute',
				zIndex: this.options.zIndex + 1,
				left: this.parkPosition.left,
				top: this.parkPosition.top,
				width: this.options.width,
				height: this.options.height
			}
		}).inject(document.body);
		if (this.options.iframeShim) {
			this.shim = new Element('iframe', {
				src: 'javascript:false;document.write("");',
				scrolling: 'no',
				frameborder: 0,
				styles: {
					position: 'absolute',
					left: this.parkPosition.left,
					top: this.parkPosition.top,
					zIndex: this.options.zIndex - 1,
					border: 'none',
					filter: 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)'
				}
			}).inject(document.body);
		}
		if (this.options.overlay)
			this.overlay = new Element('div', {
				styles: {
					position: 'absolute',
					top: 0,
					left: 0,
					width: '100%',
					display: 'none',
					zIndex: this.options.zIndex - 2
				}
			}).inject(document.body);
		this.elements = this.getElements(this.element);
		if (this.options.liveResize)
			this.elements.push(this.content);
		if (this.contentData)
			this.setHtml(this.contentData);
		this.fx = new Fx.Elements(this.elements, {
			fps: 60,
			duration: this.options.duration,
			link: 'cancel',
			transition: Fx.Transitions.Quart.easeInOut,
			onStart: this.fxbound.start,
			onComplete: this.fxbound.complete
		});
		this.state = 'hidecomplete';
		this.fireEvent('initialize');
	},

	fxStart: function() {
		if (this.showInProgress) {
			this.state = 'showstart';
			this.showstart();
		} else {
			this.state = 'hidestart';
			this.hidestart();
		}
	},

	fxComplete: function() {
		if (this.showInProgress) {
			this.state = 'showcomplete';
			this.content.setStyles({left: this.end.x, top: this.end.y});
			this.showcomplete();
		} else {
			this.state = 'hidecomplete';
			this.close();
			this.hidecomplete();
		}
	},

	showstart: function() {
		this.fireEvent('showstart');
	},

	hidestart: function() {
		this.fireEvent('hidestart');
	},

	showcomplete: function() {
		this.fireEvent('showcomplete');
	},

	hidecomplete: function() {
		this.fireEvent('hidecomplete');
	},

	getElements: function(element) {
		var elements = [
			element,
			element.getElement('.tl'),
			element.getElement('.tr'),
			element.getElement('.bl'),
			element.getElement('.br')
		];
		if (this.options.iframeShim)
			elements.push(this.shim);
		return elements;
	},

	setContent: function(data) {
		if (this.state == 'new')
			this.lazyInit();
		if ($type(data) == 'array') {
			var container = new Element('div');
			data.each(function(element) {
				element.inject(container);
			});
			data = container;
		}
		if (typeof data == 'string')
			data = {content: data};
		this.setHtml(data);
		this.contentData = data;
		if (this.options.height == 'auto')
			this.height = this.content.getHeight();
		this.fx.cancel();
		this.park();
		this.state = 'hidecomplete';
		this.fireEvent('contentready', this.content);
	},

	setHtml: function(data) {
		if ($type(data) == 'element') {
			this.content.empty();
			this.content.adopt(data);
		} else {
			this.content.set('html', this.options.contentTemplate.substitute(data));
		}
	},

	pin: function(element) {
		if (!element) {
			this.nopin = true;
			return
		}
		this.set($(element).getCoordinates());
	},

	set: function(coordinates) {
		this.start = coordinates;
	},

	park: function() {
		this.element.setStyles(this.parkPosition);
		this.content.setStyles(this.parkPosition);
		if (this.options.iframeShim)
			this.shim.setStyles(this.parkPosition);
		if (this.options.overlay)
			this.overlay.setStyle('display', 'none');
	},

	resize: function(width, height) {
		if (this.state == 'new')
			return;
		this.width = width || this.width;
		this.height = height || 'auto';
		this.content.setStyles({
			width: this.width,
			height: this.height
		});
		if (this.height == 'auto')
			this.height = this.content.getHeight();
		this.hideContent();
		if (this.state != 'hidecomplete') {
			if (this.state == 'showstart')
				this.state = 'showcomplete';
			this.end = this.options.getPosition(this.width, this.height, this.start);
			this.show();
		}
	},

	show: function() {
		if (this.state == 'showstart')
			return;
		if (this.state == 'new')
			this.lazyInit();
		if (this.state == 'hidecomplete') {
			this.end = this.options.getPosition(this.width, this.height, this.start);
			var fxStart = (this.nopin) ?
				this.getFxStartParams(this.end.x, this.end.y, this.width, this.height) :
				this.getFxStartParams(this.start.left, this.start.top, this.start.width, this.start.height);
			if (this.options.opacity)
				fxStart[0].opacity = 0;
			this.fx.set(fxStart);
			if (this.options.overlay)
				this.overlay.setStyles({
					height: window.getScrollSize().y,
					display: 'block'
				});
		}
		var fxEnd = this.getFxEndParams(this.end.x, this.end.y, this.width, this.height);
		if (this.options.opacity)
			fxEnd[0].opacity = 1;
		this.showInProgress = true;
		this.fx.start(fxEnd);
	},

	hide: function() {
		if (this.state == 'hidestart' || this.state == 'hidecomplete' || this.state == 'new')
			return true;
  		if (!this.options.liveResize)
			this.hideContent();
		var fxEnd = (this.nopin) ?
			this.getFxStartParams(this.end.x, this.end.y, this.width, this.height) :
			this.getFxStartParams(this.start.left, this.start.top, this.start.width, this.start.height);
		if (this.options.opacity)
			fxEnd[0].opacity = 0;
		this.showInProgress = false;
		this.fx.start(fxEnd);
		return true;
	},

	hideContent: function() {
		this.content.setStyles({
			left: this.parkPosition.x,
			top: this.parkPosition.y
		});
	},

	close: function() {
		if (this.options.persistent) {
			this.park();
		} else {
			this.fireEvent('dispose');
			this.element.dispose();
			this.content.dispose();
			if (this.options.iframeShim)
				this.shim.dispose();
			if (this.options.overlay)
				this.overlay.dispose();
			this.fx = null;
			this.state = 'new';
		}
	},

	getFxStartParams: function(left, top, width, height) {
		return this.getFxEndParams(left, top, width, height);
	},

	getFxEndParams: function(left, top, width, height) {
		var params = {
			0: {left: left - 6, top: top - 6, width: width + 18, height: height + 18},
			1: {width: width, height: height},
			2: {left: width, height: height},
			3: {width: width, top: height},
			4: {left: width, top: height}
		};
		var num = 5;
		if (this.options.iframeShim)
			params[num++] = {left: left, top: top, width: width, height: height};
		if (this.options.liveResize)
		    params[num++] = {left: left, top: top, width: width, height: height};
		return params;
	}

});
new Asset.images(
	(Browser.Engine.trident) ?
		[
			'/popup.png',
			'/popup-tr.png',
			'/popup-bl.png',
			'/popup-br.png'
		] : [
			'/popup.png'
		]
);

function createCSSRule(rule, attributes) {
	var newRule = rule +" {";
	for (var attribute in attributes) {
		newRule += attribute + ": " + attributes[attribute] + "; ";
	}
	newRule += "}";
	styleTag = $(document.body).getElement('style[type="text/css"]') || new Element("style").set('type', 'text/css').inject(document.head);
	if (Browser.Engine.trident)
		styleTag.styleSheet.cssText += newRule;
	else
		styleTag.appendText(newRule);
}

var IndexLayout = new Class({

	initialize: function() {
		this.calcWidth();
		createCSSRule('.header', {width: this.width + 'px'});
		createCSSRule('.indexcontentwrapper', {width: this.width + 'px'});
		window.addEvent('resize', function() {
			this.fixcomblog.delay(100, this);
		}.bind(this));
		window.addEvent('resize', function() {
			$clear(this.timeout);
			this.calcWidth();
			this.timeout = (function() {
				$(document.body).getElement('div.header').setStyle('width', this.width - ((Browser.Engine.trident4) ? 1 : 0));
				$(document.body).getElement('div.indexcontentwrapper').setStyle('width', this.width);
			}).delay(1, this);
			this.lowres();
		}.bind(this));
	},

	calcWidth: function() {
		var newWidth = Math.floor($('indexbody').getSize().x / 240) * 240;
		if (newWidth < 960)
			newWidth = 960;
		else if (newWidth > 1680)
			newWidth = 1680;
		this.width = newWidth;
	},

	lowres: function() {
		var elements = $('indexdigest').getElement('.editorial').getElements('li');
		if (this.width <= 960) {
			elements.each(function(element, index){
				element.setStyle('display', (index > 2) ? 'none': 'block');
			});
			$('indexdigest').addClass('lowres');
		} else {
			$('indexdigest').removeClass('lowres');
			elements.each(function(element, index){
				element.setStyle('display', 'block');
			});
 		}
	},

	fixcomblog: function() {
    var wrapper = $('comblogs');
    var items = $('comblogs').getElements('li');
    var height = wrapper.getHeight();
    for (var i = items.length - 1; i >= 0; i--) {
        var bottom = items[i].getCoordinates(wrapper).bottom + 5;
        items[i].setStyle('visibility', (bottom > height) ? 'hidden' : 'visible');
	  }
	}

});

var Scroller = new Class({

	initialize: function(holder, options) {
		this.holder = $(holder);
		this.wrapper = this.holder.getElement('.wrapper');
		this.direction = 0;
		this.size = 0;

		this.prepare();

		this.fx = new Fx.Scroll(this.holder, {
			wheelStops: false,
			duration: 200,
			transition: Fx.Transitions.Quad.easeIn,
			onStart: function() {
				this.over((this.direction > 0) ? this.arrowl : this.arrowr);
			}.bind(this),
			onComplete: function() {
			    this.out(this.arrowl);
			    this.out(this.arrowr);
				this.getData();
				this.fixRender();
				this.fixArrows();
				this.direction = 0;
			}.bind(this)
		});

		this.reinit();

		this.arrowr.addEvents({
			click: function(event) {
				event = new Event(event).stop();
				this.scrollRight();
			}.bind(this),
			mouseover: this.over.bind(this, this.arrowr),
			mouseout: this.out.bind(this, this.arrowr)
		});

		this.arrowl.addEvents({
			click: function(event) {
				event = new Event(event).stop();
				this.scrollLeft();
			}.bind(this),
			mouseover: this.over.bind(this, this.arrowl),
			mouseout: this.out.bind(this, this.arrowl)
		});

		this.holder.addEvent('mousewheel', function(event) {
		    if (scrollMode == 0)
		        return;
			event = new Event(event).stop();
			if (event.wheel < 0)
				this.scrollRight();
			else if (event.wheel > 0)
				this.scrollLeft();
		}.bind(this));

		window.addEvent('resize', this.reinit.bind(this));
	},

	over: function(element) {
		element.addClass('arrow-active');
	},

	out: function(element) {
		element.removeClass('arrow-active');
	},

	prepare: $empty,

	fixRender: $empty,

	fixArrows: function() {
		this.arrowl.setStyle('display', (this.current == 0) ? 'none' : 'block');
		this.arrowr.setStyle('display', (this.current + this.size >= this.length) ? 'none' : 'block');
	},

	scrollLeft: function() {
		if (this.direction != 0 || this.current == 0)
			return;
		this.direction = 1;
		this.fx.start(--this.current * 120, 0);
	},

	scrollRight: function() {
		if (this.direction != 0 || (this.current + this.size >= this.length))
			return;
		this.direction = -1;
		this.fx.start(++this.current * 120, 0);
	},

	reinit: function() {
		var newSize = $(document.body).getElement('.body').getSize().x;
		newSize = Math.floor(newSize / 120) - 3;
		newSize = ((newSize - 3) & ~1) + 3;
		if (this.size == newSize)
			return;
		this.size = newSize;

		this.length = this.wrapper.getElements('.sni').length;
		this.holder.setStyle('width', 120 * this.size);
		this.wrapper.setStyle('width', 120 * this.length);
		this.fx.set(0, 0);
		this.current = 0;
		if (!this.arrowr || !this.arrowl) {
			var pos = this.holder.getPosition();
			this.arrowr = new Element('div', {'class': 'arrowr', styles: {top: pos.y + 16}}).inject(document.body);
			this.arrowl = new Element('div', {'class': 'arrowl', styles: {top: pos.y + 45}}).inject(document.body);
		}
		this.arrowr.set('styles', {left: this.size * 120 + 365});
		this.arrowl.set('styles', {left: this.size * 120 + 365, display: 'none'});
	},

	getData: function() {
		var elements = this.wrapper.getElements('.sni');
		var rbound = this.current + this.size + 2;
		if (rbound > this.length)
			rbound = this.length;
		for (var i = this.current; i < rbound; i++) {
		    var img = elements[i].getElement('img');
			if (img.title != '') {
				img.src = img.title;
				img.title = '';
			}
		}
	}

});

//Scroller.Big = new Class({
//
//	Extends: Scroller,
//
//	prepare: function() {
//		this.holder.set('styles', {overflow: 'hidden', position: 'relative', 'float': 'left'});
//		if ($('banner480x180')) {
//			$('banner480x180').set('styles', {position: 'absolute', left: '480px'});
//			new Element('div', {'class': 'ni'}).inject(this.wrapper, 'top');
//			new Element('div', {'class': 'ni'}).inject(this.wrapper, 'top');
//		}
//	},
//
//	fixArrows: function() {
//			this.arrowl.setStyle('display', (this.data.length == 0) ? 'none' : 'block');
//			this.arrowr.setStyle('display', (this.wrapper.getElements('.ni').length < (2 + this.size << 1)) ? 'none' : 'block');
//	},
//
//	fixRender: function() {
//		if (this.direction < 0) {
//			var element = this.wrapper.getElement('.ni');
//			this.data.push(element)
//			element.dispose();
//		} else if (this.direction > 0) {
//			this.data.pop().inject(this.wrapper, 'top');
//		}
//		this.fx.set(240, 0);
//		this.teleport();
//	},
//
//	teleport: function() {
//		var elements = this.wrapper.getElements('.ni');
//		elements[this.size + 3].clone().replaces(elements[this.size + 1]);
//		elements[this.size].clone().replaces(elements[this.size + 2]);
//	},
//
//	scrollLeft: function() {
//		if (this.direction != 0 || this.data.length == 0)
//			return;
//		this.direction = 1;
//		this.fx.start(0, 0);
//	},
//
//	scrollRight: function() {
//		if (this.direction != 0 || (this.wrapper.getElements('.ni').length < (2 + this.size << 1)))
//			return;
//		this.direction = -1;
//		this.fx.start(480, 0);
//	},
//
//	reinit: function() {
//		var newSize = $(document.body).getElement('.body').getSize().x;
//		newSize = Math.floor(newSize / 240) - 2;
//		if (this.size == newSize)
//			return;
//		this.size = newSize;
//
//		if (this.backup) {
//			this.backup.clone().replaces(this.wrapper);
//			this.wrapper = this.holder.getElement('.wrapper');
//		} else
//			this.backup = this.wrapper.clone();
//		this.data = [];
//		this.holder.setStyle('width', 240 * this.size);
//		this.wrapper.setStyle('width', 240 * this.size + 480);
//		this.elements = this.wrapper.getElements('.ni');
//		new Element('div', {'class': 'ni'}).inject(this.wrapper, 'top');
//		new Element('div', {'class': 'ni'}).inject(this.elements[this.size], 'before');
//		new Element('div', {'class': 'ni'}).inject(this.elements[this.size], 'before');
//		this.fx.set(240, 0);
//		this.teleport();
//		if (!this.arrowr || !this.arrowl) {
//			var pos = this.wrapper.getPosition();
//			this.arrowr = new Element('div', {'class': 'arrowbr', styles: {top: pos.y + 122}}).inject(document.body);
//			this.arrowl = new Element('div', {'class': 'arrowbl', styles: {top: pos.y + 180}}).inject(document.body);
//		}
//		this.arrowr.set('styles', {left: 480 + this.size * 240 + 5});
//		this.arrowl.set('styles', {left: 480 + this.size * 240 + 5, display: 'none'});
//	},
//
//	getData: function() {
//		var limit = this.size * 2 + 5;
//		var elements = this.wrapper.getElements('.ni');
//		if (limit > elements.length)
//			limit = elements.length;
//		for (var i = 0; i < limit; i++) {
//			var img = elements[i].getElement('img');
//			if (img && img.title != '') {
//				img.src = img.title;
//				img.title = '';
//			}
//		}
//	}
//
//});

//var ShortnewsScroller = new Class({
//
//	initialize: function() {
//		this.wrapper = $(document.body).getElement('.shortnews');
//		this.element = this.wrapper.getElement('ul');
//
//		this.fx = new Fx.Scroll(this.element, {
//			wheelStops: false,
//			duration: 200,
//			transition: Fx.Transitions.Quad.easeIn,
//			onComplete: function() {
//				this.out(this.arrowu);
//				this.out(this.arrowd);
//				this.fixArrows();
//			}.bind(this)
//		});
//
//		var pos = this.wrapper.getPosition();
//		this.arrowu = new Element('div', {'class': 'arrowu', styles: {left: pos.x + 84, top: pos.y + 574}}).inject(document.body);
//		this.arrowd = new Element('div', {'class': 'arrowd', styles: {left: pos.x + 114, top: pos.y + 574}}).inject(document.body);
//		this.fixArrows();
//
//		this.arrowu.addEvents({
//			click: function(event) {
//				event = new Event(event).stop();
//				this.fx.start(0, this.element.getScroll().y - 240);
//			}.bind(this),
//			mouseover: this.over.bind(this, this.arrowu),
//			mouseout: this.out.bind(this, this.arrowu)
//		});
//
//		this.arrowd.addEvents({
//			click: function(event) {
//				event = new Event(event).stop();
//				this.fx.start(0, this.element.getScroll().y + 240);
//			}.bind(this),
//			mouseover: this.over.bind(this, this.arrowd),
//			mouseout: this.out.bind(this, this.arrowd)
//		});
//
//		this.element.addEvent('mousewheel', function(event) {
//			if (scrollMode == 0)
//				return;
//			event = new Event(event).stop();
//			this.over((event.wheel > 0) ? this.arrowu : this.arrowd);
//			(function() {
//				this.out((event.wheel > 0) ? this.arrowu : this.arrowd);
//				this.fixArrows();
//			}).delay(100, this);
//			this.element.scrollTo(0, this.element.getScroll().y - event.wheel * 40);
//		}.bind(this));
//	},
//
//	fixArrows: function() {
//		this.arrowu.setStyle('display', (this.element.getScroll().y == 0) ? 'none' : 'block');
//		this.arrowd.setStyle('display', (this.element.getScrollSize().y - this.element.getScroll().y <= 560) ? 'none' : 'block');
//	},
//
//	over: function(element) {
//		element.addClass('arrow-active');
//	},
//
//	out: function(element) {
//		element.removeClass('arrow-active');
//	}
//});

//var scrollMode = Cookie.read('scrollMode') || 0;
//function scrollModeInit() {
//	var tbutton = $(document.body).getElement('span.scrolltoggle span');
//	var setMode = function() {
//		tbutton.set('html', (scrollMode == 1) ? 'Не прокручивать<br> новости колёсиком' : 'Прокручивать<br> новости колёсиком');
//	}
//	setMode();
//	tbutton.addEvent('click', function() {
//		scrollMode = (scrollMode == 1) ? 0 : 1;
//		setMode();
//		Cookie.write('scrollMode', scrollMode, {domain: 'drive.ru', path: '/', duration: 365});
//	});
//}
//window.addEvent('domready', function() {
//	new Asset.images(['/images/arrows/big-right.png', '/images/arrows/big-left.png', '/images/arrows/right.png', '/images/arrows/left.png', '/images/arrows/up.png', '/images/arrows/down.png']);
//	new Scroller.Big('indexnews');
//	new Scroller('indexshortnews');
//	if (document.location.hash.indexOf('#s') == 0) {
//		var id = document.location.hash.substring(1);
//		if ($(id)) {
//		    var offset = $(id).offsetTop;
//		    if (Browser.Engine.gecko || (Browser.Engine.trident && Browser.Engine.version == 6) || Browser.Engine.presto)
//		        offset -= 35;
//		    $(document.body).getElement('.shortnews ul').scrollTop = offset;
//		}
//	}
//	new ShortnewsScroller();
//
//});

window.addEvent('domready', function() {
	var wrapper = $(document.body).getElement('.zoom');
	if (!wrapper)
		return;
	var inprogress = false, current = null, popup = [];
	var elements = wrapper.getElements('a[rel=zoomy]');
	var hideall = function() {
		popup.each(function(p) {
			p.hide();
		});
	}
	var show = function(element) {
		if (inprogress)
			return;
	 	if (current != null)
	 		hideall();
		if (current == element)
			return;
	 	inprogress = true;
		var zoomimg = new Asset.image(element.href, {
			onerror: function() {
				current = null; inprogress = false;
			},
			onload: function() {
				current = element;
				var tmpPopup = new Popup({
					width: zoomimg.width + 2,
					height: zoomimg.height + 2,
					instantInit: true,
					liveResize: true,
					onShowcomplete: function() {current = element; inprogress = false;},
					onHidecomplete: function() {current = null; inprogress = false;}
				});
				popup.push(tmpPopup);
				tmpPopup.setContent('<div style="padding:1px; background-color:#666; line-height: 0;"><img src="' + element.href + '" width="100%" height="100%"></div>');
				var coords = element.getElement('img').getCoordinates();
				coords.top -= 1;
				coords.left -= 1;
				coords.width += 2;
				coords.height += 2;
				tmpPopup.set(coords);
				tmpPopup.show();
			}
		});
	};
	var hide = function() {
		if (inprogress || current == null)
			return;
	 	hideall();
	};
	elements.each(function(element) {
		element.addEvent('click', function(event) {
			event = new Event(event).stop();
			show(element);
		});
	});
	document.addEvent('click', hide);
	document.addEvent('keydown', function(event) {
		if (event.key == 'esc' && this.open)
			hide();
	}.bind(this));
});

function fixNOD() {
	var element = $('nod');
	var length = element.get('html').trim().length;
	if (length > 5)
		length = 5;
	element.addClass('o' + length);
}