/*
 * module: dsbox_menu 
 * 
 */
YUI.add('dsbox_menu', function(Y) {

	Y.namespace('dsbox');
 
    var contextObj = {
    	name: "context"
    };
    
    Y.dsbox.Menu = function(config) {
    	this._config = config;
    	this._menu_div_id = "dsbox_menu";
    	this._profile_submenu_shown = false;
    	this._menu_data = null;
    };

    Y.dsbox.Menu.prototype.start = function () {
    	this._init();
    };

    Y.dsbox.Menu.prototype._init = function () {
    	this._update_menu();
    	this._load_menu_start();
    	Y.on("click", Y.bind(this.toggle_submenu_profile, this), "#dsbox_menuitem_profile");
    }    	

    Y.dsbox.Menu.prototype._update_menu = function () {
    	var html = '';
    	html += '<div class="dsbox_menu_box_begin">';
		html += '</div>';

		html += '<div class="dsbox_menu_box">';
		html += '  <div class="dsbox_menu_item_text">';
		html += '    dailyshopbox.dk';
		html += '    <div class="dsbox_font_tiny">';
		if (this._menu_data) {
		html += this._menu_data.version;
		}
		html += '    </div>';
		html += '  </div>';
		
		if (this._menu_data) {
			for(menu_item_id in this._menu_data.menu_items) {
				var menu_item =  this._menu_data.menu_items[menu_item_id];
		    	if(this._config.selected_node_id == menu_item.node_id) {
					html += '  <div class="dsbox_menu_item_box_selected" id="'+menu_item.node_id+'">';
		    	}
		    	else {
		    		html += '  <div class="dsbox_menu_item_box" id="'+menu_item.node_id+'">';
		    	}
				html += menu_item.title;
				html += '  </div>';
		    	Y.on("click", Y.bind(this._resolve_click, this), "#"+menu_item.node_id, contextObj, menu_item.url);
			}
		}
		html += '  <div class="dsbox_menu_item_logo"></div>';
		if (this._menu_data) {
			if (this._menu_data.username) {
		    	if(this._config.selected_node_id == 'dsbox_menuitem_profile') {
		    		html += '  <div class="dsbox_menu_item_box_right_selected" id="dsbox_menuitem_profile">';
		    	}
		    	else {
		    		html += '  <div class="dsbox_menu_item_box_right" id="dsbox_menuitem_profile">';
		    	}
				html += 'profile';
				html += '  </div>';
			}
			html += '</div>';
			if (this._menu_data.username) {
				html += '<div class="dsbox_submenu" id="profile_submenu_overlay">';
				html += '  <div class="dsbox_submenu_item_text">';
				html += this._menu_data.username;
				html += '  </div>';
				for(menu_item_id in this._menu_data.menu_items_profile) {
					var menu_item =  this._menu_data.menu_items_profile[menu_item_id];
					html += '  <div class="dsbox_submenu_item_box" id="'+menu_item.node_id+'">';
					html += menu_item.title;
					html += '  </div>';
			    	Y.on("click", Y.bind(this._resolve_click, this), "#"+menu_item.node_id, contextObj, menu_item.url);
				}
			}
		}
		html += '</div>';

		html += '<div class="dsbox_menu_box_end>';
		html += '</div>';
    	Y.one("#"+this._menu_div_id).setContent(html);

    	
		if (this._menu_data && (this._menu_data.menu_items_manager.length > 0)) {
			html = '';
			html += '<div class="dsbox_box_with_margin_and_background dsbox_manager">';
			html += '  <div class="dsbox_box_headline dsbox_manager_inverse">';
			html += 'Manager';
			html += '  </div>';
			for(menu_item_id in this._menu_data.menu_items_manager) {
				var menu_item =  this._menu_data.menu_items_manager[menu_item_id];
				html += '<div class="dsbox_submenu_item_box_manager" id="'+menu_item.node_id+'">';
				html += menu_item.title;
				html += '</div>';
		    	Y.on("click", Y.bind(this._resolve_click, this), "#"+menu_item.node_id, contextObj, menu_item.url);
			}
			html += '  <div class="dsbox_box_with_margin">';
			html += '  </div>';
			html += '</div>';
	    	Y.one("#dsbox_manager_menu").setContent(html);
		}
    	
    	this.profile_submenu_overlay = new Y.Overlay({
    		srcNode:"#profile_submenu_overlay",
    		width:"200px",
    		height:"145px",
    		zIndex:10,
    		align: {node:"#dsbox_menuitem_profile", points:["tc", "bc"]}
    	});
    	this.profile_submenu_overlay.render();
    	this.profile_submenu_overlay.hide();
    };
    
    Y.dsbox.Menu.prototype.toggle_submenu_profile = function () {
    	if(this._profile_submenu_shown) {
    		this.profile_submenu_overlay.hide();
        	this._profile_submenu_shown = false;
    	}
    	else {
    		this.profile_submenu_overlay.show();
        	this._profile_submenu_shown = true;
    	}
    };

    Y.dsbox.Menu.prototype._load_menu_start = function () {
    	var self = this;
		datasource = new Y.DataSource.Get({source:'/start/ajax_request_menu?'}),
		datasource.plug(Y.Plugin.DataSourceJSONSchema, {
	    schema: {
			resultListLocator: "menu",
			resultFields: ["menu_items", "menu_items_profile", "menu_items_manager", "username", "version"]
	    }
	    });
		datasource_cfg = { 
			callback: {
				success: function(e) {
					self._menu_data = e.response.results[0];
					self._update_menu();
				},
				failure: function(e) {
				}
			}
		};
		datasource.sendRequest(datasource_cfg);
	};


    Y.dsbox.Menu.prototype._resolve_click = function (event, url) {
		window.open(url, '_self'); 
    };
    
}, '1.0.0', { requires: ['base', 'base-base', "anim-base", "overlay", "datasource-get", "datasource-jsonschema"] });

