/**
 * 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.styleSwitcher();
	hr.commentNumbers();
	hr.dynamicComments.init();
});

var hr = {};

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 ...
	if ( $("*").index( $('a[name=preview]')[0]) != -1 ){
		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.styleSwitcher = function(){
	var stylesheet = '<link href="http://www.highresolution.info/layout/css/screen/smallscreen.css" rel="stylesheet" type="text/css" title="smallscreen"/>';
	var style = $.cookie('hrRecentStyle'); // get cookie

	$('#styleswitcher').remove();
	$('#header .page').append('<div id="styleswitcher"></div>');
	$('#styleswitcher').prepend('<a class="big" href="#top" title="Optimiertes Layout für große Bildschirme">&rarr; big screen</a>');
	$('#styleswitcher').prepend('<a class="small" href="#top" title="Optimiertes Layout für kleine Bildschirme">&rarr; small screen</a>');

	if (style !== 'undefined') {
		switch(style) {
			case 'small':
				$('#styleswitcher a.small').hide();
				$('#styleswitcher a.big').show();
				$('head').append(stylesheet);
				break;

			case 'big':
			default:
				$('#styleswitcher a.small').show();
				$('#styleswitcher a.big').hide();
				break;
		}
	} else {
		$('#styleswitcher a.small').show();
		$('#styleswitcher a.big').hide();
	}

        // small-screen stylesheet entfernen ...
	$('#styleswitcher a.big').click(function(){
		$('body').removeClass('small');

		if ( $('link[title="smallscreen"]').length > 0 ) {
			$('link[title="smallscreen"]')[0].disabled = true;
		} 
		$('link[title="smallscreen"]').remove();

		// Cookie setzen ...
		$.cookie('hrRecentStyle', 'big', {expire: 365, path: '/'});
		$('#styleswitcher a.small').show();
		$('#styleswitcher a.big').hide();
	});

	// small-screen stylesheet einbauen ...
	$('#styleswitcher a.small').click(function(){
		if ( $('link[title="smallscreen"]').length > 0 ) {
			$('link[title="smallscreen"]')[0].disabled = false;
		} else {
			$('head').append(stylesheet);
		}

		$('body').addClass('small');

		// Cookie setzen ...
		$.cookie('hrRecentStyle', 'small', {expire: 365, path: '/'});
		$('#styleswitcher a.small').hide();
		$('#styleswitcher a.big').show();
	});

};


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

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