window.addEvent("domready", function() {

    new SmoothScroll({ "links": $$(".scroll") });
    new AlphaPng();

    $$(".volatile").each(function(el) {
        $(el).addEvent("focus", function() {
            if (this.getValue() == this.getProperty("title")) {
                this.value = "";
            }
        });
    });

    $$("a[rel*='external']").each(function(a) {
        $(a).setProperty("target", "_blank");
    });



    $$(".formOptions a, .pageOptions a").each(function(el) {
        new ListItemSelector($(el), { "fxMin": 0.5, "fxMax": 1 });
    });





    // Case Study form
    /*
    if ($("addcompanybutton")) {
    addCompanyBoxes();
    

        $("addcompanybutton").addEvent('click', function(e) {
    new Event(e).stop();
    showingCompBoxes++;
    addCompanyBoxes();
    });

    }
    */


    // Only display extra companies if there are any present otherwise they get hidden.

    if ($("Step1")) {
        hideExtraCompanies();
        $("addcompanybutton").addEvent('click', function(e) {
            if (companiesHidden) {
                var e = new Event(e).stop();
                showExtraCompanies();
            } else {
                var compCookie = Cookie.set('keepopen', '1', { duration: 1 });
            }
        });
    }

  
    
    // Create a new multibox and assign it to the Scalextric object
    this.multibox = new MultiBox('multibox', {
        showControls: false,
        useOverlay: true
    });

    // Catch the close links for multibox popups
    $$(".multiboxClose").each(function(el) {
        el.addEvent("click", function(e) {
            new Event(e).stop();
            window.parent.Scalextric.multibox.close();
        });
    });
    

});

var showingCompBoxes = 4;

function addCompanyBoxes() {
    var counter = 0;
    $$("form fieldset").each(function(el) {
        var isFilled = false;
        var inputBox = el.getElements('input')[0];
        if (inputBox) {
            if (inputBox.value.length>0) {
                isFilled = true;
            }
        }
        if (counter > showingCompBoxes && !isFilled) {
            el.setStyle("display", "none");
        } else {
            el.setStyle("display", "block");
        }
        counter++;
    });
}

var companiesHidden;

function hideExtraCompanies() {

    var compCookie = Cookie.get('keepopen');
    if (!compCookie) {
        for (var x = 1; x <= 100; x++) {
            if ($("Company_" + x)) {
                if ($("CompanyName_" + x).value.length == 0 && $("CompanyShortDescription_" + x).value.length == 0 && !$("CompanyLogoURI_" + x).value) {
                    $("Company_" + x).setStyle("display", "none");
                }
            }
        }
        companiesHidden = 1;
    }
}

function showExtraCompanies() {
    for (var x = 1; x <= 100; x++) {
        if ($("Company_" + x)) {
             $("Company_" + x).setStyle("display", "block");
        }
    }
    companiesHidden = 0;
}