/**
 * file Menu.js with Menu class
 * @author Janusz Rygał
 * @date 12.08.2008 
 */


/**
 * @class Menu class using to load menu
 * Menu class
 */
Menu = function(){
	/**menu loader*/
	this.menuLoader;
	/**ajax sectors loader*/
	this.ajaxSectorsLoader;
	/**ajax departments loader*/
	this.ajaxDepartmentsLoader
	/**most frequency open Applications*/
	this.mostFreqAppLoader;
	/**programs object*/
	this.programs;
	/**programs menu*/
	this.programsMenu;
	/**context menu*/
	this.conMenu; 
	/**clicked shortcut*/
	this.clickedShortcut;
	/**creating icon*/
	this.creatingIcon;
	/**fesh icon id*/
	this.freshIcon; 
	/**fresh icon data*/
	this.freshIconLoader;
	/**icons loader*/
	this.iconsLoader;
	/**disable create shortcut or not*/
	this.disabled;

    /**zombie icons array*/
	this.zombieIcons = new Array();


    this.refactorIcons = function(){
        var prevWidth  = context.clientData.previousScreenWidth;
        var prevHeight = context.clientData.previousScreenHeight;
        
        var curWidth   = context.clientData.getScreenWidth();
        var curHeight  = context.clientData.getScreenHeight();

        iconsNum = context.desk.MyDesktop.shortcuts.length;
        for(var i=0; i<iconsNum; ++i){
            context.desk.MyDesktop.shortcuts[i].x = parseInt((curWidth * context.desk.MyDesktop.shortcuts[i].x) / prevWidth);
            context.desk.MyDesktop.shortcuts[i].y = parseInt((curHeight * context.desk.MyDesktop.shortcuts[i].y) / prevHeight);

            context.desk.MyDesktop.shortcuts[i].panel.setPosition(context.desk.MyDesktop.shortcuts[i].x,
                                                                  context.desk.MyDesktop.shortcuts[i].y);

                    context.menu.updateIconPosition(context.desk.MyDesktop.shortcuts[i].id,
					context.desk.MyDesktop.shortcuts[i].x,
					context.desk.MyDesktop.shortcuts[i].y);
             context.desk.MyDesktop.shortcuts[i].panel.show();

        }

    }


    this.isThereIcon = function(x_, y_, xSpace_, ySpace_){
        iconsNum = context.desk.MyDesktop.shortcuts.length;
        for(var i=0; i<iconsNum; ++i){
            if(( context.desk.MyDesktop.shortcuts[i].x > x_ - xSpace_ ) &&
               ( context.desk.MyDesktop.shortcuts[i].x < x_ + xSpace_ ) &&
               ( context.desk.MyDesktop.shortcuts[i].y > y_ - ySpace_ ) &&
               ( context.desk.MyDesktop.shortcuts[i].y < y_ + ySpace_ )){
               return true;
            }
        }
       return false;
    }

    this.refactorZombiesIcons = function(){
        var lastIter = 0;
        var bMaxWidth  = Ext.getBody().getWidth()  - 50;
        var bMaxHeight = Ext.getBody().getHeight() - 50;

		for(var y=0; y< bMaxHeight; y=(y+20)){
            for(var x=0; x< bMaxWidth; x=(x+20)){
                if(!context.menu.isThereIcon(x, y, 60, 100)){
                    //alert('nowy x -'+x);
                   // alert('nowy y -'+y);
                    context.desk.MyDesktop.shortcuts[context.menu.zombieIcons[lastIter]].x = x;
                    context.desk.MyDesktop.shortcuts[context.menu.zombieIcons[lastIter]].y = y;
                    context.desk.MyDesktop.shortcuts[context.menu.zombieIcons[lastIter]].panel.setPosition(x,y);

                    context.menu.updateIconPosition(context.desk.MyDesktop.shortcuts[context.menu.zombieIcons[lastIter]].id,
											context.desk.MyDesktop.shortcuts[context.menu.zombieIcons[lastIter]].x,
											context.desk.MyDesktop.shortcuts[context.menu.zombieIcons[lastIter]].y);
                    context.desk.MyDesktop.shortcuts[context.menu.zombieIcons[lastIter]].panel.show();
                    ++lastIter;
                    
                    if(lastIter == context.menu.zombieIcons.length){
                       context.menu.zombieIcons = new Array();
                       return;
                    }

                }
            }
		}

        
        for(var i=lastIter; i<context.menu.zombieIcons.length; ++i){
            var newX = 50 + (Math.random()*(bMaxWidth - 50));
            var newY = 50 + (Math.random()*(bMaxHeight - 50));
            context.desk.MyDesktop.shortcuts[context.menu.zombieIcons[i]].x = newX;
            context.desk.MyDesktop.shortcuts[context.menu.zombieIcons[i]].y = newY;
            context.desk.MyDesktop.shortcuts[context.menu.zombieIcons[i]].panel.show();
        }
    }

    this.checkForZombieIcons = function(){
        iconsNum = context.desk.MyDesktop.shortcuts.length;
        var bWidth = Ext.getBody().getWidth();
        var bHeight = Ext.getBody().getHeight();

        var iter = 0;
        for(var i=0; i<iconsNum; ++i){
            if(context.desk.MyDesktop.shortcuts[i].x >= (bWidth - 50)  ){
                context.menu.zombieIcons[iter] = i;
                ++iter;
                continue;
            }
            if(context.desk.MyDesktop.shortcuts[i].y >= (bHeight - 50)  ){
                context.menu.zombieIcons[iter] = i;
                ++iter;
                continue;
            }
        }

        if(iter > 0){
            Ext.MessageBox.show({
                           title: context.translator.translate('zombie_icons_found'),
                           msg: context.translator.translate('zombie_icons_move'),
                           width:300,
                           buttons: Ext.MessageBox.OKCANCEL,
                           multiline: false,
                           fn: function(result_){
                               if(result_ == 'ok'){
                                   context.menu.refactorZombiesIcons();
                               }
                           }
                       });

        }
    }

	/**
	 * function to sort by name
	 */
	function sortByNameAsc(){
		//sorting
		sorted = false;
		iconsNum = context.desk.MyDesktop.shortcuts.length;
		while(!sorted){
			sorted = true;
			for(i=0; i< (iconsNum - 1); ++i){
					if(context.desk.MyDesktop.shortcuts[i].winText > context.desk.MyDesktop.shortcuts[i+1].winText){
							tmp = context.desk.MyDesktop.shortcuts[i+1];
							context.desk.MyDesktop.shortcuts[i+1] = context.desk.MyDesktop.shortcuts[i];
							context.desk.MyDesktop.shortcuts[i]   = tmp;
							sorted = false;
					}
			}
		}	
	}
	
	/**
	 * function to arrange icons vertical
	 */
	function arrangeVertical(){
		width  = Ext.getBody().getWidth();
		height = Ext.getBody().getHeight() - 80; 
		
		x = 20;
		y = 20;
		maxWidth = 0;
		
		for(i=0; i< iconsNum; ++i){
			
			context.desk.MyDesktop.shortcuts[i].x = x;
			context.desk.MyDesktop.shortcuts[i].y = y;
			context.desk.MyDesktop.shortcuts[i].panel.setPosition(x,y);
			
			context.menu.updateIconPosition(context.desk.MyDesktop.shortcuts[i].id,
											context.desk.MyDesktop.shortcuts[i].x,
											context.desk.MyDesktop.shortcuts[i].y);
			
			if(context.desk.MyDesktop.shortcuts[i].panel.width > maxWidth)
				maxWidth = context.desk.MyDesktop.shortcuts[i].panel.width;
			
			y += 80;
			if(y > height){
			   y  = 20;
			   x += (20 + maxWidth);
			   maxWidth = 0; 	
			}
			
			context.desk.MyDesktop.shortcuts[i].panel.show();  	 	
		}
	}
	
	/**
	 * function to arrange icons horizontal
	 */
	function arrangeHorizontal(){
		width  = Ext.getBody().getWidth();
		height = Ext.getBody().getHeight() - 80; 
		
		x = 20;
		y = 20;
		maxWidth = 0;
		var xShift   = 0;
		
		for(i=0; i< iconsNum; ++i){
			if(context.desk.MyDesktop.shortcuts[i].panel.width > maxWidth)
				maxWidth = context.desk.MyDesktop.shortcuts[i].panel.width;
		}
		
		xShift  = maxWidth + 10;
		
		for(i=0; i< iconsNum; ++i){
			
			context.desk.MyDesktop.shortcuts[i].x = x;
			context.desk.MyDesktop.shortcuts[i].y = y;
			context.desk.MyDesktop.shortcuts[i].panel.setPosition(x,y);
			
			context.menu.updateIconPosition(context.desk.MyDesktop.shortcuts[i].id,
											context.desk.MyDesktop.shortcuts[i].x,
											context.desk.MyDesktop.shortcuts[i].y);
			
			x += xShift;
			if(x > (width - 100)){
			   y += 80;
			   x = 20;	
			}
			
			context.desk.MyDesktop.shortcuts[i].panel.show();  	 	
		}
	}
	
	/**
	 * function to arrange spiral
	 */
	function arrangeSpiral(){
		 var width  = Ext.getBody().getWidth();
		 var height = Ext.getBody().getHeight() - 80;
		 
		 var halfW  = parseInt(width/2);
		 var halfH  = parseInt(height/2);
		 
		 var a = 13;
		 var fi = 2;
		 var r = 5;
		 var x  = halfW;
		 var y  = halfH;	
		 	
		 for(i=0; i< iconsNum; ++i){

		 	r = a*fi;
			x = parseInt(r*Math.cos(fi)) + halfW;
			y = parseInt(r*Math.sin(fi)) + halfH;
			fi += 2.0;
		 
			context.desk.MyDesktop.shortcuts[i].x = x;
			context.desk.MyDesktop.shortcuts[i].y = y;
			context.desk.MyDesktop.shortcuts[i].panel.setPosition(x,y);
			
			context.menu.updateIconPosition(context.desk.MyDesktop.shortcuts[i].id,
											context.desk.MyDesktop.shortcuts[i].x,
											context.desk.MyDesktop.shortcuts[i].y);
			
			
			context.desk.MyDesktop.shortcuts[i].panel.show();  	 	
		}
	} 
	
	/**
	 * method to arrange icons vertical by name
	 */
	this.sortIconsVerticalAscByName = function(){
		sortByNameAsc();
		arrangeVertical();	
	} 
	
	/**
	 * method to arrange icons horizontal by name
	 */
	this.sortIconsHorizontalAscByName = function(){
		sortByNameAsc();
		arrangeHorizontal();	
	} 
	
	/**
	 * method to arrange icons spiral by name
	 */
	this.sortIconsSpiralAscByName = function(){
		sortByNameAsc();
		arrangeSpiral();	
	} 
	
	/**
 	 * method to update icon position in the database
 	 * @param icon id
 	 * @param x coordinate
 	 * @param y coordinate
 	 */
 	this.updateIconPosition = function(id_, x_, y_){

                for(var i=0; i<context.desk.MyDesktop.shortcuts.length; ++i){
                      if(context.desk.MyDesktop.shortcuts[i].id == id_){
                         context.desk.MyDesktop.shortcuts[i].x = x_;
                         context.desk.MyDesktop.shortcuts[i].y = y_;
                         break;
                      }
                }

 				conn = new Ext.data.Connection({
								url: project_url + 'moveIcon?icon='+id_+'&x='+x_+'&y='+y_
							});
		   		conn.request({
								method: 'GET',
							    success: function(responseObject) {
							    		 },
							    failure: function() {
							         			Ext.Msg.alert('Warning', 'Some problems occured while removing shortcut!');
							     		 }
				    });	
 	}
	
	/**
	 * method to create fresh icon loader
	 * @param icon id
	 */
	this.loadIcon = function(id_){
		this.freshIcon = id_;	
	 	this.freshIconLoader = new Ext.data.Store({
						                    proxy: new Ext.data.HttpProxy({ 
						                                      url: project_url + 'GetCurrIconData'+'?icon='+this.freshIcon,
						                                      method: 'GET',
						                                      waitMsg:'Loading ...' 
						                                     }),
						                   reader : new Ext.data.JsonReader({
									                            id: 'icon_reader',
									                            idProperty:'url',
									                            root: 'icon',
									                            fields: [	
									                            		 {name: 'image'			, type: 'string'},
									                            		 {name: 'url'			, type: 'string'},
									                            		 {name: 'title'			, type: 'string'},
									                            		 {name: 'module'		, type: 'int'},
									                            		 {name: 'type'			, type: 'string'}
									                            		]
							                         		 })     		 
						                });	
		this.freshIconLoader.load();	
	}
	
	/**
	 * method to create shortcut
	 * @param node
	 * @param id of the module
	 */
	this.createShortcut = function(node, id){
			this.loadIcon(id);
			this.freshIconLoader.on('load', function(loader, record){
					iconsNum = context.desk.MyDesktop.shortcuts.length;
					context.desk.MyDesktop.shortcuts[iconsNum] =  new Shortcut( context.menu.freshIcon, 
																			    context.menu.freshIconLoader.getAt(0).get('image'), 
																			    context.menu.freshIconLoader.getAt(0).get('title'), 
																			    15, 
																			    80, 
																			    context.menu.freshIconLoader.getAt(0).get('url'), 
																			    context.menu.freshIconLoader.getAt(0).get('module'),
																			    context.menu.freshIconLoader.getAt(0).get('type'));
					context.desk.MyDesktop.shortcuts[iconsNum].createShortcut();	
			}, this.freshIconLoader);	
			
	} 
	
	this.hideShortcut = function(id_){
		iconsNum = context.desk.MyDesktop.shortcuts.length;
		for(i=0; i< iconsNum; ++i){
			if(context.desk.MyDesktop.shortcuts[i].id == id_){
					context.desk.MyDesktop.shortcuts[i].panel.hide();
					return;
			}
		}
	}
	
	/**
 	 * method to remove shortcut
 	 * @param item with data
 	 */
 	 this.removeShortcut = function(item){
 		   conn = new Ext.data.Connection({
								url: project_url + 'removeShortcut?shortcut='+context.menu.clickedShortcut.shid
							});
		   conn.request({
								method: 'GET',
							    success: function(responseObject) {
							    		 },
							    failure: function() {
							         			Ext.Msg.alert('Warning', 'Some problems occured while removing shortcut!');
							     		 }
				    });	
		context.menu.hideShortcut(context.menu.clickedShortcut.shid);
		iconsNum = context.desk.MyDesktop.shortcuts.length;

		tmpArr   = new Array(iconsNum - 1);
		it       = 0;
		for(i=0; i< iconsNum; ++i){
			if(context.desk.MyDesktop.shortcuts[i].id != context.menu.clickedShortcut.shid){
					tmpArr[it] = context.desk.MyDesktop.shortcuts[i];
					++it;
			}
		}
		context.desk.MyDesktop.shortcuts = tmpArr;	
			    	    
 	 }
	
	/**
	 * method to add shortCut to database
	 * @param node
	 */
	this.addShortcut = function(node){
	
		   iconsNum = context.desk.MyDesktop.shortcuts.length;
			for(i=0; i< iconsNum; ++i){
				if(context.desk.MyDesktop.shortcuts[i].url == node.url){
						Ext.Msg.alert('Info', 'Shortcut to this module already exists on your desktop.');
						return;
				}
			}
	
		   context.menu.creatingIcon = node;
	       conn = new Ext.data.Connection({
								url: project_url + 'addShortcut?mod='+node.windowId
							});					
		   conn.request(
				    	{	
								method: 'GET',
							    success: function(responseObject) {
							    			id = responseObject.responseText.split(':')[2];
							    			id = id.substr(0,id.length - 1);
							    			context.menu.createShortcut(node, id);
							    		 },
							    failure: function() {
							         			Ext.Msg.alert('Warning', 'Some problems occured while adding new shortcut!');
							     		 }
				    	});		
	}
	
	/**
	 * method to search small icon to modules
	 * @param module id
	 */
	this.getSmallIcon = function(id_){
		for(var i=0; i<this.iconsLoader.getCount(); ++i){
			if(this.iconsLoader.getAt(i).get('id') == id_){	
				return this.iconsLoader.getAt(i).get('icon');
			}
		}
		return 'Icon_Universal.png';
	} 
	
	/**
	 * initialize method
	 */
	 this.init = function(){
	 	this.menuLoader = new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({ 
                                      url: project_url + 'LoadMenu',
                                      method: 'GET',
                                      waitMsg:'Loading ...' 
                                     }),
                   reader : new Ext.data.JsonReader({
			                            id: 'menu_loader',
			                            idProperty:'id',
			                            root: 'menu',
			                            fields: [ 	'array'/*'realId', 
			                            			'id',
			                            			'url',
			                            			'name',
			                            			'type',
			                            			'parent'*/		
			                            		]
	                         		 })     		 
                });
       
        this.iconsLoader = new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({ 
                                      url: project_url + 'GetIconsToModules',
                                      method: 'GET',
                                      waitMsg:'Loading ...' 
                                     }),
                   reader : new Ext.data.JsonReader({
			                            id: 'icons_loader',
			                            idProperty:'id',
			                            root: 'icons',
			                            fields: [ 	'id',
			                            			'icon'		
			                            		]
	                         		 })     		 
                });
       
       	this.ajaxSectorsLoader = new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({ 
                                      url: project_url + 'LoadAjaxSectors',
                                      method: 'GET',
                                      waitMsg:'Loading ...' 
                                     }),
                   reader : new Ext.data.JsonReader({
			                            id: 'ajax_sectors_loader',
			                            idProperty:'id',
			                            root: 'ajax_sectors',
			                            fields: [ 
			                            			'id',
			                            			'name',
			                            			'department',
			                            			'path'		
			                            		]
	                         		 })     		 
                });
                
        this.ajaxDepartmentsLoader = new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({ 
                                      url: project_url + 'LoadAjaxDepartments',
                                      method: 'GET',
                                      waitMsg:'Loading ...' 
                                     }),
                   reader : new Ext.data.JsonReader({
			                            id: 'ajax_departments_loader',
			                            idProperty:'id',
			                            root: 'ajax_departments',
			                            fields: [ 
			                            			'id',
			                            			'parent',
			                            			'department'	
			                            		]
	                         		 })     		 
                });        
          
        this.mostFreqAppLoader =  new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({ 
                                      url: project_url + 'LoadMostFreqApp',
                                      method: 'GET',
                                      waitMsg:'Loading ...' 
                                     }),
                   reader : new Ext.data.JsonReader({
			                            id: 'most_freq_app_loader',
			                            idProperty:'id',
			                            root: 'most_freq_app',
			                            fields: [ 
			                            			'id',
			                            			'parent',
			                            			'url',
			                            			'name',
			                            			'type'	
			                            		]
	                         		 })     		 
                });  
                
	    this.conMenu = new Ext.menu.Menu({
					        id: 'contextSectorMenu',
					        items: [
					        			new Ext.menu.Item({
							                text: 'Open',
							                id: 'shortcut_open_item',
							                icon:'/../../imc/images/omsImages/main/icons/door_open.png'
							            }),
							            new Ext.menu.Item({
							                text: 'Remove shortcut',
							                id: 'shortcut_remove_item',
							                icon:'/../../imc/images/omsImages/main/icons/cross.png',
							                handler: this.removeShortcut
							            }),
							            new Ext.menu.Item({
							                text: 'Open In New Window',
							                id: 'shortcut_open_in_new_window',
							                icon:'/../../imc/images/omsImages/main/icons/application_double.png',
							                handler: this.openInNewWindow
							            })
					        	   ] 
			    	}); 	       
	 }
	 
	 /**
	  * method to show context menu on rigth click on the shortcut
	  * @param type_ type of the module ajax or standard
	  */
	 this.showConMenu = function(event_, type_){
	 		this.conMenu.items.get('shortcut_remove_item').hide();
	 		this.conMenu.items.get('shortcut_open_in_new_window').hide();
	 		this.conMenu.items.get('shortcut_open_item').hide();

	 		
	 		this.conMenu.items.get('shortcut_open_item').removeListener("click",context.menu.openFromMenuAjax);
	 		this.conMenu.items.get('shortcut_open_item').removeListener("click",context.menu.openFromMenuStandard);
	 		
	 		if(type_ == 'AJAX'){
	 			this.conMenu.items.get('shortcut_open_item').addListener("click",context.menu.openFromMenuAjax);
	 			this.conMenu.items.get('shortcut_open_item').show();
	 			this.conMenu.items.get('shortcut_remove_item').show();
	 		}else{
	 			this.conMenu.items.get('shortcut_open_item').addListener("click",context.menu.openFromMenuStandard);
	 			this.conMenu.items.get('shortcut_open_item').show();
	 			this.conMenu.items.get('shortcut_remove_item').show();	
	 			this.conMenu.items.get('shortcut_open_in_new_window').show();
	 		}
	 		context.menu.conMenu.showAt(event_.getXY());
	 }
	 
	 this.openFromMenuStandard = function(){
	 	context.menu.programs.openProgramWindow(context.menu.clickedShortcut);
	 }
	 
	 this.openFromMenuAjax = function(){
	 	context.applyModule(context.menu.clickedShortcut);
	 }
	 
	 /**
	  * method to open module in new window
	  */
	 this.openInNewWindow = function(item_){
	 	if(context.menu.clickedShortcut.type.toLowerCase() == 'standard'){
	 		tmpArray = context.menu.clickedShortcut.id.split('_-_');
	 		var elem = new Object();
		 	elem.windowId = tmpArray[1]; 
		 	elem.shid    = tmpArray[tmpArray.length - 1];
		 	elem.url  	 = tmpArray[tmpArray.length - 2];	
		 	elem.winText = tmpArray[tmpArray.length - 3];
		 	elem.type    = 'standard';
		 	context.menu.programs.openProgramInNewWindow(elem);
	 	}
	 } 
	 
	 /**
	  * method to open module in new window from menu
	  */
	 this.openInNewWindowFromMenu = function(item_){
	 		var elem = new Object();
		 	elem.windowId = item_.windowId;
		 	elem.url  	 = item_.url;	
		 	elem.winText = item_.winText;
		 	elem.type    = 'standard';
		 	context.menu.programs.openProgramInNewWindow(elem);
	 } 
	 
	 /**
	  * loading method
	  */
	 this.load = function(){
	 		this.menuLoader.load();
	 } 
	 
	 /**
	  * method to create most frequenty open applications
	  */
	 this.createMostFrequentyApp = function(){
	 	var arr = new Array(this.mostFreqAppLoader.getCount());
	 	
	 	
	 	for(var i=0; i<arr.length; ++i){
	 		
	 		id    = this.mostFreqAppLoader.getAt(i).get('id');	
	 		url   = this.mostFreqAppLoader.getAt(i).get('url');	
	 		name  = context.translator.translate("sector_"+this.mostFreqAppLoader.getAt(i).get('id'));	
	 		type  = this.mostFreqAppLoader.getAt(i).get('type');
	 		
	 		
	 		
	 		if(type == 'STANDARD')
	 			myHandler = context.menu.programs.openProgramWindow;
	 		else
	 			myHandler = context.applyModule;
	 			
	 		arr[i] = Ext.extend(Ext.app.Module, {
						    id:'mainMenuItem'+id,
						    init : function(){
						        this.launcher = {
						        	id: id,	
						            text: name,
			 						icon: '../images/omsImages/main/iconsThumb/'+context.menu.getSmallIcon(id),	
						            handler : myHandler,
						            scope: this
						        }
						    }
						});
			arr[i] = new arr[i]();			
			arr[i].launcher.url  	  = url;
			arr[i].launcher.winText    = name;
			arr[i].launcher.windowId   = id;	
			arr[i].launcher.type 	  = type;		
	 	}
	 	
	 	/*var num = arr.length;
	 	arr[num] = Ext.extend(Ext.app.Module, {
						    id:'mainMenuItem-sep',
						    init : function(){
						        this.launcher = {
						        	id: 'main_sep',
						        	height: 10,	
						            text: '----------------------------',
						            scope: this
						        }
						    }
						});
	 	arr[num] = new arr[num]();*/
	 	return arr;
	 } 
	 
	 /**
	  * method to add standard actions menu items to module
	  * @param module with data like winText etc
	  * @param menu to add new module
	  * @param id of the added module
	  */
	 this.addModuleItems = function(module_ ,moduleMenu_, id_){
	 		items = new Array(3);
	 		items[0] = new Ext.menu.Item({
			 							text: 'Open',
			 							id: id_ + 'menu_item_open',
			 							icon: '/../../imc/images/omsImages/main/icons/door_open.png',
			 							minWidth: 200	
		 						   });
			
			if(module_.type == 'STANDARD')
		 		items[0].on("click", this.programs.openProgramWindow, items[0], true);
		 	else
		 		items[0].on("click", context.applyModule, items[0], true);			
		 	
		 	items[1] = new Ext.menu.Item({
			 							text: 'Open In New Window',
			 							id: id_ + 'menu_item_open_in_new_window',
			 							icon: '/../../imc/images/omsImages/main/icons/application_double.png',
			 							minWidth: 200	
		 						   });
		 	
		 	items[1].on("click", this.openInNewWindowFromMenu, items[1], true);
		 						   
		    items[2] = new Ext.menu.Item({
			 							text: 'Create Shortcut',
			 							id: id_ + 'menu_item_shortcut',
			 							disabled: this.disabled,
			 							icon: '/../../imc/images/omsImages/main/icons/icons.png',
			 							minWidth: 200	
		 						   });
		 						   
		 	
		 			 						  
		 	items[2].on("click", this.addShortcut, items[1], true);
		 	
		 	for(i=0; i<items.length; ++i){
		 		items[i].winText  = module_.winText;
			 	items[i].url	  = module_.url;
			 	items[i].windowId = module_.windowId;
			 	if((i==1) && (module_.type != 'STANDARD'))
			 	  continue;	
		 		moduleMenu_.add(items[i]);
		 	}					  					   
	 }	
	 
	 /**
	  * method to add children to the menu
	  * @param menu to add
	  * @param menu child config array from the database user menu
	  */ 
	 this.addChilds = function(menu, array_){
	 
	 		for(var i=0; array_.length; ++i){
	 			var item;
		 		if(array_[i]){
		 			if(array_[i][3].length > 0){
		 				item = new Ext.menu.Item({
			 							text: context.translator.translate('department_'+array_[i][0]),
			 							id: array_[i][0] + 'menu_item',
			 							minWidth: 200,
			 							menu: new Ext.ux.ColumnMenu({
			 										maxHeight: Ext.getBody().getHeight(),
			 										columnWidth: 250
			 									})	
		 						   });
		 				item.menu.depId = array_[i][0];		
		 				//alert(item.menu.depId);   
		 			}			   
		 			else{
		 				item = new Ext.menu.Item({
			 							text: context.translator.translate('sector_'+array_[i][4]),
			 							id: array_[i][0] + 'menu_item',
			 							html: "<div id='"+array_[i][0] + 'menu_item_sub'+"' width='100%' height='100%'>asssss</div>",
			 							minWidth: 200,
			 							icon: '../images/omsImages/main/iconsThumb/'+context.menu.getSmallIcon(array_[i][4]),
			 							menu: new Ext.menu.Menu({})		
		 						   });
		 				
		 				item.winText  = context.translator.translate('sector_'+array_[i][4]);
		 				item.url 	  = array_[i][2];
		 				item.windowId = array_[i][4];
		 				item.type     = 'STANDARD';
		 				item.on("click", this.programs.openProgramWindow, item, true);
		 				this.addModuleItems(item ,item.menu);
		 			}
		 			menu.add(item);
		 			
		 			//elem = item.getEl();
		 				//alert(item);
		 			
		 			if(array_[i][3]){
		 				newArray = new Array();
		 				newArray = array_[i][3];
		 					this.addChilds(item.menu, newArray);	
		 			}			   	
		 		}
		 		else{	
		 			break;
		 		}	
	 		}
	 		 	
	 }
	 
	 
	 /**
	  * method to add ajax modules
	  * @param menu to add new modules in this case ajax modules
	  */
	 this.addChildsItem = function(menu){
	 	
	 	for(var j=0; j<this.ajaxDepartmentsLoader.getCount(); ++j){
	 		if(this.ajaxDepartmentsLoader.getAt(j).get('parent') == menu.depId){

			var	item = new Ext.menu.Item({
			 							text: context.translator.translate('department_'+this.ajaxDepartmentsLoader.getAt(j).get('id')),
			 							id: this.ajaxDepartmentsLoader.getAt(j).get('id') + 'menu_item',
			 							minWidth: 200,
			 							menu: new Ext.menu.Menu({
			 									 
			 									})	
		 						   });
		 		item.menu.depId = this.ajaxDepartmentsLoader.getAt(j).get('id');
		 		menu.add(item);	
				this.addChildsItem(item.menu);
	 		}	
	 	}
	 	
	 	for(var i=0; i<this.ajaxSectorsLoader.getCount(); ++i){
	 		if(this.ajaxSectorsLoader.getAt(i).get('department') == menu.depId){
				item = new Ext.menu.Item({
			 							text: context.translator.translate('sector_'+this.ajaxSectorsLoader.getAt(i).get('id')),
			 							id: this.ajaxSectorsLoader.getAt(i).get('id') + 'menu_item',
			 							minWidth: 200,
			 							icon: '../images/omsImages/main/iconsThumb/'+context.menu.getSmallIcon(this.ajaxSectorsLoader.getAt(i).get('id')),
			 							menu: new Ext.menu.Menu({
			 								 	})		
		 						   });
		 				
		 		item.winText  = this.ajaxSectorsLoader.getAt(i).get('name');		   
		 		item.url 	  = this.ajaxSectorsLoader.getAt(i).get('path');
		 		item.windowId = this.ajaxSectorsLoader.getAt(i).get('id');
		 		item.type     = 'AJAX';
		 		
		 			item.on("click", context.applyModule, item, true);		
		 		this.addModuleItems(item ,item.menu);
		 		
		 		menu.add(item);
	 		}	
	 	}
	 }
	 
	 /**
	  * method to create system menu
	  */
	 this.createMenu = function(){
	 	if(context.user.getUserSystemPosition() == 3)
	 	  this.disabled = true;	
	 	eval(this.menuLoader.getAt(0).get('array'));
	 	var myArr = new Array(JS_departments_array.length);
	 	myArr = JS_departments_array;
	 	this.programsMenu = new Ext.menu.Menu({
								id: 'programsMenu'
							});
		
		var num = myArr.length;
		for(var i=0; i<this.ajaxDepartmentsLoader.getCount(); ++i){
			if( !this.ajaxDepartmentsLoader.getAt(i).get('parent')  ){
					myArr[num]    = new Array();
					myArr[num][0] = this.ajaxDepartmentsLoader.getAt(i).get('id');
					myArr[num][1] = this.ajaxDepartmentsLoader.getAt(i).get('department');
					myArr[num][2] = this.ajaxDepartmentsLoader.getAt(i).get('path');
					myArr[num][3] = new Array();
					myArr[num][4] = this.ajaxDepartmentsLoader.getAt(i).get('id');
					num++;
			}
		}
						
	 	this.programs = new Programs();
	 	
	 	this.addChilds(this.programsMenu, myArr);
	 	
	 	this.programsMenu.depId = 0;
	 	this.addChildsItem(this.programsMenu);
	 	
	 	
	 }
	
} 
