/**
 * High Resolution Dynamic Effects
 *
 * @copyright   All rights reserved by Dirk Jesse
 * @author      Dirk Jesse
 * @version     0.7
 */

$(document).ready(function(){
	hr.setFontsXP();
	hr.commentPreviewEE();
	hr.commentNumbers();
	hr.dynamicComments.init();
	hr.colorBox();
});

var hr = {};

hr.colorBox = function() {
	
	$('div.itemtext a[rel]').colorbox()
	
};

hr.setFontsXP = function() {
	// Windows XP - disable usage of badly rendered vista fonts 
	if (navigator.userAgent.indexOf("Windows NT 5.1") > -1) {
	  $('body, textarea').css("font-family","Arial, 'Trebuchet MS', Verdana, Helvetica, sans-serif");	
	}
};

hr.commentPreviewEE = function() {
	// EE Comment-Preview anspringen ...
	var preview = $('a[name=preview]');
	if ( preview && preview.length > 0 ){
		window.location.href='#preview';
	}
};

hr.dynamicComments = {
	options: {
		commentList: '.sidebar ol.commentlist',
		navInfoClass: 'commentinfo',
		navClass: 'commentpagination',
		skip: 5
	},
	
	init: function() {
		var length = $(this.options.commentList).children('li').length;
	
		if (length <= this.options.skip) {
			$('.sidebar ol.commentlist').before('<p class="commentinfo"><b>Info: </b><span>Sie sehen alle bisher abgegebenen Kommentare.</span></p>');
			$('p.commentinfo').css({'border-bottom':'1px #000 solid','margin-bottom':'2px'});

			var url = window.location.href.split('#');
			if ( url.length > 1 ) {
				if ( url[1].indexOf('comment-') != -1 ) {
					var entry = $('a[name='+url[1]+']').parent('li');										

					var entry_bg = $(entry).css('background-color');
					$(entry).css('background-color','rgb(255,255,0)');
					$(entry).animate( { backgroundColor: entry_bg }, 3000 );
				} 
			} 

		} else {
			// Pagination erzeugen ...
			$(this.options.commentList).before('<ul class="commentpagination"></ul>');
			$(this.options.commentList).after('<ul class="commentpagination"></ul>');
			$('ul.commentpagination').before('<p class="commentinfo"><b>Info:</b> <span></span></p>');
			$('ul.commentpagination').append('<li><a class="pgall" href="#top">Alle ('+length+')</a></li>');
			$('ul.commentpagination').append('<li><a class="pgolder" href="#top">+5 davor ()</a></li>');
			$('ul.commentpagination').append('<li><a class="pglatest" href="#top">nur 5 Neueste ('+String(length-this.options.skip+1)+'-'+length+')</a></li>');

			// Suche nach Kommentar-Sprungankern ...
			var url = window.location.href.split('#');
			
			if ( url.length > 1 ) {
				if ( url[1].indexOf('comment-') != -1 ) {
					var length = $(this.options.commentList).children('li').length;
					var entry = $('a[name='+url[1]+']').parent('li');
					var epos = $(entry).prevAll().length;
					
					if ( length - epos < this.options.skip ) {
						hr.dynamicComments.showLatest('initial');				
						// Sprunganker refreshen ...
						window.location.href='#'+url[1];
					} else {
						hr.dynamicComments.showAll();				
					}
					
					var entry_bg = $(entry).css('background-color');
					$(entry).css('background-color','rgb(255,255,0)');
					$(entry).animate( { backgroundColor: entry_bg }, "slow" );
				} else {
					hr.dynamicComments.showLatest('initial');
				}
			} else { hr.dynamicComments.showLatest('initial'); }
		}

		// Button-Events einrichten ...
		$('ul.commentpagination a.pglatest').click(function(){ hr.dynamicComments.showLatest();});
		$('ul.commentpagination a.pgolder').click(function(){ hr.dynamicComments.showOlder();});
		$('ul.commentpagination a.pgall').click(function(){ hr.dynamicComments.showAll();});

	},
	setInfoText: function(sText) {
		$('p.commentinfo span').text(sText);	
	},
	
	showAll: function(anim) {
		var length = $(this.options.commentList).children('li').length;

		$(this.options.commentList).children('li').slideDown("slow");
		$('ul.commentpagination a.pgall').hide();
		$('ul.commentpagination a.pgolder').hide();
		$('ul.commentpagination a.pglatest').show();
		this.setInfoText('Sie sehen alle Kommentare (Nr. 1-'+length+').');	
	},

	showLatest: function(anim) {
		var length = $(this.options.commentList).children('li').length;
		var nextitems = $('.sidebar ol.commentlist li:visible').length;

		if ( length-this.options.skip < this.options.skip ) {
			var buttontext = '+5 davor (1-'+String(length-this.options.skip)+')';
			$('ul.commentpagination a.pgolder').text(buttontext);
		} else {
			var buttontext = '+5 davor ('+String(length-2*this.options.skip+1)+'-'+String(length-this.options.skip)+')';
			$('ul.commentpagination a.pgolder').text(buttontext);
		}

		var item = $('.sidebar ol.commentlist li:first-child');
		
		for (var i = 0; i < (length-this.options.skip); i++) {
			if (anim == 'initial') {
				$(item).hide();
			} else {
				$(item).slideUp("slow");
			}
			item = $(item).next();
		}

		$('ul.commentpagination a.pgall').show();
		$('ul.commentpagination a.pgolder').show();
		$('ul.commentpagination a.pglatest').hide();
		this.setInfoText('Sie sehen die 5 neusten Kommentare.');
	},

	showOlder: function(anim) {
		var length = $(this.options.commentList).children('li').length;
		var nextitems = $('.sidebar ol.commentlist li:visible').length;

		if (length-nextitems < this.options.skip) {
			$('ul.commentpagination a.pgolder').hide();
			$('ul.commentpagination a.pgall').hide();
		} else {
			$('ul.commentpagination a.pgall').show();
		}
		$('ul.commentpagination a.pglatest').show();			


		var item = $('.sidebar ol.commentlist li:visible:eq(0)');
		for (var i = 0; i < this.options.skip; i++) {
			item = $(item).prev();
			$(item).slideDown("normal");
		}

		var nextitems = $('.sidebar ol.commentlist li:visible').length;
		if ( length-nextitems < this.options.skip ) {
			var buttontext = '+5 davor (1-'+String(length-nextitems)+')';
			$('ul.commentpagination a.pgolder').text(buttontext);
		} else {
			var buttontext = '+5 davor ('+String(length-nextitems-this.options.skip+1)+'-'+String(length-nextitems)+')';
			$('ul.commentpagination a.pgolder').text(buttontext);
		}
		if (length-nextitems == 0) {
			this.setInfoText('Sie sehen alle Kommentare (Nr. '+String(length-nextitems+1)+'-'+length+').');			
		} else {
			this.setInfoText('Sie sehen die Kommentare Nr. '+String(length-nextitems+1)+'-'+length+'.');
		}
	}
		
}

hr.commentNumbers = function(){
	var count = 0;

	// Kommentare durchnummerieren ...
	$('.sidebar ol.commentlist .itemhead').each(function(){
		count++;
		$(this).prepend('<div class="number">' + count + '</div>');
	});
};
