// Author: mail@aivanov.com

var timeoutId = 0;
var collapseHeight = 45;
var contentBlocks = null;
var effect = null;
var loadAnimation = null;

window.addEvent('domready', function() {
/*	$('input_query').focus();*/
	contentBlocks = $$('#search_results div.block_content');
	if (navigator.userAgent.indexOf('Firefox/3') >= 0) {
		// Firefox 3 workaround
		setTimeout(function(){contentBlocks.forEach(initBlock)}, 100);
	} else {
		contentBlocks.forEach(initBlock);
	}
	loadAnimation = new Element('div', {'styles': { left: -10000, position: 'absolute', textAlign: 'center', paddingTop: '50px' }}).setHTML('\
		<object type="application/x-shockwave-flash" data="css/images/progress_sign_60px.swf" width="220" height="150">\
		<param name="movie" value="css/images/progress_sign_60px.swf" /><param name="loop" value="true" /><param name="menu" value="false" />\
		</object>'
	).injectTop($('content'));
});

function initBlock(block) {
	block.addEvents({
		'mouseenter' : blockMouseOver.bindWithEvent(block),
		'mouseleave' : blockMouseOut.bindWithEvent(block),
		'click'      : blockClick.bindWithEvent(block)
	});
	//	alert(block.scrollHeight)
	if (block.scrollHeight > collapseHeight + 4) {
		document.getElementById('expand_all_btn').style.display = '';
		var p = block.getParent();
		p.__canExpand = true;
		new Element('a', {'class' : 'expand'}).injectTop(p).addEvent('click', switchBlock.bind(block));
	}
}

function blockMouseOver(event) {
	var p = this.getParent();
	if (p.__canExpand && !isExpanded(p)) {
		timeoutId = setTimeout(autoExpand.bind(this), 1000);
	}
	p.addClass('selected_block');
}

function autoExpand() {
	this.__autoExpand = true;
	animateExpandBlock.apply(this);
}

function blockMouseOut(event) {
	if (timeoutId) {
		clearTimeout(timeoutId);
	}
	if (this.__autoExpand) {
		animateCollapseBlock.apply(this);
	}
	this.getParent().removeClass('selected_block');
}

function blockClick(event) {
	var link = $E('h2 a', this.getParent().getParent().getParent());
	if (link) {
		window.open(link.href, '');
	}
}

function switchBlock() {
	if (isExpanded(this.getParent())) {
		animateCollapseBlock.apply(this);
	} else {
		animateExpandBlock.apply(this);
	}
}

function animateExpandBlock() {
	//this.setStyle('height', this.offsetHeight);
	if (effect) {
		effect.stop()
	}
	effect = new Fx.Style(this, 'height', {
		duration: 500,
		wait: false,
		transition: Fx.Transitions.Cubic.easeOut,
		onComplete: function() {
			this.removeClass('animation');
			this.getParent().addClass('expanded_block');
			refreshExpandAllButton();
		}.bind(this)
	}).set(this.offsetHeight).start(this.scrollHeight);
	this.addClass('animation');
}

function animateCollapseBlock() {
	this.__autoExpand = false;
	//this.setStyle('height', this.scrollHeight);
	if (effect) {
		effect.stop()
	}
	effect = new Fx.Style(this, 'height', {
		duration: 500,
		wait: false,
		transition: Fx.Transitions.Cubic.easeOut,
		onComplete: function() {
			this.removeClass('animation');
			refreshExpandAllButton();
		}.bind(this)
	}).set(this.offsetHeight).start(collapseHeight);
	this.getParent().removeClass('expanded_block');
	this.addClass('animation');
}

function expandAll(link) {
	var collapse = $(link).hasClass('collapse');
	if (collapse) {
		link.removeClass('collapse');
	} else {
		link.addClass('collapse');
	}
	contentBlocks.forEach(collapse ? collapseBlock : expandBlock)
}

function refreshExpandAllButton() {
	if ($$('#search_results div.block').some(isExpanded)) {
		$('expand_all_btn').addClass('collapse');
	} else {
		$('expand_all_btn').removeClass('collapse');
	}
}

function collapseBlock(obj) {
	var p = obj.getParent();
	if (p.__canExpand) {
		obj.setStyle('height', collapseHeight);
		p.removeClass('expanded_block');
	}
}

function expandBlock(obj) {
	var p = obj.getParent();
	if (p.__canExpand) {
		obj.setStyle('height', obj.scrollHeight);
		p.addClass('expanded_block');
	}
}

function isExpanded(obj) {
	return obj.__canExpand && obj.hasClass('expanded_block')
}

function showAnimation() {
	$('content').getChildren().each(function(el){
		if (el != loadAnimation) {
			el.remove();
		}
	});
	loadAnimation.setStyle('position', 'static');
}

function disableDoubleSubmit(f) {
	if (f.__doublesubmit) {
		return false;
	}
	f.__doublesubmit = setTimeout(function() { f.__doublesubmit = 0 }, 1500);
	showAnimation();
	return true;
}
