var Browser = navigator.appName;

function isIE6()
{
	if((Browser.indexOf('Microsoft') != -1) && (typeof(XMLHttpRequest) == 'undefined'))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function setLeftHeight()
{
	if(document.getElementById('left'))
	{
		if(document.getElementById('left').offsetHeight < 400)
		{
			document.getElementById('left').style.height = '400px';
		}
	}
}

if(isIE6())
{
	window.onload = function()
	{
		setLeftHeight();
		enableFormSubmission();
		addSearchText();
	}
}
else
{
	window.onload = function()
	{
		enableFormSubmission();
		addSearchText();
	}
}

function addSearchText()
{
	if($('s').value == '')
	{
		$('s').value = 'Cauta ...';
	}
	$('s').onfocus = function()
	{
		if(this.value == 'Cauta ...')
		{
			this.value = '';
		}
	}
	$('s').onblur = function()
	{
		if(this.value == '')
		{
			this.value = 'Cauta ...';
		}
	}
}

var slider;

function releaseSlider(e)
{
	if(e < 1)
	{
		$('handle').getElementsByTagName('img')[0].src = site_url + '/wp-content/themes/alin/img/send_button.png';
		var position = parseInt($('handle').style.left.replace(/px/, ''));
		new Effect.Move('handle', {x: -position, duration: 0.2});
		new Effect.Morph('hide_div', {style: 'width: 0px;', duration: 0.2});
	}
	else
	{
		validateAndSendForm();
	}
}

var activated = 0;

function slide(e)
{
	$('hide_div').style.width = 280 * e + 'px';
	if(activated == 1)
	{
		activated = 0;
		$('handle').getElementsByTagName('img')[0].src = site_url + '/wp-content/themes/alin/img/send_button.png';
	}
	if(e == 1)
	{
		activated = 1;
		$('handle').getElementsByTagName('img')[0].src = site_url + '/wp-content/themes/alin/img/send_button_active.png';
	}
}

function enableFormSubmission()
{
	if($('comments_form'))
	{
		slider = new Control.Slider('handle', 'track', {onChange: function(e) {releaseSlider(e)}, onSlide: function(e) {slide(e)}});
		$('handle').style.left = 0;
		$('hide_div').style.width = '0';
		$('handle').onclick = function()
		{
			return false;
		}
		hideDefaultValue('nume', 'Nume');
		hideDefaultValue('email', 'E-mail (nu va fi publicat)');
		hideDefaultValue('url', 'URL');
		hideDefaultValue('comentariu', 'Comentariul tau');
	}
}

function validateAndSendForm()
{
	var error = '';
	if($('nume').value == 'Nume' || $('nume').value == '')
	{
		error = 'Nu ai introdus numele!';
	}
	else if($('email').value == 'E-mail (nu va fi publicat)' || $('email').value == '')
	{
		error = 'Nu ai introdus e-mail-ul!';
	}
	else if($('comentariu').value == 'Comentariul tau' || $('comentariu').value == '')
	{
		error = 'Nu ai scris nici un comentariu!';
	}
	if(!error)
	{
		var regEx = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(regEx.test($('email').value) == false)
		{
			error = 'Nu ai introdus un e-mail corect!';
		}
	}
	if(error)
	{
		alert(error);
		releaseSlider(0);
	}
	else
	{
		if($('url').value == 'URL')
		{
			$('url').value = '';
		}
		releaseSlider(0);
		submitForm();
	}
}

function resetForm()
{
	$('nume').value = 'Nume';
	$('email').value = 'E-mail (nu va fi publicat)';
	$('url').value = 'URL';
	$('comentariu').value = 'Comentariul tau';
}

function submitForm()
{
	$('comments_form').action = site_url + '/wp-comments-post.php';
	var postParameters = {author: $('nume').value, email: $('email').value, url: $('url').value, comment: $('comentariu').value, comment_post_ID: $('comment_post_ID').value, comment_parent: $('comment_parent').value};
	var submitAjax = new Ajax.Request($('comments_form').action, {method: 'post', parameters: postParameters, onSuccess: showAddedComment, onFailure: function() {alert('Comentariul nu a putut fi adaugat!');}}); 
}

var comment_id = 0;

function showAddedComment()
{
	if($('no_comments'))
	{
		$('no_comments').style.display = 'none';
	}
	if($('url').value != '' || $('url').value != 'URL')
	{
		url_link = '<a class="url" rel="external nofollow" href="' + $('url').value + '">' + $('nume').value + '</a>';
	}
	else
	{
		url_link = $('nume').value;
	}
	var date = new Date();
	var month = date.getMonth() + 1;
	if(month.toString().length < 2)
	{
		month = '0' + month;
	}
	var show_date = date.getDate() + '.' + month + '.' + date.getFullYear();
	comment_id++;
	$('comments_container').innerHTML = '<div id="new_comment' + comment_id + '" class="one_comment" style="display: none;"><p>' + $('comentariu').value + '</p><div><b>' + url_link + '</b> <span>' + show_date + '</span></div></div>' + $('comments_container').innerHTML;
	resetForm();
	new Effect.BlindDown('new_comment' + comment_id, {duration: 0.5});
}

function hideDefaultValue(input, value)
{
	$(input).onfocus = function()
	{
		if(this.value == value)
		{
			this.value = '';
		}	
		this.parentNode.id = 'input_focus';
	}
	
	$(input).onblur = function()
	{
		if(this.value == '')
		{
			this.value = value;
		}
		this.parentNode.id = '';
	}
}

function showCode(src, height)
{
	$('code-view').style.display = 'block';
	$('code-view').style.height = height + 'px';
	$('code-view').src = src;
}
