/*
 * gui-builder - A simple WYSIWYG HTML5 app creator
 * Copyright (c) 2011, Intel Corporation.
 *
 * This program is licensed under the terms and conditions of the
 * Apache License, version 2.0.  The full text of the Apache License is at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 */
"use strict";

/*
 * The palette.json properties are:
 *
 *   id:      String, internal reference this widget set
 *   name:    String, human readable name and/or description
 *   version: String, (XXX: Unused, possible future value?)
 *   widgets: Array of objects, each with the following properties:
 *
 *       name:   String, Human readable widget name (TODO: L10N?)
 *       type:   String, HTMLElement type to use with createElement()
 *       icon:   String, CSS class id for the icon representation
 *       helper: String, HTML to use for creating the drag helper
 *       code:   String, HTML to be created when inserting this widget
 *                       into the DOM
 */

function loadPalette(container, filename) {
	var defaultContainer = '#palette-panel';
	var myContainer = container;

        if (!myContainer) {
		myContainer = $(defaultContainer);
        }

        if (!myContainer || !myContainer.get()) {
		return false;
        }

	return $.getJSON(filename, function(data){
                console.log("Starting palette load...");
		myContainer.append('<p id="palette_header" class="ui-helper-reset ui-widget ui-widget-header">Palette</p>');
		myContainer.append('<ul id="palette_accordion"></ul>');

		var hdr = $('#palette_header');
		var acc = $('#palette_accordion');

		// Make use of the flex box model in CSS3 to allow the tabs
		// to grow/shrink with the window/container
		myContainer.addClass('vbox');
		hdr.addClass('flex0');
		acc.addClass('flex1');

                // FIXME: Eventually, all widgets should come from the BWidget
                //        global structure.  For now, we load them as their own
                //        subcategory in the palette
		$(acc).append('<li><p>Tizen Framework</p><ul id="Tizen-widgets"></ul></li>');
                $.each(BWidget.getPaletteWidgetTypes(), function(n, id) {
			// Add new <li> element to hold this widget
			var ul = $('#Tizen-widgets');
			var li = $('<li id="BWidget-'+id+'"></li>').appendTo($(ul));
			$(li).button({
				label: BWidget.getDisplayLabel(id),
				icons: {primary: BWidget.getIcon(id)}
				});
			$(li).disableSelection();
			$(li).addClass('nrc-palette-widget');
			$(li).data("code", BWidget.getTemplate(id));
			$(li).data("adm-node", {type: id});
			$(ul).append($(li));
                });

		$.each(data.palettes, function(i, p){
			$(acc).append('<li><p>'+p.name+'</p><ul id="'+p.id+'"></ul></li>');

			$.each(p.widgets, function(j, w) {
				// Add new <li> element to hold this widget
				var ul = $('#'+p.id);
				var li = $('<li id="nrc_'+w.type+'"></li>').appendTo($(ul));
				$(li).button({
					label: w.name,
					icons: {primary: w.icon}
					});
				$(li).disableSelection();
				$(li).addClass('nrc-palette-widget');
				$(li).data("code", w.code);
				$(li).data("adm-node", {type: w.type});
				$(ul).append($(li));
			});
		});

		$(acc).accordion({
			fillSpace: true,
			});

		// Must explicitly react to window resize events to be
		// able to grow/shrink if we're in a flex box layout
		$(window).resize( function () { $(acc).accordion("resize"); });
	});
}

@@ -2603,562 +2603,8 @@
 );%0A%0A
-%09%09$.each(data.palettes, function(i, p)%7B%0A%09%09%09$(acc).append('%3Cli%3E%3Cp%3E'+p.name+'%3C/p%3E%3Cul id=%22'+p.id+'%22%3E%3C/ul%3E%3C/li%3E');%0A%0A%09%09%09$.each(p.widgets, function(j, w) %7B%0A%09%09%09%09// Add new %3Cli%3E element to hold this widget%0A%09%09%09%09var ul = $('#'+p.id);%0A%09%09%09%09var li = $('%3Cli id=%22nrc_'+w.type+'%22%3E%3C/li%3E').appendTo($(ul));%0A%09%09%09%09$(li).button(%7B%0A%09%09%09%09%09label: w.name,%0A%09%09%09%09%09icons: %7Bprimary: w.icon%7D%0A%09%09%09%09%09%7D);%0A%09%09%09%09$(li).disableSelection();%0A%09%09%09%09$(li).addClass('nrc-palette-widget');%0A%09%09%09%09$(li).data(%22code%22, w.code);%0A%09%09%09%09$(li).data(%22adm-node%22, %7Btype: w.type%7D);%0A%09%09%09%09$(ul).append($(li));%0A%09%09%09%7D);%0A%09%09%7D);%0A%0A
 %09%09$(

Remove obsolete/non-working widgets