TLH.pgSubmit = {};

Event.observe(window, 'load', pageLoad);
function pageLoad()
{
	if( ! TLH.scriptsloaded )
	{
		setTimeout('pageLoad()',50);
		return false;
	}
	TLH.pgSubmit.fade = new TLH.Fade(100,3);
    TLH.initNavEffects();
	TLH.pgSubmit.initStepMenu();
	setTimeout('TLH.pgSubmit.showStep.open("title")', 500);
	//** TODO: replace this with event observer on mouseout but watch for event bubbling pain in the arse
	setTimeout('TLH.tog.nav()',1400);
}

TLH.pgSubmit.showCat = function(cnid)
{
	var num = cnid.substr(2);
	if( popStep.currCat == "" || popStep.currCat != num )
	{
		popStep.currCat = num;
		gE('cn').value = num;
		TLH.ajax_get("async.php?c=subcats&cn="+num, popStep.subcat, 1);
		TLH.ajax_get("async.php?c=tags&cn="+num, popStep.tags, 1);
	}
	return false;
}

TLH.pgSubmit.initStepMenu = function()
{
    smlinks = gE('submitsteps').getElementsByTagName('a');
    numlnks = smlinks.length;
	for( var i=0; i < numlnks; i++ )
	{
		smlinks[i].onmouseover=function() { this.className="lbl_step_active"; };
		smlinks[i].onmouseout=function() { this.className=( TLH.pgSubmit.showStep.currId == this.id ) ? "lbl_step_active" : "lbl_step"; };
		smlinks[i].onclick=function() { TLH.pgSubmit.showStep.open(this.id); };
	}
}

TLH.pgSubmit.showStep = {
	currId : "",
	open : function(did)
	{
		// did -> btn/div_step id
		if( this.currId != did )
		{
			if( did == "finish" )
			{
				if( this.chkComplete() )
					popStep.preview();
				else
					return false;
			}
			if( this.currId != "" )
			{
				// hide previous step and mark btn inactive
				$(this.currId).className="lbl_step";
				view.dispHide(this.currId+'_d');
			}
			$(did).className="lbl_step_active";

			// display step
			TLH.pgSubmit.fade._in(did+'_d',0);

			this.currId = did;

			// step is displayed, run any special fn's
			if( gE(did+'_in') )
				$(did+'_in').focus(); // auto focus on first input of div, if avail

			if( this.currId == "pic" && gE('btn_pfile').checked == false) {
				TLH.pgSubmit.pic.url(); gE('btn_purl').checked=true; } // default to URL field

			if( this.currId == "catntag" )
				popStep.prepCats();
		}
		return false;
	},
	next : function(nid)
	{
	   switch(nid) {
	       case 1 : this.open('catntag'); break;
	       case 2 : this.open('detail'); break;
	       case 3 : this.open('pic'); break;
	       case 4 : this.open('finish'); break;
	   }
	},
	chkComplete : function()
	{
		if( tinyMCE )
			tinyMCE.triggerSave(); // move wysiwyg content to textarea

		//=== basic submission form validation
		if( TLH.chkEmptyVal('title_in') )
		{
			alert("Please Name your Happy (step 1)");
			this.open('title');
		}
		else if( tagslist.tagged.length < 1 )
		{
			alert("Pick at least ONE Tag (step 2) for your Happy");
			this.open('catntag');
		}
		else if( TLH.chkEmptyVal('picfile') && TLH.chkEmptyVal('picurl') && TLH.chkEmptyVal('detail_in') )
			alert("Complete at least one of:\n- Details (step 3) \nor\n- Picture (step 4)");
		else
			return true;

		return false;
	}
}

var tagslist = {
	tids : [],
	tnames : [],
	tagged : [],
	taggedlnklst : "",
	taglink : "<a onclick=\"tagslist.tog(this);\"",
	numallowed : 5,
	tog : function(obj)
	{
		//=== if tog is triggered from a taggedlnklst evt, the id contains a 't' strip it and continue
		//** fixes multiple id identical issues
		var tagid = ( obj.id.substr(0,1) == 't' ) ? obj.id.substr(1) : obj.id;

		if( this.tagged.indexOf(tagid) == -1 )
			this.addTag(tagid);
		else
			this.remTag(tagid);

		this.taggedlnklst = "<span class=\"grey_border\">Picked Tags:</span><div>";

		this.tagged.each(function(tid) {
			var os = tagslist.findOS(tid);
			tagslist.taggedlnklst += tagslist.taglink+" id=\"t"+tagslist.tids[os]+"\" class=\"tagged\">"+tagslist.tnames[os]+"</a>\n";
		});

		this.showLnkLst();
	},
	remTag : function(tid)
	{
		$(tid).className = 'tag';
		this.tagged = this.tagged.without(tid);
	},
	addTag : function(tid)
	{
		if( this.tagged.length < this.numallowed )
		{
			$(tid).className = 'tagged';
			this.tagged.push(tid);
		}
		else
		{
			alert("* Please stick to "+this.numallowed+" or less tags.");
			return false;
		}
	},
	findOS : function(num)
	{
		for( var i=0; i < tagslist.tids.length; ++i )
			if( tagslist.tids[i] == num ) break;
		return i;
	},
	set : function(tnms,tids)
	{
		this.tnames = tnms;
		this.tids = tids;
		this.taggedlnklst = "";
		this.tagged.clear();
		this.showLnkLst();
	},
	showLnkLst : function()
	{
		view.show('currTL');
		$('currTL').update( this.taggedlnklst + "</div>" );
	}
}

var popStep = {
	currCat : "",
	prepCats : function()
	{
		var catsul = gE('tlhcats').getElementsByTagName('a');
		for( var i=0; i < catsul.length; ++i )
			catsul[i].onmousedown = function() { TLH.pgSubmit.showCat(this.id); };
	},
	tags : function(xml)
	{
		var kids = xml.documentElement.childNodes;

		var tdt = "<dt>Last, pick some <span class=\"uline info\" title=\"as few as one and as many as five\">Tags</span>:</dt>";
		var tdd = "<dd>";

		tnames = []; tids = []; ntgs = kids.length;
		for(var i=0; i < ntgs; ++i)
		{
			var tname = kids[i].getAttribute('name');
			var tid = kids[i].getAttribute('tid');
			var scn = kids[i].getAttribute('scn');

			tdd += "<span>"+tagslist.taglink+" id=\""+tid+"\" rel=\""+scn+"\">"+tname+"</a></span>\n";
			tnames.push(tname); tids.push(tid);
		}
		//=== save vals to taglist for quick access
		tagslist.set(tnames,tids);

		tdd += "</dd>";

		var tinfospan = "<dd>(pick all general <strong>keywords</strong> that associate with your happy taste.)</dd>";
		var suggtag_in = "<dd>Suggest new Tag? <input type=\"text\" id=\"newtag\" name=\"suggest_newtag\"></dd>";

		$('tags').update( tdt + tinfospan + tdd + suggtag_in );
		TLH.pgSubmit.fade._in('tags',0);
	},
	subcat : function(xml)
	{
		var kids = xml.documentElement.childNodes;
		var catname = kids[0].getAttribute('name');

		var cl = "<select id=\"scn\" name=\"scn\" size=\"1\" onchange=\"TLH.tagSoupDance('scn'+this.options[this.options.selectedIndex].value,'tags');\">";
		cl += "<option/>";

		for (var i=1; i < kids.length; i++)
		{
			var scname = kids[i].getAttribute('name');
			var scid = kids[i].getAttribute('num');
			cl += "<option value=\""+scid+"\">"+scname+"</option>\n";
		}

		var scinfospan = "<div class=\"spacer\">(choosing a subcategory highlights certain tags to make for easier tagging. it also is used to help others find your taste.)</div>";

		$('subcats_dt').update ( catname.toUpperCase()+TLH.spcr+" <span class=\"uline info\" title=\"this is optional\">Subcategory</span>?" );
		$('subcats_dd').update ( cl+"</optgroup></select>" + scinfospan );

		TLH.pgSubmit.fade._in('subcats_dt',0);
		TLH.pgSubmit.fade._in('subcats_dd');
	},
	detail_help : function()
	{
		if( ! TLH.chkEmptyVal("detail_in") )
		{
			if( ! confirm("Clicking OK will replace your text, are you sure?") )
				return false;
		}
		gE("detail_in").value = "For Who?<br><br>Why?<br><br>What?<br><br>Where?<br><br>How?";
		if( tinyMCE )
			tinyMCE.updateContent('detail_in');

		return false;
	},
	preview : function()
	{
		var taggednames = [];
		tagslist.tagged.each(function(tid) {
			var os = tagslist.findOS(tid);
			taggednames.push(tagslist.tnames[os]);
		});

		var scsel = gE('scn');
		gE('scname').value = scsel.options[scsel.options.selectedIndex].text;
		gE('taggednames').value=taggednames.sort().join(", ");

		TLH.ajax_post('?tasty&prev', $('newtlh').serialize(true), 'preview');
	},
	submit : function()
	{
		view.dispHide('finish_d');
		//view.dispHide('submitsteps');

		gE('submitstatus_d').innerHTML = "Submitting your Tasty... please wait.";

		var fi = new TLH.Fade(80,6);
		fi._in('submitstatus_d');

		gE('taggedids').value = tagslist.tagged;
		TLH.ajax_post('?submittaste', $('newtlh').serialize(true), 'submitstatus_d');
	}
}

TLH.pgSubmit.pic = {
	url : function()
	{
		view.visiShow('picurl');view.visiHide('picfile');$('picurl').focus();
	},
	file : function()
	{
		window.open("?newimg",'newimg','width=500,height=200,toolbar=0,resizable=0');
		view.visiHide('picurl');view.visiShow('picfile');
	}
}
