﻿/// <reference path="jquery-1.4.1.min-vsdoc.js" />
jQuery.fn.listBox = function (command, name, value) {

    var div = $(this);
    if (command == undefined) {
        // create list 


        $(this).before("<a class=\"__listBoxDelete cms_icon cms_delete cms_floatleft\" href=\"javascript:;\">X</a>");
        $(".__listBoxDelete").click(function () {
            $(this).next().find(".listBoxItem[selected='true']").remove();

        });
        return;
    }

    if (command == "add") {

        if (div.find(".listBoxItem[value='" + value + "']").length > 0)
            return;

        div.append("<div class=\"listBoxItem\" value=\"" + value + "\">" + name + "</div>");
        $(".listBoxItem").attr("selected", "false");
        $(".listBoxItem").unbind();
        $(".listBoxItem").click(function () {
            $(this).parent("div").find(".listBoxItem").attr("selected", "false");
            $(this).parent("div").find(".listBoxItem").css("background-color", "");
            $(this).css("background-color", "red");
            $(this).attr("selected", "true");

        });
        return;
    }
    if (command == "remove") {
        div.find("[value='" + name + "']").remove();
        return;
    }
    if (command == "text") {
        return $(this).find(".listBoxItem[selected='true']").html();
        return;
    }
    if (command == "value") {
        return $(this).find(".listBoxItem[selected='true']").attr("value");
    }
    if (command == "values") {
        var values = new Array();
        $(this).find(".listBoxItem").each(function () {
            values.push($(this).attr("value"));
        });
        return values;
    }
    if (command == "clear") {
        $(this).find(".listBoxItem").remove();
        return;
    }
    if (command == "hidedelete") {
        $(".__listBoxDelete").hide();
    }
    if (command == "modify") {
        $(this).find(".listBoxItem[value='" + name + "']").html(value);
    }
};
// Creating Pager
jQuery.fn.clientPager = function (index, count, virCount, callback) {

    if (virCount <= count) {
        $(this).hide();
        return;

    }
    else
        $(this).show();
    $(this).html("");
    for (var i = 0; i < virCount; i += count) {
        $(this).append("<a href=\"javascript:;\" class=\"__pagerButton\" value =\"" + i + "\">" + (Math.round(i / count) + 1) + "</a> ");
    }
    $(this).find("a[value='" + index + "']").addClass("selectedPage");
    $(".__pagerButton").click(function () {
        $(this).parents("div").find("a").removeClass("selectedPage");
        $(this).addClass("selectedPage");
    });
    $(this).find(".__pagerButton").click(callback);
};

jQuery.fn.JStreeView = function (command, json, selector) {
    if (typeof command != String) {
        // initialize 
    }
    if (command == "add") {
        if (selector == null) {
            selector = $(this).children("li");
            $(selector).addClass(json["class"]);
        }

        //add to root 
        //   $(this).append("<li id=\"" + json.name + "\" class=\"" + json.class + "\" ><span class=\"treespan\" >" + json.value + "</span></li>");

        if ($(selector).find("ul").length == 0) {
            $(selector).append("<ul></ul>");
        }
        else {
            //  $(selector).find("ul").html("");
        }
      //  var image = "<img src=\"/thumbgen.ashx?" + json.encodedpath + "/30/30/w" + "\" ?>";
 
        $(selector).find("ul").append("<li id=\"" + json.name + "\" class=\"" + json["class"] + "\" ><span class=\"treespan\">" +  json.value + "</span></li>");
    }
    if (command == "onclick") {
        $(this).find(".treespan").unbind("click");
        $(this).find(".treespan").click(json);
    }

};

jQuery.fn.FileNameField = function (command, json, selector) {
    $(this).keypress(function (event) {
       
        if ((";/?:@&=+$,\\&\"\'!").search(String.fromCharCode(event.which)) > -1) {
         
            event.preventDefault();
        }




    });




}; 

jQuery.extend({
    JConfirm: function (itemid, posturl, msg, yesCallback, noCallBack, arg) {
        datastore("itemid", itemid);
        datastore("posturl", posturl);
        $("body").append("<div id=\"divDeleteConfirm\" >" + msg + "</div>");
        $("#divDeleteConfirm").dialog({ modal: true, close: function () {
            $(this).dialog("destroy");
            $("#divDeleteConfirm").remove();
        },
            buttons: {
                No: function () {
                    $("#divDeleteConfirm").dialog("close");
                    if (noCallBack == undefined || noCallBack == null)
                        return;
                    else
                        noCallBack();
                },

                Yes: function () {
                    $(this).dialog("destroy");
                    $("#divDeleteConfirm").remove();
                    $.post(datastore("posturl") + datastore("itemid"), { itemid: datastore("itemid"), arg: arg }, function (data) {
                        if (data == "1") {
                            var itemid = datastore("itemid");
                            datastore("itemid", null);
                            datastore("posturl", null);
                            if (yesCallback == undefined || yesCallback == null)
                                window.location = window.location;
                            else
                                yesCallback({ itemid: itemid });
                        }
                        else {
                            ShowMessage(data);
                        }
                    });
                }

            }
        });


    }
});
