function MM_findObj(n, d)
{
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm()
{
	var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
	for(i=0; i<(args.length-2); i+=3)
	{
		test=args[i+2];
		val=MM_findObj(args[i]);
		if(val)
		{
			nm=val.name;
			
			if((val=val.value)!="")
			{
				// Test for filename: F
				if (test.indexOf('F')!=-1)
				{
					var reg = /^[A-Za-z0-9_-]+\.[a-z0-9]{3}$/;
	       			if(!reg.test(val)) errors+='- '+nm+' must contain a filename matching\n[A-Za-z0-9_-]+\.[a-z0-9]{3}\n';
				}
				// Test for email
				else if (test.indexOf('isEmail')!=-1)
				{
					var reg = /^[A-Za-z0-9][A-Za-z0-9\._-]+@[A-Za-z0-9][A-Za-z0-9\.-]+\.[a-zA-Z]{2,4}$/;
        			if(!reg.test(val)) errors+='- '+nm+' must contain an e-mail address\n';
				}
				// Test for number
				else if (test!='R')
				{
					num = parseFloat(val);
        			if(isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        			if(test.indexOf('inRange') != -1)
					{
						p=test.indexOf(':');
          				min=test.substring(8,p);
						max=test.substring(p+1);
          				if(num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    				}
				}
			}
			// Test for existance: R
			else if(test.charAt(0) == 'R') errors += '- '+nm+' is required.\n';
		}
	}
	// Print errors
	if(errors) alert('The following error(s) occurred:\n'+errors);
	document.MM_returnValue = (errors == '');
}

function changeFolder(action, table, itemid)
{
	new Ajax.Request(
		'/actionhandler.php', {
			parameters: {action: action, table: table, itemid: itemid},
			
			onComplete: function(response) {
				new Ajax.Request(
					'folderitems.php', {
						onComplete: function(response) {
							$('folderitems').innerHTML = response.responseText;
						}
					}
				);
				
				Effect.SlideDown('folderwrapper', { duration: 0.5 });
			}
		}
	);	
}

//Rememers the currently shown screenshot for movielist.php
var currentMovieId;

//Rememers the currently shown image for imagelist.php
var currentImageId;

//Rememers the currently shown youtube video for youtubelist.php
var currentYoutubeId;

function setBigPreview(prefix, id)
{
	var bigImage = $(prefix + 'list_big_image');
	var bigImageTitleDiv = $(prefix + 'list_big_title');
	var smallImage = $(prefix + 'list_small_' + id);
	
	bigImage.src = 'img/img.php?table=' + prefix + 's&size=medium&id=' + id;
	bigImageTitleDiv.innerHTML = smallImage.alt;
}

function setYoutubeId(id)
{
	var currentYoutubeUrl = 'http://www.youtube.com/v/' + id + '&hl=de&fs=1';
	var youtubeHTML = '<object width="468" height="286"><param name="movie" value="' + currentYoutubeUrl + '"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="' + currentYoutubeUrl + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="468" height="286"></embed></object>';
	
	var youtubelistBigContainer = $('youtubelist_big_container');
	var youtubelistTitleDiv = $('youtubelist_big_title');
	var smallImage = $('youtubelist_small_' + id);
	var youtubelistUrl = $('youtube_url');
	var youtubelistEmbed = $('youtube_embed');
	
	youtubelistBigContainer.innerHTML = youtubeHTML;
	youtubelistTitleDiv.innerHTML = smallImage.alt;
	youtubelistUrl.value = currentYoutubeUrl;
	youtubelistEmbed.value = youtubeHTML;
}

function openDiv(id) {
	// Only if not open already
	if($(id).style.display == 'none') {	
		Effect.SlideDown(id, { duration: 0.5 });
	}
}

function postOnTwitter() {
	BitlyCB.shortenResponse = function(data) {
		// Results are keyed by longUrl, so we need to grab the first one.
		var first_result;
		for(var r in data.results) {
			first_result = data.results[r]; break;
		}
		
		window.open('http://www.twitter.com/home?status='+first_result['shortUrl']);
	}
	
	BitlyClient.shorten(location.href, 'BitlyCB.shortenResponse');
}

