/**
 * Some JS for search comments
 *  
 * @version $Id: comments.js 8810 2009-10-21 07:38:53Z anton $
 */




/**
 * toggle comments for img
 *   
 * @param iterator - number of result
 * @param idSearchType - id of the search type
 * @param idUrl - id of the url
 * @return void
 */
function toggleCommentsI(iterator, idSearchType, idUrl, obj) {
	var compoundId = iterator+idUrl;
	
	var Id = 'comment_area_'+compoundId;
	//console.log('Id = ',Id);
	
	
	//$('.item').children().removeClass("bgrey");
	$('.item').removeClass("bgblue").addClass("bgrey");
	$(obj).removeClass("bgrey").addClass("bgblue");
	
	var commentArea = $(obj).nextAll('.comment_area')[0];
	
	if ($(commentArea).attr('id') != Id) {
		$(commentArea).html('');
	} 
	
	var commentAreaContent = $(commentArea).html();

	//console.log('area=',commentAreaContent);
	if(commentAreaContent=='') {
		$('.comment_area').removeClass('loading h50').html('');
		$(commentArea).addClass('loading h50').attr('id',Id);
		loadCommentsI(iterator, idSearchType, idUrl, commentArea);
	}
	else {
		$('.comment_area').removeClass('loading h50').html('');
		//$(commentArea).html('').removeClass('loading h50');
	}
	
	return false;
}
/**
 * toggle comments
 *   
 * @param iterator - number of result
 * @param idSearchType - id of the search type
 * @param idUrl - id of the url
 * @return void
 */
function toggleComments(iterator, idSearchType, idUrl) {
	var compoundId = iterator+idUrl;
	
	$('.item').children().removeClass("bgrey");
	$('.item').children().addClass("bgrey");
	
	var commentArea = jQuery('#comment_area_'+compoundId);
	$(commentArea).show();
	$(commentArea).addClass('loading h50');
	
	var commentAreaContent = commentArea.html();
	if(commentAreaContent=='') {
		$('.comment_area').removeClass('loading h50').html('');
		loadComments(iterator, idSearchType, idUrl);
		//$(commentArea)
	}
	else {
		$('.comment_area').removeClass('loading h50').html('');
		commentArea.html('').removeClass('loading h50');
		$(commentArea).hide();
	}
	
	if (typeof allLocalHeight == 'function') {
		allHeight = allLocalHeight();
	}

	if (typeof slideMap == 'function') {
		topLocal = $('.local:eq(0)')[0].offsetTop + $('.search_wrapper > table')[0].offsetTop - 10 + (($.browser.opera) ? 5 : 0);
		//slideMap(topLocal);
	}

	return false;
}
/**
* load comments
*   
* @param iterator - number of result
* @param idSearchType - id of the search type
* @param idUrl - id of the url
* @return void
*/
function loadComments(iterator, idSearchType, idUrl) {
	var compoundId = iterator+idUrl;	
	jQuery.getJSON(site_url+'search/ajax_comments/'+idUrl+'/'+idSearchType+'/'+iterator, {}, function(data, textStatus) {
		jQuery('#comment_area_'+compoundId).html(data.content).removeClass('loading h50');
		limitation();
		if(data.numComments!='undefined') {
			jQuery('#number_comments_'+iterator).html(data.numComments).removeClass('loading h50');;
		}
		
		if (typeof slideMap == 'function') {
			topLocal = $('.local:eq(0)')[0].offsetTop + $('.search_wrapper > table')[0].offsetTop - 10 + (($.browser.opera) ? 5 : 0);
			//slideMap(topLocal);
		}
		
	});	
}
/**
* load comments for img
*   
* @param iterator - number of result
* @param idSearchType - id of the search type
* @param idUrl - id of the url
* @return void
*/
function loadCommentsI(iterator, idSearchType, idUrl, area) {
	var compoundId = iterator+idUrl;	
	jQuery.getJSON(site_url+'search/ajax_comments/'+idUrl+'/'+idSearchType+'/'+iterator, {}, function(data, textStatus) {
		jQuery(area).html(data.content).removeClass('loading h50');
		limitation();
		if(data.numComments!='undefined') {
			jQuery('#number_comments_'+iterator).html(data.numComments);
		}		
	});	
}
/**
* post new comment
*   
* @param iterator - number of result
* @param idSearchType - id of the search type
* @param idUrl - id of the url
* @return void
*/
function postComment(iterator, idSearchType, idUrl) {
	var compoundId = iterator+idUrl;
	var subject = jQuery.trim(jQuery('#comment_area_'+compoundId+' input[name="subject"]').val());
	var message = jQuery.trim(jQuery('#comment_area_'+compoundId+' textarea[name="message"]').val());
	var replyTo= jQuery.trim(jQuery('#comment_area_'+compoundId+' input[name="reply_to"]').val());
	var url = jQuery('#comment_url_'+compoundId).val();
	if((subject=='') || (message=='') ) {
		alert('You have to put some text into subject and message area to post your comment');
		return;
	}
	jQuery.post(site_url+'search/ajax_comments/'+idUrl+'/'+idSearchType+'/'+iterator,
			{
				'action' : 'add',
				'subject' : subject,
				'message' : message,
				'replyTo' : replyTo,
				'url' : url
			},
			function(data, textStatus) {
				jQuery('#comment_area_'+compoundId).html(data.content);
				limitation();				
				if(data.numComments!='undefined') {
					jQuery('#number_comments_'+iterator).html(data.numComments);
				}
				if(data.messages!='undefined') {
					jQuery('#comment_notice_wrapper_'+compoundId).show().addClass('addedComment');
					jQuery('#comment_notice_'+compoundId).html(data.messages);
				}
			},
			'json'
	);
	jQuery('.leaveComment').parent().addClass('commentLoad');
	jQuery('.leaveComment').addClass('hide');
	jQuery('.addedComment').addClass('hide');
}

/**
* make a reply to a comment
*   
* @param iterator - number of result
* @param idSearchType - id of the search type
* @param idUrl - id of the url
* @param idComment - id of comment we reply to
* @return void
*/
function replyTo(iterator, idSearchType, idUrl, idComment) {
	var compoundId = iterator+idUrl;
	
	var userName = jQuery('#comment_'+idComment+' .comment_name').html();
	var subject = jQuery('#comment_'+idComment+' .comment_subject').html();
	var replyToString = 'In reply to comment of '+userName+ ' : '+subject;
	jQuery('#comment_to_'+compoundId).html(replyToString);
	jQuery('#comment_area_'+compoundId+' input[name="subject"]').val('Re: ' + subject);
	
	jQuery('#comment_area_'+compoundId+' input[name="reply_to"]').val(idComment);
}
