function formhint() {
	var el = arguments[0];
	var hel = el.nextSibling;
	while (hel.innerHTML == null) // ie hackery :/
		hel = hel.nextSibling;
	var t = '&nbsp;'
	if (arguments.length>1)
		return hel.innerHTML = t;
	switch (el.name) {
		case 'username':
			t = 'Minimum 3, maximum 16 characters. No weird ones.';
			break;
		case 'password':
			t = 'At least 6 characters long and different to your username.';
			break;
		case 'password2':
			t = '...Just to make sure there were no typo\'s';
			break;
		case 'email':
			t = 'Don\'t worry - we\'ll never share this with anyone.';
			break;
	}
	hel	.innerHTML = t;
}

var playlist = [];

function addToPlaylist(mid) {
	if (playlist.contains(mid)) return false;
	playlist.push(mid);
	var plt = $('playlisttotal');
	plt.innerHTML = parseInt(plt.innerHTML)+1;
	var source = $('i_'+mid); // handle to the img
	playlistFade(mid);
	var overlay = source.clone().inject(document.body);
	overlay.setStyle('display', 'block');
	overlay.setStyle('position', 'absolute');
	overlay.setStyle('height', 115);
	overlay.setStyle('width', 150);
	overlay.setPosition(source.getPosition());
	var target = $('playlist').getPosition();
	var m = new Fx.Morph(overlay, {duration: 1000, transition: Fx.Transitions.Quad.easeInOut});
	m.addEvent('complete', function() { 
		overlay.dispose();
	} );
	m.start({'top': target.y, 'left': target.x, 'width': 10, 'height': 10 });
	var r = new Request({url: '/ajax/playlist-add', method: 'GET'}).send('m='+mid);
}

function playlistFade(mid) {
	var source = $('i_'+mid); // handle to the img
	var bar = new Element('div', { 'class': 'overlay', 'styles': {'top': source.getPosition().y+(source.getSize().y)-14, 'left': source.getPosition().x, 'width': source.getSize().x }});
	bar.innerHTML = 'In your <a href="/channels/playlist">Playlist</a>';
	bar.inject(document.body);
	source.setStyle('opacity', '0.25');
	source.setStyle('-ms-filter', '"progid:DXImageTransform.Microsoft.Alpha(opacity=25)"');
	source.setStyle('filter', 'alpha(opacity=25)');
}

function watchedlistFade(mid) {
	var source = $('i_'+mid); // handle to the img
	var bar = new Element('div', { 'class': 'overlay played', 'styles': {'top': source.getPosition().y+(source.getSize().y)-14, 'left': source.getPosition().x, 'width': source.getSize().x }});
	bar.innerHTML = 'Already watched today';
	bar.inject(document.body);
	source.setStyle('opacity', '0.25');
	source.setStyle('-ms-filter', '"progid:DXImageTransform.Microsoft.Alpha(opacity=25)"');
	source.setStyle('filter', 'alpha(opacity=25)');
}


function submitcomment(media) {
	var comment = $('mycomments').value;
	if (comment == '') {
		$('mycomments').focus();
		var m = new Fx.Morph($('mycomments'), {duration: 1000, transition: Fx.Transitions.Sine.easeInOut});
		$('mycomments').setStyle('background-color', '#fff');
		m.start({'background-color': '#666' });
		return false;
	}
	$('mycomments').value = '';
	var c = new Element('div', {'class': 'comment', 'text': 'You said: '+comment });
	c.injectAfter($('addcommentform'));
	var h = c.getSize().y;
	c.setStyle('display', 'none');
	c.setStyle('overflow', 'hidden');
	c.setStyle('height', '1px');
	c.setStyle('display', 'block');
	var m = new Fx.Morph(c, {duration: 1000, transition: Fx.Transitions.Quad.easeInOut});
	m.start({'height': h-9 });
	var comm = new Request.JSON({url: '/ajax/comment'}).get({'media': media, 'comment': comment});
}

function reportProblem(m) {
	$('videoflag').innerHTML = '<a href="#" title="Report broken video" onclick="submitProblem('+m+',\'broken\'); return false">Broken</a> or <a href="#" title="Report inappropriate video" onclick="submitProblem('+m+',\'inappropriate\'); return false">Inappropriate</a>?';
}

function submitProblem(m,p) {
	var problem = new Request({method: 'get', url: '/ajax/report'});
	problem.send('m='+escape(m)+'&p='+escape(p));
	$('videoflag').innerHTML = 'Thanks!';
	var m = new Fx.Morph($('videoflag'), {duration: 2000, transition: Fx.Transitions.Quad.easeInOut});
	m.addEvent('complete', function() { 
		$('videoflag').setStyle('visibility', 'hidden');
	} );
	m.start({'color': '#333333' });
}

var nft;
var nfr;
function newforumtopic(f,s) {
	nft = $('postbox').innerHTML;
	$('postbox').innerHTML = '<form onsubmit="return post_validate(true)" method="post" action="'+document.location.href+'"><input type="hidden" name="skey" value="'+s+'" /><input type="hidden" name="action" value="newtopic" /><input type="hidden" name="parent" value="'+f+'" /><div class="form-row"><label for="posttitle">Title</label></div><div class="form-row"><input class="txt" type="text" id="posttitle" length="100" name="posttitle" /></div><div class="form-row"><label for="postbody">Your message</label></div><div class="form-row"><textarea id="postbody" name="postbody"></textarea></div><div class="form-row submit"><input class="button" type="submit" value="Post" /></div></form>';
}

function post_validate(t) {
	if (t) {
		var title = $('posttitle').value;
		if (title == '') {
			$('posttitle').focus();
			var m = new Fx.Morph($('posttitle'), {duration: 1000, transition: Fx.Transitions.Sine.easeInOut});
			$('posttitle').setStyle('background-color', '#fff');
			m.start({'background-color': '#444' });
			return false;
		}
	}
	var body = $('postbody').value;
	if (body == '') {
		$('postbody').focus();
		var m = new Fx.Morph($('postbody'), {duration: 1000, transition: Fx.Transitions.Sine.easeInOut});
		$('postbody').setStyle('background-color', '#fff');
		m.start({'background-color': '#444' });
		return false;
	}
	return true;
}

function newforumreply(f,s) {
	nfr = $('postbox').innerHTML;
	$('postbox').innerHTML = '<form onsubmit="return post_validate()" method="post" action="'+document.location.href+'"><input type="hidden" name="skey" value="'+s+'" /><input type="hidden" name="action" value="newreply" /><input type="hidden" name="parent" value="'+f+'" /><div class="form-row"><label for="postbody">Your message</label></div><div class="form-row"><textarea id="postbody" name="postbody"></textarea></div><div class="form-row submit"><input class="button" type="submit" value="Post" /></div></form>';
}

//----------------------------------------------------------
		
	if (!applesearch)	var applesearch = {};

//----------------------------------------------------------

	applesearch.init = function ()
	{
		var srchform = document.getElementById('searchform');
		
		// Paranoia! Make sure we can get the search form before trying anything
		if(srchform) {
			
			var srchinput = document.getElementById('query');
			labelText = applesearch.getLabelText();
			
			// add applesearch css for non-safari, dom-capable browsers
			if ( ((navigator.userAgent.toLowerCase().indexOf('safari') < 0 || Browser.Platform.win) || navigator.userAgent.toLowerCase().indexOf('chrome') > 0)  && document.getElementById ) {

				this.clearBtn = false;
		
				// add style sheet if not safari
				var appleStyle = document.createElement('link');
				appleStyle.rel = 'stylesheet';
				appleStyle.type = 'text/css';
				appleStyle.media = 'screen';
				appleStyle.href = '/~project/assets/css/applesearch.css';
				document.getElementsByTagName("head")[0].appendChild(appleStyle);

				//------------------
				
				// create and attach the span
				var r_crnr = document.createElement('span');
				r_crnr.className = 'sbox_r'; // IE win doesn't like setAttribute in this case
				r_crnr.setAttribute('id','srch_clear');
		
				var fieldsets = srchform.getElementsByTagName('fieldset');
				if (fieldsets) {
					fieldsets[0].appendChild(r_crnr);
				}
		
				// Create and attach a clearing div, keeps Opera happy
				var d_clr = document.createElement('div');
				d_clr.setAttribute('class','clear');
				srchform.appendChild(d_clr);
		
				//------------------
				
				// set our text colors
				var labels = srchform.getElementsByTagName('label');
	
				if (labels) {			
					if (labels[0].currentStyle) {
						// must be using IE
						labelColor = labels[0].currentStyle.color;
						inputColor = srchinput.currentStyle.color;
					} else if (document.defaultView.getComputedStyle) {
						labelColor = document.defaultView.getComputedStyle(labels[0], null).getPropertyValue('color');
						inputColor = document.defaultView.getComputedStyle(srchinput, null).getPropertyValue('color');
					}
				}
				
				//------------------
				
				// Paranoia again! Make sure the search field exist
				if(srchinput) {
					// set the placeholder text based off the label
					srchinput.value = labelText;
					srchinput.style.color = labelColor;
	
					// add some events to the input field
					srchinput.onkeyup = function () {
							applesearch.onChange('query','srch_clear');
						}
					srchinput.onfocus = function () {
							if (this.value == labelText) {
								this.value = '';
								this.style.color = inputColor;	
							}
						}
					srchinput.onblur = function () {
							if (this.value == '') {
								this.value = labelText;
								this.style.color = labelColor;
							}
						}
				}
				
				// prevent the form being submitted if the input's value is the placeholder (label) text
				srchform.onsubmit = function()
				{
					if(srchinput && srchinput != labelText) {
						return true;
					} else {
						return false;
					}
				}
			} else {
				// Paranoia again! Make sure the search field exist
				if(srchinput) {
					// Using Safari so change some attributes to get the Apple Search field
					srchinput.type = 'search';
					srchinput.setAttribute('placeholder',labelText);
					srchinput.setAttribute('autosave','bsn_srch');
					srchinput.setAttribute('results','5');
				}
			}
		}
	}

//----------------------------------------------------------

	applesearch.getLabelText = function()
	{
		var srchform = document.getElementById('searchform');
		if(srchform) {
			var labels = srchform.getElementsByTagName('label');
	
			if (labels) {
				var labelFor = labels[0].getAttribute('for');
				var labelText = labels[0].firstChild.nodeValue;
			} else {
				// just in case, set default text
				var labelText = 'Search';
			}
		} else {
			// just in case, set default text
			var labelText = 'Search';
		}
		return labelText;
	}

//----------------------------------------------------------

	// called when on user input - toggles clear fld btn
	applesearch.onChange = function (fldID, btnID)
	{
		// check whether to show delete button
		var fld = document.getElementById( fldID );
		var btn = document.getElementById( btnID );

		if (fld.value.length > 0 && fld.value != labelText && !this.clearBtn) {
/*			btn.className = 'sbox_r_f2';
			btn.fldID = fldID; // btn remembers it's field
			btn.onclick = this.clearBtnClick;
			this.clearBtn = true;
*/		} else if (fld.value.length == 0 && this.clearBtn) {
			btn.className = 'sbox_r';
			btn.onclick = null;
			this.clearBtn = false;
			// reset the field's placeholder text
			fld.value = labelText;
			fld.style.color = labelColor;
		}
	}

//----------------------------------------------------------

	applesearch.clearFld = function (fldID,btnID)
	{
		var fld = document.getElementById( fldID );
		fld.value = '';
		this.onChange(fldID,btnID);
	}


//----------------------------------------------------------

	applesearch.clearBtnClick = function ()
	{
		applesearch.clearFld(this.fldID, this.id);
	}

//----------------------------------------------------------

	window.onload = function () { applesearch.init(); }

//----------------------------------------------------------