window.addEvent('domready', function(){
    if($('search-input')){
	$('search-input').addEvent('click', function(e){
	    if ($('search-input').value == 'search...') {
		$('search-input').value = '';
	    }
	});
    }
    if($('rating')){
	$('pic-holder').addEvent('mouseover',function(){
	    $('rating').setStyle('display','block');
	});
	$('pic-holder').addEvent('mouseout',function(){
	    $('rating').setStyle('display','none');
	});
	$('rating').getElements('img').each(function(item, index){
	    item.addEvent('mouseover',function(){
		for(i=0;i<=index;i++){
		    $('rating').getElements('img')[i].setProperty('src','layout/star_full.png');
		}
	    });
	    item.addEvent('mouseout',function(){
		for(i=0;i<5;i++){
		    $('rating').getElements('img')[i].setProperty('src','layout/star_empty.png');
		}
	    });
	    item.addEvent('click',function(){
		var req = new Request({
		    method: 'post',
		    url: 'funny-picture-'+$('rating').getProperty('class')+'/rate.html',
		    onSuccess: function(txt){
			$('rating').innerHTML = txt;
		    }
		});
		req.send('value='+(index+1));
	    });
	});
    }
});

/* AJAX */
function ajaxLoad(URL, targetID, values, theMethod){
    var req = new Request({
	method: theMethod,
	url: URL,
	onSuccess: function(txt){
	    $(targetID).innerHTML = txt;
	    addFormEvents();
	}
    });
    req.send(values);
}

function htmlspecialchars(str) {
    if (typeof(str) == "string") {
	str = str.replace(/&/g, "&amp;");
	str = str.replace(/"/g, "&quot;");
	str = str.replace(/'/g, "&#039;");
	str = str.replace(/</g, "&lt;");
	str = str.replace(/>/g, "&gt;");
    }
    return str;
}

function rhtmlspecialchars(str) {
    if (typeof(str) == "string") {
	str = str.replace(/&gt;/ig, ">");
	str = str.replace(/&lt;/ig, "<");
	str = str.replace(/&#039;/g, "'");
	str = str.replace(/&quot;/ig, '"');
	str = str.replace(/&amp;/ig, '&');
    }
    return str;
}

var commentBackupID = 0;
var commentBackupContent;

function deleteComment(idComment){
    ajaxLoad('comment/delete.html', null, 'id=' + idComment, 'post');
    $('comment-' + idComment).dispose();
}


function editComment(idComment){
    if (commentBackupID != 0) {
	cancelEditComment(commentBackupID);
    }
    commentBackupID = idComment;
    textarea = new Element('textarea', {
	'cols': '90',
	'rows': '5',
	'style': 'width:99%'
    });
    submit = new Element('input', {
	'type': 'submit',
	'value': 'update',
	'onclick': 'updateComment(' + idComment + ')'
    });
    br = new Element('br');
    cancel = new Element('a', {
	'href': 'javascript:cancelEditComment(' + idComment + ')',
	'html': ' cancel'
    })
    commentBackupContent = textarea.value = rhtmlspecialchars($('comment-' + idComment).getChildren('div')[1].getChildren('p')[1].innerHTML);
    $('comment-' + idComment).getChildren('div')[1].getChildren('p')[1].empty();
    $('comment-' + idComment).getChildren('div')[1].getChildren('p')[1].adopt(textarea, br, submit, cancel);
}

function cancelEditComment(idComment){
    commentBackupID = 0;
    $('comment-' + idComment).getChildren('div')[1].getChildren('p')[1].empty();
    $('comment-' + idComment).getChildren('div')[1].getChildren('p')[1].innerHTML = commentBackupContent;
}

function updateComment(idComment){
    values = 'id=' + idComment + '&content=' + encodeURIComponent($('comment-' + idComment).getChildren('div')[1].getChildren('p')[1].getChildren('textarea')[0].value);
    ajaxLoad('ajax/comment-edit.html', null, values, 'post');
    commentBackupID = 0;
    $('comment-' + idComment).getChildren('div')[1].getChildren('p')[1].innerHTML = htmlspecialchars($('comment-' + idComment).getChildren('div')[1].getChildren('p')[1].getChildren('textarea')[0].value);
}

function deletePicture(id){
    if(confirm('Are you sure you want to permanently delete this picture?')){
	var req = new Request({
	    url: 'funny-picture-'+id+'/delete.html',
	    onSuccess: function(txt){
		alert('The picture has been deleted.');
	    }
	});
	req.send('id=' + id);
    }
}

function approvePicture(id){
    if(confirm('Are you sure you want to approve this picture?')){
	var req = new Request({
	    method: 'post',
	    url: 'funny-picture-'+id+'/approve.html',
	    onSuccess: function(txt){
		alert('The picture has been approved.');
	    }
	});
	req.send('id='+id);
    }
}

function editPicture(id){
    var req = new Request({
	method: 'get',
	url: 'ajax/picture-edit.html',
	onSuccess: function(txt){
	    $('workspace').innerHTML = txt;
	    $('workspace').style.display = 'block';
	    addFormEvents();
	}
    });
    req.send('id='+id);
}

function addFormEvents(){
    if ($('a-form')) {
	$('a-form').addEvent('submit', function(e){
	    e.stop();
	    //$('watermark-loader').setStyle('display', 'block');
	    this.set('send', {
		onComplete: function(response){
		    $('workspace').set('html', response);
		    //$('watermark-loader').setStyle('display', 'none');
		    addFormEvents();
		}
	    });
	    this.send();
	});
    }

}
