function fireEvent(elID) {	
	/*showPortfolioItem(elID);
	clearInterval(portfolioTimer);*/
	alert(elID);
}

function resetHeader() {
    $("#header1").fadeIn(300);
    $("#header2").fadeOut(300);
    $("#header3").fadeOut(300);
}

function scrollPortfolio() {
    if (activePortfolioItem == $(".jcarousel-item").length - 4 || activePortfolioItem == lastPortfolioItem) {
        scriptedClickEvent = true;
        $(".jcarousel-next").dblclick();
        scriptedClickEvent = false;
    } else {
        showPortfolioItem(activePortfolioItem+1);
    }         
}

function scrollPortfolioBack() {
    if (activePortfolioItem == 1) {
        // end
    } else if (activePortfolioItem == 5) {
        scriptedClickEvent = true;
        $(".jcarousel-prev").dblclick();
        scriptedClickEvent = false;
    } else {
        showPortfolioItem(activePortfolioItem-1);
    }         
}

var scriptedClickEvent = false;
function portfolio_initCallback(carousel) {
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        //carousel.startAuto(0);
        if ( scriptedClickEvent == false ) {
            clearInterval(portfolioTimer);
            scrollPortfolio();
        }
    });

    carousel.buttonPrev.bind('click', function() {
        //carousel.startAuto(0);
        clearInterval(portfolioTimer);
        scrollPortfolioBack();
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    /*carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });*/
}

function portfolio_itemFirstInCallback(carousel, item, idx, state) {
    showPortfolioItem(idx);
};

var lastPortfolioItem;
function portfolio_itemLastInCallback(carousel, item, idx, state) {
    lastPortfolioItem = idx;
};

var activePortfolioItem = 1;
function showPortfolioItem(nr) {
    
    activePortfolioItem = nr;
    
    $("#portfolioHighlight img").hide();
    $("#portfolioHighlight .url").hide();
    $("#portfolioHighlight .slogan").hide();
    $("#portfolioHighlight .desc").hide();

    $("#portfolioHighlight img").attr(
        "src", 
        $("#portfolio-item-" + nr + " img").attr("src")
    );
    $("#portfolioHighlight .url").html(
        $("#portfolio-item-" + nr + " .url").html()
    );
    $("#portfolioHighlight .slogan").html(
        $("#portfolio-item-" + nr + " .slogan").html()
    );
    $("#portfolioHighlight .desc").html(
        $("#portfolio-item-" + nr + " .desc").html()
    );

    $("#portfolioHighlight img").fadeIn();
    $("#portfolioHighlight .url").fadeIn();
    $("#portfolioHighlight .slogan").fadeIn();
    $("#portfolioHighlight .desc").fadeIn();
    
    // Highlight: 
    
    $(".jcarousel-item img").each( function() {
        $(this).css("opacity",  "0.5");
        $(this).css("filter",  "alpha(opacity=50)");
    });
    
    $("#portfolio-item-" + nr + " img").css("opacity",  "1.0");
    $("#portfolio-item-" + nr + " img").css("filter",  "alpha(opacity=100)");
    
}

var portfolioTimer;

$(document).ready(function() {

            // Sidebar ticker: 
            
            var headline_count;
            var headline_interval;
            var old_headline = 0;
            var current_headline=0;
            
            $(document).ready(function(){
              headline_count = $(".sidebarHeadline").size();
              $(".sidebarHeadline:eq("+current_headline+")").css('top','5px');
              
              headline_interval = setInterval(headline_rotate,5000); //time in milliseconds
              $('#sidebarTicker').hover(function() {
                clearInterval(headline_interval);
              }, function() {
                headline_interval = setInterval(headline_rotate,5000); //time in milliseconds
                headline_rotate();
              });
            });
            
            function headline_rotate() {
              current_headline = (old_headline + 1) % headline_count; //remainder will always equal old_headline until it reaches headline_count - at which point it becomes zero. clock arithmetic
              $(".sidebarHeadline:eq(" + old_headline + ")").animate({top: -205},"slow", function() {
                $(this).css('top','210px');
                });
              $(".sidebarHeadline:eq(" + current_headline + ")").show().animate({top: 5},"slow");  
              old_headline = current_headline;
            }
            
            // Image fading:
            
            $("#header2").hide();
            $("#header3").hide();
            $("#header2").css("left", "0");
            $("#header3").css("left", "0");
            
            $(".interactive1").mouseover(function(){
                $("#header1").fadeIn(300);
                $("#header2").fadeOut(300);
                $("#header3").fadeOut(300);
            });
            
            $(".interactive2").mouseover(function(){            
                $("#header1").fadeOut(300);
                $("#header2").fadeIn(300);
                $("#header3").fadeOut(300);
                
              
            });
            
            $(".interactive3").mouseover(function(){
                $("#header1").fadeOut(300);
                $("#header2").fadeOut(300);
                $("#header3").fadeIn(300);
            });
            
            $(".interactive1").mouseout(function(){
                resetHeader();
            });
            $(".interactive2").mouseout(function(){
                resetHeader();
            });
            $(".interactive3").mouseout(function(){
                resetHeader();
            });
            
            // Input helper:
            
            $(".autoKundennummer").focus(function() {
                    if ( $(this).attr("value") == "Ihre Kundennummer" ) 
                        $(this).attr("value", "");
            });
            
            $(".autoPasswort").focus(function() {
                    if ( $(this).attr("value") == "Ihr Passwort" ) 
                        $(this).attr("value", "");
            });
            
            $(".autoMail").focus(function() {
                    if ( $(this).attr("value") == "Ihre E-Mail-Adresse" ) 
                        $(this).attr("value", "");
            });
            
            $(".autoWebsite").focus(function() {
                    if ( $(this).attr("value") == "Ihre Webseite" ) 
                        $(this).attr("value", "");
            });
            
            // Submenu:
            
            if ( $("body").attr("class") != "leistungen" ) {   
                $("#menuServices").mousemove(function() {
                    $("#outerSubmenu").css("background-position", "0 0px");
                    $("#submenuServices").css("display", "inline");
                });
                $("#menu li").mousemove(function() {
                    if ( $(this).attr("id") != "menuServices" ) {
                        $("#submenuServices").css("display", "none");
                        $("#outerSubmenu").css("background-position", "0 -23px");
                    }
                });
                $("body").mousemove(function(e) {
                    if ( e.pageY > 110 || e.pageY < 30) {
                        $("#submenuServices").css("display", "none");
                        $("#outerSubmenu").css("background-position", "0 -23px");
                    }
                });
            }
            
            // Lang buttons:
            
            $("#infoLanguage li").mouseover(function() {
                $(this).css("background-position", "0px -12px");
            });
            
            $("#infoLanguage li").mouseout(function() {
                $(this).css("background-position", "0px 0px");
            });
            
            // Customer sidebar: 
            
            $("#sidebarCustomerMenu li").click(function() {
                if ($(this).attr('class') != 'menuHead' && $(this).attr('class') != 'menuEnd') 
                document.location = $(this).children(':first').attr('href');
            });
            
            $("#sidebarCustomerMenu li").each(function() {
                if ( document.location.href.indexOf($(this).children(':first').attr('href'), 0) >= 0 ) {
                    $(this).attr('class', 'activeElement');
                }  
            });
            
            // Portfolio:

            jQuery('#portfolio').jcarousel({
                scroll: 1, 
                wrap: 'last',
                initCallback: portfolio_initCallback,
                itemFirstInCallback: portfolio_itemFirstInCallback, 
                itemLastInCallback: portfolio_itemLastInCallback,
                buttonNextEvent: "dblclick",
                buttonPrevEvent: "dblclick"
            });
            
            $(".jcarousel-item").click(function() {
               showPortfolioItem(
                   $(this).attr("jcarouselindex")
               );
               clearInterval(portfolioTimer);
            });
            
            portfolioTimer = setInterval(scrollPortfolio, 4000);
            
            // Service Bullets: 
            
            $(".serviceBullet").mouseover(function() {
               $(this).css("opacity",  "1.0");
               $(this).css("filter",  "alpha(opacity=100)");
            });
            
            $(".serviceBullet").mouseout(function() {
                if ($(this).is(".activeBullet") == false) {
                    $(this).css("opacity",  "0.6");
                    $(this).css("filter",  "alpha(opacity=60)");
                }
            });
            
        });