/* 
 * David Miglior for MyVCF.com
 * V1.0 started the 28/01/2011
 * Modifications : 
 * Description : the extranet welcome page once connected
 */
 
// called for creation or opening the welcome page
function welcomePage(vc_id_toadd) {
	if (!MyVCFApp.Welcome) {
		MyVCFApp.Welcome = new MyVCFApp.WelcomeObj({}) ;
		MyVCFApp.setView(MyVCFApp.Welcome, $("#div_main_welcome")) ;
	} else {
	}
	MyVCFApp.initPage('div_main_welcome', 1) ;
	MyVCFApp.Welcome.loadCurrentUser() ;
	MyVCFApp.Welcome.vc_id_toadd = '' ;
	if (vc_id_toadd && vc_id_toadd!='') MyVCFApp.Welcome.vc_id_toadd = vc_id_toadd ;
}

// Welcome Object, to insctanciate, extends MyVCFApp.View
MyVCFApp.WelcomeObj = $.inherit(MyVCFApp.View, {
	scope: this,
	// initialization of the view
	_initView: function() {
		this.idUser = MyVCFApp.Utils.nextId() ;
	},
	searchContact: function(txt) {
		if (this.delaySearch) window.clearTimeout(this.delaySearch) ;
		if (txt=="") {
			$("#div_welcome_searchresult").html("") ;
		} else {
			this.delaySearch = window.setTimeout("MyVCFApp.Welcome.searchContactReal(\""+txt+"\")", 200) ;
		}
	},
	searchContactReal: function(txt) {
		if (this.ajSearch) this.ajSearch.abort() ;
		if (this.delaySearch) {
			window.clearTimeout(this.delaySearch) ;
			this.delaySearch = null ;
		}
		if (txt=="") {
			$("#div_welcome_searchresult").html("") ;
		} else {
			this.ajSearch = $.ajax({
				type: 'POST',
				url: 'io.php',
				data: {action: 'searchContacts', search:txt},
				context: this, // scope of the ajax request
				dataType: 'json',
				success: function(data) {
					var tabFriends = data.tabFriends ;
					var tabLastVcName = [] ;
					var lastUser = "" ;
					var txt = "" ;
					txt += "<table width='100%' class='table_contacts_list' border='0' cellpadding='0' cellspacing='0'>" ;
					for (var i=0 ; i<tabFriends.length ; i++) {
						var img = "/statics/images/im_default-femme-petit.jpg" ;
						if (tabFriends[i].vc_photo!='') img = "/dynamics/ip2_"+tabFriends[i].vc_photo+".jpg" ;
						var bgTr = "" ;
						if (i%2==0) bgTr = "background-color:#e6e6e6;" ;
						txt += "<tr style='"+bgTr+"'>" ;
						txt += "<td><img width='20' height='25' src='"+img+"' alt='' /></td>" ;
						var name = tabFriends[i].vc_firstname+" "+tabFriends[i].vc_lastname ;
						if (tabFriends[i].vc_organisation!='') name += " - <b>"+tabFriends[i].vc_organisation+"</b>" ;
						txt += "<td><a href='javascript:void(0);' onclick=\"linkPage('"+tabFriends[i].vc_id+"', 0);\">"+name+"</a></td>" ;
						txt += "</tr>" ;
					}
					txt += "</table>" ;
					$("#div_welcome_searchresult").html(txt) ;
				}
			});
		}
	},
	// create the base HTML welcome page
	_generate: function() {
		var txt = "" ;
		// html for big image to top, and 4 links
		txt += "<div style='width:779px; height:201px; background-image:url(/statics/images/extranet_top.jpg);'>" ;
			txt += "<div style='color:white; width:500px; height:50px; padding:10px 0 0 15px;'><h1 style='padding:0; margin:0; color:white;'>"+i18n.welcome+"</h1><div style='color:white;'>"+i18n.welcome+" <span id='"+this.idUser+"'></span> "+i18n.welcome_explain+"</div></div>" ;
			
			txt += "<div onclick='manageCardsPage();' style='cursor:pointer; color:white; float:left; width:150px; padding:50px 30px 0 15px;'><h2 style='color:white; padding:0; margin:0;'>"+i18n.create+"</h2>"+i18n.create_explain+"</div>" ;
			txt += "<div onclick='contactsPage();' style='cursor:pointer; color:white; float:left; width:150px; padding:50px 30px 0 0;'><h2 style='color:white; padding:0; margin:0;'>"+i18n.manage+"</h2>"+i18n.manage2+"</div>" ;
			txt += "<div onclick='importPage();' style='cursor:pointer; color:white; float:left; width:150px; padding:50px 30px 0 0;'><h2 style='color:white; padding:0; margin:0;'>"+i18n.importt+"</h2>"+i18n.importt2+"</div>" ;
			//txt += "<div onclick='pluginsPage();' style='cursor:pointer; color:white; float:left; width:100px; padding:50px 0 0 0;'><h2 style='color:white; padding:0; margin:0;'>Binetôt</h2>Télécharger vos contacts grâce à nos connecteurs Mac ou PC</div>" ;
		txt += "</div>" ;
		//txt += "<a href=\"javascript:void(0);\" onclick=\"manageCardsPage();\">Gérer mes cartes</a>" ;
		//txt += "<div class='clear'></div>" ;

		// html for the received requests
		txt += "<div id='div_welcome_requests' class='frameGray' style='margin:10px 0;'>" ;
		txt += "<h2 style='padding:0; margin:0;'>"+i18n.requests_asked+"</h2>" ;
		txt += "<div>"+i18n.requests_asked_explain+"</div>" ;
		txt += "<div id='div_part_received'></div>" ;
		txt += "</div>" ;

		// html for the lasts 10 contacts
		txt += "<div style='float:left; width:500px; margin:20px 20px 0 0;'>" ;
		txt += "<h2 style='margin:0; padding:0;'>"+i18n.your_last_contacts+"</h2>" ;
		txt += "<div style='margin:2px 0 7px 0;'>"+i18n.your_last_contacts_explain+"</div>" ;
		txt += "<div id='div_part_lastcontacts'></div>" ;
		txt += "</div>" ;

		// html for friends
		txt += "<div style='float:left; width:240px;'>" ;
		txt += "<h2 style='margin:20px 0 0 0; padding:0;'>"+i18n.search2+"</h2>" ;
		txt += "<div><input type='text' value='' name='tf_welcome_search' id='tf_welcome_search' onkeyup='MyVCFApp.Welcome.searchContact(this.value);'></div>" ;
		txt += "<div id='div_welcome_searchresult'></div>" ;
		txt += "<hr/><h2 style='margin:0; padding:0;'>"+i18n.do_you_know+"</h2>" ;
		txt += "<div>"+i18n.do_you_know_explain+"</div>" ;
		txt += "<div id='div_part_friends'></div>" ;
		txt += "</div>" ;
		txt += "<div class='clear'></div>" ;

		// html for requests posted
		txt += "<div id='div_welcome_requested' class='' style='margin:10px 0 0 0;'>" ;
		txt += "<h2 style='margin:0; padding:0;'>"+i18n.requests_proposed+"</h2>" ;
		txt += "<div>"+i18n.requests_proposed_explain+"</div>" ;
		txt += "<div id='div_part_sended'></div>" ;
		txt += "</div>" ;

		//txt += "<div style='height:300px;'></div>" ;
		return txt ;
	},
	_onRender: function() {
	},
	// load the user, requests received, requests sended and friends
	loadCurrentUser: function() {
		$.ajax({
			type: 'POST',
			url: 'io.php',
			data: {action: 'getUser'},
			context: this, // scope of the ajax request
			dataType: 'json',
			success: function(data) {
				MyVCFApp.currentUser = data.currentUser ;
				$("#"+this.idUser).html(MyVCFApp.currentUser.us_firstname+" "+MyVCFApp.currentUser.us_name) ;
				MyVCFApp.currentUser.tabMyCards = data.tabMyCards ;
				
				// draw tab of firends
				var tabFriends = data.tabFriends ;
				MyVCFApp.drawKnowPerhaps(tabFriends, $("#div_part_friends")) ;

				// draw the 10 last contacts added
				var tabLastContacts = data.tabLastContacts ;
				MyVCFApp.drawLastContacts(tabLastContacts, $("#div_part_lastcontacts")) ;
				
				// draw requests posted
				var tabSended = data.tabSended ;
				MyVCFApp.drawRequestsSended(tabSended, $("#div_part_sended")) ;
				if (tabSended.length==0) $("#div_welcome_requested").hide() ;
				else $("#div_welcome_requested").show() ;
				
				// draw requests sended
				var tabReceived = data.tabReceived ;
				MyVCFApp.drawRequestsRequested(tabReceived, $("#div_part_received")) ;
				if (tabReceived.length==0) $("#div_welcome_requests").hide() ;
				else $("#div_welcome_requests").show() ;
				
				
				if (this.vc_id_toadd!='') {
					linkPage(this.vc_id_toadd, 0) ;
					this.vc_id_toadd = '' ;
				}
				/*
				var txt = "" ;
				var tabSended = data.tabSended ;
				for (var i=0 ; i<tabSended.length ; i++) {
					txt += "<div><a href='javascript:void(0);' onclick=\"linkPage('"+tabSended[i].re_id+"', 1)\">"+tabSended[i].vc_firstname+" "+tabSended[i].vc_lastname+" (demande le "+MyVCFApp.formatDate(tabSended[i].re_date_requested)+")</a></div>" ;
				}
				$("#div_part_sended").html(txt) ;
				*/

			}
		});
	}
}) ;
