jQuery(document).ready(function() {
	jQuery(document).pngFix();
	servicemenuBuild();
	submenuBuild();
	quicklinksBuild();
	overlayBuild();
	setLeftHeight();
	highlightWord();
});

/** 
 * Build the servicemenu.
 * 
 * @return void
 */
function servicemenuBuild() {
	if(!jQuery('#triggerService').length) return;
	jQuery('#triggerService').hover(function() {
		if(jQuery(this).find('#service').is(':hidden')) {
			jQuery(this).find('#service').slideDown(function() {
				jQuery(this).bgiframe();
			});
		}
	}, function() {
		jQuery(this).find('#service').slideUp();
	}).find('#service').css({'top':jQuery('#triggerService').height(), 'left':jQuery('#triggerService').position().left});
}

/**
 * Build the submenu.
 * 
 * @return void
 */
function submenuBuild() {
    jQuery('#subMenu ul.secondline li:first-child').addClass('first');
	jQuery('#subMenu li').each(function() {
		var colorClass = jQuery(this).attr('class').match(/cc.*/g);
		if(colorClass) {
			jQuery(this).removeClass(colorClass[0]);
			jQuery(this).parent('ul').addClass(colorClass[0]);
		}
	})
	jQuery('#subMenu').contents('ul').contents('li').each(function(i) {
		jQuery(this).hover(function(e) {
			jQuery('.subSubmenu').stop().css('height', 'auto').hide();
			jQuery(this).addClass('active').find('.subSubmenu').slideDown(function() {
				jQuery(this).bgiframe();
			});
		}, function() {
			jQuery('.subSubmenu').stop().css('height', 'auto').hide();
			jQuery(this).removeClass('active');
		});
	});	
}

/**
 * Build the quicklinks-flyout.
 * 
 * @return void
 */
function quicklinksBuild() {
	jQuery('a', '#quicklinksFixed').each(function(i) {
		jQuery(this).data('margin', jQuery(this).css('marginLeft'));
		if(parseInt(jQuery('body').attr('id').split('_')[1]) == 2616 || parseInt(jQuery('body').attr('id').split('_')[1]) == 6921) {
			jQuery(this).delay(i*300+500)
			.animate({marginLeft:0}, 500)
			.animate({marginLeft:parseInt(jQuery(this).data('margin'))/2}, 500)
			.animate({marginLeft:0}, 500);
		} else {
			jQuery(this).hover(function() {
                                jQuery(this).animate({marginLeft: (jQuery(this).height() == 32 ? 0 : 35)}, 200);
			}, function() {
				jQuery(this).animate({marginLeft:jQuery(this).data('margin')}, 300);
			});
		}
	});
}

/**
 * Build the transparent text-overlays for images.
 * 
 * @return void
 */
function overlayBuild() {
	jQuery('.blackOverlay').each(function() {
		var overlayText = jQuery(this).find('.blackOverlayText');
		var overlayImg = overlayText.prev('img');
		if(overlayImg.length) {
			overlayText.css({'width': overlayImg.width() - overlayText.innerWidth() - overlayText.width(), 'height': overlayImg.height() - overlayText.innerHeight() - overlayText.height()});
		}	
	});
}

/**
 * Corrects the height of the left col by checking the content height.
 *
 * @param boolean rec recursion-helper
 * 
 * @return void
 */
function setLeftHeight(rec) {
	jQuery('#left').css('height', jQuery('#outerLeft').height() > jQuery('#mainContent').height() && !rec ? 'auto' : Math.max(jQuery('#mainContent').height(), jQuery('#outerLeft').height()));
    !rec ? setTimeout(function() {setLeftHeight(true);}, 3000) : '';
}

/**
 * Get "sword" from get-params and highlight the "sword" in content (sword = search word).
 * 
 * @return void
 */
function highlightWord() {
	var sword = decodeURI(get('sword'));
	if(sword && sword != 'null') { 
		jQuery.highlight(jQuery('#content'), sword.toUpperCase());
		jQuery('.highlight:first').parents().each(function() {
			if(jQuery(this).prev('a').length) {
				jQuery('html,body').animate({scrollTop: jQuery(this).offset().top}, 2000, 'easeOutExpo');
				return false;
			}
		});
	}
}

/**
 * Builds a loading-overlay on top of given container ("container"),
 * use second ("extraImageCSS") and third ("extraImageHTML") parameter for additional overlay-html and css.
 *
 * @param mixed container destination-container
 * @param object extraImageCSS optional css
 * @param string extraImageHTML optional html
 * 
 * @return mixed
 */
function buildLoader(container, extraImageCSS, extraImageHTML) {
	if(!jQuery(container).length) return false;
	var overlay = jQuery('<div class="loadingOverlay"></div>')
	var image = jQuery('<div class="loadingImage"></div>')
	jQuery(container).append(overlay, image);
	overlay.css({width:jQuery(container).outerWidth(), height:jQuery(container).outerHeight()});
	image.css({left:overlay.width()/2-image.width()/2});
	extraImageCSS ? image.css(extraImageCSS) : '';
	extraImageHTML ? image.html(extraImageHTML) : '';
	var yOffset = window.pageYOffset || document.documentElement && document.documentElement.scrollTop || document.body.scrollTop;
	var viewportHeight = window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.getElementsByTagName('body')[0].clientHeight;
	var topPos = yOffset - overlay.offset().top;
	if(topPos > 0) { //top is over viewport
		if(overlay.height()-topPos > viewportHeight) { //overlay bottom is under viewport
			topPos = viewportHeight/2 - image.height()/2;
		} else if(topPos+image.height() > overlay.height()) { //image does not fit into overlay 
			topPos = overlay.height()-image.height();
		} else { //overlay bottom in viewport
			topPos += (overlay.height()-topPos)/2 - image.height()/2;
		}
	} else if(viewportHeight+topPos > 0) { //top is in viewport
		if(viewportHeight+topPos < overlay.height()) { //overlay bottom is under viewport
			topPos = (viewportHeight+topPos)/2 - image.height()/2;
		} else { //whole overlay in viewport
			topPos = overlay.height()/2 - image.height()/2;			
		}
	}
	image.css({top:topPos});
	overlay.bgiframe();
	return jQuery('.loadingOverlay, .loadingImage', container);
}

/** Interactive Stuff **/

/*******************/
/** Kristenticker **/
/*******************/
if(window.location.hostname.indexOf('interactive') !=-1){
jQuery(document).ready(function() {
    if(jQuery('#krisenticker')!=-1){
    jQuery(function(){
        jQuery("ul#crisisticker").liScroll({travelocity: 0.10});
    }); 
    }
});
}
