// 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',
  path + 'components/com_chronocomments/loaderb.gif',
  path + 'components/com_chronocomments/loaderw.gif'
  ]
new Asset.images(images);
/* 
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 toggleForm(){
($('post_x1_form').getStyle('display') == 'none') ? $('post_x1_form').setStyle('display', 'block') : $('post_x1_form').setStyle('display', 'none');	
}

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));
			}
			if($('chrono_comments_AJAX_response_temp').getText() == 'pos0guest'){
				alert(GUEST_CANT_VOTE);	
			}
			if($('chrono_comments_AJAX_response_temp').getText() == 'pos0owner'){
				alert(OWNER_CANT_VOTE);	
			}
		}
	}).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));
			}
			if($('chrono_comments_AJAX_response_temp').getText() == 'neg0guest'){
				alert(GUEST_CANT_VOTE);	
			}
			if($('chrono_comments_AJAX_response_temp').getText() == 'neg0owner'){
				alert(OWNER_CANT_VOTE);	
			}
		}
	}).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_SUCCESS);//$('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(ENTER_NAME);
		$('edit_ccname_'+submitid).focus();
	}else if($('edit_'+submitid+'_text').value.trim() == ''){
		alert(ENTER_COMMENT);
		$('edit_'+submitid+'_text').focus();
	//}else if(($('edit_ccemail_'+submitid).value.trim() == '')&&(!hideemail)){
		//alert(ENTER_EMAIL);
		//$('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 );
				$('post_'+ submitid +'_form_loading').setStyle('display', 'block');
			},
			onComplete: function(){
				//showHide( 'success' );
				$('post_'+ submitid +'_form_loading').setStyle('display', 'none');
				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' );
				$('post_'+ submitid +'_form_loading').setStyle('display', 'none');
			}//,
			//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(ENTER_NAME);
		$('ccname_'+submitid).focus();
	}else if($('ccname_'+submitid).value.trim().length > 50){
		alert(NAME_LENGTH);
		$('ccname_'+submitid).focus();
	}else if($('post_'+submitid+'_text').value.trim() == ''){
		alert(ENTER_COMMENT);
		$('post_'+submitid+'_text').focus();
	}else if((!hideemail)&&($chk($('ccemail_'+submitid)))&&(($('ccemail_'+submitid).value.trim() == '')||(!(emailFilter.test($('ccemail_'+submitid).value.trim()))))){
		//if(($('ccemail_'+submitid).value.trim() == '')||(!(emailFilter.test($('ccemail_'+submitid).value.trim())))){
			alert(ENTER_EMAIL);
			$('ccemail_'+submitid).focus();
		//}
	}else if((!hideurl)&&($chk($('ccurl_'+submitid)))&&(!$('ccurl_'+submitid).value.trim().test("http://"))){
		//if(!$('ccurl_'+submitid).value.trim().test("http://")){
			alert(URL_HTTP);
			$('ccurl_'+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 );
				$('post_'+ submitid +'_form_loading').setStyle('display', 'block');
			},
			onComplete: function(){
				$('post_'+ submitid +'_form_loading').setStyle('display', 'none');
				//alert(this.response.text);
				//this.response.text = this.response.text.split('+++++')[1];
				//showHide( 'success' );
				//$('post_' + submitid + '_form').replaceWith($('chrono_comments_AJAX_response_temp').getFirst());
				if(this.response.text == 'NOTAUTH'){
					//do nothing
					//showHide( 'fail' );
				}else if(this.response.text == 'NOTAUTH_CAPTCHA'){
					//showHide( 'fail' );
					showbox(WRONG_CODE);//alert('Wrong Code Entered!');
					$('post_'+ submitid +'_form_loading').setStyle('display', 'none');
					//cleanCommentForm(submitid);
					//replace captcha
					if(captcha == '1'){
						new Asset.image(path + 'components/com_chronocomments/chrono_verification.php?imtype='+ imtype +'&anticache=' + $random(1,99999) , {
							onload: function(){
								this.setProperty('id', 'comment_captcha_img');
								this.setProperty('class', 'comment_captcha_img');
								//var image = this.clone();
								var counter = 0;
								var imagesarray = new Array();
								$$('img.comment_captcha_img').each(function(item){
									imagesarray[counter] = item;
									counter = counter + 1;
								});
								for (var i = 0; i < counter ; i++) {
									var image = this.clone();
									imagesarray[i].replaceWith(image);
								}
							}
						});
					}
					//end captcha
				}else{
					//showHide( 'success' );
					if(CommentPublished){
						if(CommentVerified){
							newDiv = new Element('div', {'class': 'reply'}).setHTML(this.response.text);
							//alert(newDiv.innerHTML);
							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);*/
								newDiv.getFirst().injectInside($('allcomments'));
								cleanCommentForm(submitid);
							}
							//replace captcha
							if(captcha == '1'){
								new Asset.image(path + 'components/com_chronocomments/chrono_verification.php?imtype='+ imtype +'&anticache=' + $random(1,99999) , {
									onload: function(){
										this.setProperty('id', 'comment_captcha_img');
										this.setProperty('class', 'comment_captcha_img');
										//var image = this.clone();
										var counter = 0;
										var imagesarray = new Array();
										$$('img.comment_captcha_img').each(function(item){
											imagesarray[counter] = item;
											counter = counter + 1;
										});
										for (var i = 0; i < counter ; i++) {
											var image = this.clone();
											imagesarray[i].replaceWith(image);
										}
									}
								});
							}
							//end captcha
						}else{
							showbox(NOT_VERIFIED);
							if(submitid != 'x1'){ hideCommentForm(submitid); }
							$('post_'+ submitid +'_form_loading').setStyle('display', 'none');
							cleanCommentForm(submitid);
						}
					}else{
						showbox(NOT_PUBLISHED);
						if(submitid != 'x1'){ hideCommentForm(submitid); }
						$('post_'+ submitid +'_form_loading').setStyle('display', 'none');
						cleanCommentForm(submitid);
					}
				}
			},
			onFailure: function(){
				//showHide( 'fail' );
				$('post_'+ submitid +'_form_loading').setStyle('display', 'none');
			}//,
			//update: $('chrono_comments_AJAX_response_temp')
		});
	}
}

function cleanCommentForm(id) {
	$('post_' + id + '_text').value = '';
	$('ccname_' + id ).value = '';
	if((!hideemail)&&($chk($('ccemail_'+ id)))){
		$('ccemail_' + id ).value = '';
	}
	if((!hideurl)&&($chk($('ccurl_'+ id)))){
		$('ccurl_' + id ).value = 'http://';
	}
	if(captcha == '1'){
		$('comment_captcha_' + id ).value = '';	
	}
}

function nextComments(component, pageid) {
	var url = "index2.php?option=com_chronocomments&task=nextcomments&component=" + component + "&pageid=" + pageid +'&anticache=' + $random(1,99999);
	new Ajax(url, {
		method: 'get',
		//update: $('allcomments'),
		onRequest: function(){
			$('allcomments').setHTML('<br /><br /><br /><br /><br /><br /><div style="text-align:center; height: 200px;"><img src="' + path + 'components/com_chronocomments/loader' + loadercolor + '.gif' + '" /></div><br /><br />');
		},
		onComplete: function(){ 
			$('allcomments').setHTML(new Element('div').setHTML(this.response.text).getFirst().innerHTML);//alert($('chrono_comments_AJAX_response_temp').getText());
			$('paginationlinks').replaceWith(new Element('div').setHTML(this.response.text).getLast());
		}
	}).request();	
}

function prevComments(component, pageid) {
	var url = "index2.php?option=com_chronocomments&task=prevcomments&component=" + component + "&pageid=" + pageid +'&anticache=' + $random(1,99999);
	new Ajax(url, {
		method: 'get',
		update: $('allcomments'),
		onRequest: function(){
			$('allcomments').setHTML('<br /><br /><br /><br /><br /><br /><div style="text-align:center; height: 200px;"><img src="' + path + 'components/com_chronocomments/loader' + loadercolor + '.gif' + '" /></div><br /><br />');
		},
		onComplete: function(){ 
			$('allcomments').setHTML(new Element('div').setHTML(this.response.text).getFirst().innerHTML);//alert($('chrono_comments_AJAX_response_temp').getText());
			$('paginationlinks').replaceWith(new Element('div').setHTML(this.response.text).getLast());
		}
	}).request();	
}

function navigateComments(component, pageid, pagenum) {
	var url = "index2.php?option=com_chronocomments&task=navigatecomments&component=" + component + "&pageid=" + pageid + "&pagenum=" + pagenum +'&anticache=' + $random(1,99999);
	new Ajax(url, {
		method: 'get',
		update: $('allcomments'),
		onRequest: function(){
			$('allcomments').setHTML('<br /><br /><br /><br /><br /><br /><div style="text-align:center; height: 200px;"><img src="' + path + 'components/com_chronocomments/loader' + loadercolor + '.gif' + '" /></div><br /><br />');
		},
		onComplete: function(){ 
			$('allcomments').setHTML(new Element('div').setHTML(this.response.text).getFirst().innerHTML);//alert($('chrono_comments_AJAX_response_temp').getText());
			$('paginationlinks').replaceWith(new Element('div').setHTML(this.response.text).getLast());
		}
	}).request();	
}
/*function submitCommentForm(id) {
	id1 = id.split('_');
	id2 = 'form_' + id1[1];
	document.getElementById(id2).submit();
}*/

// <![CDATA[
	var form_name = 'ccform_x1';
	var text_name = 'text';
	var load_draft = false;
	var upload = false;

	// Define the bbCode tags
	var bbcode = new Array();
	var bbtags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[quote]','[/quote]','[code]','[/code]','[list]','[/list]','[li]','[/li]','[img]','[/img]','[url]','[/url]','[flash=]', '[/flash]','[size=]','[/size]');
	var imageTag = false;

	// Helpline messages
	var help_line = {
		b: 'Bold text: [b]text[/b]',
		i: 'Italic text: [i]text[/i]',
		u: 'Underline text: [u]text[/u]',
		q: 'Quote text: [quote]text[/quote]',
		c: 'Code display: [code]code[/code]',
		l: 'List: [list]text[/list]',
		o: 'Ordered list: [list=]text[/list]',
		p: 'Insert image: [img]http://image_url[/img]',
		w: 'Insert URL: [url]http://url[/url] or [url=http://url]URL text[/url]',
		a: 'Inline uploaded attachment: [attachment=]filename.ext[/attachment]',
		s: 'Font colour: [color=red]text[/color]  Tip: you can also use color=#FF0000',
		f: 'Font size: [size=85]small text[/size]',
		e: 'List: Add list element',
		d: 'Flash: [flash=width,height]http://url[/flash]'
			}

	var panels = new Array('options-panel', 'attach-panel', 'poll-panel');
	var show_panel = 'options-panel';


// ]]>
