
/***************************************************************
 *  Copyright notice
 *
 *  (c) 2009 Stephan Jorek, 
 *           @rtplan21 GmbH. - webconcepts & solutions
 * 
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *  A copy is found in the textfile GPL.txt and important notices to the license
 *  from the author is found in LICENSE.txt distributed with these scripts.
 *
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

jQuery.noConflict();

var tx_a21comments_active_comment = null
var tx_a21comments_comments_total = null;
var tx_a21comments_comments_per_page = null;
var tx_a21comments_iso3166countryCodesList = null;

(function($) 
{
	$(document).ready(function(){
		/*
		$('a').click(function(event){
			alert("As you can see, this link is temporary disabled.");
			event.preventDefault();
		});
		*/
		tx_a21comments_comments_per_page = Number(tx_a21comments_comments_per_page);
		if (isNaN(tx_a21comments_comments_per_page) || tx_a21comments_comments_per_page<=0) {
			tx_a21comments_comments_per_page = 3;
		}

		tx_a21comments_iso3166countryCodesList = function(input) {
			$input = $(input);
			$.getScript('typo3conf/ext/hziir_comments/res/iso3166-country-codes-list_en_utf8.js', function(){
				// alert(iso3166countryCodesList.length);
				$input.autocomplete(iso3166countryCodesList,{
					minChars: 0,
					max: iso3166countryCodesList.length,
					width:$('#tx_comments_pi1_homepage').width(),
					matchContains: true,
					mustMatch: true,
					matchCase: false,
					autoFill: false,
					formatItem: function(row, i, max) {
						return row[0] + " [" + row[1] + "]";
					},
					formatMatch: function(row, i, max) {
						return row[0] + " " + row[1];
					},
					formatResult: function(row) {
						return row[0];
					}
				});
				$input.focus();
				$input.click();
			});
		};

		$('#tx_comments_pi1_content:not(.processed)').TextAreaResizer();
		$('div.tx-comments-comment-form form fieldset textarea.tx-comments-comment-maxlength, div.tx-comments-comment-form form fieldset input.tx-comments-comment-maxlength').each(function(){
		// $('div.tx-comments-comment-form form fieldset textarea.tx-comments-comment-maxlength').each(function(){
			var node = $(this);
			var nodeName = this.nodeName.toUpperCase();
			var maxLength = nodeName=='TEXTAREA'?2000:(nodeName=='INPUT'&&Number(node.attr('maxlength'))>0?Number(node.attr('maxlength')):255);
			node.maxlength({
				maxCharacters:		maxLength, // 10, // Characters limit
				status:				true, // True to show status indicator bewlow the element
				statusClass:		"tx-comments-comment-maxlength-status", // The class on the status div
				statusText:			"character(s) left", // The status text
				notificationClass:	"tx-comments-comment-maxlength-exceeded",	// Will be added to the emement when maxlength is reached
				showAlert: 			false, // True to show a regular alert message
				alertText:			"You have typed too many characters.", // Text in the alert message
				slider:				(nodeName!='TEXTAREA') // Use counter slider
			});
		});
		$('#tx_comments_pi1_location').focus(function(){
			if (typeof tx_a21comments_iso3166countryCodesList=='function') {
				tx_a21comments_iso3166countryCodesList(this);
				tx_a21comments_iso3166countryCodesList = null;
			}
		});
		$('#tx_comments_pi1_location').attr("autocomplete", "off");
		$('#tx_comments_pi1_location').prev().text($('#tx_comments_pi1_location').prev().text() + ' (double-click field to select)');
		
		if ($('div.tx-comments-comments').children('div.tx-comments-comment-depth0').size() > 0) {

			var commentFormHeadline = $('div.tx-comments-comment-form h4').eq(0);
			commentFormHeadline.toggle(
				function() { 
					$(this).parent().children('form').eq(0).show();
					$(this).parent().children('span.tx-comments-comment-toggle').addClass('tx-comments-comment-toggle-on');
				},
				function() {
					$(this).parent().children('form').eq(0).hide();
					$(this).parent().children('span.tx-comments-comment-toggle').removeClass('tx-comments-comment-toggle-on');
				}
			);
			commentFormHeadline.parent().children('form').eq(0).hide();
			commentFormHeadline.css('cursor','pointer');
			$('div.tx-comments-comment-form span.tx-comments-comment-toggle').css('cursor','pointer');
			$('div.tx-comments-comment-form span.tx-comments-comment-toggle').click(function(e){
				e.preventDefault();
				commentFormHeadline.trigger('click');
			});

			$('div.tx-comments-comment-header').each(function(){
				$(this).click(function () {
						tx_a21comments_active_comment.parent().children('div.tx-comments-comment-header').css('cursor','pointer');
						tx_a21comments_active_comment.parent().children('div.tx-comments-comment-header').children('span.tx-comments-comment-toggle').removeClass('tx-comments-comment-toggle-on');
						tx_a21comments_active_comment.hide();
						tx_a21comments_active_comment = $(this).parent().children('div.tx-comments-comment-content').eq(0);
						tx_a21comments_active_comment.show();
						tx_a21comments_active_comment.parent().children('div.tx-comments-comment-header').css('cursor','default');
						tx_a21comments_active_comment.parent().children('div.tx-comments-comment-header').children('span.tx-comments-comment-toggle').addClass('tx-comments-comment-toggle-on');
				});
				if (tx_a21comments_active_comment == null) {
					tx_a21comments_active_comment = $(this).parent().children('div.tx-comments-comment-content').eq(0);
					tx_a21comments_active_comment.parent().children('div.tx-comments-comment-header').children('span.tx-comments-comment-toggle').addClass('tx-comments-comment-toggle-on');
				} else {
					$(this).parent().children('div.tx-comments-comment-content').eq(0).hide();
					$(this).css('cursor','pointer');
				}
			});

			var browse = $("div.tx-pagebrowse-pi1").eq(0);

			if (browse.children('ul.tx-pagebrowse').size()>0) {

				var pageRegexp = /(tx_comments_pi1\[page\]=|tx_comments_pi1%5[bB]page%5[dD]=|\/comments\/)([0-9][0-9]*)(\/|&|$)/
				var last_page = 0;
				var last_page_href = browse.children('ul.tx-pagebrowse').eq(0).children('li.tx-pagebrowse-last').eq(0).children('a').eq(0).attr('href');
				if (last_page_href == null) {
					last_page = browse.children('ul.tx-pagebrowse').eq(0).children('li.tx-pagebrowse-pages').eq(0).children('ol').eq(0).children('li.tx-pagebrowse-current').eq(0).text(); 
					// alert('Last page by current page: ' + last_page+' '+tx_a21comments_comments_per_page);
				} else if (last_page_href.match(pageRegexp)) {
					last_page = 1 + Number(pageRegexp.exec(last_page_href)[2]);
					// alert('Last page by last page: ' + last_page + ' -> ' + last_page_href+' '+tx_a21comments_comments_per_page);
				} else {
					// alert('No Last page at all: ' + last_page+' '+tx_a21comments_comments_per_page);
				}
				if (!isNaN(last_page) && last_page>0) {
					var browseInitiated = false;
					var browseSize = last_page * tx_a21comments_comments_per_page;
					var browseOpts = {
						callback:function(idx,browsePanel){
							browseOpts.current_page = idx;
							if (browseInitiated) {
								// alert(idx);
							} else {
								browseInitiated = true;
								return false;
							}
						},
						current_page: Number(document.location.href.match(pageRegexp) ? pageRegexp.exec(document.location.href)[2] : 0),
						items_per_page: tx_a21comments_comments_per_page,
						link_to: document.location.href.replace(pageRegexp,'').replace(/\/$/,'') + '/comments/__id__/',
						num_display_entries:5,
						num_edge_entries:1,
						next_show_always:true,
						prev_show_always:true,
						next_text:'Next »',
						prev_text:'« Previous',
						ellipse_text:'…'
					};
					browse.empty();
					browse.removeClass("tx-pagebrowse-pi1");
					browse.addClass("pagination");
					browse.show();
					browse.pagination(browseSize,browseOpts);
				}
			}
		}
	});
})(jQuery);
