function FotoSjop()
{
    this.homeDisplayElement = null;
    this.homeDisplayItems = null;
    this.homeDisplayImageFront = null;
    this.homeDisplayImageBack = null;
    this.homeDisplayArrow = null;
    this.currentItemIndex = null;
    this.isPlaying = false;
    this.playStepDelay = 2000;
    this.animationSpeed = 1000;

    this.tabActivators = null;
    this.tabElements = null;
    this.currentTabIndex = -1;

    this.setupHomeDisplay = function(){
        if( (this.homeDisplayElement = $("homedisplay")) == null )
        {
            return;
        }

        this.homeDisplayItems = Array();
        this.currentItemIndex = -1;

        this.homeDisplayImageBack = new Element("div");
        this.homeDisplayImageBack.addClass("bigimage");
        this.homeDisplayImageBack.inject(this.homeDisplayElement);

        this.homeDisplayImageFront = new Element("div");
        this.homeDisplayImageFront.addClass("bigimage");
        this.homeDisplayImageFront.inject(this.homeDisplayElement);
        this.homeDisplayImageFront.fx = new Fx.Tween(this.homeDisplayImageFront,{
            wait: false,
            onComplete: function(){
                this.instance.homeDisplayImageFront.setStyle("background-image",this.instance.homeDisplayImageBack.getStyle("background-image"));
            },
            duration: this.animationSpeed
        });
        this.homeDisplayImageFront.fx.instance = this;

        this.homeDisplayArrow = new Element("div");
        this.homeDisplayArrow.addClass("arrow");
        this.homeDisplayArrow.fx = new Fx.Tween(this.homeDisplayArrow,{
            wait: false,
            duration: this.animationSpeed
        });
        this.homeDisplayArrow.inject(this.homeDisplayElement);

        var items = this.homeDisplayElement.getElements("dl dd");
        for( var i = 0; i < items.length; i++ )
        {
            var img = items[i].getElement("img");
            img.dispose();

            var preloader = new Image();
            preloader.src = img.src;
            
            this.homeDisplayItems[this.homeDisplayItems.length] = Array(items[i],img.getAttribute("src"),preloader);

            items[i].instance = this;
            items[i].itemIndex = this.homeDisplayItems.length-1;
            items[i].addEvent("mouseenter",function(){
                this.instance.itemMouseOver(this.itemIndex);
            });
            items[i].addEvent("mouseleave",function(){
                this.instance.itemMouseOut(this.itemIndex);
            });
        }

        this.showItem(0);
        this.onPlayInterval.periodical(this.playStepDelay+this.animationSpeed,this);
        this.play();
    }

    this.itemMouseOver = function(itemIndex){
        this.pause();
        this.showItem(itemIndex);
    }

    this.itemMouseOut = function(itemIndex){
        this.play();
    }

    this.onPlayInterval = function(){
        if( this.isPlaying == false )
        {
            return;
        }

        var newItemIndex = this.currentItemIndex + 1;

        if( newItemIndex >= this.homeDisplayItems.length )
        {
            newItemIndex = 0;
        }

        this.showItem(newItemIndex);
    }

    this.play = function(){
        this.isPlaying = true;
    }

    this.pause = function(){
        this.isPlaying = false;
    }

    this.showItem = function(itemIndex){
        if( this.currentItemIndex > -1 )
        {
            this.homeDisplayImageFront.fx.set("opacity",1);
            this.homeDisplayImageBack.setStyle("background-image","url('" + this.homeDisplayItems[itemIndex][1] + "')");
            this.homeDisplayImageFront.fx.start("opacity",[1,0]);
            this.homeDisplayItems[this.currentItemIndex][0].removeClass("active");
        }
        else
        {
            // Er is nog geen vorige afbeelding
            this.homeDisplayImageFront.setStyle("background-image","url('" + this.homeDisplayItems[itemIndex][1] + "')");
        }
        this.currentItemIndex = itemIndex;
        this.homeDisplayItems[itemIndex][0].addClass("active");

        var top = this.currentItemIndex*74 + 15;

        this.homeDisplayArrow.fx.start("top",top);
    }

    this.setupTabs = function(){

        this.tabActivators = Array();
        this.tabElements = Array();
        this.currentTabIndex = -1;

        var tabsContainer = $(document.body).getElement("div.tabs");
        if( tabsContainer == null )
        {
            return;
        }
        
        var activators = tabsContainer.getElements("a.tabactivator");

        for( var i = 0; i < activators.length; i++ )
        {
            var tabElement = $(document.body).getElement("div#" + activators[i].getAttribute("rel"));
            if( tabElement != null )
            {
                this.tabActivators[this.tabActivators.length] = activators[i];
                this.tabElements[this.tabElements.length] = tabElement;

                if( activators[i].hasClass("currenttab") == true )
                {
                    this.currentTabIndex = this.tabActivators.length-1;
                }
                else
                {
                    tabElement.setStyle("display","none");
                }

                activators[i].instance = this;
                activators[i].tabElementIndex = this.tabActivators.length-1;
                activators[i].addEvent("click",function(){
                    this.instance.openTab(this.tabElementIndex);
                    return false;
                });
            }
        }
    }

    this.openTab = function(tabIndex){
        if( this.currentTabIndex != -1 )
        {
            this.tabActivators[this.currentTabIndex].removeClass("currenttab")
            this.tabElements[this.currentTabIndex].setStyle("display","none");
        }
        this.tabElements[tabIndex].setStyle("display","block");
        this.tabActivators[tabIndex].addClass("currenttab")

        this.currentTabIndex = tabIndex;
    }

    this.setupContactbar = function(){

        var bar = $(document.body).getElements("div.contactbar");
        for( var i = 0; i < bar.length; i++ )
        {
            var contactBarOpener = bar[i].getElement("span");
            var contactBarForm = bar[i].getElement("form");

            contactBarOpener.addEvent("click",function(){
                if( this.fx == null )
                {
                    this.fx = new Fx.Tween(this.getParent());
                }
                if( this.hasClass("opened") == false )
                {
                    this.fx.start("height",82);
                    this.addClass("opened");
                }
                else
                {
                    this.fx.start("height",42);
                    this.removeClass("opened");
                }
            });

            contactBarForm.onsubmit = function(){
                return false;
            }

            var submitButton = contactBarForm.getElement("button");
            submitButton.inputField = contactBarForm.getElement("input[name=phonenumber]");

            submitButton.instance = this;
            submitButton.element = bar[i];
            submitButton.addEvent("click",function(){
                this.disabled = true;
                if( this.instance.contactBarTelephoneSubmit(this.inputField.value,this.element) == false )
                {
                    this.inputField.focus();
                }
                this.disabled = false;
            });
        }
    }

    this.contactBarTelephoneSubmit = function(telephoneNumber,element){

        if( (telephoneNumber = telephoneNumber.trim()) < 10 )
        {
            alert("Vul aub uw telefoonnummer in.");
            return false;
        }

        var contactBarForm = element.getElement("form");
        var contactBarMessage = element.getElement("div.message");
        var contactBarButton = contactBarForm.getElement("button");

        contactBarButton.addClass("busy");
        contactBarButton.disabled = true;

        var request = new Request.JSON({
            url: "/contact/phonebar",
            onSuccess: function(jsonData){

                if( jsonData.result != "OK" )
                {
                    alert("Kon gegevens niet inladen. Probeer het aub nogmaals. (" + jsonData.result + ")");
                }
                else
                {
                    // OK
                    this.contactBarMessage.innerHTML = "Het formulier is verzonden. Wij nemen zo spoedig mogelijk contact met u op.";

                    this.contactBarForm.fx = new Fx.Tween(contactBarForm);
                    this.contactBarMessage.fx = new Fx.Tween(contactBarMessage);

                    this.contactBarForm.fx.start("height",0);
                    this.contactBarMessage.fx.start("height",38);
                }
            },
            onFailure: function(instance){
                alert("Kon gegevens niet inladen. Probeer het aub nogmaals. (1)");
            },
            onException: function(headerName,value){
                alert("Kon gegevens niet inladen. Probeer het aub nogmaals. (2)");
            },
            onComplete: function(){
                this.contactBarButton.removeClass("busy");
                this.contactBarButton.disabled = false;
            }
        });

        request.contactBarForm = contactBarForm;
        request.contactBarMessage = contactBarMessage;
        request.contactBarButton = contactBarButton;

        var sendArg = new Object();
        sendArg.telephone = telephoneNumber;
        request.send("jsonData=" + JSON.encode(sendArg));

        return true;
    }

    this.setupContactFormSubmit = function(){
        
        var contactForm = $(document.body).getElement("form.contactform");

        if( contactForm == null )
        {
            return;
        }

        contactForm.fotosjopInstance = this;
        contactForm.addEvent("submit",function(){
            return this.fotosjopInstance.onContactFormSubmit();
        });
    }

    this.onContactFormSubmit = function(){

        var question = $(document.body).getElement("form.contactform textarea[name=question]");
        var name = $(document.body).getElement("form.contactform input[name=name]");
        var email = $(document.body).getElement("form.contactform input[name=email]");
        var send = $(document.body).getElement("form.contactform input[name=send]");


        if( (question.value = question.value.trim()).length < 4 )
        {
            alert("Vul aub uw vraag en/of opmerking in.");
            question.focus();
            return false;
        }

        if( (name.value = name.value.trim()).length < 2 )
        {
            alert("Vul aub uw naam in.");
            name.focus();
            return false;
        }

        if(
            ( (email.value = email.value.trim()).length < 5 ) ||
            ( email.value.indexOf("@") == -1 ) ||
            ( email.value.indexOf(".") == -1 )
        )
        {
            alert("Vul aub uw e-mailadres in.");
            email.focus();
            return false;
        }

        send.value = "Yes";

        return true;
    }

    this.onLoad = function(){
        this.setupHomeDisplay();
        this.setupTabs();
        this.setupContactbar();
        //this.setupContactFormSubmit();
    }
}

var fotoSjop = new FotoSjop();

window.addEvent("domready",function(){
    fotoSjop.onLoad();
});
