/**
 * file with Context class
 * @author Janusz Rygal
 * @date 13.08.2008
 */
 
 var project_url = 'main/'; 
 
 
 /** 
  * @class Context main program class in parent function 
  */
 Context = function(){
 		/**cache class object*/
    	//this.cache = new Cache("polconstruct.com");
    	//		this.cache.init();
 		/**user class object*/
 		this.user = new User();
        this.tiper = new Tiper();
        this.deskShortCreator = new DeskShortCreator();
 		
 		/**messenger class object*/
 		this.messenger = new Messenger();

        /**starters object*/
        this.starters = new Starters();

        /**linker object*/
        this.linker  = null;

 		/**clock class object*/
 		this.clock;
 		
 		/**UrlMaker*/
 		this.urlMaker = new UrlMaker();
 			this.urlMaker.setShift(1);
 			this.urlMaker.decode();

 		/**Communictor*/
 		this.communicator = new Communicator();
 		
 		/**translator*/
 		this.translator = null;
 		/**settings*/
 		this.settings = new Settings();
 		/**help*/
 		this.help = new Help();	
 		/**news*/
 		this.news = new News();
 		/**reminder*/
 		this.reminder = new Reminder();
 		
 		/**module loader*/
 		this.moduleLoader;
 		/**modules array*/
 		this.modules = new Array();
 		/**modules iter*/
 		this.modIter = 0;
 		/**non module window ids*/
 		this.appIter = -1;
 		/**modules item data*/
 		this.modItemData;
 		
 		this.loadMask;
 		
 		/**loging object*/
 		this.logging = new Logging();
 		
 		/**oms history object*/
 		this.history = new History();
 		
 		/**menu class object*/
 		this.menu    = new Menu();
 		
 		/**appliaction*/
 		this.application;

        this.bugWindow = null;

 		/**user desktop class*/
 		this.desk;


        this.clientData = new ClientData();




        this.showBugWindow = function(item_){
            
            tmpArr = new Array();
        		tmpArr = item_.id.split('_-_');
        		//url    = tmpArr[tmpArr.length - 1];

        	var winId = tmpArr[tmpArr.length - 2].replace("programWindow","");
        	var window = Ext.getCmp("programWindow_-_"+winId);

            var myframe  = document.getElementById(tmpArr[tmpArr.length - 2]+'-iframe');

            var getContent = 'none';
            
            if(myframe)
                getContent = myframe.contentWindow.document.location;

            //alert(winId);
            //alert(getContent);

            var bugForm  = new Ext.FormPanel({
											labelWidth: 250,
											url:project_url + 'ReportBug',
											id: 'bugForm_'+winId,
											name:'bugForm',
											frame:true,
											disabled: false,
											draggable: false,
											style:'margin: 0 auto;',
											bodyStyle:'padding:5px 5px 0;',
											width: 580,
											autoHeight: true,
											defaults: {width: 230},
											defaultType: 'textfield',
											buttons: [{
														text: context.translator.translate('report'),
														disabled:false,
														handler: function(){
																	if (Ext.ComponentMgr.get('bugForm_'+winId).getForm().isValid()) {
																		Ext.ComponentMgr.get('bugForm_'+winId).getForm().submit({
																			url:project_url + 'ReportBug',
																			waitMsg:'Saving Data...',
																			success: function(form, action) {
                                                                                        context.bugWindow.close();
																					 	//Ext.Msg.alert('OMS',"Success - Bug was reported successfuly !");
																					 },
										                                	failure: function(form, action) {
																					Ext.Msg.alert('OMS',"Error occured while bug reporting !");
										                                	}
																		});
																	}
																}
													 },
													 {
													   text: context.translator.translate('cancel'),
													   disabled: false,
													   handler: function(){
													   		context.bugWindow.close()
													   }
													 }]
							});

            bugForm.add( new Ext.form.TextArea({
                    width: 200,
                    height: 100,
                    name: 'problemDesc',
                    fieldLabel: context.translator.translate('problem_desc'),
                    allowBlank: false
            }));

            bugForm.add( new Ext.form.Hidden({
                    name: 'moduleId',
                    value: winId
            }));

            bugForm.add( new Ext.form.Hidden({
                    name: 'getContent',
                    value: getContent
            }));

            context.bugWindow = new Ext.Window({
                modal: true,
                width: 600,
                height: 200,
                title: context.translator.translate('report_bug'),
                items: [bugForm]
            });

            context.bugWindow.render(window.getEl());
            context.bugWindow.show();
        }

 		/**method to initialize object*/	
 		this.init = function(){
            var pageLocation = new String(window.location);
            var questCharPos = pageLocation.indexOf('?') + 1;
		  	if(questCharPos > 0){
                   context.linker = new Linker( pageLocation.substring(questCharPos) );
            }else{
                   context.linker = new Linker(pageLocation);
            }

 			this.user.init();
 			this.menu.init();
 			this.reminder.init();
 			//this.news.init();
 		}

        this.ieLuncher = function(elem_){
            alert(elem_);
        }
 		
 		
 		/** 
 		 * method to convert store object to string 
 		 * @param root_ name of the root node
 		 * @param store_ store object
 		 * @return array string
 		 */
 		this.convertStoreToString = function(root_, store_){
 				
 			/*var result = '[';
 			
 			for(var i=0; i<store_.getCount(), ++i){
 				result += '['+root_+']['+i+'][';
	 			for(var j=0; j<store_.fields.getCount(); ++j){
	 				alert(store_.fields.get(j).name);
	 			}
	 		}	*/
 		}
 		
 		this.openWindowByUrl = function(){
 		  if( !context.urlMaker.isGetSet())
 		  	return;

          if(context.linker.isNewLink()){
            context.linker.openModuleByUrl();
          }else{
 			var item = new Object();
 			item.windowId 	= context.urlMaker.getElementByName("windowId");
 			item.id 		= context.urlMaker.getElementByName("id");
 			item.url 		= context.urlMaker.getElementByName("url");
 			item.winText 	= context.urlMaker.getElementByName("winText");
 			context.menu.programs.openProgramWindow(item);
          }
 		}
 		
 		/**
 		 * method to back content in module window
 		 */
 		this.backStandardWindow = function(item_){
 			tmpArr = new Array();
        	tmpArr = item_.id.split('_-_');
        	url    = tmpArr[tmpArr.length - 1];
        	var myframe  = document.getElementById(tmpArr[tmpArr.length - 2]+'-iframe');
        		myframe.contentWindow.history.go(-1);
        	  
 		} 
 		
 		/**
 		 * method to forward content in module window
 		 */
 		this.forwardStandardWindow = function(item_){
 			tmpArr = new Array();
        	tmpArr = item_.id.split('_-_');
        	url    = tmpArr[tmpArr.length - 1];
        	var myframe  = document.getElementById(tmpArr[tmpArr.length - 2]+'-iframe');
        		myframe.contentWindow.history.go(1);
 		} 
 		
 		/**
 		 * method to print module content
 		 */
 		this.printModule = function(item_){
 			tmpArr = new Array();
        	tmpArr = item_.id.split('_-_');
        	url    = tmpArr[tmpArr.length - 1];
        	frame  = document.getElementById(tmpArr[tmpArr.length - 2]+'-iframe');
        	frame.contentWindow.print();
 		} 
 		
 		/**
 		 * @desc method to check link if not contain www part then add 
 		 * @param link
 		 * @return repaired link
 		 */
 		this.addWWWToLink = function(link_){
 			if(link_.indexOf("http://www") <0){
 			   if(link_.indexOf("http://") >= 0 )	
 			   		link_ = "http://www."+link_.substring(7);	
 			}
 			return link_;
 		}
 		
 		this.replaceStandardLinks = function(frame){
 				context.loadMask.hide();
 				
        		for(var i=0; i<frame.contentDocument.links.length; ++i){
        			frame.contentDocument.links[i].href = context.addWWWToLink(frame.contentDocument.links[i].href);
        			if(frame.contentDocument.links[i].target == "_blank"){
        			
        				var oldHref =  frame.contentDocument.links[i].href;
        				frame.contentDocument.links[i].target = "";
        				frame.contentDocument.links[i].href = "#";
        				frame.contentDocument.links[i].windowId = frame.id+"_"+oldHref; 
		 			  	frame.contentDocument.links[i].shid     = -1;
		 			  	frame.contentDocument.links[i].url  	 = oldHref;	
		 			  	frame.contentDocument.links[i].winText = frame.contentDocument.links[i].text + " submodule";
		 			  	frame.contentDocument.links[i].type    = 'standard';
        				frame.contentDocument.links[i].onclick = function(item_){
        					context.menu.programs.openProgramWindow(this);
        				}
        			}
        		}
 		}
 		
 		this.getIframeUrl = function(item_){
 				tmpArr = new Array();
        		tmpArr = item_.id.split('_-_');
        		//url    = tmpArr[tmpArr.length - 1];
        		
        		var winId = tmpArr[tmpArr.length - 2].replace("programWindow","");      		
        		var window = Ext.getCmp("programWindow_-_"+winId);
        		
        		var frame  = document.getElementById(tmpArr[tmpArr.length - 2]+'-iframe');
        		
        		var url 	 = null;
                if(Ext.isIE)
                    url = document.frames[tmpArr[tmpArr.length - 2]+'-iframe'].document.location.href;
        		else
                    url = frame.contentDocument.location.href;

                var windowId = window.windowId;
        		var id 		 = window.id;
        		var winText  = window.title;
        		
        		var preGet = new Array(4);
        			preGet["id"] 		= winId;
        			preGet["windowId"] 	= windowId;
        			preGet["url"] 		= url;
        			preGet["winText"] 	= winText;

                context.linker.createLink(preGet, window);
               /*
   			   var infoWin = new Ext.Window({
   			   				 	title:		'Module Url',
   			   				 	modal: true,
   			   				 	html:  "<div><b>"+context.urlMaker.encode(preGet)+"</b></div>",
   			   				 	width: 500,
   			   				 	height: 80,
   			   				 	autoScroll: true
   			   				 });
   			   	infoWin.show();
               */
 		}
 		
 		/**
 		 * method to refresh non standard module
 		 * @param object with module data
 		 */
 		this.refreshStandardWindow = function(item_){
        		tmpArr = new Array();
        		tmpArr = item_.id.split('_-_');
        		url    = tmpArr[tmpArr.length - 1];
        		frame  = document.getElementById(tmpArr[tmpArr.length - 2]+'-iframe');
  
  				winId = tmpArr[tmpArr.length - 2].replace("programWindow","");      		
        		win = Ext.getCmp("programWindow_-_"+winId);
        		
        		context.loadMask = new Ext.LoadMask(win.getEl(),{
										msg  : context.translator.translate('please_wait')+" "+win.title+" "+context.translator.translate('url_app_loading'),
										msgCls: 'pressiton-loading'
								});
				context.loadMask.show();	
        		frame.src = url;
        }
 		
 		/**
 		 * method to create the load mask
 		 */
 		this.createLoadMask = function(text_){
 			this.loadMask = new Ext.LoadMask(Ext.getBody(),{
									msg  : text_,
									msgCls: 'pressiton-loading'
							});
 		}
 		
 		/**to apply module*/
 		this.applyModule = function(itemData_, newWindow_){
 			 
 			 itemData_.modId = itemData_.windowId;
 			 var desktop = context.menu.programs.app.getDesktop();
			 var win = desktop.getWindow('programWindow'+itemData_.windowId);
 			 if(win && (!newWindow_)){
 			 		win.show();
 			 		return;
 			 }
 			 
 			 itemData_.newWindow = false;
 			 if(newWindow_){
 			 	itemData_.newWindow = true;
 			 }
 			 
 			 context.createLoadMask(context.translator.translate('please_wait')+' '+itemData_.winText+' '+context.translator.translate('app_code_loading'));
 			 context.loadMask.show();
 			 
 			 	
			 context.moduleLoader = new ModuleLoader(itemData_.url, itemData_.windowId);	
			 context.modItemData = itemData_;
			 context.moduleLoader.load(); 
			 	 
			 	 
			 context.moduleLoader.loader.on('load', function(loader, records){
										eval(context.moduleLoader.loader.getAt(0).get('app'));
										context.loadMask.hide();
										main();							
			 },context.moduleLoader.loader);				
 		}
 		
 		/**
 		 * method to get element by id
 		 * @param id to search
 		 * @return if exists true if not the result is false
 		 */
 		this.findById = function(id_){
 			if(Ext.getCmp(id_) == null){
 				return false;
 			}else{
 				return true;
 			}
 		} 
 		
 		/**
 		 * method to get next id of the element
 		 * @param id for search
 		 */
 		this.findNextWindowId = function(id_){
 			tmpArray = id_.split('_-_');
 			if(tmpArray.length > 1){
 				var modWinId = tmpArray[tmpArray.length -1];
 				modWinId++;
 				
 				var newId  = '';
 				for(i=0; i<(tmpArray.length-1); ++i){
 					newId += tmpArray[i]+'_-_';
 				}
 				newId += modWinId;
 				
 				if(context.findById(newId)){
 					context.findNextWindowId(newId);
 				}else{
 					return newId;
 				}
 			}else{
 				return (id_+'_-_0');
 			}
 		} 
 }
 
 var context = new Context();
 	 context.init();
 	 
 
 	 
 	 
 	 
 	 
 	 
 	 