/*
 * html script v1.0.0
 * $URL$
 *
 * Copyright (c) 2010 Mirko Brauns
 *
 * $Date$
 * $Revision$
 */ 

$(document).ready(function() {
	/* initialize the spybar on the paddingTop page side */
    $('ul.spy').simpleSpy({interval: 12000, height: 90 });


	/* the slide up/down effect of the vertical menu by opening the page */
	$('ul.vlist ul').hide();
	$('ul.vlist li.active > ul').slideDown('slow');
	$('ul.vlist li ul > li.active').parents('ul').slideDown('slow');	

	
	/* the slide down effect of the vertical menu by hover (enter) a menu */
	$('ul.vlist li').children('ul').parent('li').mouseenter(function() {
		var doIt = true;
		var $this = $(this);
		
		$this.mouseleave(function() {
			doIt = false;
			$this.children('a:first').stop(true, true).css({paddingTop: '3px', paddingBottom: '3px'});
		});
		
		if ($this.children('ul:visible').length == 0) {
			setTimeout(function() {
				if (doIt) {
					$this.children('a').first() //.css('position', 'relative')
						.animate({paddingTop: '0', paddingBottom: '6'}, 200)
						.animate({paddingTop: '6', paddingBottom: '0'}, 200)
						.animate({paddingTop: '0', paddingBottom: '6'}, 100)
						.animate({paddingTop: '6', paddingBottom: '0'}, 100, function() {
							if (doIt) {
								$this.find('>ul').stop().height('auto').slideDown('slow'); 		
							}
						}).animate({paddingTop: '3', paddingBottom: '3'}, 200);
				}
			}, 250);
		}
	});
	
	/* the slide down effect of the vertical menu by hover (leave) a menu */
	$('ul.vlist').mouseleave(function() {
		var $lis = $('ul.vlist li').children('ul:visible').parent('li');
		$lis.children('a:first').stop(true, true).css({paddingTop: '3px', paddingBottom: '3px'});

		$lis.each(function() {
			if (!$(this).hasClass('active') && $(this).find('li.active').length == 0) {
				$(this).find('>ul').stop().slideUp('slow');
			}	
		});
	});	
	
	/* the scroll to top link */
	$(window).scroll(function () {
		if ($(this).scrollTop() != 0) {
		    $('#toTop').fadeIn('slow');
		} else {
		    $('#toTop').fadeOut('slow');
		}
	});
	
	/* don't show on ios, because the fixed positioning don't functions */
	if (navigator.userAgent.toLowerCase().match(/(iphone|ipod|ipad)/)) {
		$('#toTop').remove();
	} else {
	    $('#toTop').click(function () {
	        $('body,html').animate({
	            scrollTop: 0
	        },
	        800);
	    });
	}
    
    $('#search_field').focusin(function() {
    	if ($(this).val() == 'Suchtext eingeben…') {
    		$(this).val('');
    	}
    	$(this).css('color', '#000');
    }).focusout(function() {
    	if ($.trim($(this).val()) == '') {
    		$(this).val('Suchtext eingeben…');
	    	$(this).css('color', '#999');
    	}
    });
    
    $('#search').submit(function() {
	    if ($('#search_field').val() == 'Suchtext eingeben…') {
	    	$('#search_field').val('');
	    }
    });
    
    if ($("#pageflip").length == 1) {
    	$('#topnav').css('padding-right', '40px');
    }
    
    $("#pageflip").hover(function() { //On hover...
		$("#pageflip img , .msg_block").stop()
			.animate({ //Animate and expand the image and the msg_block (Width + height)
				width: '307px',
				height: '319px'
			}, 500);
		} , function() {
		$("#pageflip img").stop() //On hover out, go back to original size 50x52
			.animate({
				width: '50px',
				height: '52px'
			}, 220);
		$(".msg_block").stop() //On hover out, go back to original size 50x50
			.animate({
				width: '50px',
				height: '50px'
			}, 200); //Note this one retracts a bit faster (to prevent glitching in IE)
	});
	
	var width = $('#col1_content').width();
	var $breadcrumbs = $('#breadcrumbs li');
	$breadcrumbs.each(function() {
		width -= $(this).outerWidth(true);
	});

	if (width < 0) {
		var i = 0;
		$breadcrumbs.each(function() {
			var $a = $(this).children('a');
			var text = $a.text();
			$a.attr('title', text);
			
			if (i < $breadcrumbs.length - 1 && text.length > 7) {
				$a.text(text.substr(0, 3) + '…' + text.substr(-3, 3));
				
				$a.hover(function() {
					$(this).text($(this).attr('title'));
					$(this).parent('li').prevAll('li').each(function() {
						var $a = $(this).children('a');
						var text = $a.text();
						if (text.length > 7) {
							$a.text(text.substr(0, 3) + '…' + text.substr(-3, 3));
						}
					});		
					$(this).parent('li').nextAll('li').each(function() {
						var $a = $(this).children('a');
						var text = $a.text();
						if (text.length > 7) {
							$a.text(text.substr(0, 3) + '…' + text.substr(-3, 3));
						}
					});		
				}, function() {
					var $last = $(this).parent('li').parent('ul').children('li:last').children('a');
					$last.text($last.attr('title'));
					var text = $(this).text();
					$(this).text(text.substr(0, 3) + '…' + text.substr(-3, 3));
				});
			}
			i++;
		});		
	}
});

function breadcrumbs(path) {
	/* the hover effect of the breadcrump menu (only the background images will set here) */
	$('#breadcrumbs li').hover(
		function() {
			var prevCount = $(this).prevAll('li').length;
			if (prevCount == 1) {
				$(this).prev().css('background-image', 'url(' + path + 'images/breadcrumb_lastDone.gif)');
				$(this).prev().css('background-image', 'url(' + path + 'images/breadcrumb_lastDone.gif)');
			}
			var nextCount = $(this).nextAll('li').length;
			if (nextCount == 1) {
				$(this).css('background-image', 'url(' + path + 'images/breadcrumb_lastDoneHover.gif)');
			} else if (nextCount > 1) {
				$(this).css('background-image', 'url(' + path + 'images/breadcrumb_doneHover.gif)');
			}
		} , 
		function() {
			var prevCount = $(this).prevAll('li').length;
			var nextCount = $(this).nextAll('li').length;
			if (prevCount == 1 && nextCount > 0) {
				$(this).prev().css('background-image', 'url(' + path + 'images/breadcrumb_done.gif)');
			}
			if (nextCount == 1) {
				$(this).css('background-image', 'url(' + path + 'images/breadcrumb_lastDone.gif)');
			
			} else if (nextCount > 1) {
				$(this).css('background-image', 'url(' + path + 'images/breadcrumb_done.gif)');
			}
		}
	);
}
