Difference between revisions of "MediaWiki:Common.js"

From Wikicarpedia
Jump to navigation Jump to search
m (added disclaimer button)
m (modify disclaimer)
Line 336: Line 336:
'tools': {
'tools': {
'disclaimer': {
'disclaimer': {
label: 'footer',  
label: 'encapsulate',  
type: 'button',
type: 'button',
icon: '//wikicarpedia.com/images/d/d4/Exclamation2.png',
icon: '//wikicarpedia.com/images/6/6c/Toolbar_disclaimer.png',
action: {
action: {
type: 'encapsulate',
type: 'encapsulate',

Revision as of 14:31, 2 November 2018

var customizeToolbar = function () {
/* --------------------------------------------------------------------------- */

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'sections': {
		'WICA': {
			'type': 'toolbar', // Can also be 'booklet'
			'label': 'WICA'
			// or 'labelMsg': 'section-emoticons-label' for a localized label
		}
	}
} );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'section': 'WICA',
	'groups': {
		'icons': {
			'label': '' // or use labelMsg for a localized label, see above
		}
	}
} );


$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'section': 'WICA',
	'group': 'icons',
	'tools': {
		'ref': {
			label: '<ref>', 
			type: 'button',
			icon: '//wikicarpedia.com/images/7/77/Toolbar_ref.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: "<ref>",
                                        peri: "",
                                        post: "</ref>"
				}
			}
		}
	}
} );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'section': 'WICA',
	'group': 'icons',
	'tools': {
		'smile': {
			label: 'Padlock', 
			type: 'button',
			icon: '//wikicarpedia.com/images/thumb/7/7b/Icon_Locked_Red.png/16px-Icon_Locked_Red.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: "{{IconPadlock}}" // text to be inserted
				}
			}
		}
	}
} );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'section': 'WICA',
	'group': 'icons',
	'tools': {
		'houserule': {
			label: 'House rule', 
			type: 'button',
			icon: '//wikicarpedia.com/images/thumb/f/fb/Icon_House_Black.png/16px-Icon_House_Black.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: "{{IconHouse}}" // text to be inserted
				}
			}
		}
	}
} );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'section': 'WICA',
	'group': 'icons',
	'tools': {
		'arrow': {
			label: 'arrow', 
			type: 'button',
			icon: '//wikicarpedia.com/images/thumb/6/6d/Icon_Double_Arrow_Black.png/16px-Icon_Double_Arrow_Black.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: "{{IconArrow}}" // text to be inserted
				}
			}
		}
	}
} );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'section': 'WICA',
	'group': 'icons',
	'tools': {
		'book': {
			label: 'book', 
			type: 'button',
			icon: '//wikicarpedia.com/images/thumb/e/ea/Icon_Open_Book.png/16px-Icon_Open_Book.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: "{{IconBook}}" // text to be inserted
				}
			}
		}
	}
} );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'section': 'WICA',
	'group': 'icons',
	'tools': {
		'world': {
			label: 'World', 
			type: 'button',
			icon: '//wikicarpedia.com/images/thumb/7/73/Icon_World_Black.png/16px-Icon_World_Black.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: "{{IconWorld}}" // text to be inserted
				}
			}
		}
	}
} );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'section': 'WICA',
	'group': 'icons',
	'tools': {
		'yellow': {
			label: 'yellow', 
			type: 'button',
			icon: '//wikicarpedia.com/images/2/2d/Toolbar_yellow.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: "{{ColorYellow|",
                                        peri: "",
                                        post: "}}"
				}
			}
		}
	}
} );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'section': 'WICA',
	'group': 'icons',
	'tools': {
		'red': {
			label: 'red', 
			type: 'button',
			icon: '//wikicarpedia.com/images/1/1b/Toolbar_red.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: "{{ColorRed|",
                                        peri: "",
                                        post: "}}"
				}
			}
		}
	}
} );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'section': 'WICA',
	'group': 'icons',
	'tools': {
		'blue': {
			label: 'blue', 
			type: 'button',
			icon: '//wikicarpedia.com/images/e/e2/Toolbar_blue.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: "{{ColorBlue|",
                                        peri: "",
                                        post: "}}"
				}
			}
		}
	}
} );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'section': 'WICA',
	'group': 'icons',
	'tools': {
		'black': {
			label: 'black', 
			type: 'button',
			icon: '//wikicarpedia.com/images/8/8b/Toolbar_black.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: "{{ColorBlack|",
                                        peri: "",
                                        post: "}}"
				}
			}
		}
	}
} );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'section': 'WICA',
	'group': 'icons',
	'tools': {
		'green': {
			label: 'green', 
			type: 'button',
			icon: '//wikicarpedia.com/images/5/55/Toolbar_green.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: "{{ColorGreen|",
                                        peri: "",
                                        post: "}}"
				}
			}
		}
	}
} );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'section': 'WICA',
	'group': 'icons',
	'tools': {
		'gray': {
			label: 'gray', 
			type: 'button',
			icon: '//wikicarpedia.com/images/4/41/Toolbar_grey.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: "{{ColorGray|",
                                        peri: "",
                                        post: "}}"
				}
			}
		}
	}
} );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'section': 'WICA',
	'group': 'icons',
	'tools': {
		'white': {
			label: 'white', 
			type: 'button',
			icon: '//wikicarpedia.com/images/7/7a/Toolbar_white.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: "{{ColorWhite|",
                                        peri: "",
                                        post: "}}"
				}
			}
		}
	}
} );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'section': 'WICA',
	'group': 'icons',
	'tools': {
		'pink': {
			label: 'pink', 
			type: 'button',
			icon: '//wikicarpedia.com/images/1/15/Toolbar_pink.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: "{{ColorPink|",
                                        peri: "",
                                        post: "}}"
				}
			}
		}
	}
} );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'section': 'WICA',
	'group': 'icons',
	'tools': {
		'calendar': {
			label: 'calendar', 
			type: 'button',
			icon: '//wikicarpedia.com/images/9/9e/Toolbar_calendar.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: "{{Year|",
                                        peri: "",
                                        post: "}}"
				}
			}
		}
	}
} );


$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'section': 'WICA',
	'group': 'icons',
	'tools': {
		'footer': {
			label: 'footer', 
			type: 'button',
			icon: '//wikicarpedia.com/images/f/f7/Toolbar_footer.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: "{{FootnoteIconPara_en}}"
				}
			}
		}
	}
} );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
	'section': 'WICA',
	'group': 'icons',
	'tools': {
		'disclaimer': {
			label: 'encapsulate', 
			type: 'button',
			icon: '//wikicarpedia.com/images/6/6c/Toolbar_disclaimer.png',
			action: {
				type: 'encapsulate',
				options: {
					pre: "{{Disclaimer|",
                                        peri: "",
                                        post: "}}"
				}
			}
		}
	}
} );



/* --------------------------------------------------------------------------- */

};


/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar … */
if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {
	mw.loader.using( 'user.options' ).then( function () {
		// This can be the string "0" if the user disabled the preference ([[phab:T54542#555387]])
		if ( mw.user.options.get( 'usebetatoolbar' ) == 1 ) {
			$.when(
				mw.loader.using( 'ext.wikiEditor' ), $.ready
			).then( customizeToolbar );
		}
	} );
}

/* 
==========================================
 @meepledrone - Dynamic text selection 
==========================================
*/


// Open and close buttons
var BUTTON_OPEN = "[+]",
    BUTTON_CLOSE = "[-]";

// Constants for hooks in HTML
var EXPANSION_SELECTOR_ID = "_Expansion_Selector_",
    SHOW_WHEN_DONE_CLASS = "showWhenDone",
    HIDE_WHEN_DONE_CLASS = "hideWhenDone",
    CITE_ID_PREFIX = "cite_note-";

// Base expansion info
var ItemTree = 
[
  { 
    label: "Expansion 1 - Inns And Cathedrals",
    inputFieldId: "inputInnsAndCathedrals",
    category: "C1",
    selected: false,
    open: false,
    children: 
    [
      {
        label: "Inns",
        inputFieldId: "inputInns",
        className: "showInns"
      },
      {
        label: "Cathedrals",
        inputFieldId: "inputCathedrals",
        className: "showCathedrals"
      },
      {
        label: "Big Follower",
        inputFieldId: "inputBigFollower",
        className: "showBigFollower"
      },
      {
        label: "Pig",
        inputFieldId: "inputPig",
        className: "showPig"
      },
    ]
  },
  { 
    label: "Expansion 2 - Traders And Builders",
    inputFieldId: "inputTradersAndBuilders",
    category: "C1",
    selected: false,
    open: false,
    children: 
    [
      {
        label: "Trade good tokens",
        inputFieldId: "inputTraders",
        className: "showTraders"
      },
      {
        label: "Builder",
        inputFieldId: "inputBuilder",
        className: "showBuilder"
      },
    ]
  },
  { 
    label: "Expansion 3 - The Princess And The Dragon",
    inputFieldId: "inputPrincessAndDragon",
    category: "C1",
    selected: false,
    open: false,
    children: 
    [
      {
        label: "Princess",
        inputFieldId: "inputPrincess",
        className: "showPrincess"
      },
      {
        label: "Dragon",
        inputFieldId: "inputDragon",
        className: "showDragon"
      },
      {
        label: "Fairy",
        inputFieldId: "inputFairy",
        className: "showFairy"
      },
      {
        label: "Magic portal",
        inputFieldId: "inputMagicPortal",
        className: "showMagicPortal"
      },
    ]
  },
  {
    label: "Expansion 4 - The Tower",
    inputFieldId: "inputTower",
    className: "showTower",
    category: "C1",
    selected: false
  },
  { 
    label: "Expansion 5 - Abbey &amp; Mayor",
    inputFieldId: "inputAbbeyAndMayor",
    category: "C1",
    selected: false,
    open: false,
    children: 
    [
      {
        label: "Abbey",
        inputFieldId: "inputAbbey",
        className: "showAbbey"
      },
      {
        label: "Mayor",
        inputFieldId: "inputMayor",
        className: "showMayor"
      },
      {
        label: "Wagon",
        inputFieldId: "inputWagon",
        className: "showWagon"
      },
      {
        label: "Barn",
        inputFieldId: "inputBarn",
        className: "showBarn"
      },
    ]
  },
  { 
    label: "Expansion 6 - Count, King &amp; Robber",
    inputFieldId: "inputCountKingAndRobber",
    category: "C1",
    selected: false,
    open: false,
    children: 
    [
      {
        label: "Count of Carcassonne",
        inputFieldId: "inputCountOfCarcassonne",
        className: "showCountOfCarcassonne"
      },
      {
        label: "King & Robber Baron",
        inputFieldId: "inputKingAndRobber",
        className: "showKingAndRobber"
      },
      {
        label: "Shrines",
        inputFieldId: "inputShrines",
        className: "showShrines"
      },
    ]
  },
  {
    label: "Expansion 7 - The Catapult",
    inputFieldId: "inputCatapult",
    className: "showCatapult",
    category: "C1",
    selected: false
  },
  { 
    label: "Expansion 8 - Bridges, Castles &amp; Bazaars",
    inputFieldId: "inputBridgesCastlesAndBazaars",
    category: "C1",
    selected: false,
    open: false,
    children: 
    [
      {
        label: "Bridges",
        inputFieldId: "inputBridges",
        className: "showBridges"
      },
      {
        label: "Castles",
        inputFieldId: "inputCastles",
        className: "showCastles"
      },
      {
        label: "Bazaars",
        inputFieldId: "inputBazaars",
        className: "showBazaars"
      },
    ]
  },
  { 
    label: "Expansion 9 - Hills &amp; Sheep",
    inputFieldId: "inputHillsAndSheep",
    category: "C1",
    selected: false,
    open: false,
    children: 
    [
      {
        label: "Hills",
        inputFieldId: "inputHills",
        className: "showHills"
      },
      {
        label: "Shepherd",
        inputFieldId: "inputSheep",
        className: "showSheep"
      },
      {
        label: "Vineyards",
        inputFieldId: "inputVineyards",
        className: "showVineyards"
      },
    ]
  },
  {
    label: "The Wheel Of Fortune",
    inputFieldId: "inputWheelOfFortune",
    className: "showWheelOfFortune",
    category: "C1",
    selected: false
  },
  {
    label: "Mini 1 - The Flier",
    inputFieldId: "inputFlier",
    className: "showFlier",
    category: "C1",
    selected: false
  },
  {
    label: "Mini 2 - Dispatches (Messages)",
    inputFieldId: "inputMessages",
    className: "showMessages",
    category: "C1",
    selected: false
  },
  {
    label: "Mini 3 - The Ferries",
    inputFieldId: "inputFerries",
    className: "showFerries",
    category: "C1",
    selected: false
  },
  {
    label: "Mini 4 - Goldmines",
    inputFieldId: "inputGoldmines",
    className: "showGoldmines",
    category: "C1",
    selected: false
  },
  {
    label: "Mini 5 - Mage & Witch",
    inputFieldId: "inputMageAndWitch",
    category: "C1",
    selected: false,
    open: false,
    children: 
    [
      {
        label: "Mage",
        inputFieldId: "inputMage",
        className: "showMage"
      },
      {
        label: "Witch",
        inputFieldId: "inputWitch",
        className: "showWitch"
      },
    ]
  },
  {
    label: "Mini 6 - Robbers",
    inputFieldId: "inputRobbers",
    className: "showRobbers",
    category: "C1",
    selected: false
  },
  {
    label: "Mini 7 - Crop Circles & Crop Circles II",
    inputFieldId: "inputCropCricles",
    className: "showCropCircles",
    category: "C1",
    selected: false
  },
  {
    label: "Cathars / Siege / Besiegers",
    inputFieldId: "inputCathars",
    className: "showCathars",
    category: "C1",
    selected: false
  },
  {
    label: "The Phantom",
    inputFieldId: "inputPhantom",
    className: "showPhantom",
    category: "C1",
    selected: false
  },
  {
    label: "Halflings",
    inputFieldId: "inputHalflings",
    className: "showHalflings",
    category: "C1",
    selected: false
  },
  {
    label: "The Plague",
    inputFieldId: "inputPlague",
    className: "showPlague",
    category: "C1",
    selected: false
  },
  {
    label: "The Tunnel",
    inputFieldId: "inputTunnel",
    className: "showTunnel",
    category: "C1",
    selected: false
  },
  {
    label: "The Festival",
    inputFieldId: "inputFestival",
    className: "showFestival",
    category: "C1",
    selected: false
  },
  {
    label: "The Wind Roses",
    inputFieldId: "inputWindRoses",
    className: "showWindRoses",
    category: "C1",
    selected: false
  },
  {
    label: "Little Buildings",
    inputFieldId: "inputLittleBuildings",
    className: "showLittleBuildings",
    category: "C1",
    selected: false
  },
  {
    label: "Darmstadt",
    inputFieldId: "inputDarmstadt",
    className: "showDarmstadt",
    category: "C1",
    selected: false
  },
  {
    label: "The School",
    inputFieldId: "inputTeacher",
    className: "showTeacher",
    category: "C1",
    selected: false
  },
  {
    label: "German Castles",
    inputFieldId: "inputGermanCastles",
    className: "showGermanCastles",
    category: "C1",
    selected: false
  },
  {
    label: "German Cathedrals",
    inputFieldId: "inputGermanCathedrals",
    className: "showGermanCathedrals",
    category: "C1",
    selected: false
  },
  {
    label: "Monasteries of Germany/Netherlands & Belgium",
    inputFieldId: "inputMonasteries",
    className: "showMonasteries",
    category: "C1",
    selected: false
  },
  {
    label: "The Abbot",
    inputFieldId: "inputAbbot",
    className: "showAbbot",
    category: "C2",
    selected: false,
    hidden: false
  },
/*
  {
    label: "Winter Edition",
    inputFieldId: "inputWinter",
    className: "showWinter",
    category: "WE",
    selected: false,
    hidden: true
  },
*/
  {
    label: "Gingerbread Man",
    inputFieldId: "inputGingerbreadMan",
    className: "showGingerbreadMan",
    category: "WE",
    selected: false,
    hidden: false
  },

];

// Constants for expresions
var AND = function ( a, b ) { return ( a && b ); }, 
    OR = function ( a, b ) { return ( a || b ); }, 
    NOT = function ( a  ) { return !( a ); };

// Derives classes definition
var DerivedClassesTable =
[
// -- Order Of Play --
  {
    className: "showBazaarsOrAbbeyOrHalflings",
    expression: [ OR, "showBazaars", "showAbbey", "showHalflings" ]
  },
  {
    className: "showNotBazaarsAndNotAbbeyAndNotHalflings",
    expression: [ NOT, "showBazaarsOrAbbeyOrHalflings" ]
  },
  {
    className: "showNotFairyAndNotPlague",
    expression: [ NOT, [ OR, "showFairy", "showPlague" ] ]
  },
  {
    className: "showMageOrWitch",
    expression: [ OR, "showMage", "showWitch" ]
  },
  {
    className: "showMageAndWitch",
    expression: [ AND, "showMage", "showWitch" ]
  },
  {
    className: "showMageAndNotWitch",
    expression: [ AND, "showMage", [ NOT, "showWitch" ] ]
  },
  {
    className: "showNotMageAndWitch",
    expression: [ AND, [ NOT, "showMage" ], "showWitch" ]
  },
  {
    className: "showNotPrincess",
    expression: [ NOT, "showPrincess" ]
  },
  {
    className: "showNotPhantom",
    expression: [ NOT, "showPhantom" ]
  },
  {
    className: "showFlierAndMagicPortal",
    expression: [ AND, "showFlier", "showMagicPortal" ]
  },
  {
    className: "showFlierAndNotMagicPortal",
    expression: [ AND, "showFlier", [ NOT, "showMagicPortal" ] ]
  },
  {
    className: "showNotFlierAndMagicPortal",
    expression: [ AND, [ NOT, "showFlier" ], "showMagicPortal" ]
  },
  {
    className: "showDragonAndPlague",
    expression: [ AND, "showDragon", "showPlague" ]
  },
  {
    className: "showNotDragonAndPlague",
    expression: [ AND, [ NOT, "showDragon" ], "showPlague" ]
  },
  {
    className: "showDragonAndNotPlague",
    expression: [ AND, "showDragon", [ NOT, "showPlague" ] ]
  },
  {
    className: "showFerriesOrSheepOrDragon",
    expression: [ OR, "showFerries", "showSheep", "showDragon" ]
  },
  {
    className: "showNotFerriesAndNotSheepAndNotDragon",
    expression: [ NOT, "showFerriesOrSheepOrDragon" ]
  },
  {
    className: "showTunnelAndBridges",
    expression: [ AND, "showTunnel", "showBridges" ]
  },
  {
    className: "showTunnelAndNotBridges",
    expression: [ AND, "showTunnel", [ NOT, "showBridges" ] ]
  },
  {
    className: "showNotTunnelAndBridges",
    expression: [ AND, [ NOT, "showTunnel" ], "showBridges" ]
  },
  {
    className: "showTradersOrKingAndRobber",
    expression: [ OR, "showTraders", "showKingAndRobber" ]
  },
  {
    className: "showNotMessages",
    expression: [ NOT, "showMessages" ]
  },
  {
    className: "showPigOrBuilderOrSheep",
    expression: [ OR, "showPig", "showBuilder", "showSheep" ]
  },
  {
    className: "showMessagesOrRobbers",
    expression: [ OR, "showMessages", "showRobbers" ]
  },
  {
    className: "showWoFOrFestivalOrAbbot",
    expression: [ OR, "showWheelOfFortune", "showFestival", "showAbbot" ]
  }, 
  {
    className: "showTowerOrLittleBuildingsOrFairy",
    expression: [ OR, "showTower", "showLittleBuildings", "showFairy" ]
  },
   {
    className: "showNotBuilderAndNotBazaarsAndNotCathars",
    expression: [ NOT, [ OR, "showBuilder", "showBazaars", "showCathars" ] ]
  },
// -- Footnotes --
  {
    className: "showGingerbreadManAndCountOfCarcassonne",
    expression: [ AND, "showGingerbreadMan", "showCountOfCarcassonne" ]
  },
  {
    className: "showFootnotes",
    expression: "showGingerbreadManAndCountOfCarcassonne"
  },
// -- Scoring During The Game--
  {
    className: "showOtherTiles",
    expression: [ OR, "showHalflings", "showGermanCastles" ]
  },
  {
    className: "showSquareTiles",
    expression: [ NOT, "showOtherTiles" ]
  },
  {
    className: "showNotHalflings",
    expression: [ NOT, "showHalflings" ]
  },
  {
    className: "showAdditionalScoringDuringGame",
    expression: [ OR, "showFairy", "showCatapult", "showMessages", "showTeacher", "showWindRoses", "showLittleBuildings", "showSheep", "showDarmstadt", "showAbbot" ]
  },
/*
  {
    className: "showAll",
    expression: true
  },
*/
  {
    className: "hideAll",
    expression: false
  },
];


//
// if ( a ) You can do A
// if ( b ) You can do B
// if ( c ) You can do C
// if ( a || b || c ) If no previous action, you can do D
// if ( !a && !b && !c ) You can do D 
//

// Map with input fields by Id
var InputFieldMap = {};

// Map with class names (original + derived)
var ClassNameMap = {};



function doEval( oParam )
{
  var bRes = false;
  
  if ( typeof( oParam ) === "string" )
  {
    //console.log( oParam )
    bRes = ClassNameMap[ oParam ].selected;
  } 
  else if ( typeof ( oParam ) === "object" )
  {
    var arrParams = oParam || [];
    var nParams = arrParams.length;

    var fnAction = arrParams[ 0 ];
    var bRes = doEval( arrParams[ 1 ] );

    if ( nParams == 2 )
    {
      // Unary Operator
      bRes = fnAction( bRes );
    }
    else if ( nParams > 2 )
    {
      // Binary Operator
      for ( var i = 2; i < nParams; i++ )
      {
        var bParam = doEval( arrParams[ i ] );
        bRes = fnAction( bRes, bParam );
      }
    }
  }
  else if ( typeof ( oParam ) === "boolean" )
  {
    bRes = oParam;
  } 
  
  return bRes;
}


function doEvalDerivedClasses()
{
  var arrDerivedClasses = DerivedClassesTable || [];
  var nDerivedClasses = arrDerivedClasses.length;
  for ( i = 0; i < nDerivedClasses; i++ )
  {
    var o = arrDerivedClasses[ i ];
    var strClassName = o.className;
    var arrExpression = o.expression;
    var bNewSelected = doEval( arrExpression );
    var bOldSelected = o.selected;
    
    if ( bNewSelected != bOldSelected )
    {
      var str = "." + strClassName;
      if ( bNewSelected )
      {
        $( str ).show();
      }
      else
      {
        $( str ).hide();
      }
      o.selected = bNewSelected;
    } 
  }
}


function doToggle ( oLinkHTML )
{
  var strInputFieldId = oLinkHTML.attr( "id" );

  var o = InputFieldMap[ strInputFieldId ];
  var bOpen = o.open;

  var strLinkId = "#link_" + strInputFieldId;
  var strToggleId = "#toggle_" + strInputFieldId;

  var oToggleHTML = $( strToggleId );
  
  if ( bOpen )
  {
    oToggleHTML.slideUp();
    oLinkHTML.html( BUTTON_OPEN );
  }
  else
  {
    oToggleHTML.slideDown();
    oLinkHTML.html( BUTTON_CLOSE );
  }
  
  o.open = !bOpen;
}


function doCheckboxSimple( oCheckHTML )
{
  var strInputFieldId = oCheckHTML.attr( "id" );
  
  var o = InputFieldMap[ strInputFieldId ];
  var bSelected = o.selected;
  
  var strClassName = o.className;
  var str = '.' + strClassName;
  if ( bSelected )
  {
    $( str ).hide();
  }
  else
  {
    $( str ).show();
  }
  
  o.selected = !bSelected;
  
  if ( o.parent != null )
  {
    doCheckParent( o.parent );
  }
  
  doEvalDerivedClasses();
}


function doCheckboxGroup( oCheckHTML )
{
  var strInputFieldId = oCheckHTML.attr( "id" );
  
  var o = InputFieldMap[ strInputFieldId ];
  var bSelected = o.selected;
  
  //o.selected = !bSelected;

  var arrChildren = o.children;
  var nChildren = arrChildren.length;
  for ( var i = 0; i < nChildren; i++ )
  {
    var oChild = arrChildren[ i ];
    var bChildSelected = oChild.selected;
    var strChildInputFieldId = oChild.inputFieldId;
    
    if ( bSelected == bChildSelected )
    {
      $('#' + strChildInputFieldId ).prop( "checked", !bSelected );
      
      var strChildClassName = oChild.className;
      var str = '.' + strChildClassName;
      if ( bSelected )
      {
        $( str ).hide();
      }
      else
      {
        $( str ).show();
      }
  
      oChild.selected = !bSelected;
    }
  }
    
  o.selected = !bSelected;
  
  doEvalDerivedClasses();
}

function doCheckParent( oToggleCheck )
{
  var arrChildren = oToggleCheck.children;
  var nChildren = arrChildren.length;
  var bNewSelected = false;
  for ( var i = 0; i < nChildren; i++ )
  {
    var oChild = arrChildren[ i ];
  	bNewSelected = bNewSelected || oChild.selected;
  }
  
  var bOldSelected = oToggleCheck.selected;
  if( bOldSelected != bNewSelected )
  {
    var strInputFieldName = oToggleCheck.inputFieldId;
    $( "#" + strInputFieldName ).prop( "checked", bNewSelected );
    oToggleCheck.selected = bNewSelected;
  }
}


function doInit()
{
  var n = ItemTree.length;
  var strRes = "";
  for (var i = 0; i < n; i++ )
  {
    var o = ItemTree[ i ];
    var strLabel = o.label;
    var strInputFieldId = o.inputFieldId;
    var bSelected= o.selected || false;
    var arrChildren = o.children || [];
    var nChildren = arrChildren.length;
    var bOpen = o.open || false;
    var strClassName = o.className || "";
    var bHidden = o.hidden || false;
    
    InputFieldMap[ strInputFieldId ] = o;
    
    if ( strClassName.length > 0 )
    {
      ClassNameMap[ strClassName ] = o;
    }
    
    if ( !bHidden )
    { 
      var strChecked = ( bSelected ? ' checked="checked"' : '' );
      var strClassName = (nChildren > 0 ? 'checkboxGroup' : 'checkboxSimple' );
    
      strRes += '<input class="' + strClassName +'" type="checkbox" name="' + strInputFieldId + '" id="' + strInputFieldId + '"' + strChecked + '/> ' + strLabel;
      if ( nChildren )
      {
        strRes += ' <a class="linkToggle" id="' + strInputFieldId + '" href="javascript:void(0);">' + ( bOpen ? BUTTON_CLOSE : BUTTON_OPEN ) + '</a><br />';

        var strOpen = ( bOpen ? "" : " style='display:none;'"  );

        strRes += '<blockquote id="toggle_' + strInputFieldId + '"' + strOpen + '>';
        for ( var j = 0; j < nChildren; j++ )
        {
          var oChild = arrChildren[ j ];
          oChild.selected = bSelected;
          oChild.parent = o;
         
          var strChildLabel = oChild.label;
          var strChildinputFieldId = oChild.inputFieldId;
          var bChildSelected = bSelected;

          var strClassName = oChild.className;
          if ( strClassName.length > 0 )
          {
            InputFieldMap[ strChildinputFieldId ] = oChild;
            ClassNameMap[ strClassName ] = oChild;
          }

          strRes += '<input class="checkboxSimple" type="checkbox" name="' + strChildinputFieldId + '" id="' + strChildinputFieldId + '"' + strChecked + '/> ' + strChildLabel + '<br />';
        }
        strRes += '</blockquote>';
      }
      else
      {
        strRes += '<br />';
      }
    }
  }

  var arrDerivedClasses = DerivedClassesTable || [];
  var nDerivedClasses = arrDerivedClasses.length;
  for ( i = 0; i < nDerivedClasses; i++ )
  {
    var o = arrDerivedClasses[ i ];
    var strClassName = o.className;
    var arrExpresion = o.expression;
    var bSelected = doEval( arrExpresion );
    
    ClassNameMap[ strClassName ] = o;
    o.selected = bSelected;
  }

  var oHTML = $( "#" + EXPANSION_SELECTOR_ID );
  oHTML.html( strRes );
  
  for ( var strClassName in ClassNameMap )
  {
    var o = ClassNameMap[ strClassName ];
    if ( !o.selected )
    {
      $( "." + strClassName ).hide();
    }
  }
  
  $( ".linkToggle" ).click( 
    function() { doToggle( $(this) ); } 
  );

  $( ".checkboxSimple" ).click( 
    function() { doCheckboxSimple( $(this) ); } 
  );

  $( ".checkboxGroup" ).click( 
    function() { doCheckboxGroup( $(this) ); } 
  );
}

function doWrapInit()
{
  if ( $( "#" + EXPANSION_SELECTOR_ID ).hasClass( "showAll" ) )
  {
    // Tweak the footnotes
    $( "#" + CITE_ID_PREFIX + "1" ).addClass( "showGingerbreadManAndCountOfCarcassonne" );

    doInit();

    $( "." + HIDE_WHEN_DONE_CLASS ).hide();
    $( "." + SHOW_WHEN_DONE_CLASS ).show();
  }
}

$( document ).ready( doWrapInit );