var LOADED_MEDIA = 0;
var LOADED_MEDIA_DIV = null;
var LOADED_MEDIA_TYPE = "";
var IN_TRANSITION = false;
var PLAYLIST_ARRAY = new Array();

function load_media(mID, clicked, forceReload) {
	if (typeof forceReload == "undefined" || forceReload == null) forceReload = false;
	if ((mID != LOADED_MEDIA && !IN_TRANSITION) || forceReload) {
		IN_TRANSITION = true;
		if(LOADED_MEDIA_DIV != null) {
			dehighlight(LOADED_MEDIA_DIV);
		}
		highlight(clicked);
		LOADED_MEDIA_DIV = clicked;
		LOADED_MEDIA = 	mID;
		
		picBox = document.getElementById("pl_mdp");
		infoBox = document.getElementById("pl_info_box");
		infoBox.innerHTML = '<img src="'+ajaxLoadImage.src+'" />  Loading...';
		
		ajax("mediaComponent.php?ajax=getMediaDetails&m=" + mID, function (res) {
			var row = ajaxResponseArray(res);
			var ih = new String();
			
			
			ih = "<div class=\"media_infoTitle\">" + row['title'] + "</div>";
			ih += "<div class=\"media_infoBody\">" + row['description'] + "</div><br /><br />";
			ih += "<a href=\"media/"+ row['rf'] +"\">Download Full File</a><br />";
			ih += "<a href=\""+ location.pathname +"\">Back to Gallery</a><br />";
			
			ih += "<br />Permanent Link<br />";
			ih += "<input class=\"hover_form\" style=\"width: 100px;\" value=\"" + location.href + "&locate=" + row['id'] +"\" /><br />";
			if (ADMINMODE) {
				ih += "<input type=\"button\" value=\"Edit Media\" onmouseup=\"editMedia("+row['id']+", this)\" /><br />";
				ih += "<input type=\"button\" value=\"Delete Media\" "+((row['parentID'] == 0)?"disabled=\"disabled\"":"")+" onmouseup=\"deleteMedia("+row['id']+", "+row['parentID']+")\" /><br />";
				if (row['parentID'] == 0) {
					//ih += "<br />This file is the Gallery Root Image.  Deleting this means that you will be deleting the gallery.  If you really want to do this, there is a button below for that."
				}
			}
			infoBox.innerHTML = ih;
			//handle different media types
			if (row['type'] == "pic") {
				var img = new Image();
				img.src = row['ref'];
				img.style.opacity = 0;
				
				var b = popupFactory();
				b.setText(AJAX_IMAGE + " Loading Image");
				b.showBox(picBox);
				
				img.onload = function() {
					b.destroy();
					if (picBox.firstChild != null && LOADED_MEDIA_TYPE == "pic") {
						fade_out(picBox.firstChild, function() {
							picBox.removeChild(picBox.firstChild);
							picBox.appendChild(img);
							fade_in(picBox.firstChild, function() {IN_TRANSITION = false;});
						});
					}else{
						if (LOADED_MEDIA_TYPE == "vid") {
							picBox.innerHTML = "";
						}
						picBox.appendChild(img);
						fade_in(picBox.firstChild, function() {IN_TRANSITION = false;})
					}
					LOADED_MEDIA_TYPE = "pic";
				}
				img.onerror = function() {
					alert("An error has occurred while loading the image.\r\n" + img.src);
				}
			}else{
				if (LOADED_MEDIA_TYPE != "vid" && LOADED_MEDIA_TYPE != "") {
					fade_out(picBox.firstChild, function() {
						var s1 = new SWFObject("media/flvplayer.swf","single","100%","100%","7");
						s1.addParam("allowfullscreen","true");
						s1.addVariable("file",row['ref']);
						s1.addVariable("overstretch","false");
						s1.write("pl_mdp");
						LOADED_MEDIA_TYPE = "vid";
						IN_TRANSITION = false;
					});
				}else{
					var s1 = new SWFObject("media/flvplayer.swf","single","100%","100%","7");
					s1.addParam("allowfullscreen","true");
					s1.addVariable("file",row['ref']);
					s1.addVariable("overstretch","false");
					s1.write("pl_mdp");
					LOADED_MEDIA_TYPE = "vid";
					IN_TRANSITION = false;
				}
			}
		});
	}
}

function fade_out(elem, functionWhenFaded, lastOpacity) {	
	opacity = parseFloat(elem.style.opacity);
	
	if (opacity <= .08 || opacity == lastOpacity) {
		functionWhenFaded();
	}else{
		opacity -= .12;
		elem.style.opacity = opacity;
		elem.style.filter = "alpha(opacity=" + (opacity*100) + ")";
		
		window.setTimeout(function() {
			fade_out(elem, functionWhenFaded);
		}, 50);
	}
}

function fade_in(elem, functionWhenFaded, lastOpacity) {
	opacity = parseFloat(elem.style.opacity);
	
	if (opacity <= 1 || opacity == lastOpacity) {
		opacity += .12;
		elem.style.opacity = opacity;
		elem.style.filter = "alpha(opacity=" + (opacity*100) + ")";
		
		window.setTimeout(function() {
			fade_in(elem, functionWhenFaded);
		}, 50);
	}else{
		functionWhenFaded();
	}
}

function hl_media(id) {
	highlight(id);
}

function dhl_media(id) {
	if (id != LOADED_MEDIA_DIV) {
		dehighlight(id);
	} 
}

function editMedia(mID, elem) {
	var b = popupFactory();
	b.setText(AJAX_IMAGE + " Loading Details...");
	b.showBox(elem, 250);
	
	ajax("mediaComponent.php?ajax=getMediaDetails&e=true&m=" + mID, function(res) {
		var row = ajaxResponseArray(res);
		var ih = new String();
		var num = parseInt(row['num']);
		
		ih += "<form>";
		ih += "<input name=\"title\" value=\""+row['title']+"\" class=\"hover_form\" />";
		ih += "<textarea name=\"description\" class=\"hover_form\" />"+row['description']+"</textarea>";
		ih += "Gallery: <select name=\"parentID\">";
		
		for (var i = 0; i < num; i++) {
			ih += "<option value=\""+row['rootID_' + i]+"\" "+((row['rootID_' + i] == row['parentID']) ? "selected=\"selected\"" : "")+">" + row['rootTitle_' + i] + "</option>";
		}
		ih += "</select><br>";
		ih += "<input type=\"button\" value=\"Edit Media\" onmouseup=\"editMediaSubmit(this.parentNode, "+mID+", "+row['parentID']+")\" />";
		ih += "</form>";
		
		b.setText(ih);
	});
}

function editMediaSubmit(fd, mID, parentID) {
	fd = formToURI(fd);
	ajaxSend("mediaComponent.php?ajax=editFile&m=" + mID, fd, function(res) {
		document.getElementById("pl_playlist_area").innerHTML = res;
		if (document.getElementById("m_" + mID)) {
			load_media(mID, document.getElementById("m_" + mID), true);
		}else{
			load_media(mID, document.getElementById("m_" + PLAYLIST_ARRAY[0]), true);
		}
	});
}

function deleteMedia(mID, parentID) {
	if (parentID == 0) {
		alert("This file cannot be deleted this way.  Please use the \"Delete This Gallery\" button below");
	}else{
		if (confirm("Are you sure that you want to do this?  This action cannot be undone!")) {
			ajax("mediaComponent.php?ajax=deleteMedia&m=" + mID, function(res) {
				document.getElementById("pl_playlist_area").innerHTML = res;
			});
		}
	}
}

function deleteGallery(mID) {
	if (confirm("This will delete this gallery AND ALL of its pictures!\r\nAre you ABSOLUTELY sure that you want to do this?")) {
		ajax("mediaComponent.php?ajax=deleteGallery&m=" + mID, function (res) {
			alert("Function Successful.  You will be redirected to the gallery");
			window.location = location.pathname;

		});
	}
}

function addFile(giver, parentID) {
	var b = popupFactory();
	b.setText("Loading Files...");
	b.showBox(giver, 300);
	
	ajax("mediaComponent.php?ajax=getUploadedFiles", function(res) {
		var files = ajaxResponseArray(res);
		var num = parseInt(files['num']);
		var ih = new String();
		
		ih += "<form><select name=\"fileRef\">";
		for(var i = 0; i < num; i++) {
			ih += "<option value=\"" + files['file_' + i] + "\">" + files['file_' + i].substr(0, Math.min(43, files['file_' + i].length)) + "</option>";
		}
		ih += "</select><input type=\"button\" value=\"Preview\" onmouseup=\"previewPic(this.previousSibling.options[this.previousSibling.selectedIndex].value, "+b.instance()+", this)\" /><br />";
		ih += "<input name=\"title\" value=\"Media Title\" class=\"hover_form\" />";
		ih += "<textarea name=\"description\" class=\"hover_form\">Media Description</textarea>";
		ih += "<input name=\"parentID\" value=\""+parentID+"\" type=\"hidden\" />";
		ih += "<input type=\"button\" value=\"Add\" onmouseup=\"addFileSubmit(this.parentNode, "+parentID+", "+b.instance()+");\" />";
		
		ih += "</form>";
		
		b.setText(ih);
	});
}

function previewPic(fname, pb_b, giver) {
	var cb = pb_b.generateChild();
	if (fname.indexOf(".flv") == -1) {
		cb.setText("<img src=\"media/media_resizer.php?file=uploads/"+fname+"&size=200\" />");
	}else{
		var tmp = document.createElement("span");
		
		var s1 = new SWFObject("media/flvplayer.swf","single","100%","100%","7");
		s1.addParam("allowfullscreen","true");
		s1.addVariable("file","uploads/" + fname);
		s1.addVariable("overstretch","false");
		s1.write(tmp);
		
		cb.setText(tmp.innerHTML);
	}
	cb.showBox(giver, 200);
}

function addFileSubmit(fd, parentID, pb_b) {
	fd = formToURI(fd);
	pb_b.setText("Adding Picture...");
	ajaxSend("mediaComponent.php?ajax=addFile", fd, function(res) {
		if (parentID != 0) {
			document.getElementById("pl_playlist_area").innerHTML = res;
			pb_b.destroy();
		}
	});
}

function play_slideshow(ind, button) {
	load_media(PLAYLIST_ARRAY[ind], document.getElementById("m_" + PLAYLIST_ARRAY[ind]), false);
	upd_counter(button, 8);
	//alert(PLAYLIST_ARRAY[ind]);
	if(ind < PLAYLIST_ARRAY.length) {
		window.setTimeout(function() { play_slideshow(ind + 1, button); }, 8000);
	}
}

function upd_counter(button, num) {
	button.value = "Stop Slideshow (" + num + ")";
	if (num > 1) {
		window.setTimeout(function() { upd_counter(button, num - 1); }, 1000);
	}
}














