var currentAlbum = null;
var currentImage = null;
var alwaysSave = false;
var askAlwaysSave = false;

function uploadImages() {
	with (document.JUploadForm) {
		if (album_id.selectedIndex > 0) {
			document.JUpload.jsClickUpload();
		} else {
			if (album_name.value == "") {
				alert("Missing Album Name!");
			} else {
				document.JUpload.jsClickUpload();
			}
		}
	}
}
function updateThis(t) {
	if (t.selectedIndex <= 0) {
		with (document.JUploadForm) {
			album_name.disabled = false;
			album_date.disabled = false;
			album_descr.disabled = false;
		}
	} else {
		with (document.JUploadForm) {
			album_name.disabled = true;
			album_date.disabled = true;
			album_descr.disabled = true;
		}
	}
}

function Show(d) {
	d.style.visibility = "visible";
	d.style.display = "block";
}

function Hide(d) {
	d.style.visibility = "hidden";
	d.style.display = "none";
}
var ifr = null;
var ec = null;
var sb = null;
var eb = null;
var ad = null;

function pickEdit(t) {
	if (!ifr || ifr == null)
		ifr = document.getElementById('ifrdata');
	if (!ec || ec == null)
		ec = document.getElementById('editContainer');
	if (t.selectedIndex <= 0) {
		ec.innerHTML = "";
		Hide(ec);
	} else {
		var id = t[t.selectedIndex].value;
		// get data about this
		ifr.src = "albumManager.php?subAction=getData&id="+id;
	}
}

function editImg(ind,i) {
	// check if current image, if any, needs saved
	if (currentImage != null && currentImage.changed) {
		if (alwaysSave) {
			currentImage.loadFromForm(true);
		} else {
			if (confirm("You have made changes to the current image.\nDo you want to save it?\n\"OK\" for yes.\n\"Cancel\" for no.")) {
				if (!askAlwaysSave) {
					if (confirm("Do you want me to always save the current image when picking another?")) {
						alwaysSave = true;
					}
					askAlwaysSave = true;
				}
				currentImage.loadFromForm(true);
			}
		}
	}
  currentAlbum.itemIndex = ind;
	ifr.src = "albumManager.php?subAction=getImgData&id="+i;
}

function _loadData() {
	currentImage = new Img();
	currentImage.setImg(arguments);
	if (currentImage.id > 0) {
		currentImage.loadPanel();
	}
//	$loadData.="{$r[id]},'".addslashes($r[filename])."',$r[filesize],'".addslashes($r[description])."','".urlencode($r[long_descr])."','{$r[added]}'}'";
}

function updateChange() {
	if (currentImage != null) {
		currentImage.setChanged();
	}
}

/* START: IMAGE CLASS */
function setImg(args) {
	this.id = args[0];
	this.filename = decodeURIComponent(args[1]);
	this.filesize = args[2];
	this.description = decodeURIComponent(args[3]);
	this.long_descr = decodeURIComponent(args[4]);
	this.added = args[5];
	this.file_type = args[6];
}

function SaveImage() {
	var srcifr = "albumManager.php?subAction=saveImage";
	srcifr = srcifr + "&id="+this.id+"&description="+encodeURIComponent(this.description)+"&long_descr="+encodeURIComponent(this.long_descr);
	ifr.src = srcifr;
	this.resetChanged();
}

function loadFromForm(save) {
	this.description = document.editImage.description.value;
	this.long_descr = document.editImage.long_descr.value;
	if (save)
		this.SaveImage();
}

function removeImage() {
  if (confirm("Are you sure you want to remove this file?")) {
    var srcifr = "albumManager.php?subAction=removeImage";
    srcifr = srcifr + "&id="+this.id;
  	ifr.src = srcifr;
  }
}

function _loadMsg(m,reloadPage,resetAlbum) {
	if (resetAlbum) {
		document.JUploadForm.album_id.selectedIndex = 0;
	}
	if (reloadPage) {
    alert(m);
    pickEdit(document.JUploadForm.album_id);
  } else {
    var em = document.getElementById('editMsg');
  	Show(em);
	  em.innerHTML = m + "<br/>";
  }
}

function loadPanel() {
	eb.innerHTML = "";
	var d = "<form name=\"editImage\" id=\"editImage\">\n";
	d = d + "<table width='100%'><tr><td align='center'>";
	d = d + "<img src=\"/thumbnails/"+this.id+"_320.jpg\">";
	d = d + "</td></tr></table>\n";
	d = d + "<fieldset>\n";
	d = d + "<div id=\"editMsg\"></div>";
	d = d + "File Name: " + this.filename;
	d = d + "</fieldset>\n";
//	d = d + "<fieldset>\n";
//	d = d + "File Size: " + this.filesize;
//	d = d + "</fieldset>\n";
	d = d + "<fieldset>\n";
	d = d + "<label for=\"description\"> Description:</label>\n";
	d = d + "<input id=\"description\" value=\""+this.description+"\" onchange=\"updateChange()\" onblur=\"updateChange()\"/>\n";
	d = d + "</fieldset>\n";
	d = d + "<fieldset>\n";
	d = d + "<label for=\"long_descr\"> Long Description:</label><br />\n";
	d = d + "<textarea id=\"long_descr\" cols=\"50\" rows=\"5\" onchange=\"updateChange()\" onblur=\"updateChange()\">"+this.long_descr+"</textarea>\n";
	d = d + "</fieldset>\n";
	d = d + "<fieldset>\n";
	d = d + "<input type=\"button\" id=\"saveButton\" value=\"Save\" onclick=\"currentImage.loadFromForm(true)\">";
  d = d + "&nbsp;<input type=\"button\" id=\"saveButton\" value=\"Save, >\" onclick=\"currentImage.loadFromForm(true);currentAlbum.jump(1);\">";
  d = d + "&nbsp;<input type=\"button\" id=\"saveButton\" value=\"Save, <\" onclick=\"currentImage.loadFromForm(true);currentAlbum.jump(-1);\">";
  d = d + "&nbsp;<input type=\"button\" id=\"removeButton\" value=\"Remove\" onclick=\"currentImage.removeImage();\">";
	if (this.file_type == "Image") {
		d = d + "&nbsp;<input type=\"button\" value=\"Rotate 90\" onclick=\"currentImage.rotate(90)\">";
  	d = d + "&nbsp;<input type=\"button\" value=\"Rotate -90\" onclick=\"currentImage.rotate(-90)\">";
	}
	d = d + "</fieldset>\n";
	d = d + "</form>";
	eb.innerHTML = d;
}

function setChanged() {
	this.changed = true;
}

function resetChanged() {
	this.changed = false;
}

function _reloadImage() {
  //var sb = document.getElementById('sideImg'+currentImage.id);
  var sb = null;
  var name = "/thumbnails/"+currentImage.id+"_100.jpg";
  var name2 = "/thumbnails/"+currentImage.id+"_320.jpg";
  for(i=0;i<document.images.length;i++) {
    if (document.images[i].src.indexOf(name) > 0) {
      document.images[i].src = "/thumbnails/"+currentImage.id+"_100.jpg?"+ Math.random();
    }
    if (document.images[i].src.indexOf(name2) > 0) {
      document.images[i].src = "/thumbnails/"+currentImage.id+"_320.jpg?"+ Math.random();
    }
  }
}

function rotate(i) {
  if (i == 90 || i == -90) {
    var srcifr = "albumManager.php?subAction=rotate&dir="+i+"&id=" +this.id;
    ifr.src = srcifr;
  }
}

function Img() {
	this.id = 0;
	this.filename = "";
	this.filesize = 0;
	this.description = "";
	this.long_descr = "";
	this.added = "";
	this.changed = false;
	this.file_type = "Image";
	
	// functions
	this.setImg = setImg;
	this.loadPanel = loadPanel;
	this.setChanged = setChanged;
	this.resetChanged = resetChanged;
	this.loadFromForm = loadFromForm;
	this.SaveImage = SaveImage;
  this.rotate = rotate;
  this.removeImage = removeImage;
}

/* END: IMAGE CLASS */


function loadPage() {
	if (!sb || sb == null)
		sb = document.getElementById('sideBar');
	if (!eb || eb == null)
		eb= document.getElementById('editBar');
	if (!ad || ad == null)
		ad = document.getElementById('albumDetails');
	
  eb.innerHTML = "";
  ad.innerHTML = "";
	
	ad.innerHTML = "";
	var d = "";
	d = "<form name=\"albumDetails\">\n";
	d = d + "<fieldset>\n";
	d = d + "<label for=\"descr\"> Description:</label>\n";
	d = d + "<input id=\"description\" size=\"50\" value=\""+this.description+"\"/>\n";
	d = d + "</fieldset>\n";
  d = d + "<fieldset>\n";
	d = d + "<label for=\"date\"> Date:</label>\n";
	d = d + "<input id=\"date\" size=\"10\" maxlength=\"10\" value=\""+this.date+"\"/> <small>(mm/dd/yyyy) or blank for no date</small>\n";
	d = d + "</fieldset>\n";
  d = d + "<fieldset>\n";
	d = d + "<label for=\"longDesc\"> Long Description:</label><br />\n";
	d = d + "<textarea cols=\"50\" rows=\"4\" id=\"longDesc\">"+this.longDesc+"</textarea>\n";
	d = d + "</fieldset>\n";
  d = d + "<fieldset>\n";
	d = d + "<label for=\"hidden\"> Hidden?</label> \n";
	d = d + "<input type=\"checkbox\" id=\"hidden\" value=\"Y\"";
  if (this.hidden == "Y") d = d + " checked";
  d = d + ">\n";
	d = d + "</fieldset>\n";
  d = d + "<fieldset>\n";
  d = d + "<input type=\"button\" value=\"Save Album Details\" onclick=\"currentAlbum.saveForm()\">\n";
	d = d + "&nbsp;<input type=\"button\" value=\"Empty Album\" onclick=\"currentAlbum.empty()\">\n";
	d = d + "&nbsp;<input type=\"button\" value=\"Obliterate Album\" onclick=\"currentAlbum.obliterate()\">\n";
  d = d + "</fieldset>\n";
	d = d + "</form><br/>";
	ad.innerHTML = d;
  
	sb.innerHTML = "";
	d="";
	for(i=0;i<this.items.length;i++) {
		it = this.items[i];
		//if (it.file_type == "Image") {
		d = d + "<img src=\"/thumbnails/"+it+"_100.jpg\" onclick=\"editImg("+i+","+it+")\" width=\"100\" alt=\"Image ID "+it+"\"><br/><br/>";
		//} else if (it.file_type == "Video") {
		//	d = d + "<img src=\"/image_main/wmp_icon.gif\" onclick=\"editImg("+i+","+it+")\"><br/>";
		//}
	}
	sb.innerHTML = d;
	Show(ec);
}

function loadImages(args) {
	// reset images
	this.items = new Array();
	for(i=0;i<args.length;i++) {
		this.items[i] = args[i];
	}
}
function setAlbum(args) {
	this.id = args[0];
	this.date = args[1];
	this.description = decodeURIComponent(args[2]);
	this.longDesc = decodeURIComponent(args[3]);
	this.hidden = args[4];
	this.sticky = args[5];
}
function getID() {
	return this.id;
}
function getItemsLength() {
	return this.items.length;
}

function jump(dir) {
  tmp = this.itemIndex;
  tmp+=dir;
  if (tmp == -1) tmp = this.getItemsLength()-1;
  else if (tmp == this.getItemsLength()) tmp = 0;
  
  this.itemIndex = tmp;
  editImg(this.itemIndex,this.items[this.itemIndex]); 
}

function saveForm() {
  this.description = document.albumDetails.description.value;
  this.date = document.albumDetails.date.value;
  this.longDesc = document.albumDetails.longDesc.value;
  if (document.albumDetails.hidden.checked)
    this.hidden = "Y";
  else
    this.hidden = "N";
  
  this.saveAlbum();
}

function empty() {
	if (confirm("Are you sure you want to empty all of the images from this album?")) {
		var srcifr = "albumManager.php?subAction=empty";
  	srcifr = srcifr + "&id="+this.id;
  	ifr.src = srcifr;
	}
}

function obliterate() {
	if (confirm("Are you sure you want to completely destroy this album and its contents?")) {
		var srcifr = "albumManager.php?subAction=obliterate";
  	srcifr = srcifr + "&id="+this.id;
  	ifr.src = srcifr;
	}
}

function saveAlbum() {
	var srcifr = "albumManager.php?subAction=saveAlbum";
	srcifr = srcifr + "&id="+this.id+"&description="+encodeURIComponent(this.description)+"&longDesc="+encodeURIComponent(this.longDesc);
  srcifr = srcifr + "&date="+encodeURIComponent(this.date)+"&hidden="+this.hidden;
	ifr.src = srcifr;
}

function Album() {
	this.id = 0;
	this.date = "";
	this.description = "";
	this.longDesc = "";
	this.hidden = "N";
	this.sticky = "N";
	this.items = new Array();
	
  this.itemIndex = 0;
  
	this.loadImages = loadImages;
	this.getID = getID;
	this.setAlbum = setAlbum;
	this.getItemsLength = getItemsLength;
	this.loadPage = loadPage;
  this.jump = jump;
  this.saveForm = saveForm;
  this.saveAlbum = saveAlbum;
	this.empty = empty;
	this.obliterate = obliterate;
}



function _loadAlbum() {
	currentAlbum = new Album();
	currentAlbum.setAlbum(arguments);
	if (currentAlbum.getID() > 0) {
		return true;
	} else {
		return false;
	}
}
function _loadImages() {
	if (currentAlbum.id > 0) {
		currentAlbum.loadImages(arguments);
		if (currentAlbum.getItemsLength() > 0) {
			currentAlbum.loadPage();
		}
	}
}


function setBandwidth(whichVal){
var expdate = new Date();
expdate.setTime(expdate.getTime() + (60*60*24*365));
document.cookie = 'setbandwidth=' + whichVal + '; expires=' + expdate.toGMTString() + '; path=/';
}
BCt=new Date();
BCs=BCt.getTime();
function bandwidthDetect() {
var args=bandwidthDetect.arguments;
var el=(document.layers)?document.layers['Layer1'].document.Myimage1:document.Myimage1;
if(el.complete){
BCt=new Date();
BCe=BCt.getTime();
BCd=(BCe-BCs)/1000;
if(document.cookie.indexOf('setbandwidth=1')>=0) {
location.href=args[1];}else
if(document.cookie.indexOf('setbandwidth=2')>=0) {
location.href=args[2];}else
if(BCd<args[0]){setBandwidth(1);location.href=args[1];}else
if(BCd>=args[0]){setBandwidth(2);self.location.href=args[2];}
}
}


