AdvertForm = {

    IsWeeksVisible: false,
    
    SelectedPlace: 0,
    
    Initialize : function()
    {
        var thisObject = this;
        
        if($("#pictures").length == 1) { this.RefreshPictures(); }
        
        this.RefreshAvailableMoney();
        
        this.RefreshExpiration();
        
        this.RefreshExistingHighlights();
        
        this.onFileUploadComplete = function() {
            $("#uploadFile").val("");
            thisObject.RefreshPictures();
        }
        
        // Extension button clicked
        $('#extensionButton').bind('click', function(event) {
            event.preventDefault();
            $.ajax({
                type: "POST",
                url: "Components/AdvertForm/AdvertFormActionHandler.php",
                data: "action=ExpirationExtension&advertID=" + thisObject.GetAdvertID(),
                dataType: "json",
                success: function(response) {
                    thisObject.RefreshExpiration();
                }
            });
        });
        
        // Place button clicked
        $('#highlightForm div.line .button').bind('click', function(event) {
            event.preventDefault();
            var id = $(event.target).attr("id");
            var place = id.replace("PlaceButton", "");
            
            if(thisObject.SelectedPlace == place) {
                $('#highlightForm div.line .container:not(#container'+place+')').fadeTo("slow", 1);
                $("#availableWeeks").slideUp(); thisObject.IsWeeksVisible = false;
                $(event.target).val("Tovább...");
                thisObject.SelectedPlace = 0;
            }
            else {
                if(thisObject.GetAdvertID() <= 0) { alert("Elsőként mentsd le a hirdetésed. A lementett hirdetéseidhez tudsz kiemelést beállítani."); }
                else {
                    // Money check
                    $.ajax({
                        type: "POST",
                        url: "Components/AdvertForm/AdvertFormActionHandler.php",
                        data: "action=CheckMoney&placeID=" + place,
                        dataType: "json",
                        success: function(HasEnoughMoney) {
                            // Select place
                            if(HasEnoughMoney) {
                                $('#highlightForm div.line .container:not(#container'+place+')').fadeTo("slow", 0.2);
                                $('#highlightForm div.line #container'+place).fadeTo("slow", 1);
                                $("#availableWeeks").slideDown(); thisObject.RefreshAvailableWeeks(place); thisObject.IsWeeksVisible = true;
                                $(event.target).val("Mégsem");
                                $('#highlightForm div.line .button:not(#'+id+')').val("Tovább...");
                                thisObject.SelectedPlace = place;
                            }
                            else { alert("Egyenleged a pénzügyek menüpontban töltheted fel."); }
                        }
                    });
                }
            }
            
        });
        
    },
    
    GetAdvertID : function()
    {
        var advertID = window.location.toString().replace("#Form", "");
        advertID = advertID.substring(advertID.lastIndexOf("hirdetes")+8);
        if(isNaN(parseInt(advertID))) { advertID = 0; }
        return advertID;
    },
    
    RefreshPictures : function()
    {
        var thisObject = this;
        
        $.ajax({
            type: "POST",
            url: "Components/AdvertForm/AdvertFormActionHandler.php",
            data: "action=GetPictures&advertID=" + thisObject.GetAdvertID(),
            dataType: "json",
            success: function(response) {
                var pictures = response.pictures;
                var pictureDirectory = response.PictureDirectory;
                if(pictures != null)
                {
                    $("#pictures").empty();
                    for(var i in pictures) {
                        var thumbSRC = "AdvertPictures/" + pictureDirectory + "/Thumbnails/" + pictures[i].picture_src;
                        var origSRC = "AdvertPictures/" + pictureDirectory + "/" + pictures[i].picture_src;
                        var pictureID = pictures[i].picture_id;
                        $("#pictures").append('<div class="pictureFrame pictureFrameBorder"><a id="deleteButton'+pictureID+'" class="button" href="#">Törlöm</a><a class="pictureLink" href="'+origSRC+'"><img src="'+thumbSRC+'" alt="kép" /></a></div>');
                        
                        // Delete button event
                        $("#deleteButton"+pictureID).bind('click', function(event) {
                            event.preventDefault();
                            $.ajax({
                                type: "POST",
                                url: "Components/AdvertForm/AdvertFormActionHandler.php",
                                data: "action=DeletePicture&pictureID="+event.currentTarget.id.replace('deleteButton', ''),
                                dataType: "json",
                                success: function(response) { thisObject.RefreshPictures(); }
                            });
                        });
                    }
                    
                    $("#pictures .pictureLink").lightBox();
                }
                else
                {
                    $("#pictures").html("<span>Tölts fel képeket</span>");
                }
            }
        });
    },
    
    RefreshExpiration : function()
    {
        var advertID = this.GetAdvertID();
        
        if(advertID <= 0) { $("#expirationMessage").html("Még 14 napig érvényes"); $("#expirationMessage").css({"color":"#090"}); }
        else
        {
            $.ajax({
                type: "POST",
                url: "Components/AdvertForm/AdvertFormActionHandler.php",
                data: "action=GetExpiration&advertID=" + advertID,
                dataType: "json",
                success: function(response) {
                    if(response > 0) { $("#expirationMessage").html("Még " + response + " napig érvényes"); $("#expirationMessage").css({"color":"#090"}); }
                    else {
                        if(response == 0) { $("#expirationMessage").html("Ma járt le"); $("#expirationMessage").css({"color":"#f00"}); }
                        else { $("#expirationMessage").html("Már " + -1*response + " napja lejárt"); $("#expirationMessage").css({"color":"#f00"}); }
                    }
                }
            });
        }
    },
    
    RefreshAvailableWeeks: function(place)
    {
        var advertID = this.GetAdvertID();
        var thisObject = this;
        
        $.ajax({
            type: "POST",
            url: "Components/AdvertForm/AdvertFormActionHandler.php",
            data: "action=GetAvailableWeeks&advertID=" + advertID + "&place=" + place,
            dataType: "json",
            success: function(response) {
                $("#availableWeeks").html(response);
                
                // Week button clicked
                $('.placeButton').bind('click', function(event) {
                    event.preventDefault();
                    var interval = $(event.target).attr("id").replace('highlightButton', '').split("-");
                    
                    $.ajax({
                        type: "POST",
                        url: "Components/AdvertForm/AdvertFormActionHandler.php",
                        data: "action=Highlight&start=" + interval[0] + "&end=" + interval[1] + "&advertID=" + interval[2] + "&place=" + interval[3],
                        dataType: "json",
                        success: function(response) {
                            $("#availableWeeks").slideUp(); thisObject.IsWeeksVisible = false;
                            $('#highlightForm div.line .button').val("Tovább...");
                            thisObject.SelectedPlace = 0;
                            $('#highlightForm div.line .container').fadeTo("slow", 1);
                            thisObject.RefreshExistingHighlights();
                        }
                    });
                    
                });
                
            }
        });
        
    },
    
    RefreshExistingHighlights: function()
    {
        var advertID = this.GetAdvertID();
        var thisObject = this;
        
        $.ajax({
            type: "POST",
            url: "Components/AdvertForm/AdvertFormActionHandler.php",
            data: "action=GetExistingHighlights&advertID=" + advertID,
            dataType: "json",
            success: function(response) {
                $("#existingHighlights").html(response);
                thisObject.RefreshAvailableMoney();
                
                // Delete button clicked
                $('#existingHighlights .button').bind('click', function(event) {
                    event.preventDefault();
                    var highlightID = $(event.target).attr("id").replace('highlightDelete', '');
                    $.ajax({
                        type: "POST",
                        url: "Components/AdvertForm/AdvertFormActionHandler.php",
                        data: "action=DeleteHighlight&highlightID=" + highlightID,
                        dataType: "json",
                        success: function(response) {
                            thisObject.RefreshExistingHighlights();
                            thisObject.RefreshAvailableMoney();
                        }
                    });
                });
            }
        });
        
    },
    
    RefreshAvailableMoney: function()
    {
        $.ajax({
            type: "POST",
            url: "Components/AdvertForm/AdvertFormActionHandler.php",
            data: "action=GetAvailableMoney",
            dataType: "json",
            success: function(AvailableMoney) { $("#availableMoney").html(AvailableMoney + " HUF"); }
        });
    }
    
};
