﻿/// <reference path="jquery-1.4.min.vsdoc.js" />"

function magicRadios($ctx) {
    var updateBackground = function() {
        $(this).toggleClass("cbDisabled", !$(":radio", this).attr("checked"));
    };

    var $links = $ctx.find(".ruleTable .cbLink");
    
    $links.find("input:radio").hide();
    $links.each(updateBackground);

    $links.live("click", function() {
        var $tr = $(this).parent("td").parent("tr");
        var radio = $(":radio", this);
        radio.attr("checked", true);
        $(".sf-overriden", $tr).attr("checked", radio.val() != $("input[name$=Base]", $tr).val());
        $(".cbLink", $tr).each(updateBackground);
    });
}

function magicCheckBoxes($ctx) {
    var updateBackground = function() {
        $(this).toggleClass("cbDisabled", !$(":checkbox", this).attr("checked"));
    };

    var $links = $ctx.find(".ruleTable .cbLink");

    $links.find("input:checkbox").hide();
    $links.each(updateBackground);

    $links.bind("mousedown", function() { this.onselectstart = function() { return false }; });
    $links.live("click", function(evt) {
        var $tr = $(this).parent("td").parent("tr");

        var cb = $(":checkbox", this);

        if (!evt.shiftKey) {
            $(".cbLink :checkbox", $tr).attr("checked", false);
            cb.attr("checked", true);
        } else {
            var num = $(".cbLink :checkbox:checked", $tr).length
            if (!cb.attr("checked") && num == 1)
                cb.attr("checked", true);
            else if (cb.attr("checked") && num >= 2)
                cb.attr("checked", false);
        }

        var total = $.map($(".cbLink :checkbox:checked", $tr), function(a) { return $(a).attr("tag"); }).join(",");

        $(".sf-overriden", $tr).attr("checked", total != $("input[name$=Base]", $tr).val());
        $(".cbLink", $tr).each(updateBackground);
    });
}

function treeView() 
{
    $(".ruleTable a.namespace").live("click", function() {
        var tv = $(".tvExpandedLast,.tvClosedLast", this).toggleClass("tvExpandedLast").toggleClass("tvClosedLast");
        var tv = $(".tvExpanded,.tvClosed", this).toggleClass("tvExpanded").toggleClass("tvClosed");
        var ns = $("span.namespace", this).html();
        $(".ruleTable tr").filter(function() {
            return $("td > span.namespace", this).html() == ns;
        }).toggle();
        return false;
    });
}

function openDialog(e) {
    var $me = $(this);
    var navigator = new SF.ViewNavigator({
        controllerUrl: $me.attr("href"),
        requestExtraJsonData: null,
        type: 'PropertyRulePack',
        onCancelled: function () {
            $me.children("div").css("opacity", 0.5);
        },
        onLoaded: function (divId) {
            magicRadios($("#" + divId));

        }
    });
    navigator.createSave();
    e.preventDefault();
}

function postDialog(controllerUrl, prefix) {
    new SF.PartialValidator({ controllerUrl: controllerUrl, prefix: prefix }).trySave();
}
@@ -350,38 +350,30 @@
 %0A    $links.
-live(%22
 click
-%22, 
+(
 function() %7B
@@ -464,24 +464,82 @@
 o%22, this);%0D%0A
+        $(%22.cbLink :radio%22, $tr).attr(%22checked%22, false);%0D%0A
         radi
@@ -1139,30 +1139,22 @@
  $links.
-live(%22
 click
-%22, 
+(
 function
@@ -2875,26 +2875,24 @@
 + divId));%0D%0A
-%0D%0A
         %7D%0D%0A 

fix radios in popup on AuthAdmin