﻿if (!SageScripts) var SageScripts = {};
SageScripts.ImageHelper = {};
SageScripts.ImageHelper.ImageLoad = function(img, size) {
    size = !size ? 128 : size;
    var width = $(img).width();
    var height = $(img).height();
    $(img).width(null);
    $(img).height(null);
    if (width > height) {
        if (width > size) {
            $(img).width(size);
        }
    }
    else {
        if (height > size) {
            $(img).height(size);
        }
    }
}

SageScripts.ImageHelper.ImageError = function(img) {
    img.src = "/css/images/NoImage.png"; img.onerror = ""; return true;
}

SageScripts.FixSortableColumns = function() {
    var headers = $("th.igtbl_Header nobr");
    $(headers).find("img[imgType='sort']").each(
                    function(i) {
                        $(this).parent("nobr").prepend($(this));
                    }
                );

    var headers2 = $("th.igtbl_Header.SortableNoneColumn nobr");
    $(headers2).find("img[imgType='sort']").each(
        function(i) {
            $(this).remove();
        }
      );
}

SageScripts.ShowPopup = function(wdw) {
    wdw.set_windowState($IG.DialogWindowState.Normal);
};
SageScripts.ClosePopup = function(wdw) {
    wdw.set_windowState($IG.DialogWindowState.Hidden);
};

SageScripts.CheckBoxDropDownList = {};
SageScripts.CheckBoxDropDownList.EmptyText = ' - Select - ';
SageScripts.CheckBoxDropDownList.Go = function(i) {
    var label = $(this).parent().children("label");

    if (label.length == 0) {
        label = $(this).parent().children("span");
    }
    var text = $(label).text();

    // Strip count from labels
    while (text.indexOf("(") >= 0) {
        text = text.replace(text.substring(text.indexOf(" ("), text.indexOf(")")), "");
        text = text.replace(")", "");
    }

    var parent = $(this).parents('div.CheckBoxDropDownList');
    SageScripts.CheckBoxDropDownList.ddl = $(parent).find('span.DropDownListValue');
    var title = $(parent).find('h6.Title').text();

    // The following is currently causing a javascript failure
    // Do not add to list if all child nodes of parent are checked
    //var id = $(this).parent().attr("id");
    //var node = igtree_getNodeById(id);
    //    var nodeParent = node.getParent();
    //    var allSiblingsChecked = true;
    //    var hasChildren = false;
    //    var allChildrenChecked = true;
    //    if (nodeParent) {
    //        //Check for all siblings being checked
    //        var nodes = nodeParent.getChildNodes();
    //        for (i = 0; i < nodes.length; i++) {
    //            if (!nodes[i].getChecked()) {
    //                allSiblingsChecked = false;
    //                break;
    //            }
    //        }
    //    }
    //    else {
    //        var nodes = node.getChildNodes();
    //        if (nodes.length > 0) {
    //            hasChildren = true;
    //            for (i = 0; i < nodes.length; i++) {
    //                if (!nodes[i].getChecked()) {
    //                    allChildrenChecked = false;
    //                    break;
    //                }
    //            }
    //        }
    //    }
    //    if (nodeParent && !allChildrenChecked) {
    //        // add the current node
    //        alert('has parent, but not all siblings checked');
    //    }
    //    else if (!nodeParent && hasChildren && allChildrenChecked) {
    //        // add current node + (All)
    //        text = text + ' (All)';
    //        alert('does not have parent, has children, and all children checked');
    //    }


    var currentText = $(SageScripts.CheckBoxDropDownList.ddl).text();
    //if (currentText != SageScripts.CheckBoxDropDownList.EmptyText) {
    if (currentText != title) {
        $(SageScripts.CheckBoxDropDownList.ddl).text(currentText + ', ' + text);
    }
    else {
        $(SageScripts.CheckBoxDropDownList.ddl).text(text);
    }
    $(SageScripts.CheckBoxDropDownList.ddl).attr('title', title + ': ' + $(SageScripts.CheckBoxDropDownList.ddl).text());
}

SageScripts.CheckBoxDropDownList.SelectUnselect = function(item, dtree) {
    return;
    var tree = igtree_getTreeById(dtree.id);
    var isChecked = $(item).attr("checked");
    var id = $(item).parent().attr("id");
    var ss = id.split("_");
    var nodes;

    if (ss.length == 3) {
        if (!isChecked) {
            //nodes = $("div[id$='" + ss[0] + "_" + ss[1] + "']").children("input");
            /*for (i = 0; i < nodes.length; i++) {
            nodes[i].click();
            }*/
            //$("div[id$='" + ss[0] + "_" + ss[1] + "']").children("input").attr('checked', false);
        }
    }
    else if (ss.length == 2) {
        nodes = $("div[id*='" + ss[0] + "_" + ss[1] + "']").children("input");
        /*for (i = 0; i < nodes.length; i++) {
        nodes[i].click();
        }*/
        //$("div[id*='" + ss[0] + "_" + ss[1] + "']").children("input").attr('checked', isChecked);
    }


}

SageScripts.CheckBoxDropDownList.isScript = false;
SageScripts.CheckBoxDropDownList.TreeNodeChecked = function(treeName, id, bChecked) {
    if (SageScripts.CheckBoxDropDownList.isScript) {
        return;
    }

    var parent = $("div[id*='" + treeName + "']").parents('div.CheckBoxDropDownList');
    var isSelectUnselectEnabled = $(parent).attr("IsSelectUnselectEnabled");
    if (isSelectUnselectEnabled != "1") {
        return;
    }

    SageScripts.CheckBoxDropDownList.isScript = true;

    var node = igtree_getNodeById(id);
    var nodeParent = node.getParent();
    if (nodeParent) {
        //The commented section below will leave the parent node checked if it has any child nodes selected
        nodeParent.setChecked(bChecked);

        if (!bChecked) {
            //leave parent checked if any child nodes are still checked
            var nodes = nodeParent.getChildNodes();
            for (i = 0; i < nodes.length; i++) {
                if (nodes[i].getChecked())
                    nodeParent.setChecked(true);
            }
        }
    }
    else {
        var nodes = node.getChildNodes();
        for (i = 0; i < nodes.length; i++) {
            nodes[i].setChecked(bChecked);
        }
    }

    SageScripts.CheckBoxDropDownList.isScript = false;
    SageScripts.CheckBoxDropDownList.GoAll();
    //alert(treeName);
}

SageScripts.CheckBoxDropDownList.Click = function() {

    var parent = $(this).parents('div.CheckBoxDropDownList');

    var cbl = $(parent).children('div.CheckBoxList');
    //var tree = $(cbl).children('div.inner').children('div.treeview');
    var isSelectUnselectEnabled = $(parent).attr("IsSelectUnselectEnabled");
    //    if (isSelectUnselectEnabled && isSelectUnselectEnabled == "1") {
    //        SageScripts.CheckBoxDropDownList.SelectUnselect(this);
    //    }


    //SageScripts.CheckBoxDropDownList.ddl = $(parent).find('span.DropDownListValue');


    SageScripts.CheckBoxDropDownList.SetDefaultTitle(this);

    //$(SageScripts.CheckBoxDropDownList.ddl).text(SageScripts.CheckBoxDropDownList.EmptyText);
    $(cbl).find('input:checked').each(SageScripts.CheckBoxDropDownList.Go);
}
SageScripts.CheckBoxDropDownList.Bind = function(id) {
    var jID = '#' + id
    $(jID + " div.CheckBoxList input:checkbox").click(SageScripts.CheckBoxDropDownList.Click);
}

SageScripts.CheckBoxDropDownList.GoAll = function() {
    var obj = $('div.CheckBoxDropDownList div.CheckBoxList').each(function(i) {

        SageScripts.CheckBoxDropDownList.SetDefaultTitle(this);

        //$(SageScripts.CheckBoxDropDownList.ddl).text(SageScripts.CheckBoxDropDownList.EmptyText);
        //SageScripts.CheckBoxDropDownList.ddl = $(CheckBoxDropDownList).find('span.DropDownListValue');
        //var title = $(parent).find('h6.Title').text();
        //$(SageScripts.CheckBoxDropDownList.ddl).text(title);
        $(this).find('input:checked').each(SageScripts.CheckBoxDropDownList.Go);

    });
}

SageScripts.CheckBoxDropDownList.SetDefaultTitle = function(p) {

    var parent = $(p).parents('div.CheckBoxDropDownList');
    SageScripts.CheckBoxDropDownList.ddl = $(parent).find('span.DropDownListValue');
    var title = $(parent).find('h6.Title').text();
    $(SageScripts.CheckBoxDropDownList.ddl).text(title);
    
}

SageScripts.ShowingWindow = false;

SageScripts.ShowWindow = function(behaviorID) {
	SageScripts.ShowingWindow = true;
	SageScripts.StopFlashPlayback();
    $find(behaviorID).show();
}

SageScripts.StopFlashPlayback = function() {
	var e = SageScripts.GetCarousel();
	if( e ) if( e.PausePlayback ) e.PausePlayback();
}

SageScripts.StartFlashPlayback = function() {
	var e = SageScripts.GetCarousel();
	if( e ) if( e.ResumePlayback ) e.ResumePlayback();
}

SageScripts.GetCarousel = function() {
  var movieName = "SolutionRibbon";
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}

