var bgimage = false;
var bgimage_width, bgimage_ratio, page_ratio;
var currentSection = '';
var nextBgimage = 1;
var navigation = false;
var navigationTimeout = false;

function adjustBackground() {
    bgw = bgimage_width;
    wh = window.size().height;
    ww = window.size().width;
    page_ratio = ww / wh;
    if (page_ratio < bgimage_ratio) {
        bgimage.style.height = wh + 'px';
        bgimage.style.width = Math.round(wh * bgimage_ratio) + 'px';
        if (wh <= 720)
            bgimage_width = 1280;
        else if (wh <= 810)
            bgimage_width = 1440;
        else
            bgimage_width = 1920;
    }
    else {        
        bgimage.style.width = ww + 'px';
        bgimage.style.height = Math.round(ww / bgimage_ratio) + 'px';
        if (ww <= 1280)
            bgimage_width = 1280;
        else if (ww <= 1440)
            bgimage_width = 1440;
        else
            bgimage_width = 1920;
		
    }
    if (bgw != bgimage_width) {       
        switchBackground(currentSection);     
		preloadBackgrounds();
    }
}

function adjustBoxStyles(sectionName) {

    var left, top, width, border_color, color, background_color;

    // defaults:
    color = '#11100E';
    background_color = '#fff';

    switch(sectionName) {
        case 'home':
            left = window.size().width * 0.31;
            top = window.size().height * 0.5;
            width = 338 * window.size().width/1280;
            border_color =  '#66656a';
            break;
		case 'aktuell':
            left = window.size().width * 0.24;
            top = window.size().height * 0.38;
            width = 480 * window.size().width/1280;
			if (width < 470) width = 470;
            border_color =  '#52544f';
            break;
        case 'vita':
            left = window.size().width * 0.68;
            top = window.size().height * 0.12;
            width = 370 * window.size().width/1280;
			if (width < 335) width = 335;
            border_color =  '#777777';
            break;
        case 'vita/steckbrief':
            left = window.size().width * 0.69;
            top = window.size().height * 0.24;
            width = 350 * window.size().width/1280;
            border_color =  '#7f7169';
			color = '#eee';
            background_color = '#19120f';
            break;
		case 'vita/engagements':
            left = window.size().width * 0.12;
            top = window.size().height * 0.65;
            width = 980 * window.size().width/1280;
            border_color =  '#777777';
            break;
		case 'vita/biographie':
            left = window.size().width * 0.66;
            top = window.size().height * 0.24;
            width = 380 * window.size().width/1280;
            border_color =  '#777777';
            break;
        case 'vita/lebenslauf':
            left = window.size().width * 0.63;
            top = window.size().height * 0.23;
            width = 450 * window.size().width/1280;
            border_color =  '#777777';
            break;
        case 'termine':
            left = 250; //window.size().width * 0.2;
            top = window.size().height * 0.13;
            width = 425 * window.size().width/1280;
            /*left = window.size().width * 0.03;
            top = window.size().height * 0.48;
            width = 650 * window.size().width/1280;*/
            border_color =  '#9eadaa';
            break;
        case 'media':
            left = window.size().width * 0.23;
            top = window.size().height * 0.17;
            width = 260 * window.size().width/1280;
			if (width < 230) width = 230;
            border_color =  '#c6d4de';
            break;
        case 'media/audio':
            left = window.size().width * 0.70;
            top = window.size().height * 0.12;
            width = 320 * window.size().width/1280;
			if (width < 302) width = 302;
            border_color =  '#c6d4de';
            break;
        case 'media/video':
            left = window.size().width * 0.58;
            top = window.size().height * 0.09;
            width = 480 * window.size().width/1280;
            if (width < 470) width = 470;
			border_color =  '#7d7c81';
            color = '#eee';
            background_color = '#11100E';
            break;
        case 'media/fotos':
            left = window.size().width * 0.52;
            top = window.size().height * 0.10;
            width = 520 * window.size().width/1280;
            border_color =  '#A9957C';
            color = '#eee';
            background_color = '#11100E';
            break;
        case 'kontakt':
            left = window.size().width * 0.25;
            top = window.size().height * 0.2;
            width = 355 * window.size().width/1280;
            border_color =  '#a5bbcb';
            break;
        case 'kontakt/impressum':
            left = window.size().width * 0.2;
            top = window.size().height * 0.44;
            width = 505 * window.size().width/1280;
            border_color =  '#333';
			//color = '#eee';
            //background_color = '#11100E';
            break;
    }
	
	if (window.size().width < width + left + 40) 
		left = window.size().width - width - 40;	
	else if (top < 311 && left < 230)
		left = 230;
		
    $('cont').morph('left: '+left+'px; top: '+top+'px; width: '+width+'px; border-color: '+border_color+'; background: '+background_color+'; color: '+background_color+';', {
        duration: 0.88,
        transition: 'easeInOutSine'
    });
    $('cont2').morph('left: '+left+'px; top: '+top+'px; width: '+width+'px; color: '+color+';', {
        duration: 0.88,
        transition: 'easeInOutSine'
    });
	
	// scrollers
	max_height = (sectionName == 'vita/steckbrief' || sectionName == 'vita/biographie' || sectionName == 'media/video') ? window.size().height - top - 84 : window.size().height - top - 66;
	var scrollers = $$('.scroller');	
	for (i=0; i<scrollers.length; i++) {
		scrollers[i].style.maxHeight = max_height + 'px';
	}
	
	// mp3player
	if (currentSection=="media/audio") {
		$("mp3player").morph('left: '+(left + 18)+'px; top: '+((top + 75))+'px; width: '+width+'px;', {
			duration: 0.88,
			transition: 'easeInOutSine'
		});
    } 
	else {
		$("mp3player").morph('left: -9px; top: -36px; width: 302px;', {
			duration: 0.88,
			transition: 'easeInOutSine'
		});
	}
}

function switchBackground(sectionName) {
    if (!sectionName) return;
    if (bgimage_width == undefined) bgimage_width = 1280;
    if (bgimage)
        bgimage.style.zIndex = 2;
    lbgi = bgimage;
    bgimage = document.getElementById("bgimage"+nextBgimage);
    bgimage.style.zIndex = 1;
    bgimage.style.opacity = 1; //
    //if (nextBgimage==1) nextBgimage++; else nextBgimage--;
    bgimage.src = "backgrounds/larsredlich_"+sectionName.replace(/\//g, "-")+"_"+bgimage_width+".jpg";
    bgimage_ratio = 1.78; // bgimage.width / bgimage.height;
    adjustBackground();
    /*if (lbgi)
        lbgi.morph('opacity: 0;', {duration: 1, transition: 'easeInOutSine'});*/
}

function preloadBackgrounds() {
	/*var cachedImage = new Image();
	imgs = '';
	alert(sections.length)
	for (i=0; i<sections.length; i++) {		
	alert ("backgrounds/larsredlich_"+sections[i].replace(/\//g, "-")+"_"+bgimage_width+".jpg")
		//cachedImage.src = "backgrounds/larsredlich_"+sections[i].replace(/\//g, "-")+"_"+bgimage_width+".jpg";	
		imgs = imgs + " " + "backgrounds/larsredlich_"+sections[i].replace(/\//g, "-")+"_"+bgimage_width+".jpg";	
		
	)
	alert (imgs)*/
}

function getContent(sectionName) {
	
	var content = document.getElementById('pc_'+sectionName.replace('/', ':')).innerHTML;
	
	var nav = ''; 
	if (currentSection.indexOf('vita') != -1)
		nav = document.getElementById('nav_vita').innerHTML;
	else if (currentSection.indexOf('media') != -1)
		nav = document.getElementById('nav_media').innerHTML;
	else if (currentSection.indexOf('kontakt') != -1)
		nav = document.getElementById('nav_kontakt').innerHTML;
		
	document.getElementById('cont').innerHTML = content;
	document.getElementById('cont2').innerHTML = content + nav;	
	
	if (currentSection=="media/audio" && document.getElementById("mp3player").innerHTML.replace(/^\s+|\s+$/g, '')=='')
		document.getElementById("mp3player").innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="302" height="62" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="mp3player5a.swf" /><param name="wmode" value="transparent" /><param name="quality" value="autohigh" /><embed src="mp3player5a.swf" quality="autohigh" width="302" height="62" name="mp3player" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" /></object>';
	
}

function showSection(sectionName) {
	if (sectionName == currentSection && sectionName == 'home')
		toggleNavigation(0);

	location.hash = '#info/'+sectionName+'/';
    
	return false;
}

function highlightNavButton(sectionName) {	
	for (i=1; i<sections.length; i++) { // skip home -> start at index 1		
		if (sectionName.indexOf(sections[i]) != -1) {
			$('nav_button_'+sections[i].replace('/', '-')).className = 'active';
		}
		else {
			$('nav_button_'+sections[i].replace('/', '-')).className = 'inactive';
		}
	}
}

function showSubSection(subSectionName) {
	// show subsection
	var className = '.'+currentSection.replace('/', '-') + '_' + subSectionName;	
	var items = $$(className);
	for (i=0; i<items.length; i++) {
		items[i].style.display = 'block';
	}
	// hide link
	var className = '.'+currentSection.replace('/', '-') + '_' + subSectionName + '_link';	
	var items = $$(className);
	for (i=0; i<items.length; i++) {
		items[i].style.display = 'none';
	}	
	return false;
}

function hideAndShowSubSection(subSectionName, subSectionCount) {
	// hide others
	j=0;
	while (j<subSectionCount) {
		j++;
		hideSubSection(j);		
	}
	// show subsection
	var className = '.'+currentSection.replace('/', '-') + '_' + subSectionName;	
	var items = $$(className);
	for (i=0; i<items.length; i++) {
		items[i].style.display = 'block';
	}
	// hide link
	var className = '.'+currentSection.replace('/', '-') + '_' + subSectionName + '_link';	
	var items = $$(className);
	for (i=0; i<items.length; i++) {
		items[i].style.display = 'none';
	}	
	return false;
}

function hideSubSection(subSectionName) {
	// hide subsection
	var className = '.'+currentSection.replace('/', '-') + '_' + subSectionName;	
	var items = $$(className);
	for (i=0; i<items.length; i++) {
		items[i].style.display = 'none';
	}
	// show link
	var className = '.'+currentSection.replace('/', '-') + '_' + subSectionName + '_link';	
	var items = $$(className);
	for (i=0; i<items.length; i++) {
		items[i].style.display = 'block';
	}
	return false;
}

window.onload = function() {
    setInterval('handleLocationHash();', 200);
    setInterval('/*adjustBackground();*/adjustBoxStyles(currentSection);', 1000);	
}

window.onresize = function() {
    adjustBackground();
    //adjustBoxStyles(currentSection);
}

var sections = ['home', 'aktuell', 'vita/steckbrief', 'vita/engagements', 'vita/biographie', 'vita', 'termine', 'media/audio', 'media/video', 'media/fotos', 'media', 'kontakt/impressum', 'kontakt'];

var previousLocationHash = '';
var autoNewsTimeout;

function handleLocationHash() {

	currentLocationHash = getLocationHash();
	
	if (currentLocationHash == '') { // landing page ...
		showSection('home');		
		navigationTimeout = setTimeout('showNavigation(0);', 12000);
		autoNewsTimeout = setTimeout('showSection("aktuell");', 20000);			
		return;		
	}
	
	if (currentLocationHash != previousLocationHash) {

		previousLocationHash = currentLocationHash;

		for (i=0; i<sections.length; i++) {

			if (currentLocationHash.indexOf(sections[i]) != -1 && sections[i] != currentSection) {				
		
				if (currentSection == '' && !navigationTimeout) {
					$('navigationbar').style.top = '0px';
					$('navigationbar').style.left = '0px';
					$('navigation').style.top = '88px';				
					$('navigation').style.left = '0px';
					navigation = true;
				}
				
				currentSection = sections[i];
				
				switchBackground(currentSection);
				adjustBoxStyles(currentSection);
				getContent(currentSection);
				//highlightNavButton(currentSection);

				if (currentSection != 'home') {
					clearTimeout(autoNewsTimeout);
					if (!navigation)
						showNavigation(0);
				}
		
			}
		
		}		
		
	}
	
}

function getLocationHash() {
	var r = window.location.href;
	var i = r.indexOf("#");
	return (i >= 0
		? r.substr(i+1)
		: ""
	);
}


function showNavigation(secsDelay) {
	if(navigationTimeout)
		clearTimeout(navigationTimeout);
	$('navigationbar').morph('top: 0px; left: 0px;', {
		duration: 1.44,
		delay: secsDelay,
		transition: 'easeOutSine'
	});
	$('navigation').morph('top: 88px; left: 0px;', {
		duration: 1.44,
		delay: secsDelay,
		transition: 'easeOutSine'
	});
	navigation = true;
}

function hideNavigation(secsDelay) {
	$('navigationbar').morph('top: -242px; left: 0px;', {
		duration: 1.44,
		delay: secsDelay,
		transition: 'easeOutSine'
	});
	$('navigation').morph('top: -154px; left: 0px;', {
		duration: 1.44,
		delay: secsDelay,
		transition: 'easeOutSine'
	});
	navigation = false;
}

function toggleNavigation(secsDelay) {
	(navigation) ? hideNavigation(secsDelay) : showNavigation(secsDelay);
}


window.size = function()
{
    var w = 0;
    var h = 0;

    //IE
    if(!window.innerWidth)
    {
        //strict mode
        if(!(document.documentElement.clientWidth == 0))
        {
            w = document.documentElement.clientWidth;
            h = document.documentElement.clientHeight;
        }
        //quirks mode
        else
        {
            w = document.body.clientWidth;
            h = document.body.clientHeight;
        }
    }
    //w3c
    else
    {
        w = window.innerWidth;
        h = window.innerHeight;
    }
    return {
        width:w,
        height:h
    };
}

