TWEETSWITCH.Wizard = {
	activeObserver: [],//Array containing all active observers
	
	init: function(){
		$('menu_msg').insert(
			TWEETSWITCH.Wizard.getTabElement()
		);
		TWEETSWITCH.Wizard.observeElements();
		if (/*TWEETSWITCH.Config.FIRST_TIME_LOGIN || */!COMUFY.ConfigPanel.hasConfigured()){
			TWEETSWITCH.Wizard.start();	
		}
	},
	
	/**
	 * Starts the wizard
	 */
	start: function(){
		$("helpCheckbox").checked = true;
		
		//Make sure the dock is closed
		COMUFY.UI.Embed.ConfigPanel.closeAddAccountExtension();
		TWEETSWITCH.Wizard.dispStep1();	
	},
	
	/**
	 * Returns the tab element of the wizard
	 */
	getTabElement: function(){
		var tab = new Element('label', {className:"skyLabel"}).update(
				new Element('input',{type:"checkbox", className:"bannerCheckbox", id:"helpCheckbox"})
			).insert(
				TWEETSWITCH.Lang.Generic.Wizard.dispWizard
			);
		return tab;
	},
	
	observeElements: function(){
		$("helpCheckbox").observe('click', function(e){
			if (this.checked){
				TWEETSWITCH.Wizard.start();
			}
			else{
				TWEETSWITCH.Wizard.removeWizard();
			}
		});
	},
	
	/**
	 * Removes all wizard related observers
	 */
	removeWizObservers: function(){
		for (var i = 0; i < TWEETSWITCH.Wizard.activeObserver.length; i++){
			TWEETSWITCH.Wizard.activeObserver[i][0].stopObserving("click",TWEETSWITCH.Wizard.activeObserver[i][1]);
		}
	},
	
	/**
	 * Add a wizard observer
	 */
	addWizObserver: function(elem, binder){
		TWEETSWITCH.Wizard.activeObserver[TWEETSWITCH.Wizard.activeObserver.length] = [elem, binder];
		elem.observe("click",binder);
	},
	
	dispStep1: function(){
		if ($("tt1")) return;
		
		//Create the new element
		var tooltip = new Element('div', {className: 'tt1', id: 'tt1'}).update(
			new Element("span",{className:"tooltip"}).update(
				new Element("span", {className:'top'})
			).insert(
				new Element("span", {className:'middle'}).update(
					"<h2>"+TWEETSWITCH.Lang.Generic.Wizard.step1H2+"</h2><br />"+TWEETSWITCH.Lang.Generic.Wizard.step1
				)
			).insert(
				new Element("span", {className:'bottom'})
			)
		);
		$("COMUFY_confPanel").insert(tooltip);
		
		 
		//If the user clicks on + we now remove the tooltip and move to tooltip2.
		var wizobj = {
			obs: function(e){
				if ($("tt1")) $("tt1").remove();
				TWEETSWITCH.Wizard.removeWizObservers();
				TWEETSWITCH.Wizard.dispStep2();
			}
		};
		
		wizobj.binder = wizobj.obs.bindAsEventListener(wizobj);
		TWEETSWITCH.Wizard.addWizObserver($$(".COMUFY_config_account_add")[0], wizobj.binder);
		
	},
	
	
	
	dispStep2: function(){
		if ($("tt2")) return;
		var tooltip = new Element('div', {className: 'tt2', id: 'tt2'}).update(
			new Element("span",{className:"tooltip"}).update(
				new Element("span", {className:'top'})
			).insert(
				new Element("span", {className:'middle'}).update(
					"<h2>"+TWEETSWITCH.Lang.Generic.Wizard.step2H2+"</h2><br />"+TWEETSWITCH.Lang.Generic.Wizard.step2
				)
			).insert(
				new Element("span", {className:'bottom'})
			)
		);
		$("COMUFY_confPanel").insert(tooltip);
		
		$$(".COMUFY_config_dock_select ul").each(function(elem){
			var wizobj = {
				obs: function(e){
					if ($("tt2")) $("tt2").remove();
					TWEETSWITCH.Wizard.removeWizObservers();
					TWEETSWITCH.Wizard.dispStep3();
				}
			};
			
			wizobj.binder = wizobj.obs.bindAsEventListener(wizobj);
			TWEETSWITCH.Wizard.addWizObserver(elem, wizobj.binder);
		})		
	},
	
	dispStep3: function(){
		if ($("tt3")) return;
		
		var msg = TWEETSWITCH.Lang.Generic.Wizard.step3H2;
		$$(".COMUFY_config_item div").each(function(elem){
			if (elem.style.display != "none"){
				var accType = elem.className.replace("COMUFY_config_detail COMUFY_im_","");
				switch (accType){
					case "email":
						msg += "tweetswitch@provider.com";
						break;
					case "sms":
						msg += "+4401234567899";
						break;
					case "windowslive":
						msg += "tweetswitch@live.com";
						break;
					case "yahoo":
						msg += "tweetswitch";
						break;
					case "gtalk":
						msg += "tweetswitch@gmail.com";
						break;
					case "aim":
						msg += "tweetswitch";
						break;
					case "icq":
						msg += "4794589876";
						break;
					case "skype":
						msg += "tweetswitch";
						break;	
				}
			}
		});
		
		var tooltip = new Element('div', {className: 'tt3', id: 'tt3'}).update(
			new Element("span",{className:"tooltip"}).update(
				new Element("span", {className:'top'})
			).insert(
				new Element("span", {className:'middle'}).update(
					msg
				)
			).insert(
				new Element("span", {className:'bottom'})
			)
		);
		$("COMUFY_confPanel").insert(tooltip);
		
		//Now we must find which type of account the user wants to create and put an observer on the "create" button
		$$(".COMUFY_config_detail_create").each(function(elem){
			if (elem.parentNode.parentNode.parentNode.style.visibility != "hidden"){
				//If the user clicks on + we now remove the tooltip and move to tooltip2. 
				var wizobj = {
					obs: function(e){
						if (COMUFY.UI.Embed.Account.inputAddAccountValid()) {
							if ($("tt3")) 
								tooltip.remove();
							TWEETSWITCH.Wizard.removeWizObservers();
							TWEETSWITCH.Wizard.dispStep4();
						}
					}
				};
				
				wizobj.binder = wizobj.obs.bindAsEventListener(wizobj);
				TWEETSWITCH.Wizard.addWizObserver(elem, wizobj.binder);
			}
		});
		
		//Now we must find which type of account the user wants to create and put an observer on the "cancel" button
		$$(".COMUFY_config_detail_cancel").each(function(elem){
			if (elem.parentNode.parentNode.parentNode.style.visibility != "hidden"){
				//If the user clicks on + we now remove the tooltip and move to tooltip2. 
				var wizobj = {
					obs: function(e){
						if (tooltip) $("tt3").remove();
						TWEETSWITCH.Wizard.removeWizObservers();
						TWEETSWITCH.Wizard.start();
					}
				};
				
				wizobj.binder = wizobj.obs.bindAsEventListener(wizobj);
				TWEETSWITCH.Wizard.addWizObserver(elem, wizobj.binder);
			}
		});
	},
	
	dispStep4: function(){
		TWEETSWITCH.Wizard.autoDragLastItem(TWEETSWITCH.Wizard.dispStep5);		
		return;

/*		if ($("tt4")) return;
		var tooltip = new Element('div', {className: 'tt4', id: 'tt4'}).update(
			new Element("span",{className:"tooltip"}).update(
				new Element("span", {className:'top'})
			).insert(
				new Element("span", {className:'middle'}).update(
					"<h2>"+TWEETSWITCH.Lang.Generic.Wizard.step4H2+"</h2><br />"+TWEETSWITCH.Lang.Generic.Wizard.step4
				)
			).insert(
				new Element("span", {className:'bottom'})
			)
		);
		$("COMUFY_confPanel").insert(tooltip);

		//Add an observer to each box
		$$(".COMUFY_dropContentContainer").each(function(boxElem){
			Droppables.add(boxElem, {
               onDrop:function(e,x) {
                    if (e.hasClassName("COMUFY_iconSeparatorSpace") || e.hasClassName("COMUFY_section_main")) {
						if ($("tt4")) $("tt4").remove();
						COMUFY.UI.ConfigPanel.onDropPreferenceBox(e,x.parentNode);
						TWEETSWITCH.Wizard.dispStep5();
						Droppables.remove(boxElem);
					}
                 }
            });
		});*/
	},
	
	/**
	 * Creates an animation that drag&drops the last item inserted into the box N°1.
	 */
	autoDragLastItem: function(afterFinishFn){
		afterFinishFn = afterFinishFn || "";
		var elemToMove = $$(".COMUFY_iconSeparatorSpace").last()
		var moveTo = $("COMUFY_dropContentContainer1").cumulativeOffset();//Returns [left, top]
		var moveFrom = elemToMove.cumulativeOffset();
		var movex =  (moveTo[0]+50) - moveFrom[0];//Place the icon in the center
		var movey = (moveTo[1]+50) - moveFrom[1];

		new Effect.Move(elemToMove, {
		  x: movex, y: movey, mode: 'relative',
		  transition: Effect.Transitions.linear,
		  duration: 1.5,
		  delay:1.5,
		  afterFinish: function(){
		  		//Drop the element
				COMUFY.UI.ConfigPanel.onDropPreferenceBox(elemToMove,$("COMUFY_box_1"));
			
		  		//Put the element back to its original position
		  		new Effect.Move(elemToMove, {
					x: -movex, y: -movey, mode: 'relative',
					transition: Effect.Transitions.linear,
					duration: 0.2,
					afterFinish:afterFinishFn
				});
		  }
		});
	},
	
	dispStep5: function(){
		//TWEETSWITCH.ConfigPanel.dispTempMsg("Perfect! You will start receiving your tweets shortly.", 5000);
		TWEETSWITCH.ConfigPanel.dispMsg(TWEETSWITCH.Lang.Generic.Wizard.step5Header,TWEETSWITCH.Lang.Generic.Wizard.step5Body);
		$("msgDialog").insert(
			new Element("div",{className:"perfectBird"})
		);
		$("helpCheckbox").checked = false;
		return;
	},
	
	/**
	 * Removes all the tooltips visible
	 */
	removeWizard: function(){
		for (i = 1; i < 10; i++){
			if ($$("#COMUFY_confPanel .tt"+i)[0]) $$("#COMUFY_confPanel .tt"+i)[0].remove();	
		}
		TWEETSWITCH.Wizard.removeWizObservers();
	}
};
