/**
 * file News.js with News class
 * @author Janusz Rygał
 * @date 23.09.2008 
 */

/**
 * @class News with news definition
 */
 News = function(){
	
	/**news loader*/
	this.countryLoader;
	/**bussines loader*/
	this.bussinesLoader;
	/**user rss loader*/
	this.userRssLoader;
	/**pressiton loader*/
	this.pressitonLoader;
	/**news window*/
	this.window;
	/**country panel*/
	this.countryPanel;
	/**bussines panel*/
	this.bussinesPanel;
	/**pressiton panel*/
	this.pressitonPanel;
	/**user panel*/
	this.userPanel;
	/**user rss form*/
	this.userRssForm;
	/**
	 * load method
	 */
	this.load = function(){
		this.init();	
	    
	    this.pressitonLoader.load();
	    this.pressitonLoader.on('load', function(loader, record){
				context.news.countryLoader.load();
				context.news.countryLoader.on('load', function(loader, record){
					context.news.bussinesLoader.load();
					context.news.bussinesLoader.on('load', function(loader, record){
						
						context.news.createUserRssForm();
						context.news.createCountryNewsPanel();
						context.news.createBussinesNewsPanel();
						context.news.createPressitonNewsPanel();
						context.news.createContent();
						context.news.display();
					
					}, context.news.bussinesLoader);	
				}, context.news.countryLoader);	
    	}, this.pressitonLoader);	
    } 
	
	/**
	 * method to create user rss panel
	 */
	this.createUserRssPanel = function(){
		var rssChanel = Ext.get('userRssUrl').getValue();
		context.news.userRssLoader = new Ext.data.Store({
					        url: '/imc/feed-proxy.php?feed='+rssChanel,
					        reader: new Ext.data.XmlReader
					        (
					       		  {
					               	record: 'item'
					           	  }, [
					               	'title', 
					               	'description',
					               	'link'
					           	  ]
					        )
					    });	
		context.news.userRssLoader.load();
		
		context.news.userRssLoader.on('load', function(loader, records){
		
				var html  = "<div>";
					for(var i=0; i<context.news.userRssLoader.getCount(); ++i ){	
				        html += "<br/><div id='user_"+i+"' style=\"cursor: pointer;\" onclick=\"context.news.showNews(this)\" ><div><b>";
				        	html += context.news.userRssLoader.getAt(i).get('title');
				        html += "</b></br></div>";
				        html += "<div>";
				        	html += context.news.userRssLoader.getAt(i).get('description').substr(0,100);
				        html += "</div>";	
				        html += "<hr/></div>";    
				    }	
				    html += "</div>";
				var loaded = false;
				
				if(context.news.userPanel)
					loaded = true;    
					
					context.news.userPanel= new Ext.Panel({
											title: context.translator.translate('user_rss_channel'),
											id: 'userNewsPanel',
											autoHeight: true,
											autoScroll: true,
											collapsible: true,
											collapsed: false,
											html: html
										});
										
					context.news.userPanel.on('expand', function(panel){
									context.news.countryPanel.collapse();
									context.news.bussinesPanel.collapse();	
									context.news.pressitonPanel.collapse();	
								});						
				if(loaded)
					context.news.window.remove('userNewsPanel');
												
					context.news.window.add(context.news.userPanel);		
					context.news.window.hide();
					context.news.window.show();	
					context.news.bussinesPanel.collapse();
					context.news.countryPanel.collapse();	
					context.news.pressitonPanel.collapse();	
		}, context.news.userRssLoader);			    
	} 
	
	/**
	 * method to create panel with the input field and button 
	 * to add user defined rss channel
	 */
	this.createUserRssForm = function(){
			this.userRssForm = new Ext.Panel({
									id: 'userRssForm',
									autoHeight: true
							   });
							   
			this.userRssForm.add(
							new Ext.form.Label({
													text: context.translator.translate('rss_channel')+' '
											   })	
						);	
													   
			this.userRssForm.add(
							new Ext.form.TextField({
													id:  'userRssUrl',
													name: 'userRssUrl',
													emptyText: context.translator.translate('paste_rss_url')+' ...'
												  })	
						);	
			this.userRssForm.add(
							new Ext.Button({
													text: context.translator.translate('add'),
													handler: context.news.createUserRssPanel
												  })	
						);							   
	}
	
	/**
	 * initialize method
	 */
	this.init = function(){
		this.countryLoader = new Ext.data.Store({
					        url: '/imc/feed-proxy.php?feed='+context.user.preferences.getCountryRssById( context.user.preferences.getCountry() ),
					        reader: new Ext.data.XmlReader
					        (
					       		  {
					               	record: 'item'
					           	  }, [
					               	'title', 
					               	'description',
					               	'link'
					           	  ]
					        )
					    });
					    
		this.bussinesLoader = new Ext.data.Store({
					        url: '/imc/feed-proxy.php?feed=http://irishnews.com/rss/business.xml',
					        reader: new Ext.data.XmlReader
					        (
					       		  {
					               	record: 'item'
					           	  }, [
					               	'title', 
					               	'description',
					               	'link'
					           	  ]
					        )
					    });		    
		this.pressitonLoader = new Ext.data.Store({
					        url: '/imc/feed-proxy.php?feed=http://www.polconstruct.com/imc/oms.php/main/ShowRss?langId='+context.user.preferences.getLanguageSymbolById(context.user.preferences.getLanguage()),
					        reader: new Ext.data.XmlReader
					        (
					       		  {
					               	record: 'item'
					           	  }, [
					               	'title', 
					               	'description',
					               	'link'
					           	  ]
					        )
					    });				    		    
	}
	
	/**
	 * method to display news
	 */
	this.display = function(){
		if(context.user.preferences.isNewsEnabled()){
			context.news.window.show();
			context.news.window.alignTo(document, 'r-r');
			context.news.window.toBack();
		}
		else{
			if(context.news.window.isVisible())
				context.news.window.hide();
		}
	} 
	
	/**
	 * method to display clicked news item in new window 
	 * @param clicked element
	 */
	this.showNews = function(item_){
		tmpArr = new Array();
		tmpArr = item_.id.split('_');
		
		var item = new Ext.Element();
		
		if(tmpArr[0] == 'country'){
							  item.winText 	= this.countryLoader.getAt(tmpArr[1]).get('title');
							  item.url 		= this.countryLoader.getAt(tmpArr[1]).get('link');	
		}
		else if(tmpArr[0] == 'bussines'){
							  item.winText 	= this.bussinesLoader.getAt(tmpArr[1]).get('title');
							  item.url 		= this.bussinesLoader.getAt(tmpArr[1]).get('link');	
		}
		else if(tmpArr[0] == 'user'){
							  item.winText 	= this.userRssLoader.getAt(tmpArr[1]).get('title');
							  item.url 		= this.userRssLoader.getAt(tmpArr[1]).get('link');	 				  				  
		}
		else if(tmpArr[0] == 'pressiton'){
							  item.winText 	= this.pressitonLoader.getAt(tmpArr[1]).get('title');
							  item.url 		= this.pressitonLoader.getAt(tmpArr[1]).get('link');	 				  				  
		}
		
		item.windowId = item_.id;
		item.icon     = 'newspaper.png';
		item.type     = 'STANDARD';
		context.menu.programs.openProgramWindow(item);
		this.window.toBack();
	}

	/**
	 * method to create pressiton news panel
	 */
	this.createPressitonNewsPanel = function(){
		var html  = "<div>";
		for(var i=0; i<this.pressitonLoader.getCount(); ++i ){	
	        html += "<br/><div id='pressiton_"+i+"' style=\"cursor: pointer;\" onclick=\"context.news.showNews(this)\" ><div><b>";
	        	html += this.pressitonLoader.getAt(i).get('title');
	        html += "</b></br></div>";
	        html += "<div>";
	        	html += this.pressitonLoader.getAt(i).get('description').substr(0,100);
	        html += "</div>";	
	        html += "<hr/></div>";    
	    }	
	    html += "</div>";
		this.pressitonPanel = new Ext.Panel({
								title: context.translator.translate('pressiton_news'),
								id: 'pressitonNewsPanel',
								collapsible: true,
								autoHeight: true,
								collapsed: false,
								autoScroll: true,
								html: html
							});
		this.pressitonPanel.on('expand', function(panel){
			context.news.countryPanel.collapse();
			context.news.bussinesPanel.collapse();
			if(context.news.userPanel)
			  context.news.userPanel.collapse();	
		});						
	} 
	
	/**
	 * method to create bussines news panel
	 */
	this.createBussinesNewsPanel = function(){
		var html  = "<div>";
		for(var i=0; i<this.bussinesLoader.getCount(); ++i ){	
	        html += "<br/><div id='bussines_"+i+"' style=\"cursor: pointer;\" onclick=\"context.news.showNews(this)\" ><div><b>";
	        	html += this.bussinesLoader.getAt(i).get('title');
	        html += "</b></br></div>";
	        html += "<div>";
	        	html += this.bussinesLoader.getAt(i).get('description').substr(0,100);
	        html += "</div>";	
	        html += "<hr/></div>";    
	    }	
	    html += "</div>";
		this.bussinesPanel = new Ext.Panel({
								title: context.translator.translate('bussines_news'),
								id: 'bussinesNewsPanel',
								collapsible: true,
								collapsed: true,
								autoHeight: true,
								autoScroll: true,
								html: html
							});
		this.bussinesPanel.on('expand', function(panel){
			context.news.countryPanel.collapse();
			context.news.pressitonPanel.collapse();
			if(context.news.userPanel)
			  context.news.userPanel.collapse();	
		});						
	} 
	
	/**
	 * method to create country new panel
	 */
	this.createCountryNewsPanel = function(){
		var html  = "<div>";
		for(var i=0; i<this.countryLoader.getCount(); ++i ){	
	        html += "<br/><div id='country_"+i+"' style=\"cursor: pointer;\" onclick=\"context.news.showNews(this)\" ><div><b>";
	        	html += this.countryLoader.getAt(i).get('title');
	        html += "</b></br></div>";
	        html += "<div>";
	        	html += this.countryLoader.getAt(i).get('description').substr(0,100);
	        html += "</div>";	
	        html += "<hr/></div>";  
	    }	
	    html += "</div>";
		this.countryPanel = new Ext.Panel({
								title: context.translator.translate('country_news'),
								id: 'countryNewsPanel',
								collapsible: true,
								collapsed: true,
								autoHeight: true,
								autoScroll: true,
								html: html
							});
		this.countryPanel.on('expand', function(panel){
			context.news.bussinesPanel.collapse();
			context.news.pressitonPanel.collapse();
			if(context.news.userPanel)
			  context.news.userPanel.collapse();	
		});					
	} 
	
	/**
	 * method to create container
	 */
	this.createContent = function(){
	
		//var maxHeight = Ext.getBody().getHeight() - 34;
		//this window must have manager option in config!!!
	    var desktop = context.desk.MyDesktop.getDesktop();
		this.window = new Ext.Window({
						  		id: 'newsWindow',
								title: context.translator.translate('news'),
								collapsible: true,
								width: 300,
								//autoHeight: true,
								autoScroll: true,
								iconCls: 'news',
								height: 300,
								manager: desktop.getManager(),
								shadow: false,
								items: [
										 this.userRssForm,	
										 this.pressitonPanel,
										 this.countryPanel,
										 this.bussinesPanel	
									   ]
						  });	
						  			  
		this.window.on('beforeclose', function(e){
			if (context.user.getUserSystemPosition() != 3){
				var desktop_img = context.user.preferences.getDesktopIdByName( context.user.preferences.getDesktopImage() );

                var g_levels = context.user.preferences.preferencesLoader.getAt(0).get('graphic_level');
                var s_clock = context.user.preferences.preferencesLoader.getAt(0).get('show_clock');
				var s_reminder = context.user.preferences.preferencesLoader.getAt(0).get('show_reminder');
                var s_tips     = context.user.preferences.preferencesLoader.getAt(0).get('show_tips');

				if(s_clock == 1)
					s_clock = 'on';
                else
                    s_clock = 'off';

                if(s_reminder == 1)
					s_reminder = 'on';
                else
                    s_reminder = 'off';

                if(s_tips == 1)
					s_tips = 'on';
                else
                    s_tips = 'off';
				
				Ext.Ajax.request({
								url: project_url + 'SaveScreenSettings',
								method: 'GET',
							   	params: { background_image: desktop_img, 
                                          show_clock: s_clock,
                                          show_news: 0,
                                          graphic_levels: g_levels,
                                          show_reminder: s_reminder,
                                          show_tips: s_tips
                                       }
							});
			}
		});
	} 
	
		
	
}




