/* Author: Htmlhero (http://htmlhero.ru) */

$(function(){

	(function(){
		if (document.createElement('div').style.textShadow === '') {
			$('html').addClass('text-shadow');
		}
	})();

	$('[placeholder]').each(function(){
		new Placeholder(this);
	});

	$('.b-head-widget').each(function(){
		new Widget(this);
	});

	$('.b-head-panel-menu').each(function(){
		new Menu(this);
	});

	$('.b-head-panel-menu .search').each(function(){
		new Search(this);
	});

	$('.b-news-two-block__col_left').each(function(){
		new News(this);
	});

	$('.b-dropdown').each(function(){
		new Dropdown(this);
	});

	$('.b-tabs').each(function(){
		new Tabs(this);
	});

	$('.b-chat-block .messages').each(function(){
		new Scroll(this);
	});

	$('.b-banners-block__right').each(function(){
		new MediaFM(this);
	});

	$('.b-ragfair-block__col').each(function(){
		new Ragfair(this);
	});

	$('.b-affiche-block').each(function(){
		new Affiche(this);
	});

	$('.b-popup').each(function(){
		new Popup(this);
	});

	$('.b-popup_add .help').each(function(){
		new Help(this);
	});

	$('.b-popup-open').live('click', function(){
		var popup = $(this).data('popup');
		$('.' + popup).data('popup').show();
		return false;
	});


	$('.authors-ext-btn').click(function(){
		if($('.authors-ext').hasClass('authors-vis')){
			$('.authors-ext').slideUp(300);
			$('.authors-ext').removeClass('authors-vis');
			var txt_cntnr = $(this).find('.b-btn__text');
			txt_cntnr.text($(this).attr('data'));
		} else {
			$('.authors-ext').slideDown(300);
			$('.authors-ext').addClass('authors-vis');
			var txt_cntnr = $(this).find('.b-btn__text');
			txt_cntnr.text('скрыть');
		}
	});

	lastBoxIndex = 0;
	newBoxIndex = 0;
	$('.j-cite-item-changer').click(function(){
		lastBoxIndex = newBoxIndex;
		var active_item_container = $(this).parent().parent().find('.cite-item-container');
		if($('div.active_item', active_item_container).index()+1 == $('div.item', active_item_container).size()){
			newBoxIndex = 0;
		} else {
			newBoxIndex = $('div.active_item', active_item_container).index() + 1;
		}
		$('div.active_item', active_item_container).removeClass('active_item');
		$('div.item', active_item_container).eq(lastBoxIndex).hide();
		$('div.item', active_item_container).eq(newBoxIndex).fadeIn(500);
		$('div.item', active_item_container).eq(newBoxIndex).addClass('active_item');
	});
});

/* Placeholder */

function Placeholder(container){

	this.container = $(container);

	var _this = this,
		placeholder = this.container.attr('placeholder');

	if (this.container.val() === '' || this.container.val() === placeholder) {
		this.container.addClass('placeholder').val(placeholder);
	}

	this.container.focus(function(){
		_this.focus();
	});

	this.container.blur(function(){
		_this.blur();
	});

}

Placeholder.prototype.focus = function(){

	var placeholder = this.container.attr('placeholder');

	if (this.container.val() === placeholder) {
		this.container.removeClass('placeholder').val('');
	}

};

Placeholder.prototype.blur = function(){

	var placeholder = this.container.attr('placeholder');

	if (this.container.val() === '') {
		this.container.addClass('placeholder').val(placeholder);
	}

};

/* Widget */

function Widget(container){

	var _this = this;

	this.container = $(container);
	this.head = this.container.find('.b-head-widget__head');
	this.body = this.container.find('.b-head-widget__body');

	this.head.click(function(){
		_this.body.show();
		return false;
	});

	this.body.bind('click', function(){
		return false;
	});

	this.body.find('.control i').click(function(){
		_this.body.hide();
	});

	$(document).bind('click.widget', function(){
		_this.body.hide();
	});

}

/* Menu */

function Menu(container){

	var _this = this;

	this.container = $(container);
	this.items = this.container.find('.item');
	this.submenu = this.container.next().find('.menu');

	this.items.find('a').not('.b-popup-open').mouseenter(function(){
		_this.switchItem(this);
	});

}

Menu.prototype.switchItem = function(handler){

	handler = $(handler);

	var item = handler.closest('.item'),
		index = this.items.index(item);

	this.items.removeClass('item_act');
	item.addClass('item_act');

	this.submenu.eq(index)
		.addClass('menu_act')
		.siblings().removeClass('menu_act');

};

/* Search */

function Search(container){

	var _this = this;

	this.container = $(container);
	this.input = this.container.find('input');
	this.type = this.container.find('.input__type');
	this.handler = this.container.find('.input__arr');
	this.dropdown = this.container.find('.input__dropdown');

	this.type.click(function(){
		_this.dropdown.toggle();
		return false;
	});

	this.handler.click(function(){
		_this.dropdown.toggle();
		return false;
	});

	this.dropdown.find('.input__dropdown__item_site').click(function(){
		_this.switchSite(this);
	});

	this.dropdown.find('.input__dropdown__item_users').click(function(){
		_this.switchUsers(this);
	});

	$(document).bind('click.search', function(){
		_this.hide();
	});

}

Search.prototype.managePlaceholder = function(handler){

	var placeholder = $(handler).html();

	if (this.input.val() === this.input.attr('placeholder')) {
		this.input.val(placeholder);
	}

	this.input.attr('placeholder', placeholder);

};

Search.prototype.switchUsers = function(handler){

	this.managePlaceholder(handler);

	this.type
		.removeClass('input__type_site')
		.addClass('input__type_users');

};

Search.prototype.switchSite = function(handler){

	this.managePlaceholder(handler);

	this.type
		.removeClass('input__type_users')
		.addClass('input__type_site');

};

Search.prototype.hide = function(){

	this.dropdown.hide();

};

/* News */

function News(container){

	var _this = this;

	this.container = $(container);
	this.size = this.container.data('size');
	this.index = 0;

	this.container.find('.list__body').scrollable({
		prev: '.list__nav_top',
		next: '.list__nav_bot',
		vertical: true,
		onBeforeSeek: function(event, target){
			_this.before(target);
		}
	});

	this.container.delegate('a', 'mouseover', function(){
		_this.select(this);
		return false;
	});

	$(window).bind('resize.news', function(){
		_this.resize();
	});

}

News.prototype.before = function(target){

	if (target >= this.size) {
		return;
	}

	var next = this.size - target - 1,
		prev = this.size - next - 1;

	this.container.find('.list__nav_bot .list__nav__num').html(this.format(next));
	this.container.find('.list__nav_top .list__nav__num').html(this.format(prev));

};

News.prototype.format = function(input){

	var output = '';
	input = input.toString();

	while (input.length >= 3){
		output = ' ' + input.substr(input.length - 3, 3) + output;
		input = input.substr(0, input.length - 3);
	}

	return input + output;

};

News.prototype.select = function(handler){

	$(handler).parent()
		.addClass('items__act')
		.siblings().removeClass('items__act');

};

News.prototype.resize = function(){

};

/* Tabs */

function Tabs(container){

	var _this = this;

	this.container = $(container);
	this.heads = this.container.find('.b-tabs__head__item');
	this.bodies = this.container.find('.b-tabs__body__item');

	this.heads.click(function(){
		_this.switchTab(this);
	});

}

Tabs.prototype.switchTab = function(handler){

	handler = $(handler);

	var index = this.heads.index(handler);

	handler
		.addClass('b-tabs__head__item_act')
		.siblings().removeClass('b-tabs__head__item_act');

	this.bodies.eq(index)
		.addClass('b-tabs__body__item_act')
		.siblings().removeClass('b-tabs__body__item_act');

};

/* Scroll */

function Scroll(container){

	var _this = this;

	this.container = $(container);

	this.container.jScrollPane({
		showArrows: true,
		horizontalGutter: 2
	});

	this.api = this.container.data('jsp');

	$(window).resize(function(){
		_this.resize();
	});

}

Scroll.prototype.resize = function(){

	this.api.reinitialise();

};

/* Media FM */

function MediaFM(container){

	$(container).scrollable({
		prev: '.arr__top',
		next: '.arr__bot',
		vertical: true
	});

}

/* Ragfair */

function Ragfair(container){

	var _this = this;

	this.container = $(container);

	this.container.delegate('.list__item__head', 'click', function(){
		_this.show(this);
		return false;
	});

	this.container.delegate('.tooltip__close', 'click', function(){
		_this.hide();
	});

	this.container.delegate('.tooltip', 'click', function(){
		return false;
	});

	$(document).bind('click.ragfair', function(){
		_this.hide();
	});

}

Ragfair.prototype.show = function(handler){

	this.hide();
	$(handler).parent().addClass('list__item_act');

};

Ragfair.prototype.hide = function(){

	this.container.find('.list__item_act').removeClass('list__item_act');

};

/* Affiche */

function Affiche(container){

	var _this = this;

	this.container = $(container);

	this.images = this.container.find('.images')
		.scrollable({
			speed: 1000,
			onBeforeSeek: function(event, target){
				_this.imagesChange(target);
			}
		})
		.autoscroll({
			interval: 6000,
			autopause: false
		})
		.data('scrollable');

	this.container.mouseenter(function(){
		_this.images.pause();
	});

	this.container.mouseleave(function(){
		_this.images.play();
	});

	this.slider = this.container.find('.slider__right');

	this.size = this.images.getSize();
	this.index = 0;

	this.slider.slider({
		slide: function(e, ui){
			_this.sliderChange(e, ui);
		},
		change: function(e, ui){
			_this.sliderChange(e, ui);
		}
	});

}

Affiche.prototype.imagesChange = function(target){

	if (target >= this.size) {
		this.images.seekTo(0);
		return;
	}

	var part = 100 / (this.size - 1),
		left = part * target;

	this.index = target;
	this.slider.find('a').animate({left: left + '%'}, 1000);

};

Affiche.prototype.sliderChange = function(e, ui){

	var part = 100 / this.size,
		index = Math.floor(ui.value / part);

	if (index >= this.size) {
		index--;
	}

	if (index === this.index) {
		return;
	}

	this.index = index;
	this.images.seekTo(index);

};

/* Dropdown */

function Dropdown(container){

	var _this = this;

	this.container = $(container);
	this.head = this.container.children('.b-dropdown__head');
	this.body = this.container.children('.b-dropdown__body');

	this.container.click(function(e){
		e.stopPropagation();
	});

	this.head.click(function(){
		_this.toggle();
	});

	this.body.delegate('a', 'click', function(){
		_this.toggle();
	});

	$(document).bind('click.dropdown', function(){
		_this.hide();
	});

}

Dropdown.prototype.toggle = function(){

	this.container.toggleClass('b-dropdown_act');

	if (this.api) {
		return;
	}

	this.body.jScrollPane({
		showArrows: true,
		horizontalGutter: 6
	});

	this.api = this.body.data('jsp');

};

Dropdown.prototype.hide = function(){

	this.container.removeClass('b-dropdown_act');

};

/* Popup */

function Popup(container){

	var _this = this;

	this.container = $(container);
	this.overlay = $('.b-overlay');
	this.body = $(document.body);
	this.page = $('div.l-page');
	this.scrollbarWidth = this.getScrollbarWidth();

	this.container.click(function(e){
		_this.hide();
	});

	this.container.find('.close').click(function(e){
		_this.hide();
	});

	this.container.find('.b-popup__border').click(function(e){
		$(document).click();
		e.stopPropagation();
	});

	this.container.data('popup', this);

}

Popup.prototype.getScrollbarWidth = function(){

	var outer = document.createElement('div');
	outer.style.position = 'absolute';
	outer.style.top = '0';
	outer.style.left = '0';
	outer.style.visibility = 'hidden';
	outer.style.width = '200px';
	outer.style.height = '150px';
	outer.style.overflow = 'hidden';

	var inner = document.createElement('div');
	inner.style.width = '100%';
	inner.style.height = '200px';

	outer.appendChild (inner);
	document.body.appendChild (outer);

	var w1 = inner.offsetWidth;
	outer.style.overflow = 'scroll';

	var w2 = inner.offsetWidth;
	if (w1 === w2) {
		w2 = outer.clientWidth;
	}

	document.body.removeChild (outer);

	return (w1 - w2);

};

Popup.prototype.show = function(){

	var _this = this;

	this.resize();
	this.container.show();
	this.overlay.show();

	$(window).bind('resize.popup', function(){
		_this.resize();
	});

};

Popup.prototype.hide = function(){

	this.visible = false;

	if ($.browser.msie) {
		document.documentElement.style.overflowY = '';
	} else {
		document.body.style.overflowY = '';
	}

	this.container.hide();
	this.overlay.hide();
	$(window).unbind('resize.popup');
	this.page.css({width: "100%"});
	this.page.css({paddingRight: 0});
};


Popup.prototype.resize = function(){

	if (document.documentElement.scrollHeight <= document.documentElement.offsetHeight) {
		return;
	}

	if ($.browser.msie) {
		document.documentElement.style.overflowY = 'hidden';
	} else {
		document.body.style.overflowY = 'hidden';
	}

	this.page.width(this.body.width() - this.scrollbarWidth);
	this.page.css({paddingRight: this.scrollbarWidth});

};

/* Help */

function Help(container){

	var _this = this;

	this.container = $(container);

	this.container.find('.b-pseudo-link').click(function(){
		_this.container.toggleClass('help_act');
	});

}
