/**
 * file User.js with user class
 * @author Janusz Rygał
 * @date 04.09.2008 
 */

/**
 * @class Preferences class
 */
 Preferences = function(){
	/**loader object*/
	this.preferencesLoader;
	/**countries store*/
	this.countriesStore;
	/**desktops store*/
	this.desktopsStore;
	/**languages store*/
	this.langStore;
	/**graphic level store*/
	this.graphicLevelsStore;
	/**animated collapse*/
	this.animateCollapseWindow; 
	/**windows with shadow or not*/
	this.windowShadow;
	
	
	this.defineGraphicPreferences = function(){
		var level = this.getGraphicLevel();
		
		switch(level){
			case 1:
					this.animateCollapseWindow  = true;
					this.windowShadow 		    = true;	
					break;
			case 2: 
					this.animateCollapseWindow  = false;
					this.windowShadow 			= true;	
					break;
			case 3: 
					this.animateCollapseWindow  = false;
					this.windowShadow 			= false;	
					break;		
					
		}
	}
	
	/**
	 * method to apply preferences
	 */
	this.apply = function(first_){
		document.getElementById('x-desktop-img').src = "/../../imc/images/omsImages/main/desktops/"+context.user.preferences.getDesktopImage() ;
		document.getElementById('x-desktop-img').style.display = 'block';
		
		context.user.preferences.defineGraphicPreferences();
		
		if(context.user.preferences.isClockEnabled())
			document.getElementById('clock').style.display = 'block';
		else
			document.getElementById('clock').style.display = 'none';
		
		if(first_ == null){
			context.translator.setVersion(context.user.preferences.getLanguage());
			context.translator.init();
			context.translator.langStore.load();
			context.translator.langStore.on('load', function(loader, records){
						context.translator.createTransArray();	
			}, context.translator.langStore );	
		}
		
		if(context.menu.programs){
			if(context.news.window)
				context.news.display();
			else
				context.news.load();	
	  	}		
	} 
	
	/**
	 * method to get desktop image id by desktop name
	 * @param desktop name
	 * @return desktop id
	 */
	 this.getDesktopIdByName = function(name_){
	 	for(var i=0; i<this.desktopsStore.getCount(); i++){
	 		if(this.desktopsStore.getAt(i).get('name') == name_){
	 			return this.desktopsStore.getAt(i).get('id');
	 		}
	 	}
	 }
	 
	 /**
	 * method to get desktop image name by desktop id
	 * @param desktop id
	 * @return desktop name
	 */
	 this.getDesktopNameById = function(id_){
	 	for(var i=0; i<this.desktopsStore.getCount(); i++){
	 		if(this.desktopsStore.getAt(i).get('id') == id_){
	 			return this.desktopsStore.getAt(i).get('name');
	 		}
	 	}
	 }
	
    this.isReminderEnable = function(){
        if(this.preferencesLoader.getAt(0).get('show_reminder') == 0)
            return false;
        return true;
    }
    
    this.isTipEnable = function(){ 
        if(this.preferencesLoader.getAt(0).get('show_tips') == 0)
            return false;
        return true;
    }
    
    this.getCurrTip = function(){
        return this.preferencesLoader.getAt(0).get('curr_tip');
    }

	/**
	 * method to get graphic level values
	 */
	this.getGraphicLevel = function(){
		return this.preferencesLoader.getAt(0).get('graphic_level');
	} 
	
	/**
	 * method to load loader
	 */
	this.load = function(){
			 	this.preferencesLoader.load();	 
	}
	
	/**
	 * method to get view id
	 * @return view id
	 */
	this.getView = function(){
		return this.preferencesLoader.getAt(0).get('view');
	}

	/**
	 * method to get desktop image name
	 * @return name of desktop image file
	 */	
	this.getDesktopImage = function(){
		return this.preferencesLoader.getAt(0).get('desktop_image');
	}

	/**
	 * method to get language id 
	 * @return language id
	 */	
	this.getLanguage = function(){
		return this.preferencesLoader.getAt(0).get('language');
	}

	/**
	 * method to get language symbol by id
	 * @param language id
	 * @return language symbol
	 */	
	this.getLanguageSymbolById = function(id_){
		for(var i=0; i<this.langStore.getCount(); ++i){
			if(this.langStore.getAt(i).get('id') == id_){
				return this.langStore.getAt(i).get('symbol');
			}
		}
	}
	
	/**
	 * method to get country id
	 * @return country id
	 */	
	this.getCountry = function(){
		return context.user.preferences.preferencesLoader.getAt(0).get('country');
	}

	/**
	 * method to check is clock enabled or not
	 * @return true if clock visible false if not
	 */	
	this.isClockEnabled = function(){
		if (this.preferencesLoader.getAt(0).get('show_clock') == 1)
			return true;
		return false;
	}

	/**
	 * method  to get rss url propper to the user country
	 * @param  country id
	 * @return rss url 
	 */	
	this.getCountryRssById = function(id_){
		for(var i=0; i<context.user.preferences.countriesStore.getCount(); ++i){
			if( id_ == context.user.preferences.countriesStore.getAt(i).get('id') ){
				return 	context.user.preferences.countriesStore.getAt(i).get('rss');
			}
		}
	}

	/**
	 * method to check is news enable 
	 * @return false if not true if enable
	 */	
	this.isNewsEnabled = function(){
		if (this.preferencesLoader.getAt(0).get('show_news') == 1)
			return true;
		return false;
	}


	/**
	 * method to load countries
	 */
	this.loadCountries = function(){
	    
	    var myData				   = new Array();
			myData['countries']    = new Array();
		    myData['countries'][0] = new Array();
		myData['countries'][0]['name'] = 'Polska';
		myData['countries'][0]['id'] = '1';
		myData['countries'][0]['rss'] = 'dupa.rss';

    	/*			 
	    context.user.preferences.countriesStore.loadData(myData);
	    alert(context.user.preferences.countriesStore.getAt(0).get('name'));*/
	    
	    context.user.preferences.countriesStore.load();
	    context.user.preferences.countriesStore.on('load', function(rec, store){
	    	//var items = context.user.preferences.countriesStore.getRange(0, context.user.preferences.countriesStore.getCount());
	    	//context.user.preferences.countriesStore.loadData(items);
	    	//alert(items.length);
	    }, context.user.preferences.countriesStore);
	}

	/**
	 * method to create country store
	 */	
	this.createCountryStore = function(){
			this.countriesStore    = 	 new Ext.data.JsonStore({
    									    url: project_url + 'GetCountries',
									        fields: ['name','id','rss'],
									        root:'countries',
									        autoLoad:false,
									        waitMsg:'Loading countries...'
									     }); 
	}

	/**
	 * method to create language store
	 */	
	this.createLangStore = function(){
			this.langStore = new Ext.data.JsonStore({
    									    url: project_url + 'GetLanguages',
									        fields: ['name','symbol','id','active'],
									        method:'GET',
									        root:'languages',
									        autoLoad:true,
									        waitMsg:'Loading languages...'
									     }); 
	}

	/**
	 * method to create desktop images store
	 */	
	this.createDesktopsStore = function(){
			this.desktopsStore = new Ext.data.JsonStore({
    									    url: project_url + 'GetDesktops',
									        fields: ['name','id', 'short_desc'],
									        method:'GET',
									        root:'desktops',
									        autoLoad:true,
									        waitMsg:'Loading desktops...'
									     });
	}
	
	/**
	 * method to create graphic levels store 
	 */
	this.createGraphicLevelsStore = function(){
		this.graphicLevelsStore = new Ext.data.JsonStore({
    									    url: project_url + 'GetGraphicLevels',
									        fields: ['id','level','symbol'],
									        method:'GET',
									        root:'levels',
									        autoLoad:true,
									        waitMsg:'Loading graphic levels...'
								}); 
		
		this.graphicLevelsStore.on('load', function(store_, records_){
			var store = context.user.preferences.graphicLevelsStore;
			for(var i=0; i<store.getCount(); ++i){
				var symbol = store.getAt(i).get('symbol');
				store.getAt(i).set('symbol', context.translator.translate(symbol));
			}
		}, context.user.preferences.graphicLevelsStore);						
	} 
	
	
	/**
	 * method to initialize user class object
	 */
	this.init = function(){
	          this.preferencesLoader = new Ext.data.Store({
						                    proxy: new Ext.data.HttpProxy({ 
						                                      url: project_url + 'GetPreferences',
						                                      method: 'GET',
						                                      waitMsg:'Loading ...' 
						                                     }),
						                   reader : new Ext.data.JsonReader({
									                            id: 'preferences_reader',
									                            idProperty:'id',
									                            root: 'preferences',
									                            fields: [
									                            		 {name: 'view'					, type: 'int'},
									                            		 {name: 'desktop_image'			, type: 'string'},
									                            		 {name: 'language'				, type: 'int'},
									                            		 {name: 'country'				, type: 'int'},
									                            		 {name: 'show_clock'			, type: 'int'},
									                            		 {name: 'show_news'				, type: 'int'},
									                            		 {name: 'open_modules'			, type: 'string'},
									                            		 {name: 'graphic_level'			, type: 'int'},
                                                                         {name: 'show_reminder'			, type: 'int'},
                                                                         {name: 'show_tips'             , type: 'int'},
                                                                         {name: 'curr_tip'              , type: 'int'}
									                            		]
							                         		 })     		 
						                });			                				                
	}	
}