﻿jQuery.ajaxSettings.cache = false;
var mark = 0;

 $(document).ready(function() {
    $("#gutter_content_wide").sortable({ handle: ".widget_name", placeholder: "hover", revert: false, update: function(e, ui) {
        setTimeout("save_widget_positions_('false');",100);
    }});
});

/**
 * Opens/Closes the gutter
 *
 * @return bool Returns false to deactivate the link
 */
function gutter_toggle() {
    $("#gutter_open").hide();
    
    if (SHOW_GUTTER_CONTENT == 1) {
        // Close the gutter
        $("#gutter_collapse").fadeOut(200);
        $(".customize_link").fadeOut(500);
        var items = $("#gutter_content_wide").children().length;
        $("#gutter_content_wide").children().fadeOut(500, function() {
            if (--items == 0) {
                $("#gutter_content_wide").animate({"width": "13px"}, 500, function() {
                    $("#gutter_collapse").attr({"src": IMGDIR_BUTTON + "/collapse_h_collapsed.gif"});
                    $("#gutter_open").show();
                    $("#gutter_collapse").show();
                });
            }
        });    
    } else {
        // Open the gutter
        //$("#gutter_content_wide").animate({"width": (is_ie) ? "239px" : "234px"}, function() {
        $("#gutter_content_wide").animate({"width": (is_ie) ? "262px" : "262px"}, function() {
        
            load_gutter_content(function() {
                $(".customize_link").show();
                $(".customize_link_collapsed").removeClass("customize_link_collapsed").addClass("customize_link");
                $("#gutter_collapse").attr({"src": IMGDIR_BUTTON + "/collapse_h.gif"});
                $("#gutter_open").show();
            });
        });        
    }
    
    gutter_set_show();
    return false;
}

/**
 * Ajax call to save the users show_gutter selection to the database
 */
function gutter_set_show() {
    SHOW_GUTTER_CONTENT = SHOW_GUTTER_CONTENT == 1 ? 0 : 1
    $.post(APIURL, {"do": "set_show_gutter", show_gutter: SHOW_GUTTER_CONTENT});
}

/**
 * Ajax call to save the positions of the widgets
 */
function save_widget_positions_() {
    var data = "";

//    $("#gutter_content .widget").each(function(i) {
//        data += this.id + "_g_" + (i + 1) + ";";
//    });

//    $.ajax({
//        url:  APIURL,
//        type: "POST",
//        data: "do=save_widget_positions&data=" + data
//    });
}

/**
 * Ajax call to fill the gutter with content when the gutter is opened
 */
function load_gutter_content(callback) {
    $.getJSON(APIURL + "?do=get_gutter_content",
        function(data) {
            $("#gutter_content").html(data.params);
            if (SHOW_GUTTER_SORT) $("#gutter_content_wide").sortable("refresh");
            
            if (callback) callback();
        }
    );
}

/**
 * Ajax call to fill the widget with content when it's reloaded or added
 */
//function load_widget_content_old(widgetid) {
//    $.getJSON(APIURL + "?do=get_widget_content&widgetid=" + widgetid,
//        function(data) {
//            $("#widget"+widgetid).after(data.params).remove(); 
//            /* TODO: I think the brief duplication of ID might cause that slight
//             * flicker on widget add that I'm seeing. Is there a way to parse
//             * out the child elements and just use those as the inner html? */
//            if (SHOW_GUTTER_SORT) $("#gutter_content").sortable("refresh");
//        }
//    );
//}
function load_widget_content(widgetid) {
    $.getJSON("rssoutput.aspx?wigId="+widgetid,widgetid,
        //$.getJSON("rssoutputtest.aspx",
        function(data) {
            $("#widget"+widgetid).after(data.params).remove(); 
            /* TODO: I think the brief duplication of ID might cause that slight
             * flicker on widget add that I'm seeing. Is there a way to parse
             * out the child elements and just use those as the inner html? */
            if (SHOW_GUTTER_SORT) $("#gutter_content_wide").sortable("refresh");
        }
    );
}



/**
 * Inits widget, hides divs and such
 */
function widgets_init(img, content){
    $("." + img).click(function () {
        if (mark == 0) {
            mark = 1;
            $(this).parent().parent().find("." + content).toggle("slow", function(){ mark = 0; });
            $fn = $(this).find('img').attr('src');

            if ($fn == IMGDIR_BUTTON + '/plus.gif') {
                $(this).find('img').attr({src: IMGDIR_BUTTON + '/minus.gif'});
            } else{
                $(this).find('img').attr({src: IMGDIR_BUTTON + '/plus.gif'});
            }
        }
        
        return false;
    });
}

/**
 * Fetches a widget options page and populates a popup box with it
 * @param string widgetid Id of the widget
 *
 * @return bool Returns false to avoid the processing of the click
 */
function get_widget_options(widgetid) {
    $.getJSON(APIURL + "?do=get_widget_options&widgetid=" + widgetid,
        function(data) {
            $.facebox(data.params);
        }
    );
    
    return false;
}

/**
 * Runs an Ajax call to save the widget options to the database
 * @param string widgetid Id of the widget
 */
function save_widget_options(widgetid) {
    $(".option_content").find(".widget_options_error").css("visibility", "hidden")
    
    $.getJSON(
        APIURL,
        {"do": "save_widget_options","widgetid": widgetid, "options": $("#widget_options_form").serialize()},
        function(data) {
            if (data.params == "1") {
                $.facebox.close();
                load_widget_content(widgetid);
            } else {
                $("#errormessages").text("");
                $.each(data.params, function (i, item) {
                    $("#error_" + i).css("visibility", "visible");
                    $("#errormessages").append(item);
                    $("#errormessages").append('<br />');
                });
            }
        }
    );

    return false;
}

/**
 * Adds Widget to gutter and calls save funtion
 * @param string widgetid Id of the widget to add
 */
function add_wide_widget(widgetid){
    $("#gutter_content_wide").append('<div id="widget' + widgetid + '" class="widget_wide">&nbsp;</div>');
    //$("#add_link_" + widgetid).hide();
    //$("#remove_link_" + widgetid).show();
    //save_widget_positions();
    load_widget_content(widgetid);
    return false;
 }

/**
 * Removes a widget from the gutter
 * @param string widgetid Id of the widget
 */
function close_wide_widget(widgetid) {
    $("#widget" + widgetid).remove();
    //save_widget_positions();
    $("#added_link_" + widgetid).hide();
    $("#remove_link_" + widgetid).hide();
    $("#add_link_" + widgetid).show();
    return false;
}

/**
 * Runs an Ajax call to save the widget maximization state to the database
 * @param string widgetid Id of the widget 
 */
function minimize_wide_widget(widgetid) {
    $("#widget_body" + widgetid).toggle("fast");

    if($("#widgetminimize" + widgetid).attr('src') == IMGDIR_MISC + "/widget_collapse.gif") {
        $("#widgetminimize" + widgetid).attr({"src": IMGDIR_MISC + "/widget_expand.gif"})    
    } else {
        $("#widgetminimize" + widgetid).attr({"src": IMGDIR_MISC + "/widget_collapse.gif"})    
    }

   // $.post(APIURL, {"do": "save_widget_maximization_state", "widgetid": widgetid});
    return false;
}


