var TellFriend = Class.create({
    initialize: function(){
        this.name = "";
        this.emailValue = "";
        this.email = false;
        this.form = false;
    },
    setTellFriend: function(obj){
        if (obj.tellStatus === 0) {
            this.tellStatus = 0;
            this.name = obj.tellName;
            this.emailValue = obj.tellEmail;
            this.email = true;
        }
    },
    buildHTML: function(){
        var locX = tempX - 400;
        var locY = tempY;
        this.floatDiv = new Div(locY, locX, true);
        var x = '<form method="get" action="javascript:void(0);" onsubmit="TF.checkForm(); return false;" name="formTaf" id="formTaf"><table cellpadding="0" cellspacing="0" border="0"><tr><td class="firstcol">Je naam</td><td class="td"><input type="text" name="je_naam" id="je_naam" class="tb" value="' + this.name + '" /></td></tr><tr><td class="firstcol">Je e-mail</td><td class="td"><input type="text" name="je_e-mail" id="je_e-mail" onblur="TF.checkEmail($(\'je_e-mail\').value);" class="tb" value="' + this.emailValue + '" /></td></tr><tr><td class="firstcol">Naam vriend</td><td class="td"><input type="text" name="naam_vriend" id="naam_vriend" class="tb" value="" /></td></tr><tr><td class="firstcol">E-mail vriend</td><td class="td"><input type="text" name="e-mail_vriend" id="e-mail_vriend" onblur="TF.checkEmail($(\'e-mail_vriend\').value);" class="tb" value="" /></td></tr><tr><td valign="top" class="firstcol">Je eigen boodschap</td><td class="td"><textarea name="bericht" rows="5" class="tb" id="bericht"></textarea></td></tr><tr><td class="firstcol"></td><td class="td"><input type="image" src="/img/btns/btnOKVersturenS.gif" class="imgBtn" /></td></tr><tr><td class="td" colspan="2"><div id="returnError"></div></div></td></tr></table></form>';
        var popup = new Element("div").addClassName("popup").setStyle("width: 400px");
        var popupHeader = new Element("div").addClassName("popupHeader").setStyle("width: 400px");
        var imgHolder = new Element("img", {
            src: "/img/btnPopupClose.gif",
            alt: "Sluiten"
        });
        var a = new Element("a", {
            href: "javascript:void(0);"
        }).insert(imgHolder);
        var func = this.floatDiv.close.bind(this);
        a.observe('click', func, false);
        popupHeader.insert(a);
        popupHeader.insert("<h3>Tell a Friend</h3>");
        popup.insert(popupHeader);
        popupBody = new Element("div").insert(x).addClassName('popupBody');
        popup.insert(popupBody);
        $('floatDiv').update(popup);
    },
    checkForm: function(){
        if (this.email) {
            this.form = true;
        }
        if ($F('je_naam') !== "" && $F('je_e-mail') !== "" && $F('naam_vriend') !== "" && $F('e-mail_vriend') !== "" && $F('bericht') !== "") {
            this.naam = $F('je_naam').stripScripts().stripTags();
            this.emailInp = $F('je_e-mail').stripScripts().stripTags();
            this.naamVriend = $F('naam_vriend').stripScripts().stripTags();
            this.emailVriend = $F('e-mail_vriend').stripScripts().stripTags();
            this.bericht = $F('bericht').stripScripts().stripTags();
            this.form = true;
        }
        else {
            this.form = false;
        }
        if (this.form) {
            $('returnError').update();
            this.submitTellFriend();
        }
        else {
            $('returnError').update(NF.notificatieObj[130]).appear();
        }
    },
    checkEmail: function(email){
        $('returnError').update().hide();
        var str = email;
        var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        if (filter.test(str)) {
            this.email = true;
        }
        else {
            $('returnError').insert(NF.notificatieObj[131]).appear();
            this.email = false;
        }
    },
    submitTellFriend: function(){
        var func = this.floatDiv.close.bind(this);
        var obj = {};
        obj.naam = this.naam;
        obj.email = this.emailInp;
        obj.naamVriend = this.naamVriend;
        obj.emailVriend = this.emailVriend;
        obj.bericht = this.bericht;
        obj.newType = 'Gebruiker';
        obj.newMethod = 'TellFriend';
        ARRM.jsonFormRequest(obj, func);
    },
    resetField: function(){
        Form.reset('formTaf');
        $('returnError').insert("You just told a friend !! Je kan er nog meer sturen...").hide().appear();
        this.email = false;
        this.form = false;
    },
    tellUs: function(){
        var locX = tempX - 400;
        var locY = tempY - 400;
        this.floatDiv = new Div(locY, locX, true);
        var x = '<form method="get" action="javascript:void(0);" onsubmit="TF.checkFormUs(); return false;" name="formTaf" id="formTaf"><table cellpadding="0" cellspacing="0" border="0"><tr><td class="firstcol">Je naam</td><td class="td"><input type="text" name="je_naam" id="je_naam" class="tb" value="' + this.name + '" /></td></tr><tr><td class="firstcol">Je e-mail</td><td class="td"><input type="text" name="je_e-mail" id="je_e-mail" onblur="TF.checkEmail($(\'je_e-mail\').value);" class="tb" value="' + this.emailValue + '" /></td></tr><tr><td class="firstcol">Onderwerp</td><td class="td"><select name="onderwerp" id="onderwerp"><option value="mening">mening</option><option value="bug">bug</option></select></td></tr><tr><td valign="top" class="firstcol">Je boodschap aan ons</td><td class="td"><textarea name="bericht" rows="5" class="tb" id="bericht"></textarea></td></tr><tr><td class="firstcol"></td><td class="td"><input type="image" src="/img/btns/btnOKVersturenS.gif" class="imgBtn" /></td></tr><tr><td class="td" colspan="2"><div id="returnError"></div></div></td></tr></table></form>';
        var popup = new Element("div").addClassName("popup").setStyle("width: 400px");
        var popupHeader = new Element("div").addClassName("popupHeader").setStyle("width: 400px");
        var imgHolder = new Element("img", {
            src: "/img/btnPopupClose.gif",
            alt: "Sluiten"
        });
        var a = new Element("a", {
            href: "javascript:void(0);"
        }).insert(imgHolder);
        var func = this.floatDiv.close.bind(this);
        a.observe('click', func, false);
        popupHeader.insert(a);
        popupHeader.insert("<h3>Tell Us</h3>");
        popup.insert(popupHeader);
        popupBody = new Element("div").insert(x).addClassName('popupBody');
        popup.insert(popupBody);
        $('floatDiv').update(popup);
    },
    checkFormUs: function(){
        if (this.email) {
            this.form = true;
        }
        if ($F('je_naam') !== "" && $F('je_e-mail') !== "" && $F('onderwerp') !== "" && $F('bericht') !== "") {
            this.naam = $F('je_naam').stripScripts().stripTags();
            this.emailInp = $F('je_e-mail').stripScripts().stripTags();
            this.bericht = $F('bericht').stripScripts().stripTags();
            this.onderwerp = $F('onderwerp').stripScripts().stripTags();
            this.form = true;
        }
        else {
            this.form = false;
        }
        if (this.form) {
            $('returnError').update();
            this.submitTellUs();
        }
        else {
            $('returnError').update(NF.notificatieObj[130]).appear();
        }
    },
    submitTellUs: function(){
        var func = this.floatDiv.close.bind(this);
        var obj = {};
        obj.naam = this.naam;
        obj.email = this.emailInp;
        obj.onderwerp = this.onderwerp + " gemeld";
        obj.bericht = this.bericht;
        obj.newType = 'Gebruiker';
        obj.newMethod = 'TellUs';
        ARRM.jsonFormRequest(obj, func);
    },
    resetFieldUs: function(){
        Form.reset('formTaf');
        $('returnError').update("Bedankt voor je mening! We zullen indien nodig contact opnemen met je...").appear().setStyle("display: none;");
        this.email = false;
        this.form = false;
    }
});
var Notificatie = Class.create({
    initialize: function(){
        this.notificatieObj = {};
    },
    setNotificatie: function(obj){
        if (obj.notificaties) {
            this.notificatieObj = obj.notificaties;
        }
    }
});
var initLoader = Class.create({
    initialize: function(arr){
        var func = this.initValues.bind(this);
        var standardArr = [1, 2];
        var arrt = arr.join();
        arrt = standardArr.join() + "," + arrt;
        var obj = {};
        obj.newType = 'Gebruiker';
        obj.newMethod = 'initLoader';
        obj.notificaties = arrt;
        ARRM.jsonRequest(obj, func);
    },
    initValues: function(obj){
        NF = new Notificatie();
        NF.setNotificatie(obj.notificaties);
        TF = new TellFriend();
        TF.setTellFriend(obj.tell);
    }
});
var Div = Class.create();
Div.prototype = {
    initialize: function(locY, locX, drag){
        this.locY = locY;
        this.locX = locX;
        this.drag = drag;
        this.c = false;
        this.d = true;
        this.appear();
    },
    appear: function(){
        if ($("floatDiv")) {
            document.body.removeChild($('floatDiv'));
        }
        this.d = new Element("div", {
            id: "floatDiv"
        }).setStyle("z-index: 200; position: absolute; top: " + this.locY + "px; left: " + this.locX + "px;");
        document.body.appendChild(this.d);
        if (this.c) {
            new Draggable(this.d);
        }
    },
    insert: function(html){
    },
    close: function(){
        new Effect.Fade($('floatDiv'), {
            duration: 0.4,
            afterFinish: function(effect){
                $('floatDiv').remove();
            }
        });
    }
};
var FotoItem = Class.create();
FotoItem.prototype = {
    initialize: function(obj, stuurBericht, verwijder){
        var contactImg = new Element("a", {
            href: obj.url
        }).addClassName("foto").setStyle("background-image: url(" + obj.avatar + ");");
        this.div = new Element("div").addClassName("fotoItem").insert(contactImg);
        var contactName = new Element("a", {
            href: obj.url
        }).addClassName("naam").update(obj.naam);
        this.div.insert(contactName);
        var controls = new Element("div").addClassName("controls");
        if (verwijder) {
            var verwijderIcon = new Element("img", {
                src: "/img/btns/btnVerwijderenXS.gif"
            });
            var verwijderLink = new Element("a", {
                href: "javascript:void(0);"
            }).insert(verwijderIcon);
            var func = this.removeContactHTML.bind(this, obj.id, obj.naam, obj.popupCat);
            verwijderLink.observe('click', func, false);
            controls.insert(verwijderLink);
        }
        if (stuurBericht) {
            var sendMessageIcon = new Element("img", {
                src: "/img/btns/btnSchrijfXS.gif"
            });
            var sendMessageLink = new Element("a", {
                href: "/mijn/nieuw/" + obj.naam + "/" + obj.popupCat + "/" + obj.id + ""
            }).insert(sendMessageIcon);
            controls.insert(sendMessageLink);
        }
        this.div.insert(controls);
    },
    removeContactHTML: function(id, naam, cat){
        var locX = tempX - 200;
        var locY = tempY;
        this.floatDiv = new Div(locY, locX, true);
        var popup = new Element("div").addClassName("popup").setStyle("width: 400px");
        var popupHeader = new Element("div").addClassName("popupHeader").setStyle("width: 400px");
        var imgHolder = new Element("img", {
            src: "/img/btnPopupClose.gif",
            alt: "Sluiten"
        });
        var a = new Element("a", {
            href: "javascript:void(0);"
        }).insert(imgHolder);
        var func = this.floatDiv.close.bind(this);
        a.observe('click', func, false);
        popupHeader.insert(a);
        popupHeader.insert("<h3>Verwijder</h3>");
        popup.insert(popupHeader);
        popupBody = new Element("div", {
            id: "returnM"
        }).insert("" + naam + " verwijderen?").addClassName('popupBody');
        var spacer = new Element("div").addClassName("spacer");
        popupBody.insert(spacer);
        if (cat == 1) {
            func = AC.removeContactSubmit.bind(AC, id);
        }
        if (cat == 2) {
            func = GC.Groepen.removeGroepSubmit.bind(GC, id);
        }
        var input = new Element("input", {
            id: "submitRemove",
            src: "/img/btns/btnVerwijderenS.gif",
            type: "image"
        }).addClassName("imgBtn");
        input.observe('click', func, true);
        popupBody.insert(input);
        popup.insert(popupBody);
        $('floatDiv').update(popup);
    }
};
var User = Class.create();
User.prototype = {
    initialize: function(){
        this.username = true;
        this.id = true;
        this.dropBoxes = [];
    },
    login: function(){
        var func = this.showLogin.bind(this);
        var obj = {};
        obj.newType = 'Gebruiker';
        obj.newMethod = 'Inloggen';
        obj.action = "login";
        obj.gebruikersnaam = $F('loginNaam').stripScripts().stripTags();
        obj.wachtwoord = $F('loginWachtwoord').stripScripts().stripTags();
        ARRM.jsonRequest(obj, func);
    },
    logout: function(){
        var func = this.showLogout.bind(this);
        var obj = {};
        obj.newType = 'Gebruiker';
        obj.newMethod = 'Uitloggen';
        obj.action = "logout";
        ARRM.jsonRequest(obj, func);
    },
    reset: function(){
        var func = this.redirectReset.bind(this);
        var obj = {};
        obj.newType = 'Gebruiker';
        obj.newMethod = 'resetWachtwoord';
        obj.email = $F('loginEmail').stripScripts().stripTags();
        ARRM.jsonRequest(obj, func);
    },
    showLogin: function(obj){
        if (obj.status == 2) {
            $('returnMessage').update(obj.returnMessage);
        }
        else 
            if (obj.status == 3) {
                document.location.href = "/mijn/?boxType=2";
            }
            else if (obj.status == 1) {
                document.location.href = "/mijn/";
            }
            else {
                this.id = obj.id;
                this.username = obj.username;
                $('login').replace("<div id=\"logout\"><p>Ingelogd als: <span><a href=\"javascript:void(0);\" title=\"" + obj.username + "\">" + obj.username + "</a> | <a href=\"javascript:void(0);\" title=\"Afmelden\" onclick=\"user.logout();\">Afmelden</a></span></p></div>");
                var func = this.reloadLogin.bind(this);
                obj = {};
                obj.newType = 'Gebruiker';
                obj.newMethod = 'haalGebruikerData';
                ARRM.jsonRequest(obj, func);
            }
    },
    showLogout: function(obj){
        document.location.href = "/home/";
    },
    redirectReset: function(obj){
        if (obj.success == 'yes') {
            document.location.href = "/reset/";
        }
        else {
            $('returnMessage').update('E-mail adres niet gevonden!');
        }
    },
    showReset: function(obj){
        $('tiUsername').update();
        $('bgUsername').update();
        $('bgUsername').removeClassName('bgUsername');
        $('bgUsername').addClassName('bgEmpty');
        $('tiPassword').update('<h1>e-mail adres</h1>');
        $('bgPassword').update('<input type="text" name="loginEmail" id="loginEmail" value="" />');
        $('returnMessage').update();
        $('loginBut').update('<input type="image" src="/img/btnLogin.gif" alt="Inloggen" onclick="user.reset(); return false;" />');
    },
    wachtwoordWijzigen: function(){
        $('returnMessage').addClassName("formError");
        if ($F('wachtwoord_oud')) {
            if ($F('wachtwoord_nieuw').length >= 6) {
                if ($F('wachtwoord_nieuw') == $F('wachtwoord_bevestiging')) {
                    var func = this.wachtwoordWijzigenAfronden.bind(this);
                    var obj = {};
                    obj.newType = 'Gebruiker';
                    obj.newMethod = 'WachtwoordWijzigen';
                    obj.wachtwoord_oud = $F('wachtwoord_oud').stripScripts().stripTags();
                    obj.wachtwoord_nieuw = $F('wachtwoord_nieuw').stripScripts().stripTags();
                    obj.wachtwoord_bevestiging = $F('wachtwoord_bevestiging').stripScripts().stripTags();
                    ARRM.jsonRequest(obj, func);
                }
                else {
                    $('returnMessage').update(NF.notificatieObj[40]);
                }
            }
            else {
                $('returnMessage').update(NF.notificatieObj[39]);
            }
        }
        else {
            $('returnMessage').update(NF.notificatieObj[39]);
        }
    },
    wachtwoordWijzigenAfronden: function(obj){
        if (obj.fout == 1) {
            $('returnMessage').update(obj.melding);
        }
        else {
            $("instellingenWachtwoord").reset();
            $('returnMessage').update(NF.notificatieObj[38]);
        }
    },
    weergaveWijzigen: function(){
        var func = this.weergaveWijzigenAfronden.bind(this);
        var obj = {};
        obj.newType = 'Gebruiker';
        obj.newMethod = 'WeergaveWijzigen';
        obj.skin = $('selecteerSkin').getValue();
        obj.tips = $F('tips');
        ARRM.jsonRequest(obj, func);
    },
    weergaveWijzigenAfronden: function(obj){
        switchStyle(obj.skin);
        $('returnMessage').update(obj.melding).addClassName("formError");
    }
};
