
var Wizard = {

	types : {
	"" : "Other",
	"culture_entertainment culture" : "Culture",
	"culture_entertainment historic_site" : "Historic site",
	"culture_entertainment entertainment" : "Entertainment",
	"nature" : "Nature",
	"activity" : "Activity",
	"accomodation" : "Accommodation",
	"eat_drink_shop" : "Eating",
	"eat_drink_shop nightlife" : "Nightlife",
	"eat_drink_shop shopping" : "Shopping", 
	"infrastructure" : "Transportation",
	"miscellaneous" : "Miscellaneous"
	},


	typeTags : {
	""	: [],
	"accomodation" 				:["hotel","b_and_b","rental","boutique","design","exclusive","hideaway","historic","panoramic","romantic","spa"],
	"eat_drink_shop" 			:['restaurant', 'cafe', "chinese","fish","french","indian","international","italian","japanese","mediterranean","middle_eastern","thai"],
	"eat_drink_shop nightlife" 	:["bar", "pub", "night_club"],
	"eat_drink_shop shopping" 	:["market", "department_store", "craft", "souvenirs", "food","local","clothes"],
	"culture_entertainment historic_site" 	:['art__architecture','museum','festival','culture','religious',"castle","cathedral","prehistoric","ruin","temple","town"],
	"culture_entertainment culture" 	:['art__architecture','museum','festival','historic_site','religious',"castle","cathedral","prehistoric","ruin","temple","town"],
	"culture_entertainment entertainment" 	:['children','resort_theme_park','performance_art',"festival","wine"],
	"nature" 					:['landscape','wildlife','gardens'],
	"activity" 					:['sport','adventure','winter_sports','journey','mind_body_spirit',"cycling","diving","fishing","golf","rafting","safari","sailing","snorkelling","surfing","trekking"],
	"infrastructure" 			:['airport','rail_station','bus_station','ferry_terminal'],
	"miscellaneous"				:['personal', 'rants', 'note']
	},

	wizardDom :null,
	wizardStage : 1,
	stages : [],
	
	//Object holding new item data
	iba : null,   //item being added
	
	//Hide the wizard
	hide : function (){
		Wizard.wizardDom.style.display = 'none';
		$("sidePanel").style.display = "block";
	},
	
	//Initiate the wizard... 
	init: function(){
				
		if (MS.user == null){
			MS.loginOrSignup('&adding=true');	
			return;
		}
		
		Wizard.iba = {
			id : 'new',
			title: '',
			body : '',
			tags : '',
			link : ''
		}
		
		Wizard.wizardStage = 1;	
		Wizard.wizardDom = $("wizardDiv");
		
		Wizard.wizardDom.style.display = 'block';
		$("sidePanel").style.display = "none";
		
		Wizard.wizardDom.innerHTML = '<h2>Add something [<a href="javascript: Explore.cancelAddItem()">cancel</a>]</h2>';
			
		delete Wizard.stages;
		Wizard.stages = [];
		Wizard.addStage(1, Wizard.getStage1());
		Wizard.addStage(2, Wizard.getStage2());
		Wizard.addStage(3, Wizard.getStage3());

		Wizard.updateStage(1);
	},
	
	//Call to update the wizard to a new stage
	updateStage : function(stage){
		
		// Save current stage state
		switch(Wizard.wizardStage){
			
			case 2 :
				Wizard.iba.title = $('add_item_title').value;	
				Wizard.iba.body = $('add_item_body').value;
				Wizard.iba.link = $('add_item_link').value;
				break;
				
			case 3 :
				Wizard.iba.tags = $('add_item_tags').value;	
				Wizard.iba.systag = $('add_item_systags').value;
				break;
			
		}
		
		// Set new stage
		Wizard.wizardStage = stage;
		
		// Go through each stage and update selected
		for(var i=0 ; i < Wizard.stages.length ; i++){
			if ((stage-1) == i){
				Wizard.stages[i].addClass('selected');	
			} else {
				Wizard.stages[i].removeClass('selected');
			}
		}
		
		State.mode = State.MODE_EXPLORE;   //stupid bug fix- opening new window caused close window event to cancel posting...
		
		//Open the info window for this stage
		if (stage > 1) Markers.s_markers.new_item.openInfoWindowHtml(Wizard.getInfoWindowHTML(stage));
		GEvent.addListener(MS.map.getInfoWindow(), 'closeclick', Explore.cancelAddItem);
	
		//If tagging state... Compute the category and tag select box
		if (stage == 3){
			Wizard.initTypes();
			Wizard.updateType();
		}
		
		//Set the state mode at ADDING
		State.mode = State.MODE_ADD;
	},

	nextStage : function(){
		Wizard.updateStage(Wizard.wizardStage+1);
	},
	
	prevStage : function(){
		Wizard.updateStage(Wizard.wizardStage-1);
	},
	
	addStage : function(stage, html){
		var div = new Element('div');
		div.className="wizardPanel";
		div.innerHTML = html;
		Wizard.stages.push(div);
		Wizard.wizardDom.appendChild(div);
	},
	
	titles : [
			  '',
			'<img src="/images/icons/wizard/1.gif" width="20" height="20" align="absmiddle" alt="1"/> Tell us where the place is',
			'<img src="/images/icons/wizard/2.gif" width="20" height="20" align="absmiddle" alt="1"/> Tell us about the place',
			'<img src="/images/icons/wizard/3.gif" width="20" height="20" align="absmiddle" alt="1"/> Apply tags so that people can find your place',
			'Done'
	],
	
	getStage1 : function(){
		return '<h3>'+Wizard.titles[1]+'</h3>Click on the map to set the marker location,<br/><div id="add_search_area" style="margin-top: 8px;">or <a href="#" onclick="Wizard.openSearchArea(); return false;">search for city or place</a>.</div>';	
	},
	
	getStage2 : function(){
		return '<h3>'+Wizard.titles[2]+'</h3>Enter a title and description.';	
	},
	
	getStage3 : function(){
		return '<h3>'+Wizard.titles[3]+'</h3>You can apply keywords to help people find your post.';	
	},

	getStage4 : function(){
		return '<h3>'+Wizard.titles[4]+'</h3>';	
	},
	
	
	/*
	*	Get HTML for info window depending on wizard stage
	*/
	getInfoWindowHTML : function(stage){
		var mid = '';
		var bot = '<a style="float: right" href="." onclick="Markers.closeWindow(); return false;">Cancel</a>';
		
		if (stage > 2)
			bot += '<a href="." onclick="Wizard.prevStage(); return false;">&laquo; Previous</a> ';
			
		if (stage < 3)	
			bot +='<a href="." onclick="Wizard.nextStage(); return false;">Next &raquo;</a>';
		
		if (stage == 3)	
			bot +='<a href="." id="finish_add_item_button" onclick="Explore.addItemClicked(); return false;">Finish</a>';
		
		switch (stage){
			case 2:
				mid =  	Wizard.getIW2();
				break;
			case 3:
				mid =  	Wizard.getIW3();
				break;
		}
		
		return '<div id="infoWindow"><div id="itop"><h3>'+Wizard.titles[stage]+'</h3></div><div id="imid">'+mid+'</div><div id="ibot">'+bot+'</div></div>';
	},
	
	getIW2 : function (){
		return 	'Title:<br />'+
			'<input id="add_item_title" size="30" value="'+Wizard.iba.title+'"/><br />'+
			'Description:<br />'+
			'<textarea id="add_item_body" rows="6" style="width: 100%">'+Wizard.iba.body+'</textarea><br />'+
			'Link to webpage (optional)<br />'+
			'<input id="add_item_link" style="width: 100%"/>';
	},
	
	
	getIW3 : function (){
		return 	'<p><b>Category:</b> <select id="add_item_systags" onchange="Wizard.updateType(this);"></select></p>'+
				'<b>Tags</b><br />'+
				'<div>Tags are words that categorise your place or posting. You can select from the list of common tags, or type or own.</div>'+
				'<br />Common tags<div id="common_tags" style="height: 2em"></div><br />'+
				'Selected tags: <input id="add_item_tags" style="width: 100%;" value="'+Wizard.iba.tags+'"/><br />';	
	},
	
	/*
	*	Location search
	*/
	
	openSearchArea : function(){
		$('add_search_area').innerHTML = '<img src="/images/icons/magnifier.gif" width="16" height="16" align="absmiddle" /> <input style="border: 1px solid #666;" id="add_search" onchange="Wizard.search()"/> <input style="border: 1px solid #666;" type="button" value="Go" onclick="Wizard.search()"><div style="margin: 8px 0px;" id="add_search_results"></div>';
	},
	
	search : function(page){
		var terms = $('add_search').value;
		var point = MS.map.getCenter();
		var lat = point.x;
		var lng = point.y;
		
		var point2 = MS.map.getBounds().getNorthEast();
		
		var radius = point.distanceFrom(point2) * 0.000621371192 * 0.7;
		
		var request = {
			'terms' : terms,
			'lat' : lat,
			'lng' : lng,
			'page' : page,
			'radius' : radius
		};
		
		var url = '/interface/lookup/place.php';
		MS.setStatus("Searching...", true);
		
		Conn.doPost(url, request , Wizard.searchResults);
	},
	
	searchResults : function(result){
		//MS.setStatus("", false);
		MS.setStatus('Now click where the post should go... [<a href="#" onclick="Explore.cancelAddItem();">cancel</a>]');
		//MS.debug(result);
		$('add_search_results').innerHTML = result;
	},

	/*
	*	Stuff for tagging
	*/

	addTag : function(element,tag){
		var tb = $('add_item_tags');
		
		if (tb){
			if (element.className != 'stag'){
				tb.value += " "+tag+" ";
				element.className = 'stag';
			} else {
				tb.value= tb.value.replace(" "+tag+" ", " ").replace(/\s\s+/, " ");
				element.className = 'tag';
			}
		}
	},
	
	setCommonTags : function(tags){
		var div = $('common_tags');
		var html = '';
		
		var tagtext = '';
		if ($('add_item_tags')) tagtext = $('add_item_tags').value;

		
		for (var i=0; i < tags.length; i++){
			var tag = tags[i];
			var cls = (tagtext.indexOf(" "+tag+" ")>=0)? 'stag' : 'tag';
			
			html += '<a href="#" class="'+cls+'" onclick="Wizard.addTag(this,\''+tag+'\')">'+tag+'</a>&nbsp; &nbsp;';
		}
		if (tags.length ==0){
			html = "None<br /><br />";	
		}
		
		div.innerHTML = html;
	},
	
	updateType :  function(){
		var type = $('add_item_systags').value;	
		Wizard.setCommonTags(Wizard.typeTags[type]);
	},
	
	initTypes : function(){
		var sel = $('add_item_systags');
		var value = Wizard.iba.systag;
		
		for (var key in Wizard.types){
			var type = 	Wizard.types[key];
			var opt = document.createElement("option");
			opt.setAttribute("value",key);
			opt.innerHTML = type;
			
			if (value == key) opt.setAttribute("selected","selected");
			sel.appendChild(opt);
		}
	}
	
}