
function debug(message)
{
    if (message == null)
    {
        System.Debug.outputString("null");
    }
    else
    {
        System.Debug.outputString(message);
    }
}


MessengerGadgetSettingsObj = function()
{
	// SETTINGS VARS
	this.gadgetColor = null;
	this.gadgetHeight = null;
	this.gadgetGetUpdateNotifications = null;
	this.sortContactsBy = null;
	this.groupOffline = null;
	this.groupMobile = null;
	this.filterContactsBy = null;
	this.viewContactsBy = null;
	this.showEmailCounter = null;
	
	this.registry = new RegistryObj();
	
	this.settingsLoaded = null;
}

MessengerGadgetSettingsObj.GADGET = 0;
MessengerGadgetSettingsObj.UPDATE = 1;

MessengerGadgetSettingsObj.prototype = 
{
	// Get settings stored in registry
	loadGadgetSettings:function()
	{
		// Gadget color
	    this.gadgetColor = this.registry.readStringValue("GadgetColor");
	    if (null == this.gadgetColor)
	    {
	        this.gadgetColor = SettingsConstants.DEFAULT_GADGET_COLOR;
	    }
		
	    // Gadget height
	    this.gadgetHeight = this.registry.readDWORDValue("GadgetHeight");
	    if (null == this.gadgetHeight)
	    {
	        this.gadgetHeight = SettingsConstants.DEFAULT_GADGET_SIZE;
	    }
		
		// Gadget Get Update Notifications
	    this.gadgetGetUpdateNotifications = this.registry.readDWORDValue("UserWantsUpdates");
	    if (null == this.gadgetGetUpdateNotifications)
	    {
	        this.gadgetGetUpdateNotifications = SettingsConstants.DEFAUL_UPDATE_OPTION;
	    }
		
	    // Sort contacts by
	    this.sortContactsBy = this.registry.readDWORDValue("SortContactsBy");
	    if (null == this.sortContactsBy)
	    {
	        this.sortContactsBy = SettingsConstants.DEFAULT_SORT_BY;
	    }
	    
	    // Group all offline contacts together
	    this.groupOffline = this.registry.readDWORDValue("GroupOffline");
	    if (null == this.groupOffline)
	    {
	        this.groupOffline = SettingsConstants.DEFAULT_GROUP_OFFLINE;
	    }
		
		// Group all mobile contacts together
	    this.groupMobile = this.registry.readDWORDValue("GroupMobile");
	    if (null == this.groupMobile)
	    {
	        this.groupMobile = SettingsConstants.DEFAULT_GROUP_MOBILE;
	    }
	    
	    // Filter contacts by
	    this.filterContactsBy = this.registry.readDWORDValue("FilterContactsBy");
	    if (null == this.filterContactsBy)
	    {
	        this.filterContactsBy = SettingsConstants.DEFAULT_FILTER_BY;
	    }
	    
	    // View contacts by
	    this.viewContactsBy = this.registry.readDWORDValue("ViewContactsBy");
	    if (null == this.viewContactsBy)
	    {
	        this.viewContactsBy = SettingsConstants.DEFAULT_VIEW_CONTACTS_BY;
	    }
		
		this.showEmailCounter = this.registry.readDWORDValue("ShowEmailCounter");
		if (null == this.showEmailCounter)
	    {
	        this.showEmailCounter = SettingsConstants.DEFAULT_SHOW_EMAIL_COUNTER;
	    }
		
		this.settingsLoaded = MessengerGadgetSettingsObj.GADGET;
	},
	
	loadUpdateSettings:function()
	{
		// Gadget Get Update Notifications
	    this.gadgetGetUpdateNotifications = this.registry.readDWORDValue("UserWantsUpdates");
	    if (null == this.gadgetGetUpdateNotifications)
	    {
	        this.gadgetGetUpdateNotifications = SettingsConstants.DEFAUL_UPDATE_OPTION;
	    }
		
		this.settingsLoaded = MessengerGadgetSettingsObj.UPDATE;
	},
	
	saveSettings:function()
	{
		this.registry.createKey();
	
		if (this.settingsLoaded == null)
		{
			//error: Settings has not been loaded
		}
		else 
		{
			if (this.settingsLoaded == MessengerGadgetSettingsObj.GADGET)
			{
				// Gadget color
			    this.registry.writeStringValue("GadgetColor", this.gadgetColor);
				
			    // Gadget height
			    this.registry.writeDWORDValue("GadgetHeight", this.gadgetHeight);

			    // Sort contacts by
			    this.registry.writeDWORDValue("SortContactsBy", this.sortContactsBy);
			    
			    // Group all offline contacts together
			    this.registry.writeDWORDValue("GroupOffline", this.groupOffline);
				
				// Group all mobile contacts together
			    this.registry.writeDWORDValue("GroupMobile",  this.groupMobile);
			    
			    // Filter contacts by
			    this.registry.writeDWORDValue("FilterContactsBy", this.filterContactsBy);

			    // View contacts by
			    this.registry.writeDWORDValue("ViewContactsBy", this.viewContactsBy);
				
				this.registry.writeDWORDValue("ShowEmailCounter", this.showEmailCounter);
			}
			
			// Gadget Get Update Notifications
			this.registry.writeDWORDValue("UserWantsUpdates", this.gadgetGetUpdateNotifications);
		}
	}
}




	
/************************************************************
          VARS
 ************************************************************/
 


MessengerGadgetSettingsUIObj = function()
{
	var self = this;

	this.gadgetSettings = new MessengerGadgetSettingsObj();
	this.gadgetSettings.loadGadgetSettings();

	var tabContainer = new TabsContainerObj(document.getElementById("tabContainer"));
	
	
	// GADGET SETTINGS
	var divGadgetSettings = document.createElement("div");
	
	// Add gadget bg color settings
	this.gadgetBGColorSettingContent = new SelectorWithImgObj();
	this.gadgetBGColorSettingContent.addOption(new OptionObj(SettingsLocalizedConstants.LOC_COLOR_BLUE, "Blue", "icons/color-preview/ColorPreview-Blue.png"));
	this.gadgetBGColorSettingContent.addOption(new OptionObj(SettingsLocalizedConstants.LOC_COLOR_BROWN, "Brown", "icons/color-preview/ColorPreview-Brown.png"));
	this.gadgetBGColorSettingContent.addOption(new OptionObj(SettingsLocalizedConstants.LOC_COLOR_GRAY, "Gray", "icons/color-preview/ColorPreview-Gray.png"));
	this.gadgetBGColorSettingContent.addOption(new OptionObj(SettingsLocalizedConstants.LOC_COLOR_GREEN, "Green", "icons/color-preview/ColorPreview-Green.png"));
	this.gadgetBGColorSettingContent.addOption(new OptionObj(SettingsLocalizedConstants.LOC_COLOR_ORANGE, "Orange", "icons/color-preview/ColorPreview-Orange.png"));
	this.gadgetBGColorSettingContent.addOption(new OptionObj(SettingsLocalizedConstants.LOC_COLOR_PINK, "Pink", "icons/color-preview/ColorPreview-Pink.png"));
	this.gadgetBGColorSettingContent.addOption(new OptionObj(SettingsLocalizedConstants.LOC_COLOR_RED, "Red", "icons/color-preview/ColorPreview-Red.png"));
	// create setting group
	var gadgetBGColorSetting = new SettingGroupObj(divGadgetSettings, SettingsLocalizedConstants.LOC_SETTINGS_GADGET_BG_COLOR);
	gadgetBGColorSetting.addContent(this.gadgetBGColorSettingContent.getDiv());

	// Add gadget height settings
	this.gadgetHeightSettingContent = new NumberBoxObj(new OptionObj(SettingsLocalizedConstants.LOC_SETTINGS_GADGET_HEIGHT_MIN));
	this.gadgetHeightSettingContent.setMin(GadgetConstants.MIN_GADGET_HEIGHT);
	this.gadgetHeightSettingContent.setMax(GadgetConstants.MAX_GADGET_HEIGHT);
	this.gadgetHeightSettingContent.setWarning(SettingsLocalizedConstants.LOC_SETTINGS_GADGET_HEIGHT_WARNING);
	// create setting group
	var gadgetHeightSetting = new SettingGroupObj(divGadgetSettings, SettingsLocalizedConstants.LOC_SETTINGS_GADGET_HEIGHT);
	gadgetHeightSetting.addContent(this.gadgetHeightSettingContent.getDiv());
	
	// Add unread email counter settings
	this.emailCounterSettingContent = new RadioSelectorObj("COUNTER_SHOW");
	this.emailCounterSettingContent.addOption(new OptionObj(SettingsLocalizedConstants.LOC_EMAIL_COUNTER_ALWAYS_SHOW, GadgetConstants.SHOW_EMAIL_COUNTER_ALWAYS));
	this.emailCounterSettingContent.addOption(new OptionObj(SettingsLocalizedConstants.LOC_EMAIL_COUNTER_SHOW_ONLY_WHEN_UNREAD, GadgetConstants.SHOW_EMAIL_COUNTER_ONLY_WHEN_UNREAD));
	this.emailCounterSettingContent.addOption(new OptionObj(SettingsLocalizedConstants.LOC_EMAIL_COUNTER_NEVER_SHOW, GadgetConstants.SHOW_EMAIL_COUNTER_NEVER));
	// create setting group
	var emailCounterSetting = new SettingGroupObj(divGadgetSettings, SettingsLocalizedConstants.LOC_SHOW_EMAIL_COUNTER);
	emailCounterSetting.addContent(this.emailCounterSettingContent.getDiv());
	
	// Add update option
	this.gadgetUpdateNotificationSettingContent = new CheckboxObj(new OptionObj(SettingsLocalizedConstants.LOC_SETTINGS_GADGET_UPDATE_NOTIFICATION_CHCKBOX, 0));
	// create setting group
	var gadgetUpdateNotificationSetting = new SettingGroupObj(divGadgetSettings, SettingsLocalizedConstants.LOC_SETTINGS_GADGET_UPDATE_NOTIFICATION);
	gadgetUpdateNotificationSetting.addContent(this.gadgetUpdateNotificationSettingContent.getDiv());
	
	
	// CONTACTS SETTINGS
	var divContactsSettings = document.createElement("div");	
	
	// Add sort contacts by setting
	this.contactsSortBySettingContent = new RadioSelectorObj("SORT_BY");
	this.contactsSortBySettingContent.onSelectedValueChanged = function(sender) {self.onRadioSelectorObj_SelectedValueChanged(sender)};
	this.contactsSortBySettingContent.addOption(new OptionObj(SettingsLocalizedConstants.LOC_SORT_STATUS, MessengerConstants.SORT_BY_STATUS));
	this.contactsSortBySettingContent.addOption(new OptionObj(SettingsLocalizedConstants.LOC_SORT_GROUPS, MessengerConstants.SORT_BY_GROUP));
	this.contactsGroupMobileContent = new CheckboxObj(new OptionObj(SettingsLocalizedConstants.LOC_GROUP_MOBILE, 0));
	this.contactsGroupOfflineContent = new CheckboxObj(new OptionObj(SettingsLocalizedConstants.LOC_GROUP_OFFLINE, 0));
	// create setting group
	var contactsSortBySetting = new SettingGroupObj(divContactsSettings, SettingsLocalizedConstants.LOC_SORT_CONTACTS_BY);
	contactsSortBySetting.addContent(this.contactsSortBySettingContent.getDiv());
	contactsSortBySetting.addContent(this.contactsGroupMobileContent.getDiv());
	contactsSortBySetting.addContent(this.contactsGroupOfflineContent.getDiv());
	
	// Add filter contacts setting
	this.contactsFilterSettingContent = new RadioSelectorObj("FILTER_BY");
	this.contactsFilterSettingContent.addOption(new OptionObj(SettingsLocalizedConstants.LOC_FILTER_SHOW_ALL, MessengerConstants.FILTER_SHOW_ALL));
	this.contactsFilterSettingContent.addOption(new OptionObj(SettingsLocalizedConstants.LOC_FILTER_ONLINE_ONLY, MessengerConstants.FILTER_SHOW_ONLINE_ONLY));
	// create setting group
	var contactsFilterSetting = new SettingGroupObj(divContactsSettings, SettingsLocalizedConstants.LOC_FILTER_CONTACTS);
	contactsFilterSetting.addContent(this.contactsFilterSettingContent.getDiv());
	
	// Add sort contacts by setting
	this.contactsViewBySettingContent = new RadioSelectorObj("VIEW_BY");
	this.contactsViewBySettingContent.addOption(new OptionObj(SettingsLocalizedConstants.LOC_VIEW_DISPLAY_NAME, MessengerConstants.VIEW_BY_DISPLAY_NAME));
	this.contactsViewBySettingContent.addOption(new OptionObj(SettingsLocalizedConstants.LOC_VIEW_EMAIL, MessengerConstants.VIEW_BY_EMAIL));
	// create setting group
	var contactsViewBySetting = new SettingGroupObj(divContactsSettings, SettingsLocalizedConstants.LOC_VIEW_CONTACT_BY);
	contactsViewBySetting.addContent(this.contactsViewBySettingContent.getDiv());
	
	// ABOUT SETTINGS
	var divAboutSettings = document.createElement("div");
	
	// Add info
	var aboutContent = document.createElement("div");
	aboutContent.style.padding = "10px";
	// Add content
	var messengerImg = document.createElement("img");
	messengerImg.src = "../icons/icon.png";
	messengerImg.style.styleFloat = "right";
	aboutContent.appendChild(messengerImg);
	var gadgetName = document.createElement("div");
	gadgetName.innerHTML = SettingsLocalizedConstants.LOC_ABOUT_GADGET_NAME;
	gadgetName.style.fontSize = 12;
	gadgetName.style.fontWeight = "bold";
	gadgetName.style.paddingBottom = "10px";
	
	aboutContent.appendChild(gadgetName);
	var version = document.createElement("div");
	version.innerHTML = SettingsLocalizedConstants.LOC_ABOUT_VERSION + " " + System.Gadget.version;
	version.style.paddingBottom = "10px";
	version.style.paddingLeft = "10px";
	aboutContent.appendChild(version);
	var copyright = document.createElement("span");
	copyright.innerHTML = SettingsLocalizedConstants.LOC_ABOUT_COPYRIGHT;
	copyright.style.paddingLeft = "10px";
	aboutContent.appendChild(copyright);
	aboutContent.appendChild(document.createElement("br"));
	var webPageDiv = document.createElement("div");
	webPageDiv.style.paddingLeft = "10px";
	var webPage = document.createElement("a");
	webPage.href = SettingsLocalizedConstants.LOC_ABOUT_WEB_ADDR;
	webPage.innerHTML = SettingsLocalizedConstants.LOC_ABOUT_WEB_ADDR;
	webPageDiv.appendChild(webPage);
	aboutContent.appendChild(webPageDiv);
	
	
	
	// create setting group
	var aboutGroup = new SettingGroupObj(divAboutSettings, SettingsLocalizedConstants.LOC_ABOUT_TAB);
	aboutGroup.addContent(aboutContent);
	
	
	
	
	// Add tabs
	tabContainer.addTab(SettingsLocalizedConstants.LOC_GADGET_SETTINGS_TAB, divGadgetSettings);
	tabContainer.addTab(SettingsLocalizedConstants.LOC_CONTACTS_SETTINGS_TAB, divContactsSettings);
	tabContainer.addTab(SettingsLocalizedConstants.LOC_ABOUT_TAB, divAboutSettings);
	
	this.gadgetBGColorSettingContent.setCurrentValue(this.gadgetSettings.gadgetColor);
	this.gadgetHeightSettingContent.setCurrentValue(this.gadgetSettings.gadgetHeight);
	this.gadgetUpdateNotificationSettingContent.setCurrentValue(this.gadgetSettings.gadgetGetUpdateNotifications);
	this.contactsSortBySettingContent.setCurrentValue(this.gadgetSettings.sortContactsBy);
	this.contactsGroupMobileContent.setCurrentValue(this.gadgetSettings.groupMobile);
	this.contactsGroupOfflineContent.setCurrentValue(this.gadgetSettings.groupOffline);
	this.contactsFilterSettingContent.setCurrentValue(this.gadgetSettings.filterContactsBy);
	this.contactsViewBySettingContent.setCurrentValue(this.gadgetSettings.viewContactsBy);
	this.emailCounterSettingContent.setCurrentValue(this.gadgetSettings.showEmailCounter);
}

MessengerGadgetSettingsUIObj.prototype =
{
	onSettingsClosing:function(event)
	{
		if (event.closeAction == event.Action.commit)
		{
			System.Gadget.Settings.writeString("needGadgetRefresh", "true");
			
			this.gadgetSettings.gadgetColor = this.gadgetBGColorSettingContent.getValue();
			this.gadgetSettings.gadgetHeight = this.gadgetHeightSettingContent.getValue();
			this.gadgetSettings.gadgetGetUpdateNotifications = this.gadgetUpdateNotificationSettingContent.getValue();
			this.gadgetSettings.sortContactsBy = this.contactsSortBySettingContent.getValue();
			this.gadgetSettings.groupOffline = this.contactsGroupOfflineContent.getValue();
			this.gadgetSettings.groupMobile = this.contactsGroupMobileContent.getValue();
			this.gadgetSettings.filterContactsBy = this.contactsFilterSettingContent.getValue();
			this.gadgetSettings.viewContactsBy = this.contactsViewBySettingContent.getValue();
			this.gadgetSettings.showEmailCounter = this.emailCounterSettingContent.getValue();
		
			this.gadgetSettings.saveSettings();
		}
		else
		{
			System.Gadget.Settings.writeString("needGadgetRefresh", "false");
		}
	},

	onRadioSelectorObj_SelectedValueChanged:function(sender)
	{
		if (this.contactsSortBySettingContent.selectedValue == MessengerConstants.SORT_BY_GROUP)
		{
			this.contactsGroupMobileContent.enable();
			this.contactsGroupOfflineContent.enable();
		}
		else
		{
			this.contactsGroupMobileContent.disable();
			this.contactsGroupOfflineContent.disable();
		}
	}
}


