/* jTruncate */
/* http://www.jeremymartin.name/projects.php?project=jTruncate */
(function($){$.fn.jTruncate=function(h){var i={length:300,minTrail:20,moreText:"more",lessText:"less",ellipsisText:"...",moreAni:"",lessAni:""};var h=$.extend(i,h);return this.each(function(){obj=$(this);var a=obj.html();if(a.length>h.length+h.minTrail){var b=a.indexOf(' ',h.length);if(b!=-1){var b=a.indexOf(' ',h.length);var c=a.substring(0,b);var d=a.substring(b,a.length-1);obj.html(c+'<span class="truncate_ellipsis">'+h.ellipsisText+'</span>'+'<span class="truncate_more">'+d+'</span>');obj.find('.truncate_more').css("display","none");obj.append('<div class="clearboth">'+'<a href="#" class="truncate_more_link">'+h.moreText+'</a>'+'</div>');var e=$('.truncate_more_link',obj);var f=$('.truncate_more',obj);var g=$('.truncate_ellipsis',obj);e.click(function(){if(e.text()==h.moreText){f.show(h.moreAni);e.text(h.lessText);g.css("display","none")}else{f.hide(h.lessAni);e.text(h.moreText);g.css("display","inline")}return false})}}})}})(jQuery);



// http://web.enavu.com/tutorials/making-a-jquery-pagination-system/
// PAGINATION: show elements on pages - by hannesscheffel.de
function check_link_hiding(page_num){
	//getting the amount of elements inside content div
	var number_of_items = jQuery('.paginationelements').children().size();
	//calculate the number of pages we are going to have
	var number_of_pages = Math.ceil(number_of_items / jQuery('#show_per_page').val());

	// hide pagination, if only 1 site
    if (number_of_pages == 1)
		jQuery('.paginationcontainer').css('visibility', 'hidden');
	else
		jQuery('.paginationcontainer').css('visibility', 'visible');

	// hide previous_link if first page is shown
	if (page_num == 0)
		jQuery('#page_navigation .previous_link').css('visibility', 'hidden');
	else
		jQuery('#page_navigation .previous_link').css('visibility', 'visible');

	// hide next_link if last page is shown
	if (page_num == number_of_pages - 1)
		jQuery('#page_navigation .next_link').css('visibility', 'hidden');
	else
		jQuery('#page_navigation .next_link').css('visibility', 'visible');
}

function startpagination(){
	//how much items per page to show
	var show_per_page = 6;
	//getting the amount of elements inside content div
	var number_of_items = jQuery('.paginationelements').children().size();
	//calculate the number of pages we are going to have
	var number_of_pages = Math.ceil(number_of_items / show_per_page);

	//set the value of our hidden input fields
	jQuery('#current_page').val(0);
	jQuery('#show_per_page').val(show_per_page);

	//add items vor page navigation: link to previous page + links to specific pages + link to next page
	var navigation_html = '<div class="nav-previous"><a rel="prev" class="previous_link" href="javascript:previous();">nächste Artikel</a></div>';
	var current_link = 0;

	while(number_of_pages > current_link){
		navigation_html += '<a style="display: none;" class="page_link" href="javascript:go_to_page(' + current_link +')" longdesc="' + current_link +'">'+ (current_link + 1) +'</a>';
		current_link++;
	}

	navigation_html += '<div class="nav-next"><a rel="next" class="next_link" href="javascript:next();">vorherige Artikel</a></div>';

	jQuery('.paginationcontainer #page_navigation').html(navigation_html);

	//add active_page class to the first page link
	jQuery('.paginationcontainer #page_navigation .page_link:first').addClass('active_page inactive');

	//hide all the elements inside content div
	jQuery('.paginationelements').children().hide();

	//and show the first n (show_per_page) elements
	jQuery('.paginationelements').children().slice(0, show_per_page).show();

	check_link_hiding(0);
}

// PAGINATION: previous page
function previous(){
	new_page = parseInt(jQuery('#current_page').val()) - 1;

	//if there is an item before the current active link run the function
	if(jQuery('.active_page').prev('.page_link').length==true){
		go_to_page(new_page);
	}
}

// PAGINATION: next page
function next(){
	new_page = parseInt(jQuery('#current_page').val()) + 1;

	//if there is an item after the current active link run the function
	if(jQuery('.active_page').next('.page_link').length==true){
		go_to_page(new_page);
	}
}

// PAGINATION: go to page
function go_to_page(page_num){
	//get the number of items shown per page
	var show_per_page = parseInt(jQuery('#show_per_page').val());

	//get the element number where to start the slice from
	start_from = page_num * show_per_page;

	//get the element number where to end the slice
	end_on = start_from + show_per_page;

	//hide all children elements of content div, get specific items and show them
	jQuery('.paginationelements').children().css('display', 'none').slice(start_from, end_on).css('display', 'block');

	//get page link that has longdesc attribute of the current page and add active_page class to it and remove that class from previously active page link
	jQuery('.page_link[longdesc=' + page_num +']').addClass('active_page inactive').siblings('.active_page').removeClass('active_page inactive');

	//update the current page input field
	jQuery('#current_page').val(page_num);

	check_link_hiding(page_num);
}
// PAGINATION END



// You can safely use $ in this code block to reference jQuery
jQuery(document).ready(function($){

	// COMMENT: toggle comment form
	$("#commentform").hide();
	$("#comments h3#reply-title").click(function()
	{
		$("#commentform").toggle( "blind", 500 );
		return false;
	});
	
	/* dont show submenu on hover
	// MENU: show menu list on hover
	$('#access .menu > ul > li').hover(
	    // on hover
		function() {
			//$('#access .menu ul ul').hide();
			//$(this).find('ul').show();
		},
		// not hover
		function() {
			$('#access .menu ul ul').hide();
			// if no menus hovered, show current active menu list
	      	if ( $('#access .menu ul ul:visible').length === 0 ) {
				$('#access .menu ul li.current_page_parent ul').show();
				$('#access .menu ul li.current_page_item ul').show();
			}
		}
	);
	*/

	// GALLERY: delete headline and move text to gallery
	$('.format-gallery .photospace .thumbs_wrap').append('<div class="thumnail_text"></div>');
	$('.format-gallery .photospace .thumnail_text').append( $('div.format-gallery p') );

	// MENU: change order and name
	$('#access .menu a[title="Blog"]').parent().insertBefore('#access .menu > ul > li:first-child');
	$('#access .menu a[title="Home"]').html('Aktuell');
	$('#access .menu a[title="Home"]').attr('title', 'Aktuell');
    $('#access .menu > ul').css("width", $('#access .menu > ul').width() + 10);
    $('#access ul ul').css("left", $('#access .menu > ul').width());

	$('#access .menu a[title="Impressum und Datenschutz"]').html('Impressum<br />Datenschutz');

	// get window address
	url = window.location + '';
	// HOME/AKTUELL
    if ( $('body#aktuell').length > 0 || $('body').is('.home') || url.indexOf('/aktuell') >= 0 )
	{
		// hightlight current main menu
        $('#access .menu ul li a[title="Aktuell"]').parent().addClass('current_page_item');
		// create new div mainarticles
		$('#content .entry-content').append('<div class="mainarticles"></div>');
		$('#content .entry-content .mainarticles').insertBefore('#content .associated-posts');
		$('#content .entry-content .associated-posts').addClass('subarticles');
		// move first three articles in new div
		$('#content .entry-content .mainarticles').append( $('#content .entry-content .associated-post:nth-child(2)') );
		$('#content .entry-content .mainarticles').append( $('#content .entry-content .associated-post:first-child') );
		$('#content .entry-content .mainarticles').append( $('#content .entry-content .associated-post:first-child') );
		$('#content .entry-content .mainarticles .associated-post:first-child').addClass('first');
		$('#content .entry-content .mainarticles .associated-post:nth-child(2)').addClass('second');
		$('#content .entry-content .mainarticles .associated-post:nth-child(3)').addClass('last');
		//$('#content .entry-content h3.post-title').detach();
		// truncate text in all articles, after date (23/10/2011) = line break after date
		$('#content .entry-content .subarticles .post-excerpt p').each(function()
		{
			$(this).html($(this).html().slice(0, 36) + ' ... ');
		});
		// first: trunc date
		$('#content .entry-content .post-excerpt p').jTruncate({
			length: 10,
			minTrail: 0,
			moreText: "",
			ellipsisText: ""
		});
		/*
		// second: trunc further text in next line
		//alert($('#content .entry-content .subarticles .post-excerpt p .truncate_more').html());
		//$('#content .entry-content .subarticles .post-excerpt .truncate_more').removeClass("truncate_more").addClass("show");
		$('#content .entry-content .subarticles .post-excerpt .truncate_more').jTruncate({
			length: 15,
			minTrail: 0,
			moreText: ""
		});
		//*/

		// if text is clicked, go to url from image-link
		$('#content .entry-content .subarticles .associated-post').click(function(){
			//alert($(this).find('.thumb-frame a').attr('href'));
			window.location = $(this).find('.thumb-frame a').attr('href');
		});

	    $('.entry-content > .associated-posts').addClass("paginationelements");
	    // pagiante elements
	    if( $('.paginationelements .associated-post').length > 6) {
		    // Pagination Navi hinzufügen
			$('#content > div.type-page').before("<div id='nav-above' class='paginationcontainer navigation'><input type='hidden' id='current_page' /><input type='hidden' id='show_per_page' /><div id='page_navigation'></div></div>");
			startpagination();
		}

	}
	// BLOG
	else if( $('body#blog').length > 0 || url.indexOf('/blog') >= 0 )
	{
		// hightlight current main menu
	    $('#access .menu ul li a[title="Blog"]').parent().addClass('current_page_item');
	}
	// LEISTUNGEN
	else if( $('body#leistungen').length > 0 || url.indexOf('/leistungen') >= 0 )
	{
	    $('.associated-posts').addClass("paginationelements");
	    // pagiante elements
	    if( $('.paginationelements .associated-post').length > 6) {
		    // Pagination Navi hinzufügen
			$('#content > div.type-page').before("<div id='nav-above' class='paginationcontainer navigation'><input type='hidden' id='current_page' /><input type='hidden' id='show_per_page' /><div id='page_navigation'></div></div>");
			startpagination();
		}
		
		// hightlight current main menu
		$('#access .menu ul li.current_page_item ul').show();
		$('#access .menu ul li a[title="Leistungen"]').parent().addClass('current_page_parent');
		$('#access .menu ul li.current_page_parent ul').show();

		//move text under images
		$('#content .entry-content p').insertAfter('#content .entry-content .associated-posts');

		// highlight submenu if article is shown
		if(url.indexOf('corporate-design') >= 0)
			$('li a[title="Corporate Design"]').parent().addClass('current_page_item');
		else if(url.indexOf('corporate-identity') >= 0)
			$('li a[title="Corporate Identity"]').parent().addClass('current_page_item');
		else if(url.indexOf('corporate-communication') >= 0)
			$('li a[title="Corporate Communication"]').parent().addClass('current_page_item');
		else if(url.indexOf('interactive-design') >= 0)
			$('li a[title="Interactive Design"]').parent().addClass('current_page_item');
	}
	// AGENTUR
	else if( $('body#agentur').length > 0  || url.indexOf('/agentur') >= 0 )
    {
		// hightlight current main menu
		$('#access .menu ul li.current_page_item ul').show();
		$('#access .menu ul li a[title="Agentur"]').parent().addClass('current_page_parent');
		$('#access .menu ul li.current_page_parent ul').show();
	}
	// KONTAKT
	else if( $('body#kontakt').length > 0 || url.indexOf('/kontakt') >= 0 )
    {
		// hightlight current main menu
		$('#access .menu ul li.current_page_item ul').show();
		$('#access .menu ul li a[title="Kontakt"]').parent().addClass('current_page_parent');
		$('#access .menu ul li.current_page_parent ul').show();
	}

	// ARTICLE NAVIGATION: move link-text to title attribute
	$('div.navigation .nav-previous a').attr('title', $('div.navigation .nav-previous a').text());
	$('div.navigation .nav-previous a').text('');
	$('div.navigation .nav-next a').attr('title', $('div.navigation .nav-next a').text());
	$('div.navigation .nav-next a').text('');
	if( ! $('body#leistungen').length > 0 && ! url.indexOf('/leistungen') >= 0 &&
	    ! $('body#aktuell.page').length > 0 && ! url.indexOf('/aktuell') >= 0 ) {
		$('div.navigation .nav-previous a').attr('title', "vorheriger Artikel: " + $('div.navigation .nav-previous a').attr('title') );
		$('div.navigation .nav-next a').attr('title', "nächster Artikel: " + $('div.navigation .nav-next a').attr('title') );
	}
	
	// AKTUELL: hover text when hover main article image, and still show first text + hover first image
	$("#content .mainarticles .associated-post.first .post-excerpt").show();
	$("#content .mainarticles .associated-post.first").addClass("hovered");
	// hover mainarticles
	$("#content .mainarticles .associated-post .thumb-frame img").mouseenter(function(){
		$("#content .mainarticles .associated-post .post-excerpt").hide();
		$("#content .mainarticles .associated-post").removeClass("hovered");
		$(this).parent().parent().parent().find(".post-excerpt").fadeIn("slow");//show();
		$(this).parent().parent().parent().addClass("hovered");
	}).mouseleave(function(){
		//$(this).parent().parent().parent().find(".post-excerpt").hide();
		//$("#content .mainarticles .associated-post.first .post-excerpt").show();
	});
	// hover subarcticles
	$("#aktuell #content .subarticles .associated-post").mouseenter(function(){
		$(this).addClass("hovered");
	}).mouseleave(function(){
		$(this).removeClass("hovered");
	});

	// IE hacks for :last-child
	$("#blog #content .associated-post:last-child").addClass('last');
	$("#blog #content .associated-post p:last-child a:last-child").addClass('last');
	$("#content .entry-summary p:last-child").addClass('last');
	$("#content .entry-summary p:last-child a:last-child").addClass('last');
	$("#comments .comment-body p:last-child").addClass('last');
	$("#comments .comment-body blockquote p:last-child").addClass('last');
	$(".commentlist li:last-child").addClass('last');
	$(".archive #content p:last-child a:last-child").addClass('last');

	// http://code.google.com/intl/de-DE/apis/maps/documentation/javascript/services.html#GeocodingRequests
    // GOOGLE MAPS:
	if ($("#map_canvas").length > 0)
	{
	    $('#mapinfo').parent().css('overflow', 'hidden !important');
	    $('#mapinfo').parent().parent().css('overflow', 'hidden !important');
	    
		var latlng = new google.maps.LatLng(0, 0);
		var myOptions = {
			zoom: 11,
			center: latlng,
			scrollwheel: false,
		/*	scaleControl: false,
			disableDefaultUI: false,*/
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var gmap = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	    var geocoder_gmap = new google.maps.Geocoder();
		var address = 'Heidelbergstraße 13, D-07554 Korbußen';
	    var infowindow = new google.maps.InfoWindow();

		geocoder_gmap.geocode( { 'address': address}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				gmap.setCenter(results[0].geometry.location);

				var marker = new google.maps.Marker({
					map: gmap,
					position: gmap.getCenter()
				});

				var html  = '<div id="mapinfo">UEBEL KREATIVAGENTUR<br />Heidelbergstraße 13<br />07554 Korbußen</div>';
					html += '<div id="mapsform"><form method="get" target="_blank" action="http://maps.google.com/maps">';
					html += '	Ihre Adresse: z. B. Straße, PLZ Ort<br />';
					html += '	<input class="mapsformtext" type="text" name="saddr">';
					html += '	<input type="hidden" name="daddr" value="Heidelbergstraße 13, D-07554 Korbußen"><br />';
					html += '	<input class="mapsformsubmit" type="submit" value="Route berechnen">';
					html += '</form></div>';

				infowindow.setContent(html);
				infowindow.setPosition(results[0].geometry.location);
				infowindow.open(gmap);
				
				//add event to marker
				google.maps.event.addListener(marker, 'click', function() {
					infowindow.open(gmap);
				});
					
			} else {
			alert("Geocode was not successful for the following reason: " + status);
			}
		});
	}
	
});



// crypt email link with unicode chars
function cryptmail(name, domain, tld) {
	var url = name + "&#64;" + domain + "&#46;" + tld;
    var link = "<a href='&#109;&#97;&#105;&#108;&#116;&#111;&#58;" + url + "'' title='" + url + "'>" + url + "</a>";
    document.write(link);
}

