function setImgTopMargin() {
    var blockHeight = $("#imgHome").height();
    var imgHeight = $("#imgHome .mainImg").height();
    var marginTop = Math.floor((blockHeight - imgHeight) / 2);
    $("#imgHome .mainImg").css('margin-top', marginTop);
}

function gotoHome(mode) {
    var activeId = $("#imgHome .icons .active").attr('id');
    var imageLength = $("#imgHome .icons img").length;
    activeId = parseInt(activeId.replace('iconImg', ''));

    switch (mode) {
        case 'previous':
            newActiveId = (activeId + imageLength - 1) % imageLength;
            break;
        case 'next':
            newActiveId = (activeId + 1) % imageLength;
            break;
    }

    var newSrc = $("#imgHome .url img:eq(" + newActiveId + ")").attr('src');
    var newAlt = $("#imgHome .url img:eq(" + newActiveId + ")").attr('alt');

    $("#imgHome .mainImg").attr('src', newSrc);
    $("#imgHome .mainImg").attr('alt', newAlt);
    $("#imgHome .text").html(newAlt);
    setImgTopMargin();

    $("#imgHome .options .icons img").attr('src', 'images/btnPictItemInact.gif');
    $("#imgHome .options .icons img").removeClass('active');
    $("#imgHome .options .icons img:eq(" + newActiveId + ")").attr('src', 'images/btnPictItemAct.gif');
    $("#imgHome .options .icons img:eq(" + newActiveId + ")").addClass('active');
}

function setNavigationHome() {
    $("#imgHome .url img").each(function(i) {
        if (i == 0) {
            var altVal = $("#imgHome .url img:eq(" + i + ")").attr('alt');
            $("#imgHome .icons").append("<img src='images/btnPictItemAct.gif' class='active' id='iconImg" + i + "' alt='" + altVal + "' />");
            $("#imgHome .text").html(altVal);
        } else
            $("#imgHome .icons").append("<img src='images/btnPictItemInact.gif' id='iconImg" + i + "' alt='' />");
    });
    $("#imgHome .navigation .options").css('visibility', 'visible');
    $("#navPrevious").bind("click", function() {
    gotoHome('previous');
    Cufon.refresh();
    });
    $("#navNext").bind("click", function() {
    gotoHome('next');
    Cufon.refresh();
    });
}

function setNavigationPhotoalbum() {
    var count = 0;

    $(".innerCnt a").each(function(i, val) {
        var altValue = $(".innerCnt img:eq(" + i + ")").attr('alt');
        $("#fotoDetail .navigation .images").append("<img src='images/btnPictItemInact.gif' id='iconImg" + i + "' alt='" + altValue + "' />");
        count++;
    });

    var navigationWidth = 52 + (count * 11);
    var marginLeft = Math.floor((640 - navigationWidth) / 2);

    $("#fotoDetail .navigation").css('margin-left', marginLeft);

    $("#navPrevious").hover(
      function() {
          $(this).css('cursor', 'pointer');
      },
      function() { }
    );
    $("#navPrevious").bind("click", function() {
        gotoPhotoalbum('previous');
    });

    $("#navNext").hover(
      function() {
          $(this).css('cursor', 'pointer');
      },
      function() { }
    );
    $("#navNext").bind("click", function() {
        gotoPhotoalbum('next');
    });

}

function gotoPhotoalbum(mode) {

    var activeId = $("#fotoDetail .navigation .images .active").attr('id');
    var imageLength = $("#fotoDetail .navigation .images img").length;
    activeId = parseInt(activeId.replace('iconImg', ''));

    switch (mode) {
        case 'previous':
            newActiveId = (activeId + imageLength - 1) % imageLength;
            break;
        case 'next':
            newActiveId = (activeId + 1) % imageLength;
            break;
    }

    var newSrc = $("#fotoOverzicht a:eq(" + newActiveId + ")").attr('href');
    var newAlt = $("#fotoOverzicht img:eq(" + newActiveId + ")").attr('alt');

    $("#fotoDetail .thumbItemLarge img").attr('src', newSrc).attr('alt', newAlt);
    $("#fotoDetail h1").html(newAlt).removeClass("sIFR-replaced");
    //doSifr();
    Cufon.refresh();

    $("#fotoDetail .navigation .images img").attr('src', 'images/btnPictItemInact.gif').removeClass('active');
    $("#fotoDetail .navigation .images img:eq(" + newActiveId + ")").attr('src', 'images/btnPictItemAct.gif').addClass('active');
}

$(document).ready(function() {
    $(document).pngFix();
    //doSifr();
    Cufon.refresh();

    /* photoalbum page specific */
    if ($("#pagePhotoalbum").length > 0) {
        $(".thumbItem").hover(
		    function() {
		        $(this).css('cursor', 'pointer');
		        $(this).addClass('act');
		    },
		    function() {
		        $(this).removeClass('act');
		    }
	    );

        setNavigationPhotoalbum();

        $(".innerCnt .thumbItem").click(function() {
            var thisId = $(this).attr('id');
            var locationHref = $("#" + thisId + ' a').attr('href');
            var altValue = $('#' + thisId + ' img').attr('alt');

            $(".thumbItemLarge img").attr('src', locationHref);

            $("#fotoDetail .navigation .images img").attr('src', 'images/btnPictItemInact.gif');
            $("#fotoDetail .navigation .images img").removeClass('active');
            $("#fotoDetail .navigation .images img:eq(" + (thisId.replace('img', '') - 1) + ")").attr('src', 'images/btnPictItemAct.gif');
            $("#fotoDetail .navigation .images img:eq(" + (thisId.replace('img', '') - 1) + ")").addClass('active');
            $("#fotoDetail h1").html(altValue).removeClass("sIFR-replaced");

            $("#fotoOverzicht").hide();
            $("#fotoDetail").show();
            //doSifr();
            Cufon.refresh();
            return false;
        });

        $("#backbutton").click(function() {
            $("#fotoDetail").hide();
            $("#fotoOverzicht").show();
        });
        /* homepage specific */
    } else if ($("#pageHome").length > 0) {
        setImgTopMargin();
        setNavigationHome();
    }

    //go tohomepage
    $("#menu").click(function() { window.location.href = "/index.html"; });

    //cufon init
    Cufon.replace('h1', { hover: true });
    Cufon.replace('h2', { hover: true });
    Cufon.replace('h3', { hover: true });
    Cufon.replace('#imgHome .navigation .text', {hover: true});
});
