Profile = new ( Class.create({
    
    initialize : function()
    {
    },
    
    LockFunctions : false,
    
    LoadTo: function(container)
    {
        var thisClass = this;
        
        new Ajax.Updater(
            container,
            'Components/Profile/Profile.php',
            {
                onComplete: function() { thisClass.AlignForm(container); thisClass.SetEventsAndValues(); }
            }
        );
    },
    
    AlignForm: function(container)
    {
        $("profileForm").setStyle({
            marginLeft: Math.floor(($(container).getWidth() - $("profileForm").getWidth()) / 2) + "px"
        });
    },
    
    SetEventsAndValues : function()
    {
        GlobalLoading.Start();
        
        var profileData = ActionHandler.GetResponse(
        {
            url: "Components/Profile/ProfileActionHandler.php",
            action: "GetProfileData"
        });
        
        $("lastName").value = profileData.user_lastname;
        $("firstName").value = profileData.user_firstname;
        $("birthYear").value = profileData.user_birthyear;
        $("birthMonth").value = profileData.user_birthmonth;
        $("birthDay").value = profileData.user_birthday;
        $("sex").value = profileData.user_sex;
        $("country").value = profileData.user_country;
        $("street").value = profileData.user_street;
        $("mobile").value = profileData.user_mobile;
        $("elementarySchool").value = profileData.user_elementary_school;
        $("secondSchool").value = profileData.user_second_school;
        $("university").value = profileData.user_university;
        $("workplace").value = profileData.user_workplace;
        $("workAddress").value = profileData.user_work_address;
        $("workPhone").value = profileData.user_work_phone;
        $("workEmail").value = profileData.user_work_email;
        $("workWebPage").value = profileData.user_work_webpage;
        
        this.CountryCheck();
        Object.isElement($("city")) ? $("city").value = profileData.user_city : $("cityName").value = profileData.user_city_name;
        $("lastName").focus();
        
        var thisClass = this;
        
        Event.observe("picture", 'load', this.AlignPicture.bindAsEventListener(this));
        if(profileData.user_picture != null)
        {
            $("picture").src = "ProfilePictures/" + profileData.user_picture;
        }
        else
        {
            this.SexCheck();
            Event.observe("sex", "change", this.SexCheck.bindAsEventListener(this) );
        }
        Event.observe("country", "change", this.CountryCheck.bindAsEventListener(this) );
        Event.observe("profileModify", "click", this.ModifyProfile.bindAsEventListener(this));
        Event.observe("uploadButton", 'click', this.UploadFile.bindAsEventListener(this));
        Event.observe("deleteButton", 'click', this.DeleteFile.bindAsEventListener(this));
        
        GlobalLoading.Stop();
    },
    
    CountryCheck : function()
    {
        if($("country").value == "HUN")
        {
            var cityCombo = ActionHandler.GetResponse(
            {
                url: "Components/Configuration/ConfigurationActionHandler.php",
                action: "GetCityCombo"
            });
            $("cityLine").update("<h1>Város:</h1>" + cityCombo.innerHTML + "<h6>&nbsp;</h6>");
        }
        else
        {
            $("cityLine").update('<h1>Város:</h1><input id="cityName"/><h6>&nbsp;</h6>');
        }
    },
    
    SexCheck : function()
    {
        if($("sex").value == 0)
        {
            $("picture").src = "Themes/DefaultImages/male.png";
        }
        else
        {
            $("picture").src = "Themes/DefaultImages/female.png";
        }
    },
    
    ModifyProfile : function(event)
    {
        Event.stop(event);
        
        GlobalLoading.Start();
        
        if(!this.LockFunctions)
        {
            this.LockFunctions = true;
            if(this.CheckStrings())
            {
                var pars = "&lastName=" + $("lastName").value;
                    pars += "&firstName=" + $("firstName").value;
                    pars += "&birthYear=" + $("birthYear").value;
                    pars += "&birthMonth=" + $("birthMonth").value;
                    pars += "&birthDay=" + $("birthDay").value;
                    pars += "&sex=" + $("sex").value;
                    pars += "&country=" + $("country").value;
                    Object.isElement($("city")) ? pars += "&city=" + $("city").value : pars += "&cityName=" + $("cityName").value;
                    pars += "&street=" + $("street").value;
                    pars += "&mobile=" + $("mobile").value;
                    pars += "&elementarySchool=" + $("elementarySchool").value;
                    pars += "&secondSchool=" + $("secondSchool").value;
                    pars += "&university=" + $("university").value;
                    pars += "&workplace=" + $("workplace").value;
                    pars += "&workAddress=" + $("workAddress").value;
                    pars += "&workPhone=" + $("workPhone").value;
                    pars += "&workEmail=" + $("workEmail").value;
                    pars += "&workWebPage=" + $("workWebPage").value;
                ActionHandler.GetResponse(
                {
                    url: "Components/Profile/ProfileActionHandler.php",
                    action: "ModifyProfileData",
                    parameters: pars
                });
                IconView.LoadTo("content");
                var tools = new Tools({container: "tools", url: "Components/Tools/Tools.php", role: AccountRole});
                tools.Show();
            }
            this.LockFunctions = false;
        }
        
        GlobalLoading.Stop();
    },
    
    CheckStrings : function()
    {
        var result = true;
        var tmpDate = new Date();
        tmpDate.setFullYear( $("birthYear").value, $("birthMonth").value, $("birthDay").value );
        if(tmpDate.getMonth() != $("birthMonth").value)
        {
            $("dateMessage").innerHTML = "Érvénytelen dátum!";
            result = false;
        }
        else { $("dateMessage").innerHTML = "&nbsp;"; }
        
        if(!Utils.IsValidString($("lastName").value, "NotNullOrEmpty"))
        {
            $("lastNameMessage").innerHTML = "Nincs megadva családnév!";
            result = false;
        }
        else { $("lastNameMessage").innerHTML = "&nbsp;"; }
        
        if(!Utils.IsValidString($("firstName").value, "NotNullOrEmpty"))
        {
            $("firstNameMessage").innerHTML = "Nincs megadva utónév!";
            result = false;
        }
        else { $("firstNameMessage").innerHTML = "&nbsp;"; }
        
        return result;
    },
    
    UploadFile: function(event)
    {        
        if($("uploadFile").value != "")
        {
            AIM.submit(
                $("uploadForm"),
                {
                    "onStart" : this.StartUpload.bindAsEventListener(this),
                    "onComplete" : this.CompleteUpload.bindAsEventListener(this)
                }
            );
        }
        else
        {
            Event.stop(event);
        }
    },
    
    StartUpload: function(event)
    {
        $("uploadForm").submit();
        return true;
    },
    
    CompleteUpload: function(event)
    {
        var profilePicture = ActionHandler.GetResponse(
        {
            url: "Components/Profile/ProfileActionHandler.php",
            action: "GetProfilePicture"
        });
        $("picture").src = "ProfilePictures/" + profilePicture;
        Event.stopObserving("sex", "change");
    },
    
    DeleteFile: function(event)
    {
        Event.stop(event);
        ActionHandler.GetResponse(
        {
            url: "Components/Profile/ProfileActionHandler.php",
            action: "DeletePicture"
        });
        this.SexCheck();
        Event.observe("sex", "change", this.SexCheck.bindAsEventListener(this) );
    },
    
    AlignPicture: function()
    {
        $("picture").setStyle({
            marginRight: Math.floor(($("pictureFrame").getWidth() - $("picture").getWidth()) / 2) + "px",
            marginTop: Math.floor(($("pictureFrame").getHeight() - $("picture").getHeight()) / 2) + "px"
        });
    }
    
}))();
