/* 3spin | cm | 2010 */
jQuery.noConflict();
jQuery(document).ready(function() {
/*	Execute this when the document is fully loaded.
 *	@params: none
 *	@return: void
 */
	/* Initialize global functions: */
	globalFunctions.init();
		
});

/* Main Code: */
var globalFunctions = {
	
	/* ==================================================================================================================== */
	
	_browserAgent : navigator.userAgent.toLowerCase(),	
	_isIE6 : false,
	
	init : function()
	{
		/* Check browser agent: */
		if (globalFunctions._browserAgent.indexOf('msie 6') >= 0) {globalFunctions._isIE6 = true;}
		
		var pageID = jQuery('#contentMain').children().first().attr('id');		
		switch (pageID) {
			
			case 'intro':
				/* Initialize Logo Animation: */
				globalFunctions._initLogoAnimation();
				break;				
					
			default:
				//
		}
		
		/* Add custom scrollbars: */
		var scrollableObjs = jQuery('.scroll');
		if (scrollableObjs.length > 0) {
      scrollableObjs.jScrollPane({verticalDragMinHeight: 197, verticalDragMaxHeight: 197});
		}
		
		/* Initialize Strategies Tables: */
		if (jQuery('.strategien_interaktiv').length > 0) {
			globalFunctions._initStrategiesTables();
		}
		
		/* Initialize Management Interactive Images: */
		if (jQuery('.management').length > 0) {
			globalFunctions._initManagementImages();
		}
		
		/* Initialize Image Galleries: */
		if (jQuery('.imagegallery').length > 0) {
			globalFunctions._initImageGalleries();
		}
		
		/* Initialize all forms: */
		if (jQuery('form').length > 0) {
			globalFunctions._initForms();
		}
		
		/* Fix all broken JS links (href="javascript://"): */
		jQuery('#contentMain').find('a').each(function() {
			var thisHref = jQuery(this).attr('href');
			if (typeof thisHref != 'undefined') {
				if (thisHref.indexOf('javascript://') > 0) {
					jQuery(this).attr('href','javascript://');
				}
			}
		});
		
		/* Remove empty headlines: */
		var removeEmptyHeadlines = function() {
		if (jQuery(this).html() == '') {
			jQuery(this).remove();
		}
	  
    };
		jQuery('.contentMain').find('h1,h2,h3').each(removeEmptyHeadlines);
	},
	
	/* ==================================================================================================================== */
	
	_initLogoAnimation : function() {
		/* Add click handler to language links: */
		var selectLanguageFct = function () {
			var thisLanguageID = jQuery(this).attr('id').replace('btn_', '');
			if (globalFunctions._isIE6) {
				/* No animation variant: */
				jQuery('#intro .sprachwahl').hide();
			}else{
				/* Fade animation variant: */
				jQuery('#intro .sprachwahl').fadeOut('fast');
			}
			jQuery('#animatedlogo').animate({marginLeft: '-143px'}, 1000, 'linear', function() {
				/* Load the start page on animation complete: */
				switch (thisLanguageID) {
					case 'DE':
						//alert('DE');
						window.location = jQuery('#btn_DE').data('thisHref');
						break;
					case 'EN':
						//alert('EN');
						window.location = jQuery('#btn_EN').data('thisHref');
						break;
					default:
						//alert('DE');
						window.location = jQuery('#btn_DE').data('thisHref');
				}
			});
		};
		jQuery('#btn_DE, #btn_EN').each(function() {jQuery(this).data('thisHref', jQuery(this).attr('href'));}).attr('href', 'javascript://').click(selectLanguageFct);
	},
	
	/* ==================================================================================================================== */
	
	_initStrategiesTables : function() {
		var interactiveObjs = jQuery('#contentMain').find('.strategien_interaktiv');
		/* Initialize all interactive tables: */
		interactiveObjs.each(function() {
			var textBlockHTML = '';
			var thisObj = jQuery(this);
			/* Move all text blocks to "strategien_text" block and add jScrollPane: */
			thisObj.find('.strategien_body .strategien_text_block').each(function() {
				textBlockHTML += '<div class="strategien_text_block scroll displayNone">' + jQuery(this).html() + '</div>';				
			}).remove();
			thisObj.find('.strategien_text').append(textBlockHTML).find('.scroll').removeClass('displayNone').show().jScrollPane().hide().addClass('displayNone');
			
			/* Add click handler to table objects: */
			var counter = 0;
			thisObj.find('a').each(function() {
				var thisNr = counter;
				jQuery(this).click(function() {
					var thisElem = jQuery(this);
					var rootElem = thisElem.parent().parent().parent().parent().parent();
					var textBlocks = rootElem.find('.strategien_text').find('.strategien_text_block');
					rootElem.find('.strategien_grafik .colRight .active').removeClass('active');
					thisElem.addClass('active');					
					/* Show textblock: */
					if (textBlocks.eq(thisNr + 1).css('display') == 'none') {
					  /* Reinitialise jScrollpane: */
					  //textBlocks.eq(thisNr + 1).css('visibility', 'hidden').show();
					  //textBlocks.eq(thisNr + 1).data('jsp').reinitialise();
					  //textBlocks.eq(thisNr + 1).hide().css('visibility', 'visible');
						if (globalFunctions._isIE6) {
							/* No animation variant: */
							textBlocks.not('.displayNone').addClass('displayNone');
							textBlocks.eq(thisNr + 1).removeClass('displayNone');
						}else{
							/* Fade animation variant: */
							textBlocks.not('.displayNone').fadeOut(100, function() {
								jQuery(this).addClass('displayNone');
								textBlocks.eq(thisNr + 1).fadeIn(200, function() {
									jQuery(this).removeClass('displayNone');
								});
							});
						}		
					}
				});
				counter++;
			});
		});
	},
		
	/* ==================================================================================================================== */
	
	_initManagementImages : function() {
		var interactiveObjs = jQuery('#contentMain').find('.management');
		/* Initialize all interactive images: */
		interactiveObjs.each(function() {
			/* Move all textblocks to the management text block: */
			var textBlockHTML = '';
			var thisObj = jQuery(this);
			var thisImage = thisObj.find('.management_image img');
			var thisImageUrl = thisImage.attr('src');
			var thisImageHeight = parseInt(thisImage.attr('height'), 10);
			var thisPanels = thisObj.find('.management_panels .panel');
			var totalWidthOfPreviousPanels = 0;
			var thisWidth = 0;
			thisPanels.each(function() {
				var thisPanel = jQuery(this);
				textBlockHTML += '<div class="management_text displayNone">' + thisPanel.children('.management_text').html() + '</div>';
				/* Adjust background image: */
				var backgroundCSS = "transparent url('" + thisImageUrl + "') no-repeat scroll -" + totalWidthOfPreviousPanels + "px top";
				thisPanel.css('background',backgroundCSS).children('.management_text').remove();
				/* Adjust panel caption width: */
				thisWidth = parseInt(thisPanel.css('width').replace(/[^\d]/g, ''), 10);
				thisPanel.children().first().css('width',(thisWidth - 20) + 'px');
				if (globalFunctions._isIE6) {thisPanel.children().first().css('width',thisWidth + 'px');}
				totalWidthOfPreviousPanels += thisWidth;
				/* Adjust panel height: */
				if (thisImageHeight > 0) {
					thisPanel.css('height',thisImageHeight + 'px');
				}
			});
			thisObj.children('.management_textblock').html(textBlockHTML);							
			
			/* Add hover and click handlers to panels: */
			var counter = 0;
			thisPanels.hover(function() {
				jQuery(this).removeClass('hidden');//.parent().parent().parent().find('.management_image').addClass('transparent');
			},function() {
				if (!jQuery(this).hasClass('active')) { jQuery(this).addClass('hidden'); }
				//if (jQuery(this).parent().find('.active').length == 0) { jQuery(this).parent().parent().parent().find('.management_image').removeClass('transparent'); }				
			}).each(function() {				
				var thisNr = counter;
				/* Add click handler: */
				jQuery(this).click(function() {					
					var panels = jQuery(this).parent().find('.panel');
					var textBlock = jQuery(this).parent().parent().parent().find('.management_textblock');
					/* Set active class: */
					panels.removeClass('active').addClass('hidden');
					jQuery(this).addClass('active').removeClass('hidden');
					/* Fade image: */
					jQuery(this).parent().parent().parent().find('.management_image').addClass('transparent');
					/* Show textblock: */
					if (globalFunctions._isIE6) {
						/* No animation variant: */
						textBlock.children().addClass('displayNone').eq(thisNr).removeClass('displayNone');
					}else{
						/* Fade animation variant: */
						if (textBlock.children().not('.displayNone').length == 0) {
							textBlock.children().eq(thisNr).fadeIn(200, function() {
								jQuery(this).removeClass('displayNone');
							});
						}else{
							textBlock.children().not('.displayNone').fadeOut(100, function() {
								jQuery(this).addClass('displayNone');
								textBlock.children().eq(thisNr).fadeIn(200, function() {
									jQuery(this).removeClass('displayNone');
								});
							});
						}
					}
				});
				counter++;
			});
		});
	},
	
	/* ==================================================================================================================== */
	
	_imageGalleriesUseFading : true,	/* Set to true to use fading in all browser except IE6 */
	_imageGalleriesFadeSpeed : 200,		/* Set image fading speed in milliseconds */
		
	_initImageGalleries : function() {
		var imageGalleries = jQuery('#contentMain').find('.imagegallery');
		imageGalleries.each(function() {
			var thisGallery = jQuery(this);
			var thisPreviewImageDiv = thisGallery.find('.image_preview_full');
			var imageThumbs = thisGallery.find('a.image_thumb');
			var thumbCountTotal = imageThumbs.length;
			var thumbCounter = 0;
			var currentNrHtml = thisGallery.find('.image_preview_controls span');
			/* Save total thumb count and current number as data: */
			thisGallery.data('thumbCountTotal', thumbCountTotal).data('currentThumbNr', -1);
			/* Add functionality to thumbs (hover effect and click handler): */
			imageThumbs.each(function() {
				var thisThumb = jQuery(this);
				/* Save current number and image URL as data: */
				thisThumb.data('count', thumbCounter).data('href', thisThumb.attr('href')).attr('href', 'javascript://');
				thumbCounter++;
				/* Hover effect: */
				thisThumb.hover(function() {jQuery(this).addClass('hover');},function(){ if (!jQuery(this).hasClass('active')) { jQuery(this).removeClass('hover'); } });
				/* Click handler: */
				thisThumb.click(function() {
					/* Get number from thumb: */
					var thisNr = jQuery(this).data('count');
					/* Update current number and show image: */
					if (thisNr != thisGallery.data('currentThumbNr')) {
						globalFunctions._showImagePreviewFull(thisGallery, imageThumbs, thisNr, thisPreviewImageDiv);
					}
				});
			}).first().trigger('click');
			/* Add functionality to next/previous buttons: */
			thisGallery.find('.image_preview_previous').click(function() {
				var currentThumbNr = thisGallery.data('currentThumbNr');
				if (currentThumbNr > 0) {
					globalFunctions._showImagePreviewFull(thisGallery, imageThumbs, currentThumbNr - 1, thisPreviewImageDiv);
				}else if (currentThumbNr == 0 && thumbCountTotal > 1) {
					globalFunctions._showImagePreviewFull(thisGallery, imageThumbs, thumbCountTotal - 1, thisPreviewImageDiv);
				}
			});
			thisGallery.find('.image_preview_next').click(function() {
				var currentThumbNr = thisGallery.data('currentThumbNr');
				if (currentThumbNr < thumbCountTotal - 1) {
					globalFunctions._showImagePreviewFull(thisGallery, imageThumbs, currentThumbNr + 1, thisPreviewImageDiv);
				}else if (currentThumbNr == thumbCountTotal - 1 && thumbCountTotal > 1) {
					globalFunctions._showImagePreviewFull(thisGallery, imageThumbs, 0, thisPreviewImageDiv);
				}
			});
		});
	},
	
	_showImagePreviewFull : function(jQueryImageGallery, jQueryImageThumbs, thumbNr, jQueryPreviewImageDiv) {
		var thisObj = jQueryImageThumbs.eq(thumbNr);
		jQueryImageThumbs.removeClass('active').removeClass('hover');
		thisObj.addClass('active');
		var thisHref = thisObj.data('href');										
		/* Update current number and show image: */
		if (thumbNr != jQueryImageGallery.data('currentThumbNr')) {
			jQueryImageGallery.data('currentThumbNr', thumbNr);
			var newImageTagHtml = '<img src="' + thisHref + '" alt="" border="0" />';
			if (globalFunctions._isIE6 || globalFunctions._imageGalleriesUseFading == false) {
			/* No animation variant: */
				jQueryPreviewImageDiv.html(newImageTagHtml);
			}else{
				/* Fade animation variant: */
				jQueryPreviewImageDiv.fadeOut(globalFunctions._imageGalleriesFadeSpeed, function(){
					jQueryPreviewImageDiv.html(newImageTagHtml).fadeIn(globalFunctions._imageGalleriesFadeSpeed);
					/* Only fade in the image after it is loaded: */
					/*jQueryPreviewImageDiv.html(newImageTagHtml);
					var previewImage = jQueryPreviewImageDiv.find('img');
					if (previewImage.width() == 0 || previewImage.height() == 0) {
						previewImage.load(function() {
							jQueryPreviewImageDiv.fadeIn(globalFunctions._imageGalleriesFadeSpeed);
							//alert('loaded ' + jQuery(this).attr('src'));
						});
					}else{
						jQueryPreviewImageDiv.fadeIn(globalFunctions._imageGalleriesFadeSpeed);
						//alert('cached ' + previewImage.attr('src'));
					}*/
				});
			}
			/* Update counter display (with leading zero for numbers smaller than 10): */
			var displayHtml = '';
			if (thumbNr < 9) {displayHtml = '0' + (thumbNr + 1).toString();}else{displayHtml = (thumbNr + 1).toString();}
			if (jQueryImageGallery.data('thumbCountTotal') <= 9) {displayHtml += '/0' + jQueryImageGallery.data('thumbCountTotal');}else{displayHtml += '/' + jQueryImageGallery.data('thumbCountTotal');}
			jQueryImageGallery.find('.image_preview_controls span').html(displayHtml);
		}
	},
	
	/* ==================================================================================================================== */
	
	_initForms : function() {
		var forms = jQuery('#contentMain').find('form');
		forms.each(function() {
			var thisForm = jQuery(this);
			/* Fix textarea input fields: */
			thisForm.find('textarea').parent().addClass('textarea');
			/* Replace submit button with custom styled anker: */
			var submitButton = thisForm.find('input[type="submit"]');
			if (submitButton.length > 0) {
				/* This condition is only required for IE6 which doesn't submit the form correctly: */
				if (thisForm.attr('onsubmit')) {
					var newSubmitLinkHTML = '<a href="javascript://" title="' + submitButton.attr('value') + '">' + submitButton.attr('value') + '</a>';
					submitButton.parent().append(newSubmitLinkHTML).find('a').click(function() {
						//jQuery(this).parent().parent().trigger('submit');
						/* Find the parent form and submit it: */
						var parentForm = globalFunctions._findParentForm(jQuery(this).parent());
						if (parentForm != false) {						
							if (parentForm.attr('onsubmit')) {
								parentForm.trigger('submit');
							}else{
								parentForm.submit();
							}						
						}
					});	
				}else{
					var newSubmitLinkHTML = jQuery('<a href="#" title="' + submitButton.attr('value') + '">' + submitButton.attr('value') + '</a>');
					newSubmitLinkHTML.click(function() { thisForm.trigger('submit'); return false; });
					submitButton.parent().append(newSubmitLinkHTML);
				}
				submitButton.remove();
			}
			/* Add focus, blur and keypress handlers to inputs and textareas: */
			var labelHideFct = function() {
				if (jQuery(this).val() != '') { jQuery('label[for="' + jQuery(this).attr('id') + '"]').addClass('invisible');
				}else{ jQuery('label[for="' + jQuery(this).attr('id') + '"]').removeClass('invisible'); }
			};		
			thisForm.find('input[type="text"], input[type="password"], textarea').focus(labelHideFct).blur(labelHideFct).keypress(function(evt) {
				// Submit form on enter key:
				if (jQuery(evt.target).attr('tagName') == 'INPUT' && evt.keyCode == 13) {
					/* Find the parent form and submit it: */
					var parentForm = globalFunctions._findParentForm(jQuery(this).parent());
					if (parentForm != false) {						
						if (parentForm.attr('onsubmit')) {
							parentForm.trigger('submit');
						}else{
							parentForm.submit();
						}						
					}
				}
			}).keypress(labelHideFct).keyup(labelHideFct).keydown(labelHideFct).trigger('blur');
		});
	},
	
	_findParentForm : function(jQueryObj) {
		if (jQueryObj.attr('tagName') != 'BODY') {
			if (jQueryObj.attr('tagName') != 'FORM') {
				return globalFunctions._findParentForm(jQueryObj.parent());
			}else{
				return jQueryObj;
			}
		}else{
			return false;
		}
	}
	
	/* ==================================================================================================================== */
}






