var currentArticle = 0;
var timeoutId;
var time = 10000;
var maxArticle = 3;
var commentsCaptchaRequested = false;

function submitCommentsForm() {
	var o = document.forms.commentsForm;
	if (o.anonymous_author_name.value == "") {
		o.anonymous_author_name.focus();
		alert("Introduceti nume autor");
		return false;
	} else if (o.anonymous_author_name.value == "" || o.message.value == "") {
		o.message.focus();
		alert("Introduceti mesaj");
		return false;
	}
	o.submit();
	return true;
}
function setUsername (id) {
	var userName = getCookie('user_name');
	if (userName && document.getElementById('message'+ id).value.length == 0) {
		document.getElementById('anonymous_author_name'+ id).value = userName;
	}
}
function enableCaptcha(id, state) {
	var user = arguments[2] || '';
	if (state && !commentsCaptchaRequested && !getCookie(user) && (document.getElementById('commentsDetails').value.length > 0)) {
		document.getElementById("imgCaptcha"+ id).src = "/cms/images/captcha.jpg.php?" + Math.random();
		document.getElementById('divCaptcha'+ id).style.display = 'block';
		commentsCaptchaRequested = true;
	}
}
function showCaptcha(id) {
	if (document.getElementById("imgCaptcha"+id).src.match("spacer")) {
		document.getElementById("imgCaptcha"+id).src = '/cms/images/captcha.jpg.php?' + Math.random();
	}
	document.getElementById("divCaptcha"+id).style.display = 'block';
}

function focusArticle(id)
{

	if(document.getElementById('stiri'+id).style.display=='none')
	{
		document.getElementById('stiri1').style.display='none';
		document.getElementById('stiri2').style.display='none';
		document.getElementById('stiri3').style.display='none';
		document.getElementById('stiri'+id).style.display='block';
		document.getElementById('li'+id+'_1').setAttribute('class','');
		document.getElementById('li'+id+'_2').setAttribute('class','');
		document.getElementById('li'+id+'_3').setAttribute('class','');
		document.getElementById('li'+id+'_'+id).setAttribute('class','selected');
	}
}

function rotate()
{
	if(currentArticle==maxArticle)
	{
		currentArticle = 1;
		focusArticle(1);
	}else
	{
		currentArticle = currentArticle+1;
		focusArticle(currentArticle)
	}
    window.clearTimeout(timeoutId);
    timeoutId = window.setTimeout('rotate()', time);	
}

function popup (url, w, h) {
	pictures = window.open(url, 'popup', 'width='+ w +',height='+ h +',top='+ (screen.height/2-h/2) +',left='+ (screen.width/2-w/2) +',scrollbars=1,toolbar=0,resizable=1');
}

function ShowDetails(div_id, button_id)
{
	document.getElementById(div_id).style.display='block';
	document.getElementById(button_id+'_show').style.display='none';
	document.getElementById(button_id+'_hide').style.display='inline';
}

function HideDetails(div_id, button_id)
{
	document.getElementById(div_id).style.display='none';
	document.getElementById(button_id+'_show').style.display='inline';
	document.getElementById(button_id+'_hide').style.display='none';
}

function submitPoll (file, params, id) {
	xmlAsyncRequestText (file, params, id);
}

function pollVote(box_id, poll_id) {
	xmlAsyncRequestText ('/ajax?'+box_id+'&submit=1&itemID='+poll_id+'&service_type=PollContest'
		+'&rnd='+Math.random()
		, Form_MakeQueryString('fp'+poll_id)
		, 'dinamicContent'+box_id+'_'+poll_id);
	return false;
}
function pollShowResults(box_id, poll_id) {
	xmlAsyncRequestText ('/ajax?'+box_id+'&submit=1&itemID='+poll_id+'&service_type=PollContest&output=results'
		+'&rnd='+Math.random()
		, ''
		, 'dinamicContent'+box_id+'_'+poll_id);
	return false;
}
function pollShowForm(box_id, poll_id) {
	xmlAsyncRequestText ('/ajax?'+box_id+'&submit=0&itemID='+poll_id+'&service_type=PollContest'
		, ''
		, 'dinamicContent'+box_id+'_'+poll_id);
	return false;
}
function doSearch (f) {
	if (f.q.value.length == 0) {
		alert('Introduceţi un text pentru căutare');
		return false;
	}
	return true;
}

//Uses values set in the tpl
function changePress(total)
{
	prevPressDiv(total);
	window.clearTimeout(pressTimeoutId);
	pressTimeoutId = window.setTimeout('changePress(' + total + ')', interval);
}
function prevPressDiv(total)
{
	var auxcnt = 1;
	for(var i=1; i<=total; i++)
	{
	if (i+currentpressDiv <= total) aux = i+currentpressDiv;
	else aux = auxcnt++;
	document.getElementById('pressDiv' + i).innerHTML = myPressArray[aux];
	}
	currentpressDiv++;
	if (currentpressDiv > total) currentpressDiv = 1;
	if (total > 4)
	{
	window.clearTimeout(pressTimeoutId);
	pressTimeoutId = window.setTimeout('changePress(' + total + ')', 5000);
	}
}
function nextPressDiv(total)
{
	var auxcnt = total;
	var aux = currentpressDiv-1;
	for(var i=1; i<=total; i++)
	{
	if (aux < 1) aux = total;
	if (aux > total) aux = 1;
	document.getElementById('pressDiv' + i).innerHTML = myPressArray[aux++];
	}
	currentpressDiv--;
	if (currentpressDiv < 1) currentpressDiv = total;
	if (total > 4)
	{
	window.clearTimeout(pressTimeoutId);
	pressTimeoutId = window.setTimeout('changePress(' + total + ')', 5000);
	}
}
$(document).ready(function() {
	if ($('#commentsForm').length > 0)
	{
		$('#commentsForm').nextComments();
	}
});
