
(function ($) {

    //------------------------------------------------------------------------------------------
    // build SAGE command model 
    //------------------------------------------------------------------------------------------
    // parameters : 
    //------------------------------------------------------------------------------------------
    // return : SAGE commands group list
    //------------------------------------------------------------------------------------------
    $.fn.buildSageCmd = function (filter,page) {
        var groupList = new Array();

       // window.external.Groups exist, we build it with  with com interface
        if ((window.external !== null) && (typeof window.external != 'undefined') && (window.external.Groups !== null) && (typeof window.external.Groups != 'undefined')) {
            groupList = $.fn.buildComSageCmd(filter,page);
       }
       else {
            groupList = $.fn.buildMokeSageCmd(filter);
            //throw "window.external.Groups is undefined !";
       }

        return groupList;
    }

    //------------------------------------------------------------------------------------------
    // build SAGE command model with a moke
    //------------------------------------------------------------------------------------------
    // parameters : 
    //------------------------------------------------------------------------------------------
    // return : SAGE commands group list
    //------------------------------------------------------------------------------------------
    $.fn.buildMokeSageCmd = function (filter) {

        var groupList = new Array();
        var cmd = {};
        var group1 = new $.fn.sageGroupModel("Compta");
        for (var i = 1; i <= 3; i++) {
            cmd = new $.fn.sageCmdModel();
            cmd.id = i;
            cmd.name = "Compta " + i.toString();
            cmd.tips = "commande compta " + i.toString();
            cmd.longName = "Compta " + i.toString() + " long";
            cmd.backgroudColor = 0x249C23;
            cmd.enable = true;
			cmd.url = '';
            group1.cmdList.push(cmd);
        }

        var group2 = new $.fn.sageGroupModel("Gescom");
        for (var i = 1; i <= 8; i++) {
            cmd = new $.fn.sageCmdModel();
            cmd.id = i;
            cmd.name = "Gescom " + i.toString();
            cmd.tips = "commande gescom " + i.toString();
            cmd.longName = "Compta " + i.toString() + " long";
            cmd.backgroudColor = 0x249C23;
            cmd.enable = true;
			cmd.url = '';
            group2.cmdList.push(cmd);
        }

        var group3 = new $.fn.sageGroupModel("Divers");
        for (var i = 1; i <= 2; i++) {
            cmd = new $.fn.sageCmdModel();
            cmd.id = i;
            cmd.name = "Divers " + i.toString();
            cmd.tips = "commande divers " + i.toString();
            cmd.longName = "Compta " + i.toString() + " long";
            cmd.backgroudColor = 0x249C23;
            cmd.enable = false;
			cmd.url = '';
            group3.cmdList.push(cmd);
        }

        
        if (filter == null || ($.isArray(filter) && $.inArray(group1.name, filter) >= 0)) {
            groupList.push(group1);
        }
        if (filter == null || ($.isArray(filter) && $.inArray(group2.name, filter) >= 0)) {
            groupList.push(group2);
        }
        if (filter == null || ($.isArray(filter) && $.inArray(group3.name, filter) >= 0)) {
            groupList.push(group3);
        }
        
        return groupList;
    }

    //------------------------------------------------------------------------------------------
    // build SAGE command model with a moke
    //------------------------------------------------------------------------------------------
    // parameters : 
    //------------------------------------------------------------------------------------------
    // return : SAGE commands group list
    //------------------------------------------------------------------------------------------
    $.fn.buildComSageCmd = function (filter,page) {
        var groupList = new Array();

        var groupCount = window.external.Groups.count;
        if (groupCount > 0) {
            for (var i = 1; i <= groupCount; i++) {
                var comGroup = window.external.Groups.Item(i);
                if (comGroup.PageName == page) {
                    var group = new $.fn.sageGroupModel(comGroup.Name);
					group.pageName = comGroup.PageName
                    var cmdCount = comGroup.Commands.count;
                    if (cmdCount > 0) {
                        for (var j = 1; j <= cmdCount; j++) {
                            var comCmd = comGroup.Commands.Item(j);

                            if (typeof comCmd.id == "undefined") {
                                throw "Sage command : Id is undefined !";
                            }
                            if (typeof comCmd.name == "undefined") {
                                throw "Sage command : Name is undefined !";
                            }
                            if (typeof comCmd.longName == "undefined") {
                                throw "Sage command : LongName is undefined !";
                            }
                            if (typeof comCmd.tips == "undefined") {
                                throw "Sage command : Tips is undefined !";
                            }
                            if (typeof comCmd.enable == "undefined") {
                                throw "Sage command : Enable is undefined !";
                            }
                            if (typeof comCmd.backColor == "undefined") {
                                throw "Sage command : BackColor is undefined !";
                            }
                            if (typeof comCmd.count == "undefined") {
                                throw "Sage command : Count is undefined !";
                            }
                            var cmd = new $.fn.sageCmdModel();
                            cmd.id = comCmd.Id;
                            cmd.name = comCmd.Name;
                            cmd.longName = comCmd.LongName;
                            cmd.tips = comCmd.Tips;
                            cmd.enable = comCmd.Enable;
                            cmd.backgroudColor = comCmd.BackColor;
                            cmd.count = comCmd.count;
							cmd.icon = comCmd.Icon;
							cmd.url = comCmd.Url;						
                            group.cmdList.push(cmd);
                        }
                    }
                    groupList.push(group);
                }
            }
        }
        return groupList;
    }
	
    $.fn.getGroupCount = function (page) {
	    var groupPageCount = 0;
        var groupCount = window.external.Groups.count;
        if (groupCount > 0) {
            for (var i = 1; i <= groupCount; i++) {
                var comGroup = window.external.Groups.Item(i);
                if (comGroup.PageName == page) {
                   groupPageCount++;
                }
            }
        }
        return groupPageCount;
    }	

})(jQuery);

