﻿var ContactImporterEvents = {};
ContactImporterEvents.ContactEmail = 'ContactImporterEvents.ContactEmail';

var ContactImporter = Block.extend
({
	data: null,
	currentTab: null,
	currentOrigin: null,
	emails: { 'pix': null, 'msn': null, 'google': null, 'yahoo': null },
	parentDialog: null,

	init: function(blockElement, data, parentDialog)
	{
		this._super(blockElement);

		this.parentDialog = parentDialog;
		var importer = this;
		
		$('#btnShareLogin', this.blockElement).click(function()
		{
			importer.login();
			return false;
		});

		var tabs = $('li > a', this.blockElement);
		$(tabs).click(function()
		{
			var tab = $(this).parent()[0];
			var tabType = tab.id.replace('tab_', '');
			importer.showTab(tabType);
		});

		$('#lstEmails').dblclick(function()
		{
			importer.triggerEmail($('#lstEmails').val());
		});

		this.data = data;

		this.displayTab('pix');
		this.login();
	},

	triggerEmail: function(email)
	{
		$(this).trigger(ContactImporter.Events.ContactEmail, email);
	},

	displayTab: function(origin)
	{
		if (this.currentTab != null)
		{
			this.currentTab.removeClass('selected');
		}

		this.currentTab = $('#tab_' + origin, this.blockElement);
		this.currentOrigin = origin;
		this.currentTab.addClass('selected');

		$('#pnlContacts', this.blockElement).css('display', this.emails[origin] != null ? 'block' : 'none');
		$('#pnlLogin', this.blockElement).css('display', this.emails[origin] == null ? 'block' : 'none');
		$('.tabheader', this.blockElement).hide();
		$('#pnlLogin_' + origin, this.blockElement).show();

		$('#lblError', this.blockElement).html('');
	},

	showTab: function(origin)
	{
		this.displayTab(origin);

		if (this.emails[origin] != null)
		{
			this.showEmails(this.emails[origin]);
		}
	},

	login: function()
	{
		var login = $('#login', this.blockElement).val();
		var pass = $('#password', this.blockElement).val();
		var importer = this;
		$.ajax
		({
			type: "POST",
			url: this.data.ShareLoginUrl,
			data:
			{
				Origin: this.currentOrigin,
				Login: login,
				Password: pass
			},
			beforeSend: function()
			{
				$('#lblError', this.blockElement).html('');
				$(importer.parentDialog).trigger(PixDialog.Events.ShowUpdater)
			},
			complete: function() { $(importer.parentDialog).trigger(PixDialog.Events.HideUpdater) },
			success: function(data, result) { importer.showResult(data, result) },
			error: function(data, result) { $('#lblError', this.blockElement).html('Wrong login/password') },
			dataType: 'json'
		});
	},

	showResult: function(data, result)
	{
		if (!data.Succeeded)
		{
			return;
		}

		this.emails[this.currentOrigin] = data.Emails;
		this.displayTab(this.currentOrigin);

		this.showEmails(data.Emails);
	},

	showEmails: function(emails)
	{
		var emailList = $('#lstEmails', this.blockElement);
		emailList.empty();

		var options = [];
		for (var index = 0; index < emails.length; index++)
		{
			options.push
			(
				$('<option></option>')
					.val(emails[index].Email)
					.text(emails[index].Name)
			);
		}
		
		$(options).appendTo($(emailList));
	}
});

ContactImporter.Events = ContactImporterEvents;
