// random headerimage
function randomheader() {

    var random = Math.floor(Math.random() * document.getElementById("random_bg").getElementsByTagName("a").length);
    var random_bg = document.getElementById("random_bg").getElementsByTagName("a")[random].href;

    document.getElementById("sujet").src = random_bg;
}

function activenav(){

    if(document.getElementById("active_category")) {

        active_element = document.getElementById("active_category").innerHTML;
    
        if(active_element == "Chasings") {
            document.getElementById("nav").getElementsByTagName("a")[1].style.backgroundPosition = "0 -27px";
        }
        else if(active_element == "Naturaufnahmen") {
            document.getElementById("nav").getElementsByTagName("a")[2].style.backgroundPosition = "0 -27px";
        }
        else if(active_element == "Herzlich willkommen") {
            document.getElementById("nav").getElementsByTagName("a")[0].style.backgroundPosition = "0 -27px";
        }
        else if(active_element == "Impressum") {
            document.getElementById("functions").getElementsByTagName("a")[0].style.backgroundPosition = "0 -20px";
        }
        else if(active_element == "Kontakt") {
            document.getElementById("functions").getElementsByTagName("a")[1].style.backgroundPosition = "0 -20px";
        }
    }
}

// Doomready - The End Is Near
/*var doomreadywaiter = window.setInterval(function() {
	if (document.body) {
		window.clearInterval(doomreadywaiter);
		doomready();
	}
}, 20);
function doomready() {
*/
window.addEvent('domready', function() {
	initGallery();
	activenav();
	randomheader();
});


/** IMAGE GALLERY **/
function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [ scrOfX, scrOfY ];
}

var gallimages = new Array();
var bigimages = new Array();
function initGallery() {
    var images = $$('.lastpost img');

    for(i=0; i<images.length; i++) {
        el = images[i];
        if(el.className.indexOf('wp-smiley') == -1) {
            el.parentNode.id = "galleryimg_"+i;
            bigimages.push(el.parentNode.href);
            gallimages.push(el.parentNode.id);
            el.parentNode.href = "javascript:showImagePopup('" + el.parentNode.id + "')";
        }
    }

	objTemp = document.createElement("div");
	objTemp.id = 'blank';
	objTemp.style.visibility='hidden';
	document.body.appendChild(objTemp);
}

function nextImage(obj, go) {
	var id = obj.className;
	var activeImage = 0;
	for(i=0; i<gallimages.length; i++) {
		if(gallimages[i] == id)
			activeImage = i;
	}

	var nextImage = activeImage + go;
	if(nextImage == gallimages.length)
		nextImage = 0;

	if(nextImage < 0)
		nextImage = gallimages.length - 1;

	showImagePopup(gallimages[nextImage]);
}

function showImagePopup(id) {
	var a  = $(id);

	var activeImage = 0;
	for(i=0; i<gallimages.length; i++) {
		if(gallimages[i] == id)
			activeImage = i;
	}

	activeImage++;

    h3title = getH3Title();

    //popup navigation
	showPopup('blank', 0, 0, '<div id="popup_title">' + h3title + '</div><a id="popup_image_link" class="' + id + '" href="javascript:void(0)" onclick="nextImage(this, 1)"><img id="popup_image" src="' + bigimages[activeImage-1] + '" alt="" /></a><br/><div id="popup_navigation"><span class="nav"><a class="' + id + '" href="javascript:void(0)" onclick="nextImage(this, -1)">&laquo; Voriges Bild</a> [ <b>' + activeImage + '</b> / ' + gallimages.length + ' ]  <a class="' + id + '" href="javascript:void(0)" onclick="nextImage(this, 1)">N&auml;chstes Bild &raquo;</a></span><span class="overview"><a href="javascript:showThumbNails()">&Uuml;bersicht</a></span></div>');
}

function getH3Title() {
    h3title = $$('.lastpost h3')[0];
    if(h3title == undefined)
        h3title = "";
    else
        h3title = h3title.innerHTML;

    return h3title;
}

function showThumbNails() {
    h3title = getH3Title();
    var thumbnailcontent = '<div id="popup_title">' + h3title + '</div><div class="popup_thumb_container">';

	for(i=0; i<gallimages.length; i++) {
        img = $(gallimages[i]).getElementsByTagName('img')[0];
        if(img)
            thumbnailcontent += '<div onclick="javascript:showImagePopup(\'' + gallimages[i] + '\')" class="popup_thumb" style="background: url(' + img.src + ') no-repeat center center"></div>';
	}

    thumbnailcontent += '</div>';

    var maxwidth = 130;
    var maxheight = 110;

    if(gallimages.length >= 4)
        width = maxwidth * 5;
    else
        width = maxwidth * (gallimages.length + 1);

    width += 10;

    height = (Math.ceil(gallimages.length / 4)+1) * maxheight;

    if(gallimages.length <= 8)
        height = 260;

    if(gallimages.length <= 4)
        height = 125;

    if(height > 500)
        height = 500;

    //popup thumbnails size
    showPopup('blank', width, height, thumbnailcontent);
}

/** IMAGE GALLERY END **/

// BO Popup
function hideSelectBoxes() {
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) { selects[i].style.visibility = "hidden";}

	if(!window.ie)
		document.getElementById('content').style.overflow='hidden';
}

function showSelectBoxes() {
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) { selects[i].style.visibility = "visible";}

	if(!window.ie)
		document.getElementById('content').style.overflow='auto';
}

var scrollChecker;

function showPopup(id, width, height, popupinnerHTML) {
    scrollChecker = window.setInterval(function() {
        var scrolled = getScrollXY();
        if($('popup_background')) {
            $('popup_background').style.top = scrolled[1] + 'px';
            $('popup_background').style.height = document.body.offsetHeight + 'px';
        }
        if($('popup_content')) {
            $('popup_content').style.top = (($('popup_background').offsetHeight/2) + scrolled[1]) + 'px';
        }
    }, 1000);

    hideSelectBoxes();
    var scrolled = getScrollXY();

    if(document.getElementById('popup_background')) {
            objTemp = document.getElementById('popup_background');
    }
    else {
            objTemp = document.createElement("div");
            objTemp.id = 'popup_background';
            objTemp.style.visibility='hidden';
            objTemp.onclick=function() { }
    }

    objTemp.style.top = scrolled[1] + 'px';
    objTemp.style.height = document.body.offsetHeight + 'px';

    if(objTemp.style.display != 'block') {
            objTemp.style.display='block';

            popupBackground = document.body.appendChild(objTemp);
            $(popupBackground).effects({duration:400,onComplete:function(){      $(popupBackground).effects({}).set({'opacity':[0.5]}); showPopupContent(id, width, height, popupinnerHTML);     }}).start({'opacity':[0,0.5]});
    }
    else {
            $(popupBackground).effects({duration:0,onComplete:function(){      $(popupBackground).effects({}).set({'opacity':[0.5]}); showPopupContent(id, width, height, popupinnerHTML);     }}).start({'opacity':[0.5,0.5]});
    }
}

function showPopupContent(id, width, height, popupinnerHTML) {
    var scrolled = getScrollXY();

    if(popupinnerHTML == undefined)
            popupinnerHTML = '';

    if(document.getElementById('popup_content')) {
            objTemp = document.getElementById('popup_content');
    }
    else {
            objTemp = document.createElement("div");
            objTemp.id = 'popup_content';
    }

    $('popup_background').onclick=function() { popupClose(); }

    objTemp.innerHTML= '<div id="popup_close"><a href="javascript:popupClose()"><span>X</span></a></div><div id="popup_text">' + document.getElementById(id).innerHTML + '' + popupinnerHTML + '</div>';
    objTemp.style.display='block';
    popupContent = document.body.appendChild(objTemp);

    if(width > 0 && height > 0) {
    	width += 60;
    	height += 70;

    	marginLeft = '-' + Math.floor(width/2) +'px';
    	marginTop = '-' + Math.floor(height/2) +'px';

        $('popup_text').style.visibility = 'hidden';
        if($('popup_title'))
            $('popup_title').style.visibility = 'visible';

    	$('popup_content').effects({duration:400,
            onComplete:function(){
                $('popup_text').style.visibility = 'visible';
                if(document.body.offsetWidth < $('popup_content').offsetWidth)
                    $('popup_background').style.width = $('popup_content').offsetWidth + 'px';
                else
                    $('popup_background').style.width = document.body.offsetWidth + 'px';
            }
        }).start({'width':width, 'height':height, 'marginLeft':marginLeft, 'marginTop':marginTop});
    }

    if($('popup_image'))
        $('popup_image').onload = fixPopupImageSize;

    objTemp.style.top = (($('popup_background').offsetHeight/2) + scrolled[1]) + 'px';
}

var tooBig = false;
function fixPopupImageSize() {
    height = $('popup_image').offsetHeight + 80;
    width = $('popup_image').offsetWidth + 60;

    if(height > document.body.offsetHeight) {
        height = document.body.offsetHeight - 5;
        $('popup_image').style.height = (height - 75) + 'px';
        width = $('popup_image').offsetWidth + 60;
    }

    if(height > 0 && width > 0) {
    	marginLeft = '-' + Math.floor(width/2) +'px';
    	marginTop = '-' + Math.floor(height/2) +'px';

    	$('popup_content').effects({duration:400,
            onComplete:function(){
                $('popup_image').effects({}).set({'opacity':0});
                $('popup_image').style.visibility = 'visible';
                $('popup_image').effects({}).start({'opacity':[0,1]});
                $('popup_navigation').style.visibility = 'visible';
                $('popup_title').style.visibility = 'visible';
                if(document.body.offsetWidth < $('popup_content').offsetWidth)
                    $('popup_background').style.width = $('popup_content').offsetWidth + 'px';
                else
                    $('popup_background').style.width = document.body.offsetWidth + 'px';
            }
        }).start({'width':width, 'height':height, 'marginLeft':marginLeft, 'marginTop':marginTop});
    }
    else {
        setTimeout('fixPopupImageSize()', 100);
    }
}

function popupClose()
{
	popupContent = document.getElementById('popup_content');
	popupContent.style.display='none';

	popupBackground = $('popup_background');
	popupBackground.onclick=function() { }

	$(popupBackground).effects({duration:400,onComplete:function(){      	popupBackground.style.display='none'; showSelectBoxes();    }}).start({'opacity':[0.5,0]});

    if($('popup_navigation'))
        $('popup_navigation').style.visibility = 'hidden';

    $('popup_title').style.visibility = 'hidden';
	window.clearInterval(scrollChecker);		
}
// EO Popup (div einblenden)