var BrandVitals = new Class({

    twitterScreenName: "BrandVitals",

    onDomReady: function(){
        this.fixToolDownloadAnchors();
        this.fixLastTweet();
        this.fixCecileScreenBooster();
        this.fixBollen();
        this.fixTools();
        this.fixForwardEmail();
        MediaView.updateInstances();
    },
    formContactPageSubmit: function(form){

        FormNoticeElement.hideElementNotice();

        form = $(form);

        var name = form.getElement("input[name=name]");
        var company = form.getElement("input[name=company]");
        var email = form.getElement("input[name=email]");
        var comments = form.getElement("textarea[name=comments]");
        var submitButton = form.getElement("input[type=submit]");
        var sendForm = form.getElement("input[name=sendForm]");

        if( (name.value = name.value.trim()).length < 2 )
        {
            FormNoticeElement.showElementNotice(name,"Vul a.u.b. uw naam in.");
            name.focus();
            return false;
        }

        if( (company.value = company.value.trim()).length < 2 )
        {
            FormNoticeElement.showElementNotice(company,"Vul a.u.b. uw bedrijfsnaam in.");
            company.focus();
            return false;
        }

        if(
            ( (email.value = email.value.trim()).length < 5 ) ||
            ( email.value.indexOf("@") == -1 ) ||
            ( email.value.indexOf(".") == -1 )
        )
        {
            FormNoticeElement.showElementNotice(email,"Vul a.u.b. uw e-mail adres in.");
            email.focus();
            return false;
        }

        if( (comments.value = comments.value.trim()).length < 2 )
        {
            FormNoticeElement.showElementNotice(comments,"Vul a.u.b. uw bericht in.");
            comments.focus();
            return false;
        }

        submitButton.setStyle("visibility","hidden");

        sendForm.value = "True";

        return true;
    },
    formBlogCommentSubmit: function(form){
        FormNoticeElement.hideElementNotice();

        form = $(form);

        var name = form.getElement("input[name=name]");
        var comments = form.getElement("textarea[name=comments]");
        var submitButton = form.getElement("input[type=submit]");
        var sendForm = form.getElement("input[name=sendForm]");

        if( (name.value = name.value.trim()).length < 2 )
        {
            FormNoticeElement.showElementNotice(name,"Vul a.u.b. uw naam in.");
            name.focus();
            return false;
        }

        if( (comments.value = comments.value.trim()).length < 2 )
        {
            FormNoticeElement.showElementNotice(comments,"Vul a.u.b. uw reactie in.");
            comments.focus();
            return false;
        }

        submitButton.setStyle("visibility","hidden");

        sendForm.value = "True";

        return true;
    },
    fixToolDownloadAnchors: function(){
        var anchors = $$("a.toolDownload");

        for( var i = 0; i < anchors.length; i++ )
        {
            anchors[i].addEvent("click",this.onToolDownloadAnchorClick.bind(this));
        }
    },
    onToolDownloadAnchorClick: function(e){
        e = new Event(e);
        var el = $(e.target);

        while(( el.get("tag") != "a" ) && ( el != null ))
        {
            el = el.getParent();
        }

        if( el == null )
        {
            return true;
        }

        var obtainType = parseInt(el.get("obtainType"));

        if(
            ( obtainType == BrandVitals.OBTAIN_TYPE_FREE ) ||
            ( obtainType == BrandVitals.OBTAIN_TYPE_LOGIN )
        )
        {
            return true;
        }
        else if(
            ( obtainType == BrandVitals.OBTAIN_TYPE_FORM ) ||
            ( obtainType == BrandVitals.OBTAIN_TYPE_IDEAL )
        )
        {
            e.preventDefault();
            var popup = new HTMLPopup(el.get("href"),564,380);
            popup.open();
            return;
        }
    },

	formDownloadToolIdealSubmit: function(form){

        FormNoticeElement.hideElementNotice();

        form = $(form);

        var popup = form;
        while(( popup != null ) && ( popup.hasClass("HTMLPopup") == false ))
        {
            popup = popup.getParent();
        }

        var first = form.getElement("input[name=first]");
        var company = form.getElement("input[name=company]");
        /*var tel = form.getElement("input[name=tel]");*/
        var last = form.getElement("input[name=last]");
        var street = form.getElement("input[name=street]");
        var houseNumber = form.getElement("input[name=houseNumber]");
        var postalCode = form.getElement("input[name=postalCode]");
        var city = form.getElement("input[name=city]");
        var email = form.getElement("input[name=email]");
        var newsletter = form.getElement("input[name=newsletter]");
        var submitButton = form.getElement("input[name=submitDownload]");
        var sendForm = form.getElement("input[name=sendForm]");

		var strippedPostalCode = this.ignoreSpaces(postalCode.value);

        if( (first.value = first.value.trim()).length < 2 )
        {
            FormNoticeElement.showElementNotice(first,"Vul a.u.b. uw voornaam in.");
            first.focus();
            return false;
        }

        if( (last.value = last.value.trim()).length < 2 )
        {
            FormNoticeElement.showElementNotice(last,"Vul a.u.b. uw achternaam in.");
            last.focus();
            return false;
        }

		if( (street.value = street.value.trim()).length < 2 )
        {
            FormNoticeElement.showElementNotice(street,"Vul a.u.b. uw straatnaam in.");
            street.focus();
            return false;
        }

		if( (houseNumber.value = houseNumber.value.trim()).length < 1 )
        {
            FormNoticeElement.showElementNotice(houseNumber,"Vul a.u.b. uw huisnummer in.");
            houseNumber.focus();
            return false;
        }

		if( (strippedPostalCode = strippedPostalCode.trim()).length != 6 )
        {
            FormNoticeElement.showElementNotice(postalCode,"Vul a.u.b. uw postcode zonder spaties in.");
            postalCode.focus();
            return false;
        }

		if( (city.value = city.value.trim()).length < 2 )
        {
            FormNoticeElement.showElementNotice(city,"Vul a.u.b. uw woonplaats in.");
            city.focus();
            return false;
        }

        if(
            ( (email.value = email.value.trim()).length < 5 ) ||
            ( email.value.indexOf("@") == -1 ) ||
            ( email.value.indexOf(".") == -1 )
        )
        {
            FormNoticeElement.showElementNotice(email,"Vul a.u.b. uw e-mailadres in.");
            email.focus();
            return false;
        }

//        if( (tel.value = tel.value.trim()).length < 10 )
//        {
//            FormNoticeElement.showElementNotice(tel,"Vul a.u.b. uw telefoonnummer in.");
//            tel.focus();
//            return false;
//        }

        sendForm.value = "True";

        submitButton.setStyle("display","none");

        if( popup != null )
        {
            popup.HTMLPopupInstance.close.delay(1000,popup.HTMLPopupInstance);
        }

        return true;
    },

    formDownloadToolFormSubmit: function(form){

        FormNoticeElement.hideElementNotice();

        form = $(form);

        var popup = form;
        while(( popup != null ) && ( popup.hasClass("HTMLPopup") == false ))
        {
            popup = popup.getParent();
        }

        var first = form.getElement("input[name=first]");
        var company = form.getElement("input[name=company]");
        /*var tel = form.getElement("input[name=tel]");*/
        var last = form.getElement("input[name=last]");
        var email = form.getElement("input[name=email]");
        var newsletter = form.getElement("input[name=newsletter]");
        var submitButton = form.getElement("input[name=submitDownload]");
        var sendForm = form.getElement("input[name=sendForm]");

        if( (first.value = first.value.trim()).length < 2 )
        {
            FormNoticeElement.showElementNotice(first,"Vul a.u.b. uw voornaam in.");
            first.focus();
            return false;
        }

        if( (last.value = last.value.trim()).length < 2 )
        {
            FormNoticeElement.showElementNotice(last,"Vul a.u.b. uw achternaam in.");
            last.focus();
            return false;
        }

        if(
            ( (email.value = email.value.trim()).length < 5 ) ||
            ( email.value.indexOf("@") == -1 ) ||
            ( email.value.indexOf(".") == -1 )
        )
        {
            FormNoticeElement.showElementNotice(email,"Vul a.u.b. uw e-mailadres in.");
            email.focus();
            return false;
        }

//        if( (tel.value = tel.value.trim()).length < 10 )
//        {
//            FormNoticeElement.showElementNotice(tel,"Vul a.u.b. uw telefoonnummer in.");
//            tel.focus();
//            return false;
//        }

        sendForm.value = "True";

        submitButton.setStyle("display","none");

        if( popup != null )
        {
            popup.HTMLPopupInstance.close.delay(1000,popup.HTMLPopupInstance);
        }

        return true;
    },
    fixLastTweet: function(){
        var lastTweetElement = document.id("lastTweet");
        if( lastTweetElement == null )
        {
            return;
        }

        lastTweetElement.set("text","Bezig met laden...");

        var request = new Request.Twitter(
            this.twitterScreenName,
            {
                data: {
                    screen_name: this.twitterScreenName,
                    count: 1
                },
                onComplete: this.fixLastTweetOnComplete.bind(this)
            }
        );
        request.send();
    },
    fixLastTweetOnComplete: function(data){
        if( data.length > 0 )
        {
            document.id("lastTweet").set("html",data[0].text);
        }
        else
        {
            document.id("lastTweet").set("html","Geen tweets beschikbaar voor @" + this.twitterScreenName + ".");
        }
    },
    fixCecileScreenBooster: function(){
        this.cecileScreenBooster = $(document.body).getElement("div.cecileScreenBooster");
        if( this.cecileScreenBooster == null )
        {
            return;
        }
        this.cecileScreenBooster.fx = new Fx.Tween(this.cecileScreenBooster);
        this.cecileScreenBooster.addEvent("click",this.onCecileScreenBoosterClick.bind(this));
    },
    onCecileScreenBoosterClick: function(e){
        this.cecileScreenBooster.fx.start("opacity",0);
        ScreenBooster.load(321);
    },
    fixBollen: function(){
        var bollen = document.id("bollen");
        if( bollen == null )
        {
            return;
        }

        var anchors = bollen.getElements("a");
        for( var i = 0; i < anchors.length; i++ )
        {
            anchors[i].bollen = bollen;
            anchors[i].addEvent("mouseenter",function(){
                this.bollen.addClass(this.className);
            });
            anchors[i].addEvent("mouseout",function(){
                this.bollen.removeClass(this.className);
            });
        }

    },
    fixTools: function(){
        var tools = $(document.body).getElements("div#overviewDownload ol li div.item");
        for( var i = 0; i < tools.length; i++ )
        {
            var next = tools[i].getNext();
            if(( next.get("tag") != "div" ) || ( next.hasClass("summary") == false ))
            {
                continue;
            }
            next.requiredHeight = next.getHeight();
            next.setStyle("height",0);
            next.item = tools[i];

            tools[i].next = next;
            tools[i].next.fx = new Fx.Tween(tools[i].next,{
                link: "cancel",
                onComplete: function(el){
                    if( el.getHeight() == 0 )
                    {
                        el.item.removeClass("open");
                    }
                }
            });
            tools[i].addEvent("click",function(){
                if( this.next.getHeight() == 0 )
                {
                    this.addClass("open");
                    this.next.fx.start("height",this.next.requiredHeight);
                }
                else
                {
                    //this.removeClass("open");
                    this.next.fx.start("height",0);
                }
            });
        }
    },

	ignoreSpaces: function(string) {
		var temp = "";
		string = '' + string;
		splitstring = string.split(" ");
		for(i = 0; i < splitstring.length; i++)
		temp += splitstring[i];
		return temp;
		},
	fixForwardEmail: function(){
        var anchors = $(document.body).getElements("a.forwardEmail");
        for( var i = 0; i < anchors.length; i++ )
        {
            anchors[i].addEvent("click",function(e){

                e = new Event(e);
                var el = $(e.target);

                while(( el.get("tag") != "a" ) && ( el != null ))
                {
                    el = el.getParent();
                }

                if( el == null )
                {
                    return true;
                }

                e.preventDefault();
                var popup = new HTMLPopup(el.get("href"),564,302);
                popup.open();
            });
        }
    },
    blogForwardEmailFormSubmit: function(form){

        FormNoticeElement.hideElementNotice();

        form = $(form);

        var recipientName = form.getElement("input[name=recipientName]");
        var recipientEmail = form.getElement("input[name=recipientEmail]");
        var senderName = form.getElement("input[name=senderName]");
        var senderEmail = form.getElement("input[name=senderEmail]");

        if( (recipientName.value = recipientName.value.trim()).length < 2 )
        {
            FormNoticeElement.showElementNotice(recipientName,"Vul a.u.b. de naam van de ontvanger in.");
            recipientName.focus();
            return false;
        }

        if(
            ( (recipientEmail.value = recipientEmail.value.trim()).length < 5 ) ||
            ( recipientEmail.value.indexOf("@") == -1 ) ||
            ( recipientEmail.value.indexOf(".") == -1 )
        )
        {
            FormNoticeElement.showElementNotice(recipientEmail,"Vul a.u.b. het e-mailadres van de ontvanger.");
            recipientEmail.focus();
            return false;
        }



        if( (senderName.value = senderName.value.trim()).length < 2 )
        {
            FormNoticeElement.showElementNotice(senderName,"Vul a.u.b. uw naam in.");
            senderName.focus();
            return false;
        }

        if(
            ( (senderEmail.value = senderEmail.value.trim()).length < 5 ) ||
            ( senderEmail.value.indexOf("@") == -1 ) ||
            ( senderEmail.value.indexOf(".") == -1 )
        )
        {
            FormNoticeElement.showElementNotice(senderEmail,"Vul a.u.b. uw e-mail adres in.");
            senderEmail.focus();
            return false;
        }

        return true;
    }
});
BrandVitals.OBTAIN_TYPE_FREE = 1;
BrandVitals.OBTAIN_TYPE_FORM = 2;
BrandVitals.OBTAIN_TYPE_IDEAL = 3;
BrandVitals.OBTAIN_TYPE_LOGIN = 4;

Cufon.replace('h1, h2, ol#menu li a, div#homeTop div.right div, div#homeTop div.left > div div.socialMediaItem, div#homeTop div.left > div div.linkedin, div#homeMiddle div.right div strong, div#homeMiddle div.right div span, div#homeMiddle div.left form legend, div#pageContent div.left ol li a, ol#overviewPage li a, div#overviewDownload ol li a, form#download legend, em.heading1', {
	hover: true
});

var brandVitals = new BrandVitals();

window.addEvent("domready",function(){
    brandVitals.onDomReady();
});
