var chocolate_root = '', search_toggler = false;
var chocolate_scheme = '';

function switch_tab (section, post_id, tab, process) {
	if (tab.getParent().hasClass('selected') == false) {
		hide_post = new Fx.Slide($('post-loader-' + post_id), {duration: 'short', transition: Fx.Transitions.Sine.easeOut, opacity: true});
		// prevent redirect
		tab.addEvent('click', function(event){return false;});
		// apply styles
		tabs = tab.getParent().getParent('.tabs').getChildren('li');
		tabs.each(function(tab){tab.removeClass('selected');});
		tab.parentNode.addClass('selected');
		// handle requests
		xml = new Request({
			method: 'get', 
			url: chocolate_root + '/php/inline.php?post_id=' + post_id + '&section=' + section,
			onRequest: function () {
				$('post-throbber-' + post_id).set('html', '<img src="' + chocolate_root + '/img/' + chocolate_scheme + '/throbber.gif" />');
				hide_post.slideOut();
			},
			onComplete: function (response) {
				$('post-loader-' + post_id).set('html', response);
				setTimeout("$('post-throbber-" + post_id + "').set('html', '');", 600);
				hide_post.slideIn();
				eval(process);
			}
		}).send();
	}
}
function process_comment (post_id, user_id) {
	hide_post = new Fx.Slide($('post-loader-' + post_id), {duration: 'short', transition: Fx.Transitions.Sine.easeOut, opacity: true});
	parameters = 'post_id=' + post_id + '&section=process-comment&';
	parameters += user_id ? 'user_id=' + user_id + '&comment=' + $('comment').value : 'author=' + $('author').value + '&email=' + $('email').value + '&url=' + $('url').value + '&comment=' + $('comment').value;
	xml = new Request({
		method: 'get',
		url: chocolate_root + '/php/inline.php?' + parameters,
		onRequest: function () {
			$('post-throbber-' + post_id).set('html', '<img src="' + chocolate_root + '/img/' + chocolate_scheme + '/throbber.gif" />');
			hide_post.slideOut();
		},
		onComplete: function (response) {
			responses = response.clean().trim().split('||');
			$('comments-link-inline-' + post_id).set('html', responses[0]);
			switch_tab('comments', post_id, $('comments-link-inline-' + post_id), "setTimeout('fade_new_comment(" + responses[1] + ")', 1000);");
			window.location.hash = 'comment-' + responses[1];
			hide_post.slideIn();
			setTimeout("$('post-throbber-" + post_id + "').set('html', '');", 600);
		}
	}).send();
}
function fade_new_comment (comment_id) {
	if (chocolate_scheme == 'dark') {
		fade = new Fx.Morph($('comment-info-' + comment_id), {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
		fade.start({
			'background-color': '#5F3723',
			'border-color': '#4F3022',
			'border-width': 3
		});	
	} else {
		fade = new Fx.Morph($('comment-info-' + comment_id), {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
		fade.start({
			'color': '#FFFFFF',
			'background-color': '#5F3723',
			'border-color': '#4F3022',
			'border-width': 3
		});
		$$('#comment-info-' + comment_id + ' a').each(function(link){
			fade2 = new Fx.Morph($(link), {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
			fade2.start({
				'color': '#FFFFFF'
			});
		});
	}
}
function live_search () {
	keywords = $('search_keywords').value.clean();
	if (keywords.trim().length > 3) {
		// ajax request
		xml = new Request({
			method: 'get',
			url: chocolate_root + '/php/live-search.php?keywords=' + keywords,
			onRequest: function () {
				
			},
			onComplete: function (response) {
				if (response.length > 0) {
					setTimeout('show_live_search()', 500);
				} else {
					setTimeout('hide_live_search()', 500);	
				}
				$('live_search_results').set('html', response);
				// bug fix
				$$('#live_search_results li a').each(function(link){
					link.addEvent('mouseup', function(){window.location = link.href;});										  
				});
			}
		}).send();
	} else {
		setTimeout('hide_live_search()', 500);
	}
}
function hide_live_search () {
	$('live_search_results').setStyle('display', 'none');
}
function show_live_search () {
	$('live_search_results').setStyle('display', 'block');
}
function preload_images (array) {
	images = array ? array : $$('img');
	images.each(function(image){
		new_image = new Element('img', {
			'src': image.src,
			'style': {
				'display': 'none',
				'visibility': 'hidden'
			}
		});
		$$('head')[0].appendChild(new_image);
	});	
}
window.addEvent('domready', function(e){
	hide_timer = '';
	$('live_search_results').setStyle('width', parseInt($('search_keywords').offsetWidth) - 16);
	$('search_keywords').addEvent('click', function(){
		$('search_keywords').value = this.value == 'Search for...' ? '' : this.value;
	});
	$('search_keywords').addEvent('blur', function(){
		$('search_keywords').value = this.value == '' ? 'Search for...' : this.value;
		hide_timer = setTimeout('hide_live_search()', 1000);	
	});
	$('search_keywords').addEvent('mouseout', function(){
		hide_timer = setTimeout('hide_live_search()', 1000);											   
	});
	$('search_keywords').addEvent('mouseover', function(){
		window.clearTimeout(hide_timer);										   
	});
	$('live_search_results').addEvent('mouseout', function(){
		hide_timer = setTimeout('hide_live_search()', 1000);											   
	});
	$('live_search_results').addEvent('mouseover', function(){
		window.clearTimeout(hide_timer);										   
	});
	$('search_keywords').addEvent('keypress', function(){
		setTimeout('live_search()', 800);									  
	});
});