// JavaScript Document

images = [
  path + 'components/com_chronocomments/images/rating-plus-on.png',
  path + 'components/com_chronocomments/images/rating-plus.png',
  path + 'components/com_chronocomments/images/rating-minus-on.png',
  path + 'components/com_chronocomments/images/rating-minus.png',
  path + 'components/com_chronocomments/images/rep-score-left.png',
  path + 'components/com_chronocomments/images/rep-score-right.png',
  path + 'components/com_chronocomments/images/arrow.gif',
  path + 'components/com_chronocomments/images/button.gif',
  path + 'components/com_chronocomments/images/arrow.png',
  path + 'components/com_chronocomments/images/avatar1.png',
  path + 'components/com_chronocomments/images/button.png',
  path + 'components/com_chronocomments/images/comment-header-bg.png',
  path + 'components/com_chronocomments/images/rep-score-center.png',
  path + 'components/com_chronocomments/images/bg-box-bottom.png',
  path + 'components/com_chronocomments/images/bg-box-bottom.gif',
  path + 'components/com_chronocomments/images/bg-box-top.png',
  path + 'components/com_chronocomments/images/bg-box-top.gif',
  path + 'components/com_chronocomments/images/bg-box-body.png',
  path + 'components/com_chronocomments/images/bg-box-body.gif'
  ]
 
function loadImg() {
  var url = images.shift()
  if (url) new Asset.image(url, {onload: loadImg}); 
}
 
window.addEvent('domready', function() {
  loadImg();
});


function showbox(Data){
	Yourbox= new PBBAcpBox();
	Yourbox.alert(
		Data
	);	
}
///////////////////

function showPlus(id) {
	var element = document.getElementById(id);
	element.style.backgroundImage = 'url(' + path + 'components/com_chronocomments/images/rating-plus-on.png)';
	return false
}

function hidePlus(id) {
	var element = document.getElementById(id);
	element.style.backgroundImage = 'url(' + path + 'components/com_chronocomments/images/rating-plus.png)';
	return false
}
	
function showMinus(id) {
	var element = document.getElementById(id);
	element.style.backgroundImage = 'url(' + path + 'components/com_chronocomments/images/rating-minus-on.png)';
	return false
}

function hideMinus(id) {
	var element = document.getElementById(id);
	element.style.backgroundImage = 'url(' + path + 'components/com_chronocomments/images/rating-minus.png)';
	return false
}

function showShadow(id) {
	idLeft = id + '_l';
	idCenter = id + '_c';
	idRight = id + '_r';
	var left = document.getElementById(idLeft);
	var center = document.getElementById(idCenter);
	var right = document.getElementById(idRight);
	left.style.backgroundPosition = '0px -22px';
	center.style.backgroundPosition = '-6px -22px';
	right.style.backgroundPosition = '-729px -22px';
	return false
}

function hideShadow(id) {
	idLeft = id + '_l';
	idCenter = id + '_c';
	idRight = id + '_r';
	var left = document.getElementById(idLeft);
	var center = document.getElementById(idCenter);
	var right = document.getElementById(idRight);
	left.style.backgroundPosition = '0px 0px';
	center.style.backgroundPosition = '-6px 0px';
	right.style.backgroundPosition = '-728px 0px';
	return false
}

function showCommentForm(id) {
	$('post_' + id).setStyle('display' , 'none');
	document.getElementById('post_' + id + '_form').style.display = 'block';
	return false
}

function hideCommentForm(id) {
	$('post_'+ id).setStyle('display' , 'block');
	document.getElementById('post_' + id + '_form').style.display = 'none';
	return false
}

function addPlus(id) {
	var url = "index2.php?option=com_chronocomments&task=ratepos&commentid=" + id;
	var rateCookie = new Hash.Cookie('rateCookie');
	
	new Ajax(url, {
		method: 'get',
		update: $('chrono_comments_AJAX_response_temp'),
		onComplete: function(){ 
			if($('chrono_comments_AJAX_response_temp').getText() == 'pos1'){
				$('score'+id).setText((($('score'+id).getText().toInt() + 1) > 0) ? '+'+($('score'+id).getText().toInt() + 1) : ($('score'+id).getText().toInt() + 1));
			}
		}
	}).request();	
}

function addMinus(id) {
	var url = "index2.php?option=com_chronocomments&task=rateneg&commentid=" + id;
	new Ajax(url, {
		method: 'get',
		update: $('chrono_comments_AJAX_response_temp'),
		onComplete: function(){ 
			if($('chrono_comments_AJAX_response_temp').getText() == 'neg1'){
				$('score'+id).setText((($('score'+id).getText().toInt() - 1) > 0) ? '+'+($('score'+id).getText().toInt() - 1) : ($('score'+id).getText().toInt() - 1));
			}
		}
	}).request();	
}

function unpublishComment(id) {
	var url = "index2.php?option=com_chronocomments&task=unpublish&commentid=" + id;
	new Ajax(url, {
		method: 'get',
		update: $('chrono_comments_AJAX_response_temp'),
		onComplete: function(){ 
			if($('chrono_comments_AJAX_response_temp').getText() == 'unpublish1'){
				alert('Unpublished Successfully!');//$('score'+id).setText((($('score'+id).getText().toInt() - 1) > 0) ? '+'+($('score'+id).getText().toInt() - 1) : ($('score'+id).getText().toInt() - 1));
			}
		}
	}).request();	
}
function editCommentForm(id) {
	$('edit_'+id).setStyle('display' , 'none');
	document.getElementById('edit_' + id + '_form').style.display = 'block';
	return false
}
function hideeditCommentForm(id) {
	$('edit_'+id).setStyle('display' , 'block');
	document.getElementById('edit_' + id + '_form').style.display = 'none';
	return false
}

function submiteditCommentForm(submitid){
	var fx = {
		'loading': new Fx.Style( 'ccloading', 'opacity',{ duration: 200 } ),
		'success': new Fx.Style( 'ccsuccess', 'opacity',{ duration: 200 } ),
		'fail': new Fx.Style( 'ccfail', 'opacity',{ duration: 200 } )
	};
	var showHide = function( el ){
		var FxLoading = new Fx.Style('ccloading', 'opacity',{ duration: 100 });
		var FxResponse = new Fx.Style('ccresponse', 'opacity',{ duration: 100 });					
		FxLoading.start(0,0).chain(function(){
			(fx[ el ]).start(0,1);
			(function(){ (fx[ el ]).start(1,0); }).delay( 4000 );
			(function(){ FxResponse.start(1,0); }).delay( 4000 );						
		});
	}
	
	if($('edit_ccname_'+submitid).value.trim() == ''){
		alert('Please enter your name!');
		$('edit_ccname_'+submitid).focus();
	}else if($('edit_'+submitid+'_text').value.trim() == ''){
		alert('Please enter your comment!');
		$('edit_'+submitid+'_text').focus();
	}else if($('edit_ccemail_'+submitid).value.trim() == ''){
		alert('We need your email for verifications!');
		$('edit_ccemail_'+submitid).focus();
	}else{
		$('edit_ccform_'+submitid).send({
			onRequest: function(){
				var FxResponse = new Fx.Style('ccresponse', 'opacity',{ duration: 100 });
				FxResponse.start(0,1);
				fx.loading.start( 0,1 );
			},
			onComplete: function(){
				showHide( 'success' );
				if(this.response.text == 'NOTAUTH'){
					//do nothing
				}else{
					newDiv = new Element('div', {'class': 'comment-item', 'id' : 'commentID' + submitid}).setHTML(this.response.text);
					$('commentID' + submitid).getLast().injectInside(newDiv);
					$('commentID' + submitid).replaceWith(newDiv);
					//$('commentID' + submitid).replaceWith($('chrono_comments_AJAX_response_temp').getFirst());
				}
			},
			onFailure: function(){
				showHide( 'ccfail' );
			},
			update: $('chrono_comments_AJAX_response_temp')
		});
	}
}

function submitCommentForm(submitid){
	var fx = {
		'loading': new Fx.Style( 'ccloading', 'opacity',{ duration: 200 } ),
		'success': new Fx.Style( 'ccsuccess', 'opacity',{ duration: 200 } ),
		'fail': new Fx.Style( 'ccfail', 'opacity',{ duration: 200 } )
	};
	var showHide = function( el ){
		var FxLoading = new Fx.Style('ccloading', 'opacity',{ duration: 100 });
		var FxResponse = new Fx.Style('ccresponse', 'opacity',{ duration: 100 });					
		FxLoading.start(0,0).chain(function(){
			(fx[ el ]).start(0,1);
			(function(){ (fx[ el ]).start(1,0); }).delay( 4000 );
			(function(){ FxResponse.start(1,0); }).delay( 4000 );						
		});
	}
	var emailFilter=/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/;
	if($('ccname_'+submitid).value.trim() == ''){
		alert('Please enter your name!');
		$('ccname_'+submitid).focus();
	}else if($('post_'+submitid+'_text').value.trim() == ''){
		alert('Please enter your comment!');
		$('post_'+submitid+'_text').focus();
	}else if(($('ccemail_'+submitid).value.trim() == '')||(!(emailFilter.test($('ccemail_'+submitid).value.trim())))){
		alert('We need your valid email address for verifications!');
		$('ccemail_'+submitid).focus();
	}else{
		$('ccform_'+submitid).send({
			onRequest: function(){
				var FxResponse = new Fx.Style('ccresponse', 'opacity',{ duration: 100 });
				FxResponse.start(0,1);
				fx.loading.start( 0,1 );
			},
			onComplete: function(){
				showHide( 'success' );
				//$('post_' + submitid + '_form').replaceWith($('chrono_comments_AJAX_response_temp').getFirst());
				if(this.response.text == 'NOTAUTH'){
					//do nothing
				}else{
					if(CommentPublished){
						if(CommentVerified){
							newDiv = new Element('div', {'class': 'reply'}).setHTML(this.response.text);
							if(submitid != 'x1'){
								//newDiv.injectAfter($('post_' + submitid + '_form').getNext());
								newDiv.injectAfter($('commentID' + submitid).getLast());
								$('post_'+ submitid).setStyle('display' , 'block');
								hideCommentForm(submitid);
								cleanCommentForm(submitid);
							}else{
								commentitem = newDiv.getFirst();
								$('post_' + submitid + '_form').replaceWith(commentitem);
								$(newDiv.getLast()).injectAfter(commentitem);
							}
						}else{
							showbox('Thank you, We have sent you an Email with a verfication link, please click it to verify your comment!');//alert('Thank you, We have sent you an Email with a verfication link, please click it to verify your comment!');
							if(submitid != 'x1'){ hideCommentForm(submitid); }
							cleanCommentForm(submitid);
						}
					}else{
						showbox('Thank you, your comment will be published once reviewed.');//alert('Thank you, your comment will be published once reviewed.');
						if(submitid != 'x1'){ hideCommentForm(submitid); }
						cleanCommentForm(submitid);
					}
				}
			},
			onFailure: function(){
				showHide( 'ccfail' );
			},
			update: $('chrono_comments_AJAX_response_temp')
		});
	}
}

function cleanCommentForm(id) {
	$('post_' + id + '_text').value = '';
	$('ccname_' + id ).value = '';
	$('ccemail_' + id ).value = '';
	$('ccurl_' + id ).value = '';
}
/*function submitCommentForm(id) {
	id1 = id.split('_');
	id2 = 'form_' + id1[1];
	document.getElementById(id2).submit();
}*/
