$(document).ready(function () {

    // google analytics hilfe button
    $('a[title="Hilfe bei Status-Symptomen"]').click(function() {
        _gaq.push(['_trackEvent', 'clickout', 'DaciaDe', 'HomepageTeaser']);
    });

	// linkpfeile
	$("ul.blogroll li a").append(" " + URL_IMG_ARROW_RIGHT);
	$("div.entry-summary  a span.meta-nav").html(" " + URL_IMG_ARROW_RIGHT);

	// last klassen
	$(".widget-area li.widget-container:last").addClass("last");
	//$("ul.blogroll li:last").addClass("last");

	// externe Links
	$('a[rel="external nofollow"]').attr("target", "_blank");
	$('.menu-item-type-custom a').attr("target", "_blank"); // externe Links im Hauptmenü

	// fix für die Blogroll
	$("ul.blogroll li").each(function () {
		var $link = $(this).children("a").remove();
		var text = $(this).text();
		var pos = text.indexOf("|");
		if (pos >= 0) {
			var titel = '<div class="link-title">' + text.substring(0, pos) + '</div>';
			var desc = '<div class="link-desc">' + text.substring(pos+1) + '</div>';
			$(this).html(titel).append($link).append(desc);
		} else {
			$(this).prepend($link);
		}
	});

	// Fix für die Autorenanzeige
	$(".post .entry-content blockquote p").each(function () {
		$img = $(this).children("img");
		if ($img.length > 0) {
			$img.remove();
			$(this).wrapInner("<div></div>");
			$(this).prepend($img);
			$(this).addClass("clearfix");
		}
	});


	// Tagcloud fix für Internet Explorer
	if ($.browser.msie) {
		$('.widget_custom_tag_cloud div.tagcloud a').each(function () {
			var width = $(this).width();
			var height = $(this).height() * 1.2;
			var opacity = parseFloat($(this).attr('custom'));
			var rgbWert = Math.round((1-opacity)*255);
			$(this).css("color", "rgb("+rgbWert+","+rgbWert+","+rgbWert+")");
		});
	}
	Cufon.set('fontFamily', 'Agb').replace('.widget_custom_tag_cloud div.tagcloud a', {
		hover: true
	});


	// Formular Buttons
	$(function() {
		$('input[type=submit]').each(function(){
			$(this).after(unescape('%3Cspan class="button-submit"%3Eaa%3C/span%3E'));
			$(this).hide();
			$('span.button-submit').append(" " + URL_IMG_ARROW_RIGHT);
			$(this).next('span.button-submit').text($(this).val()).click(function(){
				$(this).prev('input[type=submit]').click();
			});
		});
		Cufon.replace('.button-submit');
	});

	// Reply Links
	$('.comment-reply-link').append(" " + URL_IMG_ARROW_ANSWER);

	/*
	 *
	 * Blogroll aufteilen in einzelne Seiten
	 *
	 */

	if ($("ul.blogroll li").length > BLOGROLL_ITEMS_PER_PAGE) {

		// Elemente einlesen
		var $links = $("ul.blogroll li").remove();
		var $container = $("ul.blogroll").parent();
		var $wrapperTemplate = $("ul.blogroll").remove()
										   .hide();
		// Links aufsplitten
		for (var i = 0; i<$links.length; i++) {
			if (i%BLOGROLL_ITEMS_PER_PAGE == 0) {
				var $wrapper = $wrapperTemplate.clone().appendTo($container);
			}
			$wrapper.append($links[i]);
		}


		// Blätterbuttons bauen
		var $nav = $('<div class="navigation"></div>');
		var $next = $('<a class="next" href="javascript: void(0)">' + LANGVARS.links_prev + '</a>');
		var $prev = $('<a class="prev" href="javascript: void(0)">' + LANGVARS.links_next + '</a>');
		var $first = $('<a class="next" href="javascript: void(0)">' + LANGVARS.links_prev_first + '</a>');

		$next.prepend(URL_IMG_ARROW_LEFT + " ");
		$first.prepend(URL_IMG_ARROW_LEFT + " ");
		$prev.append(" " + URL_IMG_ARROW_RIGHT);

		$next.click(next_click);
		$first.click(next_click);
		$prev.click(prev_click);

		$nav.append($next)
			.append($prev)
			.append($first)
			.appendTo($container);

		Cufon.set('fontFamily', 'Agb').replace('.widget_mylinkorder .navigation a', {hover: true});

		// init
		var $show = $("ul.blogroll:first");
		$show.show(); // ersten aktivieren
		update_blogroll_navigation();
	}

	function next_click () {
		var $current = $("ul.blogroll:visible").slideUp("fast");
		$show = $current.next("ul.blogroll").slideDown("fast");
		update_blogroll_navigation();
	}

	function prev_click () {
		var $current = $("ul.blogroll:visible").slideUp("fast");
		$show = $current.prev("ul.blogroll").slideDown("fast");
		update_blogroll_navigation();
	}

	function update_blogroll_navigation () {
		if ($show.next("ul.blogroll").length == 0) {
			$next.hide();
		} else {
			$next.show();
		}
		if ($show.prev("ul.blogroll").length == 0) {
			$prev.hide();
			$next.hide();
			$first.show();
		} else {
			$first.hide();
			$prev.show();
		}
	}

	/*
	 *
	 * Blogroll fertig
	 *
	 */
});

function rgbToHex (r, g, b) {
	return intToHex(r).toString()+intToHex(g).toString()+intToHex(b).toString();
}

function intToHex(i) {
	var hex = parseInt(i).toString(16);
	return (hex.length < 2) ? "0" + hex : hex;
}
