/**
* Javascript for Blog functionality
*
* @package js
* @author Tim Carr
* @version 1
* @copyright fish in a bottle 2009
*/

$(document).ready(function() {
    // Follow
    $("img.follow").livequery("click", function (e) {
        var currElement = this;
        var pieces = $(this).attr("id").split("_");
        var column = pieces[0];
        var userID = pieces[1];
        
        switch (column) {
            case "blog":
                var phrase = "Blog";
                break;
            case "quickclick":
                var phrase = "Quickclick";
                break;
        }       
        
        $.post(
            "ajax/ajax.Dispatcher.php",
            {
                action: 'followUser',
                followUserID: userID,
                column: column
            },
            function (data, textStatus) {
                if (textStatus == "success") {
                    if (data == 1) {
                        // Change Follow button type to unfollow where column_userID match
                        $('#background').find('img.follow').each(function(i) {
                            if($(this).attr('id').indexOf(column+"_"+userID) != -1) {
                                $(this).attr("src", "images/buttons/Button_Unfollow.gif");
                                $(this).addClass("unfollow");
                                $(this).removeClass("follow");
                            }
                        });
                        
                        // Show confirmation of follow                        
                        softMessage("You are now following this member's "+phrase+".");
                        
                        // Reload page
                        window.location = document.location.href;
                    } else {
                        softAlert("You do not have permission to follow this member's "+phrase+".", alertOff);
                    }
                } else {
                    softAlert("A "+textStatus+" error occured.  Please try again.", alertOff);    
                }
            }
        );      
    });
    
    // Unfollow
    $("img.unfollow").livequery("click", function (e) {
        var currElement = this;
        var pieces = $(this).attr("id").split("_");
        var column = pieces[0];
        var userID = pieces[1];
        
        switch (column) {
            case "blog":
                var phrase = "Blog";
                break;
            case "quickclick":
                var phrase = "Quickclick";
                break;
        }        
        
        softConfirm(
            "<p>Are you sure you want to stop following this member's "+phrase+"?</p>", 
            function(){
                $.post(
                    "ajax/ajax.Dispatcher.php",
                    {
                        action: 'unfollowUser',
                        followUserID: userID,
                        column: column
                    },
                    function (data, textStatus) {
                        if (textStatus == "success") {
                            if (data == 1) {
                                // Change Unfollow button type to follow where column_userID match
                                $('#background').find('img.unfollow').each(function(i) {
                                    if($(this).attr('id').indexOf(column+"_"+userID) != -1) {
                                        $(this).attr("src", "images/buttons/Button_Follow.gif");
                                        $(this).addClass("follow");
                                        $(this).removeClass("unfollow");
                                    }
                                });
                                
                                // Show confirmation of follow                        
                                softMessage("You are no longer following this member's "+phrase+".");
                                
                                // Reload page
                                window.location = document.location.href;  
                            } else {
                                softAlert("You do not have permission to no longer follow this member's "+phrase+".", alertOff);
                            }
                        } else {
                            softAlert("A "+textStatus+" error occured.  Please try again.", alertOff);    
                        }
                    }
                );
            },
            alertOff);      
    });
});