/* 
 * David Miglior for MyVCF.com
 * V1.0 started the 28/01/2011
 * Modifications : 
 * Description : the import page
 */

// called for the creation or opening of the import page
function importPage() {
	if (!MyVCFApp.Import) {
		MyVCFApp.Import = new MyVCFApp.ImportObj({}) ;
		MyVCFApp.setView(MyVCFApp.Import, $("#div_main_import")) ;
	} else {
	}
	MyVCFApp.initPage('div_main_import', 100) ;
}

// Import Object, to insctanciate, extends MyVCFApp.View
MyVCFApp.ImportObj = $.inherit(MyVCFApp.View, {
	scope: this,
	_initView: function() {
		this.idUser = MyVCFApp.Utils.nextId() ;
	},
	// invert selection of all contacts checkboxs
	selectAll: function() {
		$(".cb_import_send").each(function() {
			this.checked = !this.checked ;
		}) ;
	},
	// when user choose to cancel the import
	otherImport: function() {
		$("#div_import_form").slideDown() ;
		$("#div_import_tabcontacts").slideUp() ;
		$("#div_import_ok").slideUp() ;
	},
	// send selected contacts
	sendInvitations: function() {
		var tabContactsToSend = [] ;
		// check if checboxs selection is done
		$("#form_import_send .cb_import_send:checked").each(function() {
			tabContactsToSend.push($(this).val()) ;
		}) ;
		if (tabContactsToSend.length == 0) {
			MyVCFApp.Alert.show(i18n.you_must_select_one_contact) ;
			return ;
		}
		// ajax request to send selected contacts
		$.ajax({
			type: 'POST',
			url: 'io.php',
			data: {action:'sendInvitations', tabContactsToSend:tabContactsToSend, mycard:$('#sel_import_yourcard').val()},
			context: this,
			dataType: 'json',
			success: function(data) {
				$("#div_import_form").slideUp() ;
				$("#div_import_tabcontacts").slideUp() ;
				$("#div_import_ok").slideDown() ;
			}
		});
	},
	// import the contact of the service selected
	importPlease: function() {
		$.ajax({
			type: 'POST',
			url: 'io.php',
			data: {action: 'import', login:$("#tf_import_login").val(), pass:$("#tf_import_pass").val(), service:$("#tf_import_service").val()},
			context: this,
			dataType: 'json',
			success: function(data) {
				// if error display the PHP server error
				if (data.errors!='') {
					MyVCFApp.Alert.show(data.errors) ;
					return ;
				}
				// draw the html table of contacts founded on the service
				var txt = "<table class='table_contacts_list' border='0' cellpadding='0' cellspacing='0'>" ;
				var tabContacts = data.tabContacts ;
				var num = 0 ;
				for (var i=0 ; i<tabContacts.length ; i++) {
					txt += "<tr>" ;
					txt += "<td><input class='cb_import_send' checked='checked' type='checkbox' value='"+tabContacts[i][0]+"' name='contact_"+(num++)+"'></td>" ;
					var n = '' ;
					if (tabContacts[i][1]!=tabContacts[i][2]) n = tabContacts[i][2] ;
					txt += "<td>"+n+"</td>" ;
					txt += "<td>"+tabContacts[i][1]+"</div>" ;
					txt += "</tr>" ;
				}
				txt += "</table>" ;
				$("#div_import_tabcontacts2").html(txt) ;
				// show/hide the good div
				$("#div_import_form").slideUp() ;
				$("#div_import_tabcontacts").slideDown() ;
				$("#div_import_ok").slideUp() ;
			}
		});
	},
	// create the base HTML import page
	_generate: function() {
		var txt = "" ;
		// html for first card ; infos and choose service, login and pass
		txt += "<div style='float:right; margin:-10px -18px 0 0;'><img src='/statics/images/import-girl.png' alt='' /></div>" ;
		txt += "<h1>"+i18n.importt+"</h1>" ;
		txt += "<h2>"+i18n.your_contacts_other_services+"</h2>" ;
		txt += "<div id='div_import_form'>" ;
		txt += "<form action='#' id='form_import'>" ;
		txt += "<div style=''>"+i18n.import_contact_explain+"</div>" ;
		txt += "<div class='frameGray clear' style='width:500px;'>" ;
		txt += "<div style=''><label for='sel_import_yourcard'>Votre carte partagée</label><br/><div id='div_import_yourcard'></div></div>" ;
		txt += "<div><label for='tf_import_service'>Service</label><br/><select name='service' id='tf_import_service'><option value='gmail'>Gmail</option><option value='facebook'>Facebook</option><option value='hotmail'>Live/Hotmail</option><option value='twitter'>Twitter</option><option value='linkedin'>LinkedIn</option><option value='msn'>MSN</option><option value='plaxo'>Plaxo</option><option value='yahoo'>Yahoo!</option><option value='youtube'>YouTube</option><option value='badoo'>Badoo</option><option value='flickr'>Flickr</option><option value='myspace'>MySpace</option><option value='orkut'>Orkut</option><option value='skyrock'>Skyrock</option><option value='vimeo'>Vimeo</option></select></div>" ;
		txt += "<div style=''><label for='tf_import_login'>Identifiant</label><br/><input name='login' id='tf_import_login' type='text' value='' /></div>" ;
		txt += "<div style=''><label for='tf_import_pass'>Mot de passe</label><br/><input name='pass' id='tf_import_pass' type='password' value='' /></div>" ;
		txt += "<div id='div_import_btsubmit'></div>" ;
		txt += "</div>" ;
		txt += "</form>" ;
		txt += "</div>" ;
		
		// html for the result card (display contacts)
		txt += "<div id='div_import_tabcontacts' style='display:none;'><div>"+i18n.you_must_select_contacts+"</div><form action='#' id='form_import_send'><div><a href='javascript:void(0);' onclick='MyVCFApp.Import.selectAll();'>"+i18n.invert_selection+"</a></div><div id='div_import_tabcontacts2' style=''></div><div id='div_import_btsend'></div><div><a href='javascript:void(0);' onclick='MyVCFApp.Import.otherImport();'>"+i18n.do_another_import+"</a></div></form></div>" ;
		
		// html for the confirmation card
		txt += "<div id='div_import_ok' style='display:none;'>"+i18n.we_have_informed_contacts+"<br/><a href='javascript:void(0);' onclick='MyVCFApp.Import.otherImport();'>"+i18n.do_another_import+"</a></div>" ;
		txt += "<div style='height:50px;'></div>" ;
		return txt ;
	},
	// called after _generate, to render buttons
	_onRender: function() {
		var bt = new MyVCFApp.Button({
			text: "Importer",
			onClick: MyVCFApp.Import.importPlease
		}) ;
		bt.render($("#div_import_btsubmit")) ;
		var bt2 = new MyVCFApp.Button({
			text: "Envoyer les invitations",
			onClick: MyVCFApp.Import.sendInvitations
		}) ;
		bt2.render($("#div_import_btsend")) ;
		var txt = "" ;
		//log('MyVCFApp.currentUser',MyVCFApp.currentUser) ;
		var tabMyCards = MyVCFApp.currentUser.tabMyCards ;
		txt += "<select name='sel_import_yourcard' id='sel_import_yourcard'>" ;
		for (var i=0 ; i<tabMyCards.length ; i++) {
			txt += "<option value='"+tabMyCards[i].vc_id+"'>"+tabMyCards[i].vc_cardname+"</option>" ;
		}
		txt += "</select>" ;
		$('#div_import_yourcard').html(txt) ;
	}
}) ;
