var FADE_OUT_SPEED = 100;
var FADE_IN_SPEED_COLUMN = 100;
var FADE_IN_SPEED_CONTENT = 300;
var MOVE_SPEED = 300;
var IMAGE_BLEND_PAGE_CHANGE_LENGTH = FADE_OUT_SPEED + FADE_IN_SPEED_COLUMN + FADE_IN_SPEED_CONTENT + MOVE_SPEED;
var IMAGE_BLEND_SLIDE_SHOW_LENGTH = 2000;
var SLIDE_SHOW_INTERVAL = 10000 + IMAGE_BLEND_SLIDE_SHOW_LENGTH;

// time to wait for image to load until next callback trying to blend over to next image
var LOAD_IMAGE_GRACE_TIME = 200;

var COLUMN_MARGIN = 2;

var DOMAINS = ['www.erste-elf.de', '213.133.104.27', 'http://erste-elf.de.www27.your-server.de/', 'localhost', '192.168.11.155'];

var historyFrameUrl = 'tl_files/common/history.html';
var lastFrameUrl = self.location.href;
var refererUrl   = document.referrer;

var currentPageStatus = null;
var pageChangeInProgess = false;
var isMsie = jQuery.browser.msie;
var isMsie6 = jQuery.browser.msie && jQuery.browser.version < 7;

var backgroundController = new BackgroundController();

var piwikTracker = null;

var columnTemplate = '<div class="columnContainer" id="#{columnID}" style="display:none;">\n'+
					 '	<div class="marginTop"></div>\n'+
					 '		<div class="heightAdjustment">\n'+
					 '			<div class="contentContainer" id="#{contentID}">\n'+
					 '			</div>\n'+
					 '		</div>\n'+
					 '	<div class="marginBottom"></div>\n'+
					 '</div>\n';

/*** general purpose flash vars for swfobject ***/
var defaultFlashParams = {
		quality: "high",
		allowScriptAccess: "always",
		allowfullscreen: "true",
		bgcolor: "#ffffff",
		name: "flashContent",
		scale: "showall",
		wmode: "opaque"
};

var minimumFlashVersion = "8.0.0";
var expressInstall = '/tl_files/common/swf/expressinstall.swf';



/*** jquery custom fades to avoid cleartype being turned of in IE ***/
(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie) $(this).get(0).style.removeAttribute('filter');
			if(callback != undefined) callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie) $(this).get(0).style.removeAttribute('filter');
			if(callback != undefined) callback();
		});
	};
})(jQuery);
/*** end custom fades ***/

/*** initialize start ***/

$(document).ready(function(){
	createAdditionalMarkup();
	backgroundController.initializeImages("#imageDefinition");
	replaceLinks('a');
	currentPageStatus = new PageStatus();
	currentPageStatus.parsePage("#containerPlacer");
	// adjust width for scroller
	$("#containerPlacer").width(currentPageStatus.totalWidth);
	resizeLayout();
	
	
	// hook to show images if on gallery page
	imgNo = $(document).getUrlParam("img");
	if(imgNo != null){
		showImage('img-'+imgNo);
	}
	
	
});
$(window).resize(resizeLayout);

/*** initialize end ***/

function loadPage(event, url){ 
	if(event != null) event.preventDefault();
	if(pageChangeInProgess) return;
	
	// only load new page if not the same one as current
	if(url != lastFrameUrl){
		
		pageChangeInProgess = true;
		$("body").css("cursor", "progress");
		
		// load new content containers
		refererUrl = lastFrameUrl;
		lastFrameUrl = url;
		
		$.get(url, undefined, function(responseText, textStatus){
			if(textStatus == "success"){
				
				var content = responseText.slice(responseText.indexOf('<!--  contentstart -->')+22, responseText.indexOf('<!--  contentend -->'));
				$('#cache').append(content);
				
				var isRichMedia = responseText.indexOf('<!-- richMedia -->') < 0 ? false: true;
				
				var nextPageStatus = new PageStatus();
				nextPageStatus.isRichMedia = isRichMedia;
				nextPageStatus.parsePage("#cache");
				var pageChange = new PageChange(currentPageStatus, nextPageStatus);
				pageChange.changePage();
				// no more background image change on page change
				//backgroundController.setNewPageImages("#cache #imageDefinition");
				currentPageStatus = nextPageStatus;
				
				// adjust title
				var newTitle = $("#cache #titleDefinition").html();
				//document.title = newTitle;
				
				$("#cache").empty();
				$('#historyFrame').contents()[0].location.href=historyFrameUrl+"?page="+escape(url)+"&title="+escape(newTitle);
				return true;
				
			} else {
				alert("Die angeforderte Seite konnte nicht geladen werden.");
				pageChangeFinished();
			}
		});
		/*
		$("#cache").load(url+" #containerPlacer", undefined, function(responseText, textStatus, XMLHttpRequest){
			
			if(textStatus == "success"){ alert('loaded');
				var nextPageStatus = new PageStatus();
				nextPageStatus.parsePage("#cache");
				var pageChange = new PageChange(currentPageStatus, nextPageStatus);
				pageChange.changePage();
				backgroundController.setNewPageImages("#cache #imageDefinition");
				currentPageStatus = nextPageStatus;
				
				// adjust title
				var newTitle = $("#cache #titleDefinition").html();
				//document.title = newTitle;
				
				
				$("#cache").empty();
				$('#historyFrame').contents()[0].location.href=historyFrameUrl+"?page="+escape(url)+"&title="+escape(newTitle);
				return true;
				
			} else {
				alert("Die angeforderte Seite konnte nicht geladen werden.");
				pageChangeFinished();
			}
		});
		*/
	}
}

function pageChangeFinished(){
	pageChangeInProgess = false;
	$("body").css("cursor", "default");
	// trigger column resize, if page does no longer need scroller
	resizeColumns();
	// send tracking event
	if(piwikTracker != null){
		piwikTracker.setDocumentTitle(document.title);
		piwikTracker.trackPageView();
		piwikTracker.enableLinkTracking();
	}
}

function replaceLinks(selector){

	$(selector).each(function(i){ 
		if(isInternalUrl(this)){ 
			$(this).bind("click", function(event){ loadPage(event, this.href); });
		}
		$(this).bind("click", function(event){ this.blur(); });
	});
}

function createAdditionalMarkup(){

	// image containers for blending
	var html = '<div id="background_0">'+
			   '  <img src="tl_files/common/img/trans.gif" id="backgroundImage_0" />'+
			   '  <div id="backgroundText_0"></div>'+
			   '</div>'+
			   '<div id="background_1">'+
			   '  <img src="tl_files/common/img/trans.gif" id="backgroundImage_1" />'+
			   '  <div id="backgroundText_1"></div>'+
			   '</div>';
	$("body").prepend(html);
	
	// add cache div for loading html in
	$("body").append('<div id="cache"></div>');
	
	// add history iframe
	$("body").append('<iframe id="historyFrame" src="'+historyFrameUrl+'?page='+escape(lastFrameUrl)+'&title='+escape(document.title)+'" />');
	
	// additional columns needed
	if($("#columnMain").length == 0){
		var html = columnTemplate.replace(/#{columnID}/, "columnMain").replace(/#{contentID}/, "contentColumnMain");
		$("#containerPlacer").append(html);
	}
	if($("#columnDetail").length == 0){
		var html = columnTemplate.replace(/#{columnID}/, "columnDetail").replace(/#{contentID}/, "contentColumnDetail");
		$("#containerPlacer").append(html);
	}
	if($("#columnExtra").length == 0){
		var html = columnTemplate.replace(/#{columnID}/, "columnExtra").replace(/#{contentID}/, "contentColumnExtra");
		$("#containerPlacer").append(html);
	}
}

function historyTriggered(url){ 
	if(url != lastFrameUrl){
		loadPage(null, url);
	}
}


function resizeLayout(){
	/*** resize background images ***/
	for(var i=0; i < 2; i++){
		backgroundController.resizeBackground(i);
	}
	
	/*** resize content container(s) height ***/
	resizeColumns();
		
}

function resizeColumns(){
	var maxHeight = 0;
	$(".contentContainer").each( function(i){
		if(this.offsetHeight > maxHeight){
			maxHeight = this.offsetHeight;
		}
	});
	
	
	// check if window is higher than highest content
	var placerHeight = $("#containerPlacer").height();
	// 48px is the combined margin on top and bottom of the content containers
	if(placerHeight > maxHeight + 48){
		maxHeight = placerHeight - 48;
	}
	
	$(".heightAdjustment").height(maxHeight);
}


/*** PageStatus Object ***/

function PageStatus(){

	this.columnBrandExists     = false;
	this.columnBrandWidth      = 0;
	this.columnBrandContent  = null;
	
	this.columnMainExists      = false;
	this.columnMainWidth       = 0;
	this.columnMainContent   = null;
	
	this.columnDetailExists    = false;
	this.columnDetailWidth     = 0;
	this.columnDetailContent = null;
	
	this.columnExtraExists    = false;
	this.columnExtraWidth     = 0;
	this.columnExtraContent = null;
	
	this.isRichMedia = false
	
	
	this.parsePage = function(pageHtmlParentSelector){
		this.columnBrandWidth   = $(pageHtmlParentSelector+" #columnBrand").width();
		this.columnMainWidth    = $(pageHtmlParentSelector+" #columnMain").width();
		this.columnDetailWidth  = $(pageHtmlParentSelector+" #columnDetail").width();
		this.columnExtraWidth   = $(pageHtmlParentSelector+" #columnExtra").width();
		
		this.columnBrandContent   = jQuery.trim($(pageHtmlParentSelector+" #contentColumnBrand").html());
		this.columnMainContent    = jQuery.trim($(pageHtmlParentSelector+" #contentColumnMain").html());
		this.columnDetailContent  = jQuery.trim($(pageHtmlParentSelector+" #contentColumnDetail").html());
		this.columnExtraContent   = jQuery.trim($(pageHtmlParentSelector+" #contentColumnExtra").html());
		
		this.columnBrandExists  = (this.columnBrandWidth  == null || $(pageHtmlParentSelector+" #columnBrand").css("display")=="none") ?false:true;
		this.columnMainExists   = (this.columnMainWidth   == null || $(pageHtmlParentSelector+" #columnMain").css("display")=="none")  ?false:true;
		this.columnDetailExists = (this.columnDetailWidth == null || $(pageHtmlParentSelector+" #columnDetail").css("display")=="none")?false:true;
		this.columnExtraExists  = (this.columnExtraWidth  == null || $(pageHtmlParentSelector+" #columnExtra").css("display")=="none")?false:true;
	
	
		// set width to negative margin, so position calculations dont get messed up, if column does not exists
		if(!this.columnBrandExists)  this.columnBrandWidth  = 0 - COLUMN_MARGIN;
		if(!this.columnMainExists)   this.columnMainWidth   = 0 - COLUMN_MARGIN;
		if(!this.columnDetailExists) this.columnDetailWidth = 0 - COLUMN_MARGIN;
		if(!this.columnExtraExists)  this.columnExtraWidth = 0 - COLUMN_MARGIN;
		
		// calculate total width so scroller can be adjusted via #containerPlacer
		this.totalWidth = this.columnBrandWidth + COLUMN_MARGIN + this.columnMainWidth + COLUMN_MARGIN + this.columnDetailWidth + COLUMN_MARGIN + this.columnExtraWidth;
	};
	
	this.removePx = function(value){
		return jQuery.trim(value.replace(/px/g,""));
	}
}

/*** Page Change Object ***/

var pageChangeRef = null;

function PageChange(currentStatus, nextStatus){

	this.currentStatus = currentStatus;
	this.nextStatus = nextStatus;
	
	// content to be replaced?
	this.mustReplaceContentBrand  = currentStatus.columnBrandContent != nextStatus.columnBrandContent;
	this.mustReplaceContentMain   = currentStatus.columnMainContent != nextStatus.columnMainContent;
	this.mustReplaceContentDetail = currentStatus.columnDetailContent != nextStatus.columnDetailContent;
	this.mustReplaceContentExtra  = currentStatus.columnExtraContent != nextStatus.columnExtraContent;
	
	// is content basically the same?
	this.hasSameBasicContentBrand  = !this.mustReplaceContentBrand   || isBasicContentEqual(currentStatus.columnBrandContent, nextStatus.columnBrandContent);
	this.hasSameBasicContentMain   = !this.mustReplaceContentMain    || isBasicContentEqual(currentStatus.columnMainContent, nextStatus.columnMainContent);
	this.hasSameBasicContentDetail = !this.mustReplaceContentDetail  || isBasicContentEqual(currentStatus.columnDetailContent, nextStatus.columnDetailContent);
	this.hasSameBasicContentExtra  = !this.mustReplaceContentExtra   || isBasicContentEqual(currentStatus.columnExtraContent, nextStatus.columnExtraContent);
	
	// fade out?
	this.mustFadeOutColumnBrand   = currentStatus.columnBrandExists && !nextStatus.columnBrandExists;
	this.mustFadeOutContentBrand  = !this.mustFadeOutColumnBrand && currentStatus.columnBrandExists && !this.hasSameBasicContentBrand;
	
	this.mustFadeOutColumnMain    = currentStatus.columnMainExists && !nextStatus.columnMainExists;
	this.mustFadeOutContentMain   = !this.mustFadeOutColumnMain && currentStatus.columnMainExists && !this.hasSameBasicContentMain;
	
	this.mustFadeOutColumnDetail  = currentStatus.columnDetailExists && !nextStatus.columnDetailExists;
	this.mustFadeOutContentDetail = !this.mustFadeOutColumnDetail && currentStatus.columnDetailExists && !this.hasSameBasicContentDetail;
	
	this.mustFadeOutColumnExtra   = currentStatus.columnExtraExists && !nextStatus.columnExtraExists;
	this.mustFadeOutContentExtra  = !this.mustFadeOutColumnExtra && currentStatus.columnExtraExists && !this.hasSameBasicContentExtra;
	
	this.somethingFadesOut = this.mustFadeOutColumnBrand || this.mustFadeOutContentBrand || this.mustFadeOutColumnMain || this.mustFadeOutContentMain || this.mustFadeOutColumnDetail ||  this.mustFadeOutContentDetail || this.mustFadeOutColumnExtra ||  this.mustFadeOutContentExtra; 
	
	// fade in ?
	this.mustFadeInColumnBrand    = !currentStatus.columnBrandExists && nextStatus.columnBrandExists;
	this.mustFadeInContentBrand   = this.mustFadeInColumnBrand || (nextStatus.columnBrandExists && !this.hasSameBasicContentBrand );

	this.mustFadeInColumnMain     = !currentStatus.columnMainExists && nextStatus.columnMainExists;
	this.mustFadeInContentMain    = this.mustFadeInColumnMain || ( nextStatus.columnMainExists && !this.hasSameBasicContentMain );
	
	this.mustFadeInColumnDetail   = !currentStatus.columnDetailExists && nextStatus.columnDetailExists;
	this.mustFadeInContentDetail  = this.mustFadeInColumnDetail || ( nextStatus.columnDetailExists && !this.hasSameBasicContentDetail );
	
	this.mustFadeInColumnExtra   = !currentStatus.columnExtraExists && nextStatus.columnExtraExists;
	this.mustFadeInContentExtra  = this.mustFadeInColumnExtra || ( nextStatus.columnExtraExists && !this.hasSameBasicContentExtra );
	
	this.columnFadesIn  = this.mustFadeInColumnBrand || this.mustFadeInColumnMain || this.mustFadeInColumnDetail || this.mustFadeInColumnExtra;
	this.contentFadesIn = this.mustFadeInContentBrand || this.mustFadeInContentMain ||  this.mustFadeInContentDetail ||  this.mustFadeInContentExtra;
	
	// change width?
	this.mustChangeWidthColumnBrand   = !this.mustFadeInColumnBrand && nextStatus.columnBrandExists && currentStatus.columnBrandWidth != nextStatus.columnBrandWidth;
	this.mustChangeWidthColumnMain    = !this.mustFadeInColumnMain && nextStatus.columnMainExists && currentStatus.columnMainWidth != nextStatus.columnMainWidth;
	this.mustChangeWidthColumnDetail  = !this.mustFadeInColumnDetail && nextStatus.columnDetailExists && currentStatus.columnDetailWidth != nextStatus.columnDetailWidth;
	this.mustChangeWidthColumnExtra   = !this.mustFadeInColumnExtra && nextStatus.columnExtraExists && currentStatus.columnExtraWidth != nextStatus.columnExtraWidth;
	
	// move?
	this.mustMoveColumnBrand  =  !this.mustFadeOutColumnBrand  && (this.mustFadeInColumnMain || this.mustFadeOutColumnMain || this.mustChangeWidthColumnMain || this.mustFadeInColumnDetail || this.mustFadeOutColumnDetail || this.mustChangeWidthColumnDetail || this.mustFadeInColumnExtra || this.mustFadeOutColumnExtra || this.mustChangeWidthColumnExtra);  
	this.mustMoveColumnMain   =  !this.mustFadeOutColumnMain   && (this.mustFadeInColumnDetail || this.mustFadeOutColumnDetail || this.mustChangeWidthColumnDetail || this.mustFadeInColumnExtra || this.mustFadeOutColumnExtra || this.mustChangeWidthColumnExtra);
	this.mustMoveColumnDetail =  !this.mustFadeOutColumnDetail && (this.mustFadeInColumnExtra || this.mustFadeOutColumnExtra || this.mustChangeWidthColumnExtra);
	
	
	this.somethingMoves = this.mustChangeWidthColumnMain || this.mustChangeWidthColumnDetail || this.mustChangeWidthColumnExtra || this.mustMoveColumnBrand || this.mustMoveColumnMain || this.mustMoveColumnDetail;
	
	// new positions expressed as margins to right
	this.positionColumnExtra  = 0;
	this.positionColumnDetail = COLUMN_MARGIN + nextStatus.columnExtraWidth;
	this.positionColumnMain   = COLUMN_MARGIN + nextStatus.columnDetailWidth + COLUMN_MARGIN + nextStatus.columnExtraWidth;
	this.positionColumnBrand  = COLUMN_MARGIN + nextStatus.columnMainWidth + COLUMN_MARGIN + nextStatus.columnDetailWidth + COLUMN_MARGIN + nextStatus.columnExtraWidth;
	
	this.changePage = function(){
		// remember reference to this object
		pageChangeRef = this;
		this.fadeOut();
	}
	
	
	/*** fade out containers and content not needed on next page ***/
	this.fadeOut = function(){
		if(isMsie && this.somethingFadesOut){
			// if msie6 do not use fade on content directly, due to its ungraceful display on ie6
			if(this.mustFadeOutColumnBrand){
				$("#columnBrand").customFadeOut(FADE_OUT_SPEED);
			}
			if(this.mustFadeOutContentBrand){
				$("#contentColumnBrand").hide();
			}
			
			if(this.mustFadeOutColumnMain){
				$("#columnMain").customFadeOut(FADE_OUT_SPEED);
			}
			if(this.mustFadeOutContentMain){
				$("#contentColumnMain").hide();
			}
			
			if(this.mustFadeOutColumnDetail){
				$("#columnDetail").customFadeOut(FADE_OUT_SPEED);
			}
			if(this.mustFadeOutContentDetail){
				$("#contentColumnDetail").hide();
			}
			
			if(this.mustFadeOutColumnExtra){
				$("#columnExtra").customFadeOut(FADE_OUT_SPEED);
			}
			if(this.mustFadeOutContentExtra){
				$("#contentColumnExtra").hide();
			}
			
			if(this.mustFadeOutColumnBrand || this.mustFadeOutColumnMain || this.mustFadeOutColumnDetail || this.mustFadeOutColumnExtra){
				window.setTimeout("pageChangeRef.move()", FADE_OUT_SPEED);
			} else {
				this.move();
			}
		} else if(this.somethingFadesOut){
		
			if(this.mustFadeOutColumnBrand){
				$("#columnBrand").customFadeOut(FADE_OUT_SPEED);
			}
			if(this.mustFadeOutContentBrand){
				$("#contentColumnBrand").customFadeOut(FADE_OUT_SPEED);
			}
			
			if(this.mustFadeOutColumnMain){
				$("#columnMain").customFadeOut(FADE_OUT_SPEED);
			}
			if(this.mustFadeOutContentMain){
				$("#contentColumnMain").customFadeOut(FADE_OUT_SPEED);
			}
			
			if(this.mustFadeOutColumnDetail){
				$("#columnDetail").customFadeOut(FADE_OUT_SPEED);
			}
			if(this.mustFadeOutContentDetail){
				$("#contentColumnDetail").customFadeOut(FADE_OUT_SPEED);
			}
			
			if(this.mustFadeOutColumnExtra){
				$("#columnExtra").customFadeOut(FADE_OUT_SPEED);
			}
			if(this.mustFadeOutContentExtra){
				$("#contentColumnExtra").customFadeOut(FADE_OUT_SPEED);
			}
			
			window.setTimeout("pageChangeRef.move()", FADE_OUT_SPEED);
			
		} else {
			this.move();
		}
	}
	
	/*** move containers to new position and animate widths ***/
	this.move = function(){ 
		
		// jump to top of new page
		window.scrollTo(0,0);
		
		// switch styles for brand column
		if(this.nextStatus.isRichMedia){
			$('.brandContainer').addClass('richMediaContainer');
		} else {
			$('.brandContainer').removeClass('richMediaContainer');
		}
		
		if(this.somethingMoves){
			
			if(this.mustMoveColumnBrand && this.mustChangeWidthColumnBrand){
				$("#columnBrand").animate({marginRight:this.positionColumnBrand, width:this.nextStatus.columnBrandWidth}, MOVE_SPEED, "swing");
				
			} else if(this.mustMoveColumnBrand){
				$("#columnBrand").animate({marginRight:this.positionColumnBrand}, MOVE_SPEED, "swing");
			
			} else if(this.mustChangeWidthColumnBrand){ 
				$("#columnBrand").animate({width:this.nextStatus.columnMainBrand}, MOVE_SPEED, "swing");
			}
			
			
			if(this.mustMoveColumnMain && this.mustChangeWidthColumnMain){
				$("#columnMain").animate({marginRight:this.positionColumnMain, width:this.nextStatus.columnMainWidth}, MOVE_SPEED, "swing");
				
			} else if(this.mustMoveColumnMain){
				$("#columnMain").animate({marginRight:this.positionColumnMain}, MOVE_SPEED, "swing");
			
			} else if(this.mustChangeWidthColumnMain){ 
				$("#columnMain").animate({width:this.nextStatus.columnMainWidth}, MOVE_SPEED, "swing");
			}
			
			if(this.mustMoveColumnDetail && this.mustChangeWidthColumnDetail){
				$("#columnDetail").animate({marginRight:this.positionColumnDetail, width:this.nextStatus.columnDetailWidth}, MOVE_SPEED, "swing");
				
			} else if(this.mustMoveColumnDetail){
				$("#columnDetail").animate({marginRight:this.positionColumnDetail}, MOVE_SPEED, "swing");
			
			} else if(this.mustChangeWidthColumnDetail){ 
				$("#columnDetail").animate({width:this.nextStatus.columnDetailWidth}, MOVE_SPEED, "swing");
			}
			
			if(this.mustChangeWidthColumnExtra){ 
				$("#columnExtra").animate({width:this.nextStatus.columnDetailExtra}, MOVE_SPEED, "swing");
			}
			window.setTimeout("pageChangeRef.setContent()", MOVE_SPEED);
		} else {
			this.setContent();
		}
	}
	
	/*** set content in changing containers ***/
	this.setContent = function(){
		if(this.mustReplaceContentBrand){
			$("#contentColumnBrand").html(this.nextStatus.columnBrandContent);
			if(this.mustFadeInColumnBrand){
				// container appears anew. make sure width and position are set
				$("#columnBrand").width(this.nextStatus.columnBrandWidth);
				$("#columnBrand").css("margin-right", this.positionColumnBrand);
				// hide content at beginning, so it fades in with other column's content
				$("#contentColumnBrand").hide();
			}
		}
		if(this.mustReplaceContentMain){
			$("#contentColumnMain").html(this.nextStatus.columnMainContent);
			if(this.mustFadeInColumnMain){
				// container appears anew. make sure width and position are set
				$("#columnMain").width(this.nextStatus.columnMainWidth);
				$("#columnMain").css("margin-right", this.positionColumnMain);
				// hide content at beginning, so it fades in with other column's content
				$("#contentColumnMain").hide();
			}
		}
		if(this.mustReplaceContentDetail){
			$("#contentColumnDetail").html(this.nextStatus.columnDetailContent);
			if(this.mustFadeInColumnDetail){
				// container appears anew. make sure width and position are set
				$("#columnDetail").width(this.nextStatus.columnDetailWidth);
				$("#columnDetail").css("margin-right", this.positionColumnDetail);
				// hide content at beginning, so it fades in with other column's content
				$("#contentColumnDetail").hide();
			}
		}
		if(this.mustReplaceContentExtra){
			$("#contentColumnExtra").html(this.nextStatus.columnExtraContent);
			if(this.mustFadeInColumnExtra){
				// container appears anew. make sure width and position are set
				$("#columnExtra").width(this.nextStatus.columnExtraWidth);
				$("#columnExtra").css("margin-right", this.positionColumnExtra);
				// hide content at beginning, so it fades in with other column's content
				$("#contentColumnExtra").hide();
			}
		}
		
		this.fadeInColumns();
	}
	
	
	/*** fade in new containers and content in existing containers ***/
	this.fadeInColumns = function(){
		
		if(this.columnFadesIn){
		
			if(this.mustFadeInColumnBrand){
				$("#columnBrand").customFadeIn(FADE_IN_SPEED_COLUMN);
			}
			
			if(this.mustFadeInColumnMain){
				$("#columnMain").customFadeIn(FADE_IN_SPEED_COLUMN);
			}
			
			if(this.mustFadeInColumnDetail){
				$("#columnDetail").customFadeIn(FADE_IN_SPEED_COLUMN);
			}
			
			if(this.mustFadeInColumnExtra){
				$("#columnExtra").customFadeIn(FADE_IN_SPEED_COLUMN);
			}
			window.setTimeout("pageChangeRef.fadeInContent()", FADE_IN_SPEED_COLUMN);
		} else {
			this.fadeInContent();
		}
	}
	
	/*** fade in new content ***/
	this.fadeInContent = function(){
		if(isMsie && this.contentFadesIn){
			// if msie do not use fade on content directly, due to its ungraceful dealing with bold fonts in ie6 and all fonts in ie7
			if(this.mustFadeInContentBrand){
				$("#contentColumnBrand").show();
			}
			
			if(this.mustFadeInContentMain){
				$("#contentColumnMain").show();
			}
			
			if(this.mustFadeInContentDetail){
				$("#contentColumnDetail").show();
			}
			
			if(this.mustFadeInContentExtra){
				$("#contentColumnExtra").show();
			}
			
			replaceLinks('a');
			
			// adjust width for scroller
			$("#containerPlacer").width(this.nextStatus.totalWidth);
			
			pageChangeFinished();
			
		} else if(this.contentFadesIn){
		
			if(this.mustFadeInContentBrand){
				$("#contentColumnBrand").customFadeIn(FADE_IN_SPEED_CONTENT);
			}
			if(this.mustFadeInContentMain){
				$("#contentColumnMain").customFadeIn(FADE_IN_SPEED_CONTENT);
			}
			
			if(this.mustFadeInContentDetail){
				$("#contentColumnDetail").customFadeIn(FADE_IN_SPEED_CONTENT);
			}
			
			if(this.mustFadeInContentExtra){
				$("#contentColumnExtra").customFadeIn(FADE_IN_SPEED_CONTENT);
			}
			replaceLinks('a');
			
			// adjust width for scroller
			$("#containerPlacer").width(this.nextStatus.totalWidth);
			
			window.setTimeout("pageChangeFinished()", FADE_IN_SPEED_CONTENT);
		} else {
			replaceLinks('a');
			pageChangeFinished();
		}
	}
}

/*** BackgroundController Object ***/
var backgroundControllerRef = null;

function BackgroundController(){
	// only one backgroundController used, so:
	backgroundControllerRef = this;

	this.images = null;
	this.lastImage = null;
	
	this.activeBgElement = 0;
	this.currentBgImage = [0,0];
	
	this.slideshowTimer = null;
	
	this.initializeImages = function(selector){
		this.setImages(selector);
		
		if(this.images.length > 0){
			var imageIndex = rand(0, this.images.length-1);
			var image = this.images[imageIndex];
			$("#backgroundImage_"+this.activeBgElement).attr("src", image.url);
			$("#backgroundText_"+this.activeBgElement).html(image.text);
			replaceLinks("#backgroundText_"+this.activeBgElement+" a");
			this.currentBgImage[this.activeBgElement] = imageIndex;
			
			// remember last image
			this.lastImage = image;
		}
		if(this.images.length > 1){ 
			backgroundControllerRef = this;
			this.slideshowTimer = window.setTimeout("backgroundControllerRef.showNextImageInSlideShow()", SLIDE_SHOW_INTERVAL);
		}
	}
	
	this.setNewPageImages = function(selector){
		if(this.slideshowTimer != null){
			// clear slideshow
			window.clearTimeout(this.slideshowTimer);
			this.slideshowTimer == null;
		}
		this.setImages(selector);
		if(this.images.length > 0){ 
			var newImageIndex = rand(0, this.images.length-1);
			this.blendImage(newImageIndex, IMAGE_BLEND_PAGE_CHANGE_LENGTH);
		}
		
		if(this.images.length > 1){ 
			backgroundControllerRef = this;
			this.slideshowTimer = window.setTimeout("backgroundControllerRef.showNextImageInSlideShow()", SLIDE_SHOW_INTERVAL + IMAGE_BLEND_PAGE_CHANGE_LENGTH);
		}
	}
	
	this.setImages = function(selector){ 
		var script = $(selector).html();
		if(script != null && script.indexOf("images") != -1){
			eval(script);
			this.images = images;
			backgroundControllerRef = this;
			// start preloading
			for(var i=0; i < this.images.length; i++){
				this.images[i].loaded = false;
				this.images[i].img = new Image();
				this.images[i].img.onload = backgroundControllerRef.imageLoaded; // remember: set onload handler before the src for opera
				this.images[i].img.src = this.images[i].url;
			}
		}
	}
	
	this.imageLoaded = function(event){
		// find loaded image in array
		for(var i=0; i < backgroundControllerRef.images.length; i++){
			if(backgroundControllerRef.images[i].img.src == this.src){
				backgroundControllerRef.images[i].loaded = true;
				break;
			}
		}
	}
	
	this.resizeBackground = function(bgElementIndex){ 
		if(bgElementIndex == undefined) bgElementIndex = this.activeBgElement
	
		var width  = $("#background_"+this.activeBgElement).width(); // measure width and height on element that is NOT on display:none
		var height = $("#background_"+this.activeBgElement).height();
		var ratio =  width/height;
		
		var image = this.images[this.currentBgImage[bgElementIndex]];
		
		var originalImageRatio = image.ratio;
		if(originalImageRatio >= ratio){
			width = Math.round(height * originalImageRatio);
		} else {
			height = Math.round(width / originalImageRatio);
		}
	
		$("#backgroundImage_"+bgElementIndex).width(width);
		$("#backgroundImage_"+bgElementIndex).height(height);
		
		// place text according to image size
		var top  = Math.round(height * image.textTopPercent); 
		var left = Math.round(width  * image.textLeftPercent);
		
		$("#backgroundText_"+bgElementIndex).css("top", top).css("left", left);
	}
	
	this.showNextImageInSlideShow = function(){
		var shownImageIndex = this.currentBgImage[this.activeBgElement];
		var nextImageIndex = (shownImageIndex+1 == this.images.length) ? 0 : shownImageIndex+1;
		this.blendImage(nextImageIndex, IMAGE_BLEND_SLIDE_SHOW_LENGTH);
		this.slideshowTimer = window.setTimeout("backgroundControllerRef.showNextImageInSlideShow()",SLIDE_SHOW_INTERVAL);
	}
	
	this.blendImage = function(imageIndexToBlendTo, time, num){ if(num == undefined) num = 0;
		var nextBgElement = (this.activeBgElement==0)?1:0;
		var nextImage =  this.images[imageIndexToBlendTo];
		
		// do blend only if new image and current image are different
		if(nextImage.url != this.lastImage.url){
		
			// eventually image is not yet fully loaded. if so, try again later
			
			if(!nextImage.loaded){
				backgroundControllerRef = this;
				num++; 
				window.setTimeout("backgroundControllerRef.blendImage("+imageIndexToBlendTo+","+time+","+num+")", LOAD_IMAGE_GRACE_TIME);
				return;
			}
			
		
			// remember last image
			this.lastImage = nextImage;
			// remember which image is shown in each element
			this.currentBgImage[nextBgElement] = imageIndexToBlendTo;
			
			/*** fill in new image and text and adjust sizes ***/
			$("#backgroundImage_"+nextBgElement).attr("src", nextImage.url);
			$("#backgroundText_"+nextBgElement).html(nextImage.text);
			replaceLinks("#backgroundText_"+nextBgElement+" a");
			this.resizeBackground(nextBgElement);
			//this.resizeBackground(this.activeBgElement);
			
			
			/*** fade ***/
			$("#background_"+nextBgElement).fadeIn(time);
			$("#background_"+this.activeBgElement).fadeOut(time);
			
			this.activeBgElement = nextBgElement;
		}
	}
}


/*** ReferenzNavigation ***/
function showBar(barToShow, barToHide){ 
	$('#bar_'+barToHide).hide();
	$('#bar_'+barToShow).show();
}

/*** Helpers ***/
function rand (min, max) {
	return Math.floor(Math.random() * (max - min + 1)) + min;
}

function isInternalUrl(linkElement){
	var url = linkElement.href;
	// is internal if no "http" or "javascript" and not links to own domains
	if(url.indexOf('javascript:') != -1 
	|| url.indexOf('/downloads/') != -1
	|| url.indexOf('mailto:') != -1
	) return false;
	
	if(url.indexOf('http') == -1) return true;
	
	for(var i=0; i < DOMAINS.length; i++){
		if(url.indexOf(DOMAINS[i]) != -1){
		 	// check if this link is supposed to open a new window
		 	var onclickAtr = " "+linkElement.getAttribute('onclick'); // force string
		 	if( onclickAtr != undefined && onclickAtr != null  && onclickAtr.indexOf('open') != -1){
		 		return false;
		 	} else {
		 		return true;
		 	}
		 	
		}
	}
	return false;
}


function isBasicContentEqual(currentContent, nextContent){
	if(currentContent == undefined && nextContent == undefined) return true;
	if(currentContent == undefined || nextContent == undefined) return false;
	
	var normalizedCurrentContent = currentContent.replace(/\s*linkHigh/g, '');
	var normalizedNextContent    = nextContent.replace(/\s*linkHigh/g, '');
	
	if(normalizedCurrentContent == normalizedNextContent) return true;
}


// **** gallery display methods ****
function showImage(id){
	// hide all
	$('.bigImageContainer').hide();
	$('#'+id).show();
	
	// set all to opaque
	$('.blogGalleryThumbnail').css('opacity', 1.0);
	$('#thumbnail-'+id).css('opacity', 0.5);
	return true;
}

// **** rich media methods ****
function removeFlashMedia(){
	$('#flashMedia').remove();
}
