window.addEvent('domready', function(){
    if($('search-input')){
	$('search-input').addEvent('click', function(e){
	    if ($('search-input').value == 'caută...') {
		$('search-input').value = '';
	    }
	});
    }
    if($('articleImage')){
	$(document.body).getElement('img.thumb').addEvent('mouseover',function(event){
	    $('articleImage').setStyle('display','inherit');
	});
	$(document.body).getElement('img.thumb').addEvent('mouseleave',function(event){
	    $('articleImage').setStyle('display','none');
	});
    }
    if($('article-url')){
	$('article-url').getElement('input').onblur = function(){
	    if($(document.body).getElement('input[name=title]').value == ''){
		$('article-url-loader').innerHTML = 'îți încarc titlul...';
		new Request({
		    url: 'ajax/title-from-url.html',
		    method: 'post',
		    onSuccess: function(txt){
			$('article-url-loader').innerHTML = '';
			if($(document.body).getElement('input[name=title]').value == ''){
			    $(document.body).getElement('input[name=title]').value = txt;
			}
		    }
		}).send('url=' + encodeURIComponent($('article-url').getElement('input').value));
	    }
	};
    }
    if ($('form-login')) {
	$('form-login').addEvent('submit', function(e){
	    e.stop();
	    $('form-login').setStyle('background-image', 'url(layout/ajax-login.gif)');
	    $('login-result').set('html', '');
	    this.set('send', {
		data: {
		    'do': '1'
		},
		onComplete: function(response){
		    $('login-result').set('html', response);
		    $('form-login').setStyle('background-image', '');
		    if(response.search('success') != -1){
			location.replace(window.location);
		    }
		}
	    });
	    this.send();
	});
    }
    if ($('modal-form-login')) {
	$('modal-form-login').addEvent('submit', function(e){
	    e.stop();
	    $('modal-login-result').setStyle('background-image', 'url(layout/ajax-login.gif)');
	    $('modal-login-result').set('html', '');
	    var newHiddenField = new Element('input', {
		'type': 'hidden',
		'name': 'ajax',
		'value': 'true'
	    });
	    $('modal-form-login').adopt(newHiddenField);
	    this.set('send', {
		data: {
		    'do': '1'
		},
		onComplete: function(response){
		    $('modal-login-result').set('html', response);
		    $('modal-login-result').setStyle('background-image', '');
		}
	    });
	    this.send();
	});
    }
    if($('events')){
	var addCount = function(){
	    var lastEvent = $('events').getElements('tr')[0].id.substr(6, $('events').getElements('tr')[0].id.length-6);
	    new Request({
		url: 'ajax/get-event.html',
		method: 'get',
		onSuccess: function(txt){
		    if(txt.trim() != ''){
			$('events').getElements('tbody')[0].innerHTML = txt+$('events').getElements('tbody')[0].innerHTML;
			if($('events').getElements('tr').length > 20){
			    $('events').getElements('tr')[20].dispose();
			}
		    }
		}
	    }).send('event=' + lastEvent);
	};
	addCount.periodical(5000);
    }
    if($('article-edit')){
	if($('timer')){
	    seconds = parseInt($('timer').innerHTML);
	    var countDown = function(){
		$('timer').setStyle('display','inline');
		if(seconds > 0){
		    seconds--;
		    $('timer').innerHTML = '('+Math.floor(seconds/60)+':'+(seconds%60<10? '0'+seconds%60:seconds%60)+')';
		}
		else{
		    new Fx.Tween($('article-edit'),{
			duration:1000,
			onComplete: function() {
			    $('article-edit').dispose();
			}
		    }).start('height', '0');
		}
	    };
	    countDown.periodical(1000);
	}
    }
    if($('topRange')){
	$('topRange').addEvent('change', function(e){
	    new Request({
		url: 'ajax/set-top-range.html',
		method: 'get',
		onSuccess: function(txt){
		    window.location.reload(false);
		}
	    }).send('topRange=' + $('topRange').value);
	})
    }
});

// timer for editing
var seconds;

// comments
replyIdDisplayed = null;

function reply(idComment){
    if (replyIdDisplayed == null || idComment != replyIdDisplayed) {
	$('newCom').setStyle('display', 'none');
	if (replyIdDisplayed) {
	    $('reply-' + replyIdDisplayed).dispose();
	    replyIdDisplayed = null;
	}
	var indent = $('comment-' + idComment).getProperty('class')[6];

	var reply = new Element('li', {
	    id: 'reply-' + idComment
	});
	reply.addClass('indent' + (parseInt(indent) + 1));

	var form = new Element('form', {
	    id: 'replyComment',
	    method: 'post',
	    action: 'ajax/post-comment.html'
	});
	var table = new Element('table');
	var tr1 = new Element('tr');
	var tr2 = new Element('tr');
	var td1 = new Element('td');
	var td2 = new Element('td');
	var textarea = new Element('textarea', {
	    name: 'comment'
	});
	var hidden1 = new Element('input', {
	    type: 'hidden',
	    name: 'parentID',
	    value: idComment
	})
	var hidden2 = new Element('input', {
	    type: 'hidden',
	    name: 'indent',
	    value: parseInt(indent) + 1
	})
	var button = new Element('input', {
	    type: 'submit',
	    value: 'OK'
	});
	var cancelLink = new Element('a',{
	    href:'javascript:reply('+idComment+')',
	    text:'renunță'
	});

	td1.adopt(textarea);
	td1.adopt(hidden1);
	td1.adopt(hidden2);
	td2.adopt(button);
	td2.adopt(cancelLink);
	tr1.adopt(td1);
	tr2.adopt(td2);
	table.adopt(tr1, tr2);
	form.adopt(table);
	reply.adopt(form);



	var theTarget = $('comment-' + idComment);

	reply.inject(theTarget, 'after');
	replyIdDisplayed = idComment;


	//-------------------------------
	$('replyComment').addEvent('submit', function(e){
	    e.stop();
	    this.set('send', {
		onComplete: function(response){
		    $('reply-' + idComment).innerHTML = response;
		    if(response.trim() != ''){
			var newID = $('reply-' + idComment).getChildren()[0].innerHTML;
			$('reply-' + idComment).getChildren()[0].innerHTML = '';
			$('reply-' + idComment).setProperty('id', 'comment-' + newID);
		    }
		    else{
			$('reply-' + idComment).dispose();
		    }
		    replyIdDisplayed = null;
		    $('newCom').setStyle('display', 'inherit');
		}
	    });
	    this.send();
	});
    }
    else {
	$('reply-' + replyIdDisplayed).dispose();
	replyIdDisplayed = null;
	$('newCom').setStyle('display', 'inherit');
    }
}

function editComment(comment){
    var req = new Request({
	url: 'ajax/edit-comment.html',
	method: 'get',
	onSuccess: function(txt){
	    $('comment-' + comment).getElements('p')[0].innerHTML = txt;

	    // adding the ajax to the new form
	    $('edit-comment-'+comment).addEvent('submit', function(e){
		e.stop();
		this.set('send', {
		    onComplete: function(response){
			$('comment-' + comment).getElements('p')[0].innerHTML = response;
		    }
		});
		if($('edit-comment-'+comment).getElements('textarea[name=content]')[0].value.trim() == ''){
		    alert('Nu ai introdus mesajul editat.');
		}
		else{
		    if($('edit-comment-'+comment).getElements('input[name=motive]')[0].value.trim() == ''){
			alert('Nu ai introdus motivul editării.');
		    }
		    else{
			this.send();
		    }
		}
				
	    });
	}
    });
    req.send('comment=' + comment);
}

function cancelEditComment(comment){
    var req = new Request({
	url: 'ajax/cancel-edit-comment.html',
	method: 'get',
	onSuccess: function(txt){
	    $('comment-' + comment).getElements('p')[0].innerHTML = txt;
	}
    });
    req.send('comment=' + comment);
}

function rate(article, value, level){
    if(level == 0){
	showModal();
    }
    else{
	$('rating-box-' + article).getElement('span').setProperty('class','loading');
	$('rating-box-' + article).getElement('span').innerHTML = '';
	if(value==1){
	    $('rating-box-' + article).getElements('a')[0].setProperty('class', 'hidden');
	    $('rating-box-' + article).getElements('a')[1].removeProperty('class');
	}
	else{
	    $('rating-box-' + article).getElements('a')[0].removeProperty('class');
	    $('rating-box-' + article).getElements('a')[1].setProperty('class', 'hidden');
	}


	var req = new Request({
	    url: 'ajax/rating.html',
	    onSuccess: function(txt){
		$('rating-box-' + article).getElement('span').removeProperty('class');
		$('rating-box-' + article).getElement('span').innerHTML = txt;
	    }
	});
	req.send('article=' + article + '&value=' + value);
    }
}

function rateComment(comment, value){
    var req = new Request({
	url: 'ajax/rating-comment.html',
	onSuccess: function(txt){
	    $('comment-' + comment).getElements('span')[2].innerHTML = txt;
	    var rating = $('comment-' + comment).getElements('span')[1].innerHTML;
	    rating = rating.substr(6, rating.length-6);
	    if(value == 0){
		rating = parseInt(rating)-1;
	    }
	    else{
		rating = parseInt(rating)+1;
	    }
	    $('comment-' + comment).getElements('span')[1].innerHTML = 'șmen: '+rating;
	}
    });
    req.send('comment=' + comment + '&value=' + value);
}

function searchTimeframe(direction){
    var currentlyDisplayed;
    var nextDisplayed;
    var timeframes = new Array(0,8760,720,168,24);
    if($('search-options').getElements('strong')[0].getStyle('display') == 'inline'){
	currentlyDisplayed = 0;
    }
    if($('search-options').getElements('strong')[1].getStyle('display') == 'inline'){
	currentlyDisplayed = 1;
    }
    if($('search-options').getElements('strong')[2].getStyle('display') == 'inline'){
	currentlyDisplayed = 2;
    }
    if($('search-options').getElements('strong')[3].getStyle('display') == 'inline'){
	currentlyDisplayed = 3;
    }
    if($('search-options').getElements('strong')[4].getStyle('display') == 'inline'){
	currentlyDisplayed = 4;
    }
    nextDisplayed = currentlyDisplayed + direction;
    if(nextDisplayed>4){
	nextDisplayed = 0;
    }
    if(nextDisplayed<0){
	nextDisplayed = 4;
    }
    $('search-options').getElements('input')[1].setProperty('value',timeframes[nextDisplayed]);
    $('search-options').getElements('strong')[currentlyDisplayed].setStyle('display','none');
    $('search-options').getElements('strong')[nextDisplayed].setStyle('display','inline');
}

function searchOrder(){
    if($('search-options').getElements('strong')[5].getStyle('display') == 'inline'){
	$('search-options').getElements('strong')[5].setStyle('display','none');
	$('search-options').getElements('strong')[6].setStyle('display','inline');
	$('search-options').getElements('input')[2].setProperty('value','date');
    }else{
	$('search-options').getElements('strong')[5].setStyle('display','inline');
	$('search-options').getElements('strong')[6].setStyle('display','none');
	$('search-options').getElements('input')[2].setProperty('value','smen');
    }
}

function deleteReplyNotification(id){
    new Fx.Tween($('notification-'+id),{
	duration:100,
	onComplete: function() {
	    $('notification-'+id).dispose();
	}
    }).start('height', '0');

    new Request({
	url: 'ajax/delete-reply-notification.html',
	method: 'post'
    }).send('id=' + id);
}

function deleteCommentNotification(id){
    new Fx.Tween($('notification-'+id),{
	duration:100,
	onComplete: function() {
	    $('notification-'+id).dispose();
	}
    }).start('height', '0');

    new Request({
	url: 'ajax/delete-comment-notification.html',
	method: 'post'
    }).send('id=' + id);
}

function deleteMentionNotification(id){
    new Fx.Tween($('notification-'+id),{
	duration:100,
	onComplete: function() {
	    $('notification-'+id).dispose();
	}
    }).start('height', '0');

    new Request({
	url: 'ajax/delete-comment-mention.html',
	method: 'post'
    }).send('id=' + id);
}

function deleteMessage(id){
    if(confirm('Ești sigur că vrei să ștergi acest mesaj?')){
	new Fx.Tween($('message-'+id),{
	    duration:100,
	    onComplete: function() {
		$('message-'+id).dispose();
	    }
	}).start('height', '0');

	new Request({
	    url: 'ajax/delete-message.html',
	    method: 'post'
	}).send('id=' + id);
    }
}

function toggleArticleSubscription(idArticle, add){
    if(add == 0){
	$('article-subscription').getChildren()[0].setStyle('display','none');
	$('article-subscription').getChildren()[1].setStyle('display','inline');
    }
    else{
	$('article-subscription').getChildren()[0].setStyle('display','inline');
	$('article-subscription').getChildren()[1].setStyle('display','none');
    }

    new Request({
	url: 'ajax/article-subscription.html',
	method: 'post'
    }).send('article=' + idArticle + '&add=' + add);
}

function toggleSideBox(){
    if($('top-articles').getStyle('display') == 'block'){
	$('top-articles').setStyle('display','none');
	$('top-commented-articles').setStyle('display','block');
	$('article-toggle').setStyle('background-position', '1px -28px');
    }
    else{
	$('top-articles').setStyle('display','block');
	$('top-commented-articles').setStyle('display','none');
	$('article-toggle').setStyle('background-position', '1px 0');
    }
}

var liveOpened = false;
var refreshLive = function(){
    new Request({
	url: 'ajax/live.html',
	onSuccess: function(txt){
	    $('expansion').getChildren()[0].innerHTML = txt;
	    $('expansion').getChildren()[0].fade('in');
	}
    }).send();
};

var intervalID;
var tweenRunning = false;

function showLive(){
    if(!tweenRunning){
	tweenRunning = true;
	if(!liveOpened){
	    $('expansion').setStyle('paddingTop','15px');
	    $('expansion').getChildren()[0].fade('hide');
	    new Fx.Tween($('expansion'),{
		duration:400,
		onComplete: function(){
		    new Request({
			url: 'ajax/live.html',
			onSuccess: function(txt){
			    $('expansion').getChildren()[0].innerHTML = txt;
			    $('expansion').getChildren()[0].fade('in');
			    tweenRunning = false;
			}
		    }).send();
		}
	    }).start('height', '400');
	    liveOpened = true;
	    intervalID = refreshLive.periodical(5000);
	}
	else{
	    $clear(intervalID);
	    $('expansion').getChildren()[0].innerHTML = '';
	    new Fx.Tween($('expansion'),{
		duration:100,
		onComplete: function() {
		    $('expansion').setStyle('paddingTop','0');
		    tweenRunning = false;
		    $('expansion').getChildren()[0].innerHTML = '';
		}
	    }).start('height', '0');
	    liveOpened=false;
	}
    }
}

function showAbout(){
    if($('about').getStyle('marginTop') == '-234px'){
	$('topMargin').getChildren()[0].getChildren()[0].getChildren()[1].setProperty('class','close');
	new Fx.Tween($('about'),{
	    duration:800,
	    onComplete: function(){
		new Request({
		    method: 'post',
		    url: 'ajax/toggleAbout.html'
		}).send('action=open');
	    }
	}).start('marginTop', '0');
    }
    else{
	$('topMargin').getChildren()[0].getChildren()[0].getChildren()[1].setProperty('class','open');
	new Fx.Tween($('about'),{
	    duration:400,
	    onComplete: function(){
		new Request({
		    method: 'post',
		    url: 'ajax/toggleAbout.html'
		}).send('action=close');
	    }
	}).start('marginTop', '-234px');
    }
}

var editOpened = false;
function showEditArticle(idArticle){
    if(editOpened == false){
	editOpened=true;
	new Request({
	    url: 'ajax/article-edit.html',
	    method: 'get',
	    onSuccess: function(txt){
		$('article-edit').getChildren('div')[1].innerHTML = txt;

		// adding the ajax to the new form
		editedArticleAjaxBehaviour();
	    }
	}).send('idarticle=' + idArticle);
    }
}

function editedArticleAjaxBehaviour(){
    $('edit-article-form').addEvent('submit', function(e){
	e.stop();
	this.set('send', {
	    onComplete: function(response){
		if(response == 'ok'){
		    javascript:location.reload(true);
		}else{
		    $('article-edit').getChildren('div')[1].innerHTML = response;
		    editedArticleAjaxBehaviour();
		}
	    }
	});
	this.send();
    });
}

function closeModal(){
    $('modal').setStyle('display','none');
}

function showModal(){
    $('modal').setStyle('display','block');
}

function setCategory(id){
    $('idCategory').setProperty('value', id);
    $('description0').setStyle('display','none');
    for(var i=1;i<=7;i++){
	$('category'+i).setProperty('class','category');
	$('description'+i).setStyle('display','none');
    }

    $('category'+id).setProperty('class','category categorySelected');
    $('description'+id).setStyle('display','block');

    switch(id){
	case 1:
	    $('article-url').setStyle('display','table-row');
	    $('article-image').setStyle('display','table-row');
	    break;
	case 2:
	    $('article-url').setStyle('display','table-row');
	    $('article-image').setStyle('display','table-row');
	    break;
	case 3:
	    $('article-url').setStyle('display','table-row');
	    $('article-image').setStyle('display','none');
	    break;
	case 4:
	    $('article-url').setStyle('display','table-row');
	    $('article-image').setStyle('display','none');
	    break;
	case 5:
	    $('article-url').setStyle('display','none');
	    $('article-image').setStyle('display','table-row');
	    break;
	case 6:
	    $('article-url').setStyle('display','table-row');
	    $('article-image').setStyle('display','table-row');
	    break;
	case 7:
	    $('article-url').setStyle('display','table-row');
	    $('article-image').setStyle('display','table-row');
	    break;
    }
}

function switchTab(position){
    var i;
    for(i=0;i< $('tabContent').getChildren('div')[0].getChildren('div').length;i++){
	$('tabContent').getChildren('div')[0].getChildren('div')[i].setStyle('display','none');
    }
    $('tabContent').getChildren('div')[0].getChildren('div')[position].setStyle('display','block')

    for(i=0;i< $('tabs').getChildren('a').length;i++){
	$('tabs').getChildren('a')[i].removeProperty('class');
    }
    $('tabs').getChildren('a')[position].setProperty('class','active');
}

var articlesCount = 20;
function moreArticles(user){
    new Request({
	url: 'ajax/more-articles.html',
	method: 'get',
	onSuccess: function(txt){
	    $('tabContent').getElements('ul')[0].innerHTML+= txt;
	}
    }).send('user=' + user + '&start=' + articlesCount);
    articlesCount+=10;
}

var commentsCount = 20;
function moreComments(user){
    new Request({
	url: 'ajax/more-comments.html',
	method: 'get',
	onSuccess: function(txt){
	    $('tabContent').getElements('ul')[1].innerHTML+= txt;
	}
    }).send('user=' + user + '&start=' + commentsCount);
    commentsCount+=10;
}

var votesCount = 20;
function moreVotes(user){
    new Request({
	url: 'ajax/more-votes.html',
	method: 'get',
	onSuccess: function(txt){
	    $('tabContent').getElements('ul')[2].innerHTML+= txt;
	}
    }).send('user=' + user + '&start=' + votesCount);
    votesCount+=10;
}
