var como = como || {};
como.homeBgExpanded = false;
como.homeMouseOver = false;
como.homeAutoOrderIndex = 0;
como.homeAutoOrder = [0,7,5,3,2,6,1,8];

como.autoShowHome = function() {
    if (como.homeMouseOver) {
        como.autoShowHome.defer(3000);
        return;
    }

    var entryIndex = como.homeAutoOrder[como.homeAutoOrderIndex];

    //fade out last item
    $("#imagecontent .currentItem").removeClass("currentItem").animate({
        opacity: 0
    }, 1000, 'easeInOutQuad');

    //fade in new item
    var entryToFadeIn = $("#imagecontent").children()[entryIndex];
    $(entryToFadeIn).find(".entry_content").addClass("currentItem").animate({
        opacity: 1
    }, 1000, 'easeInOutQuad');


    como.homeAutoOrderIndex++;
    if (como.homeAutoOrderIndex > 8) {
        como.homeAutoOrderIndex = 0;
    }

    como.autoShowHome.defer(2000);
};


como.setupStartState = function() {
    $(".home_gallery_entry_empty .entry_content").animate({
        opacity: 1
    }, 1000, 'easeInOutQuad', function() {
        $(".home_gallery_entry_empty").addClass("forceVisible");
    });

//    $(".start .box_body .inner h2").animate({
//        opacity: 0
//    }, 1000, 'easeInOutQuad')

};

como.setupHomeGallery = function() {
    if ($("#body_1").length == 0) {
        return; //only on home page
    }


    $("#maincontent").addClass("start");

    como.resizeWrapper();
    $(window).resize(como.resizeWrapper);

    var entriesContents = $(".home_gallery_entry .entry_content");
    entriesContents.each(como.addBackgroundSlogan);

    entriesContents.click(function() {

        como.setupStartState();


        if (!$(this).hasClass("currentItem")) {
            //fade out last item
            $("#imagecontent .currentItem").removeClass("currentItem").animate({
                opacity: 0
            }, 1000, 'easeInOutQuad');

            $(this).addClass("currentItem");
        }


        var html = $(this).parent().find(".entry_content_sidebar").html();

        if (!como.homeBgExpanded) {
            como.homeBgExpanded = true;

            $("#maincontent").animate({opacity:0}, 600, "easeInOutCubic", function() {
                (function() {
                    $("#maincontent").removeClass("start");
                    $("#maincontent").width(365).html(html).animate({opacity:1}, 600, "easeInOutCubic");
                }).defer(800);
            });

//            $("#kontaktbutton").animate({
//                opacity:1
//            }, 2000, 'easeInOutCubic');


            $("#contentwrapper").animate({
                backgroundPosition:"(494px 65px)"
            }, 2000, 'easeInOutCubic');

        } else {
            $("#maincontent").html(html);
        }
    });

    entriesContents.hover(function() {
        como.homeMouseOver = true;

        if ($(this).hasClass("currentItem")) {
            return;
        }

        //fade out last item
        $("#imagecontent .currentItem").removeClass("currentItem").animate({
            opacity: 0
        }, 350, 'easeInOutQuad');

        $(this).animate({
            opacity: 1
        }, 350, 'easeInOutQuad');

    }, function() {
        como.homeMouseOver = false;

//        $(".entry_content").stop(true);
        $(this).animate({
            opacity: 0
        }, 350, 'easeInOutQuad');

    });

    //flieger
    $("#wrapper").append("<div class='flieger'>&nbsp;</div>");
    $(".flieger").animate({
        left:"5000px"
    }, 25000, 'easeInQuad');

    como.autoShowHome.defer(1000);


    /*   $("#contentwrapper").animate({
     backgroundPosition:"(544px 130px)"
     }, 2000, 'easeInOutCubic');*/
};

como.reAddBackgroundSlogan = function(bgEl, speed, opacity) {
    (function() {
        var winW = $(window).width();
        bgEl.css("left", winW + "px");
        como.animateSlogan(bgEl, speed, opacity);
    }).defer(3000);
};


como.animateSlogan = function(bgEl, speed, opacity) {
    if ($.browser.msie) {
        var version = parseInt($.browser.version, 10);
        if (version == 6 || version == 7) {
            return;
        }


    }
    var targetPos = -bgEl.width();
    var currentPos = bgEl.position().left;
    var dist = currentPos - targetPos;
    if (speed >= 100) {
        speed
    }
    bgEl.animate({/*opacity:opacity, */left:targetPos}, dist * (100 - speed), "linear", como.reAddBackgroundSlogan.createDelegate(this, [bgEl, speed,opacity], 0));

};

como.addBackgroundSlogan = function(idx, el) {
    var entry = $(el);
    var slogan = entry.find("p").html();
    if (jQuery.trim(slogan) == "") {
        entry.parents(".home_gallery_entry").addClass("home_gallery_entry_empty");
        entry.append("<a href='/index.php?id=kontakt'><p>Starten<br>Sie mit<br>uns durch</p></a>");
    } else {
        entry.append("<div class='number number-" + idx + "'>&nbsp;</div>");
    }


    var showBgText = function(data) {
        var posX = data.find(".posX").html();
        var posY = data.find(".posY").html();
        var opacity = data.find(".opacity").html();
        var fontSize = data.find(".fontSize").html();
        var speed = data.find(".speed").html();

        slogan = replaceAll(slogan, "-", "");
        $("#wrapper").append("<p class='bgtext bgtext-" + idx + "'>" + slogan + "</p>");


        speed = speed || 30;
        speed = parseInt(speed);
        var bgEl = $(".bgtext-" + idx);

        bgEl.css({left:posX + "px",top:posY + "px","font-size":fontSize + "px", opacity:opacity});
        como.animateSlogan(bgEl, speed, opacity);

    };


    showBgText(entry.parent().find(".data"));
//    showBgText.defer(idx * 2000, this, [entry.parent().find(".data")]);
};


$(document).ready(function() {
    //same height for both columns
    //como.setColumnHeight();

    //setup print link
    $("#entry_print").show().click(function() {
        window.print();
    });

    //home
    como.setupHomeGallery();

});
