RelationView = new ( Class.create({
    
    initialize : function() {},
    
    SearchInputDefaultValue : "Név vagy e-mail cím",
    
    InvitationInputDefaultValue : "E-mail cím",
    
    LastSearchString : null,
    
    LoadTo: function(container, searchString, page)
    {
        var thisClass = this;
        var pars = {};
        this.LastSearchString = searchString;
        
        if(this.LastSearchString != null) { pars = { action: 'ShowRelationView', searchString: this.LastSearchString }; }
        else { pars = { action: 'ShowRelationView' }; }
        
        if(page != null) { pars.pageOffset = page; }
        
        new Ajax.Updater(
            container,
            'Components/RelationView/RelationViewActionHandler.php',
            {
                parameters: pars,
                onComplete: function() { thisClass.SetEvents(); thisClass.ResizeContent(); thisClass.AlignPictures(); }
            }
        );
    },
    
    SetEvents : function()
    {
        Event.observe("relationSearchInput", "click", this.RelationSearchInputClicked.bindAsEventListener(this));
        Event.observe("relationSearchInput", "keypress", this.RelationSearchInputKeyPressed.bindAsEventListener(this));
        Event.observe("relationSearchInput", "blur", this.RelationSearchInputBlur.bindAsEventListener(this));
        Event.observe("relationSearchButton", "click", this.RelationSearchButtonClicked.bindAsEventListener(this));
        Event.observe("invitationInput", "click", this.InvitationInputClicked.bindAsEventListener(this));
        Event.observe("invitationInput", "keypress", this.InvitationInputKeyPressed.bindAsEventListener(this));
        Event.observe("invitationInput", "blur", this.InvitationInputBlur.bindAsEventListener(this));
        Event.observe("relationViewInvitationButton", "click", this.InvitationButtonClicked.bindAsEventListener(this));
        
        var thisClass = this;
        
        $$('.pictureFrame img').each(function(element) { Event.observe(element, "click", thisClass.RelationButtonClicked.bindAsEventListener(thisClass) ); });
        
        $$('.markLink').each(function(element) { Event.observe(element, "click", thisClass.MarkLinkClicked.bindAsEventListener(thisClass) ); });
        
        $$('.deleteLink').each( function(element) { Event.observe(element, "click", thisClass.DeleteLinkClicked.bindAsEventListener(thisClass) ); });
        
        $$('.markAcceptLink').each( function(element) { Event.observe(element, "click", thisClass.MarkAcceptLinkClicked.bindAsEventListener(thisClass) ); });
        
        $$('.markDeclineLink').each( function(element) { Event.observe(element, "click", thisClass.MarkDeclineLinkClicked.bindAsEventListener(thisClass) ); });
        
        if(Object.isElement($("previousPageButton")) && $("previousPageButton").className != "button disabled" ) { Event.observe("previousPageButton", "click", this.PreviousPageButtonClicked.bindAsEventListener(this) ); }
        if(Object.isElement($("nextPageButton")) && $("nextPageButton").className != "button disabled" ) { Event.observe("nextPageButton", "click", this.NextPageButtonClicked.bindAsEventListener(this) ); }
        if(Object.isElement($("previousPageButtonBottom")) && $("previousPageButtonBottom").className != "button disabled" ) { Event.observe("previousPageButtonBottom", "click", this.PreviousPageButtonClicked.bindAsEventListener(this) ); }
        if(Object.isElement($("nextPageButtonBottom")) && $("nextPageButtonBottom").className != "button disabled" ) { Event.observe("nextPageButtonBottom", "click", this.NextPageButtonClicked.bindAsEventListener(this) ); }
    },
    
    PreviousPageButtonClicked : function(event)
    {
        Event.stop(event);
        this.LoadTo("content", this.LastSearchString, parseInt(Event.element(event).name) - 1);
    },
    
    NextPageButtonClicked : function(event)
    {
        Event.stop(event);
        this.LoadTo("content", this.LastSearchString, parseInt(Event.element(event).name) + 1);
    },
    
    MarkAcceptLinkClicked : function(event)
    {
        if(IsRoleSuccess())
        {
            Event.stop(event);
            var id = Event.element(event).id.gsub("markAcceptLink", "");
            
            var pars = "relationAccountID=" + id;
            ActionHandler.GetResponse(
            {
                url: "Components/RelationView/RelationViewActionHandler.php",
                action: "MarkAccept",
                parameters: pars
            });
            
            this.LoadTo("content", null, null);
            
            ActionHandler.GetResponse(
            {
                url: "Components/RelationView/RelationViewActionHandler.php",
                action: "SendMarkingAcceptEmail",
                parameters: pars
            });
        }
        else
        {
            NoRightWindow.Show(event);
        }
    },
    
    MarkDeclineLinkClicked : function(event)
    {
        if(IsRoleSuccess())
        {
            Event.stop(event);
            var thisClass = this;
            
            var confirmWindow = new Window({
                container: "window",
                title: "Kapcsolat visszautasítás",
                width: 400,
                contentURL: "Components/RelationView/Decline.php"
            });
            
            confirmWindow.Show();
            $("Cancel").focus();
            
            var id = Event.element(event).id.gsub("markDeclineLink", "");
            var pars = "relationAccountID=" + id;
            
            Event.observe("Cancel", "click", function(event) { Event.stop(event); confirmWindow.Close(); } );
            Event.observe("OK", "click", function(event) {
                Event.stop(event);
                ActionHandler.GetResponse(
                {
                    url: "Components/RelationView/RelationViewActionHandler.php",
                    action: "MarkDecline",
                    parameters: pars
                });
                
                confirmWindow.Close();
                thisClass.LoadTo("content", null, null);
                
                ActionHandler.GetResponse(
                {
                    url: "Components/RelationView/RelationViewActionHandler.php",
                    action: "SendMarkingDeclineEmail",
                    parameters: pars
                });
            });
        }
        else
        {
            NoRightWindow.Show(event);
        }
    },
    
    MarkLinkClicked : function(event)
    {
        if(IsRoleSuccess())
        {
            Event.stop(event);
            var id = Event.element(event).id.gsub("markLink", "");
            
            var pars = "relationAccountID=" + id;
            ActionHandler.GetResponse(
            {
                url: "Components/RelationView/RelationViewActionHandler.php",
                action: "MarkRelation",
                parameters: pars
            });
            
            this.LoadTo("content", this.LastSearchString, null);
            
            ActionHandler.GetResponse(
            {
                url: "Components/RelationView/RelationViewActionHandler.php",
                action: "SendMarkingEmail",
                parameters: pars
            });
        }
        else
        {
            NoRightWindow.Show(event);
        }
    },
    
    DeleteLinkClicked : function(event)
    {
        if(IsRoleSuccess())
        {
            Event.stop(event);
            
            var confirmWindow = new Window({
                container: "window",
                title: "Kapcsolat törlés",
                width: 400,
                contentURL: "Components/RelationView/Confirm.php"
            });
            
            confirmWindow.Show();
            $("Cancel").focus();
            
            var thisClass = this;
            var pars = "relationAccountID=" + Event.element(event).id.gsub("deleteLink", "");
            
            Event.observe("Cancel", "click", function(event) { Event.stop(event); confirmWindow.Close(); } );
            Event.observe("OK", "click", function() {
                ActionHandler.GetResponse(
                {
                    url: "Components/RelationView/RelationViewActionHandler.php",
                    action: "DeleteRelation",
                    parameters: pars
                });
                confirmWindow.Close();
                thisClass.LoadTo("content", null, null);
            });
        }
        else
        {
            NoRightWindow.Show(event);
        }
    },
        
    RelationWindow : new Window({
        container: "window",
        title: "Profil",
        width: 900,
        contentURL: "Components/RelationView/RelationProfile.php"
    }),
    
    RelationButtonClicked : function(event)
    {
        Event.stop(event);
        this.RelationWindow.config.parameters = {accountID: Event.element(event).id.gsub("iconButton", "")};
        this.RelationWindow.Show();
        
        var thisClass = this;
        Event.observe("profileClose", "click", function(event) { Event.stop(event); thisClass.RelationWindow.Close(); } );
    },
    
    InvitationButtonClicked : function(event)
    {
        Event.stop(event);
        
        var message = "";
        if(Utils.IsValidString($("invitationInput").value, "Email"))
        {
            var pars = "email=" + encodeURIComponent($("invitationInput").value);
            var sendingData = ActionHandler.GetResponse(
            {
                url: "Components/Invitation/InvitationActionHandler.php",
                action: "SendInvitation",
                parameters: pars
            });
            if(sendingData.isFreeEmail)
            {
                message = "Meghívódat elküldtük a megadott e-mail címre.";
                $("invitationInput").value = this.InvitationInputDefaultValue;
                $("invitationInput").setStyle({color: "#aaa", fontWeight: "normal"});
                
            }
            else
            {
                message = "A megadott e-mail címmel már regisztráltak.";
            }
        }
        else
        {
            message = "Érvénytelen e-mail címet adtál meg.";
        }
        
        var confirmWindow = new Window({
            container: "window",
            title: "Meghívó",
            width: 400,
            contentURL: "Components/RelationView/Invitation.php"
        });
        
        confirmWindow.Show();
        $("Cancel").focus();
        
        $("invitationMessage").update(message);
        
        Event.observe("Cancel", "click", function(event) { Event.stop(event); confirmWindow.Close(); } );
    },
    
    RelationSearchButtonClicked : function(event)
    {
        Event.stop(event);
        if($("relationSearchInput").value != this.SearchInputDefaultValue)
        {
            this.LoadTo("content", $("relationSearchInput").value, null);
            $("relationSearchInput").setStyle({color: "#aaa", fontWeight: "normal"});
            $("relationSearchInput").value = this.SearchInputDefaultValue;
        }
    },
    
    RelationSearchInputClicked : function(event)
    {
        var input = Event.element(event);
        if(input.value == this.SearchInputDefaultValue)
        {
            input.value = "";
            input.setStyle({color: "#000", fontWeight: "bold"});
        }
    },
    
    RelationSearchInputKeyPressed : function(event)
    {
        if(event.keyCode == 13)
        {
            this.RelationSearchButtonClicked(event);
        }
    },
    
    RelationSearchInputBlur : function(event)
    {
        var input = Event.element(event);
        if(input.value.blank())
        {
            input.setStyle({color: "#aaa", fontWeight: "normal"});
            input.value = this.SearchInputDefaultValue;
        }
    },
    
    InvitationInputClicked : function(event)
    {
        var input = Event.element(event);
        if(input.value == this.InvitationInputDefaultValue)
        {
            input.value = "";
            input.setStyle({color: "#000", fontWeight: "bold"});
        }
    },
    
    InvitationInputKeyPressed : function(event)
    {
        if(event.keyCode == 13)
        {
            this.InvitationButtonClicked(event);
        }
    },
    
    InvitationInputBlur : function(event)
    {
        var input = Event.element(event);
        if(input.value.blank())
        {
            input.setStyle({color: "#aaa", fontWeight: "normal"});
            input.value = this.InvitationInputDefaultValue;
        }
    },
    
    ResizeContent : function()
    {
        var maxWidth = document.viewport.getWidth() - 265;
        var widthString = maxWidth + "px";
        $("content").setStyle({width: widthString });
    }
    
}))();
