﻿var speed = 'normal';

var show_title = 'кликайте, не бойтесь';
var close_title = 'уже неинтересно';
var show_all = 'показать всё';
var hide_all = 'слишком много букв';

$(document).ready(function() {
	var desc_container = $('#middleLeft');
	var headers = $('#middleCenter dt');

	$('#middleCenter .content').prepend('<a id="show_all">' + show_all + '</a> <a id="hide_all" class="inactive">' + hide_all + '</a>');

	$('#show_all, #hide_all').click(function(event) {
		$(event.target).attr('id') == 'show_all' ? condition = ':not(.opened)' : condition = '.opened';
		var headers = $('#middleCenter dt' + condition);

		$(headers).each(function() {
			$(this).click();
		});
		this.blur();

		return false;
	});

	$(headers).each(function(index) {
		var id = 'item_' + generateID($(this).text());

		$(this)
		.attr('title', show_title)
		.attr('id', id)
		.addClass('active')
		.next('dd').hide()
		.end()
		.after(' ') // ie nowrap fix
		.click(function(event) {
			var header = this;
			var description = $(this).next('dd').children('.description').hide();

			$(this).toggleClass('opened').next('dd').slideToggle(speed, function() {
				if($(header).hasClass('opened')){
					$(description).fadeIn(speed, function() {if($.browser.msie) $(this).get(0).style.removeAttribute('filter')});

					header.title = close_title;
				}else{
					$('#' + id + '_description').hide();

					header.title = show_title;

					titleMosaic($(this).children('.mosaic').rand('img').children('img'));
				}

				checkControls();
			});
		});
	});

	$('.mosaic').rand('img');
	titleMosaic();

	function generateID(str) {
		var regex = /[^-0-9a-zа-я]+/ig;

		return str.replace(regex, '_').toLowerCase();
	}

	function titleMosaic(elements) {
		if(!elements) elements = $('.mosaic img');

		$('.mosaic-title').hide();

		$(elements).each(function() {
			var id = 'title_' + generateID($(this).attr('src'));
			var title = '<div id="' + id + '" class="mosaic-title"><div>' + $(this).attr('title') + '</div></div>';

			$(this).after($(title).hide());

			$(this).data('title_element', $('#' + id));
		});

		$(elements)
		.hover(
			function(event) {
				$(this)
				.data('title', $(this).attr('title'))
				.attr('title', '');

				$($(this).data('title_element'))
				.css({'top': Math.round($(this).offset().top + $(this).height() / 2 - $(this).parent().offset().top)})
				.fadeIn(speed, function() {if($.browser.msie) $(this).get(0).style.removeAttribute('filter')});
			}, function(event) {
				$($(this).data('title_element')).fadeOut(speed);

				$(this).attr('title', $(this).data('title'));
			}
		);
	}

	function checkControls() {
		if($('#middleCenter dt.opened').length){
			$('#hide_all').removeClass('inactive');
		}else{
			$('#hide_all').addClass('inactive');
		}
		if($('#middleCenter dt:not(.opened)').length){
			$('#show_all').removeClass('inactive');
		}else{
			$('#show_all').addClass('inactive');
		}
	}
});

$.fn.rand = function(selector) {
	if(!selector) selector = '*';

	function randOrd() {return(Math.round(Math.random()) - 0.5);}

	return($(this).each(function() {
		var $this = $(this);
		var $children = $this.children(selector);
		var childCount = $children.length;

		if (childCount > 1) {
			$children.remove();

			var indices = new Array();
			for (i=0;i<childCount;i++) {indices[indices.length] = i;}
			indices = indices.sort(randOrd);
			$.each(indices,function(j,k) {$this.prepend($children.eq(k));});
		}
	}));
}