function saveSelection(newLoc){
	var selectedFiles = getFileSelection('media[]',true);
	var unselectedFiles = getFileSelection('media[]',false);
	if(selectedFiles==false){
		selectedFiles = '';
	}

	if(document.getElementById('saveSelectionOption').checked == true){
		$.ajax({
			type: "POST",
			url: "/member/ajax/media.sessionSelection.php",
			data: "action=set&files=" + selectedFiles + "&checkToRemove=" + unselectedFiles,
			success: function(newSelection){
				if(newLoc != 'null'){
					window.location = newLoc;
				}
				if(newSelection !=''){
					newSelection = newSelection.split(',');
					var totalSelected = newSelection.length;
				}else{
					var totalSelected = '0';
				}
				$("#totalFilesSelected").empty();
				$("#totalFilesSelected").append(totalSelected);
			}
		});
	}else{
		$("#totalFilesSelected").empty();
		$("#totalFilesSelected").append(selectedFiles.length);

		//WIPE OUT SAVED SELECTION
		$.ajax({
			url: "/member/ajax/media.sessionSelection.php?action=clear",
			cache: false,
			success: function(result){
				if(newLoc != 'null'){
					window.location = newLoc;
				}
			}
		});
	}
}

function getSelectedAlbum(){
	var docForms = document.forms;
	for(var i = 0; i < docForms.length; i++){
		var inputFields = docForms[i].elements;
		for(var ii = 0; ii < inputFields.length; ii++){
			if(inputFields[ii].name=='albumID' && inputFields[ii].type=='radio'){
				if(inputFields[ii].checked==true){
					return inputFields[ii].value;
				}
			}
		}
	}
	return false;
}
function deleteAlbum(albumID, placementID){
	if(albumID){
		var confirmed = confirm('Are you sure you want to delete this album and all its contents including subalbums.');
		if(confirmed){
			$.ajax({
				type: "POST",
				url: "/member/ajax/media.delete.php",
				data: "albumID=" + albumID,
				success: function(msg){
					alert(msg);
					createMoveAbleThumbs();
					return msg;
				}
			});
			if(placementID){
				vanish(placementID);
			}
		}
		return; 
	}else{
		alert('No album selected to delete.');
	}
}
function deleteSelectedFiles(){
	var pageFiles = getFileSelection('media[]',true);
	if(pageFiles==false){ pageFiles =''; }
	
	if($("#saveSelectionOption").attr("checked") == true){
		$.ajax({
			url: "/member/ajax/media.sessionSelection.php?action=show",
			cache: false,
			success: function(sessionFiles){
				var curSelection = '';
				if(pageFiles){ curSelection = pageFiles; }
				if(sessionFiles){
					if(curSelection != ''){
						curSelection = curSelection + ',' + sessionFiles;
					}else{
						curSelection = sessionFiles;
					}
				}
				//check for dups
				if(curSelection != ''){
					curSelection = curSelection.toString();
					var ids = curSelection.split(',');
					curSelection = uniqueArray(ids);
				}
				GetToIt(curSelection);
			}
		});

	}else{
		GetToIt(pageFiles);
	}

	function GetToIt(files){
		var timeDown = 0;
		if(files){
			var confirmed = confirm('Are you sure you want to delete the selected files?');
			if(confirmed){
				for(var i = 0; i < files.length; i++){
					timeDown = timeDown+100;
					setTimeout("vanish(" + files[i] + ");",timeDown);
				}
				$.ajax({
				   type: "POST",
				   url: "/member/ajax/media.delete.php",
				   data: "files=" + files,
				   success: function(msg){
					if(msg=='SUCCESS'){

					}
					return msg;
				   }
				});
			}
		}else{
			alert('No files seletected to delete.');
		}
	}
}
function deleteFile(mediaID, placement, placementID, fileType, protect){
	if(protect){
		var confirmed = confirm('Are you sure you want to delete this ' + fileType);
	}else{
		confirmed = true;
	}
	if(confirmed){
		$.ajax({
		   type: "POST",
		   url: "/member/ajax/media.delete.php",
		   data: "mediaID=" + mediaID,
		   success: function(msg){
			if(msg=='SUCCESS'){
				if(placement=='display' && document.getElementById('nextMedia') ){
					window.location.href = winLocation + '?mediaID=' + document.getElementById('nextMedia').value;
				}else if(placement=='display' && document.getElementById('prevMedia') ){
					window.location.href = winLocation + '?mediaID=' + document.getElementById('prevMedia').value;
				}else if(placement=='display'){
					window.location.href = winLocation + '?albumID=' + document.getElementById('albumID').value;
				}
			}
			return msg;
		   }
		});
		if(placement=='thumbnail' && placementID){
			vanish(mediaID);
		}
	}
	return;   
}
function emptyRecycleBin(){
	var confirmed = confirm('Are you sure you want to permantly delete all files in the recycle bin.');
	if(confirmed){
		$.ajax({
		   type: "POST",
		   url: "/member/ajax/media.emptyRecycleBin.php",
		   data: "doit=true",
		   success: function(msg){
			if(msg == 'SUCCESS'){
				alert("All files in your recycle bin have been permantly deleted.");
				window.location.href = '/member/recycle_bin.php' ;
			}
			return msg;
		   }
		});
	}
}
function restoreSelectedFiles(){
	var timeDown = 0;
	if(files = getFileSelection('media[]',true)){
		for(var i = 0; i < files.length; i++){
			timeDown = timeDown+100;
			setTimeout("restoreFile(" + files[i] + ", 'thumbnail', " + files[i] + ", 'media');",timeDown);
		}
	}else{
		alert('No files seletected to restore.');
	}
}
function restoreFile(mediaID, placement, placementID, fileType){
	$.ajax({
	   type: "POST",
	   url: "/member/ajax/media.restoreFile.php",
	   data: "mediaID=" + mediaID,
	   success: function(msg){
		return msg;
	   }
	});
	if(placement=='thumbnail' && placementID){
		flyAway(mediaID);
	}
}
function dropFileInAlbum(ev, ui) {
	//alert(ev.toSource());
	//alert(ui.toSource());
	var dropedOn = ev.id;
	var albumID = dropedOn.replace('a','');
	var dropedItem = ui.draggable[0].id;

	var dropedItemPos = getElementPosition(dropedItem);
	var dropedOnPos = getElementPosition(dropedOn);
	var moveToX = parseInt(dropedOnPos.left+50) - parseInt(dropedItemPos.left);
	var moveToY = parseInt(dropedOnPos.top+40) - parseInt(dropedItemPos.top);
	//alert(dropedItem + ' was droped on ' + dropedOn);
	document.getElementById(dropedItem).style.position = 'relative';
	$("#" + dropedItem).animate({ 
		top: moveToY + "px",
		left: moveToX + "px",
		opacity: 0.4,
		height: "20px",
		width: "20px"
	}, 600,'',function(){ $("#" + dropedItem).remove(); moveFileToAlbum(dropedItem,albumID); });
}
function dropAlbumInAlbum(album, targetID) { //album, targetID(this)
	//alert('dropAlbumInAlbum');
	var album = album.draggable[0].id;
	var albumID = album.replace('a','');
	var target = targetID.id;
	var targetID = target.replace('a','');
	if(!album || !target){
		alert('Unable to move album.');
		return;
	}
	
	var dropedItemPos = getElementPosition(album);
	var dropedOnPos = getElementPosition(target);
	var moveToX = parseInt(dropedOnPos.left+50) - parseInt(dropedItemPos.left);
	var moveToY = parseInt(dropedOnPos.top+40) - parseInt(dropedItemPos.top);
	//alert(dropedItem + ' was droped on ' + dropedOn);
	document.getElementById(album).style.position = 'relative';
	$("#" + album).animate({ 
		top: moveToY + "px",
		left: moveToX + "px",
		opacity: 0.4,
		height: "20px",
		width: "20px"
	}, 600,'',function(){ 
				$("#" + album).remove(); 
				moveAlbumToAlbum(albumID,targetID);
			});
}

function moveAlbum(albumID){
	if(albumID){
		window.location.href = '/member/media.php?albumID=' + albumID + '&action=moveAlbum';
	}else{
		alert('No album is selected to move.');
	}
}
function renameAlbum(albumID){
	if(albumID){
		window.location.href = '/member/media.php?albumID=' + albumID + '&action=renameAlbum';
	}else{
		alert('No album is selected to rename.');
	}
}
function shareAlbum(albumID){
	if(albumID){
		$.ajax({
			type: "POST",
			url: "/member/ajax/sharing.addToShare.php",
			data: "albumID=" + albumID,
			success: function(msg){
				if(msg=='SUCCESS'){
					window.location.href='/member/sharing/create_share.php';
				} else if (msg=='NONE'){
					alert('This album does not contain any files.');
				} else {
					alert('An unexpected error happened. Could not share selected files.');
				}
				return msg;
			}
		});
	}else{
		alert('There is not an album selected to share.');
	}
}
function shareSelectedFiles(){
	if($("#saveSelectionOption").attr("checked") == true){
		var urlLoc = "/member/ajax/sharing.addToShare.php?useSession=true";
		var sessionShare=true;
	}else{
		var urlLoc = "/member/ajax/sharing.addToShare.php";
	}
	var curSelection = getFileSelection('media[]',true);
	if(curSelection==false){ curSelection=''; }
	if(curSelection.length>0 || sessionShare==true){
		$.ajax({
			type: "POST",
			url: urlLoc,
			data: "shareIDs=" + curSelection,
			success: function(msg){
				if(msg=='SUCCESS'){
					window.location.href='/member/sharing/create_share.php';
				}else if(msg=='NONE'){
					alert('No files selected to share');
				}else{
					alert('An unexpected error happened. Could not share selected files.');
				}
				return msg;
			}
		});
	}else{
		alert('No files selected to share');
	}
}
function shareFile(mediaID){
	$.ajax({
		type: "POST",
		url: "/member/ajax/sharing.addToShare.php",
		data: "shareIDs=" + mediaID,
		success: function(msg){
			if(msg=='SUCCESS'){
				window.location.href='/member/sharing/create_share.php';
			}else{
				alert('An unexpected error happened. Could not share selected files.');
			}
			return msg;
		}
	});
}
function removeSelectedFromFav(){
	var timeDown = 0;
	if(files = getFileSelection('media[]',true)){
		for(i=0; i<files.length; i++){
			timeDown = timeDown +150;
			setTimeout("addToFavorite(" + files[i] + ", 'favorite', " + files[i] + ", 'remove')",timeDown);
		}
	}else{
		alert('No files selected to share');
	}
}
function addToFavorite(mediaID, location, otherID, action){
  $.ajax({
   type: "POST",
   url: "/member/ajax/media.addtofavorite.php",
   data: "mediaID=" + mediaID + "&action=" + action,
   success: function(msg){
	if(msg=='Success'){
	  if(action=='add' && location=='thumbnail'){
		var newHtml = document.getElementById(otherID).innerHTML;
		var newHtml = newHtml + '<div id="imgFavorite"><img src="/images/icons/favorite_ico.png" title="In your favorites" /></div>';
		document.getElementById(otherID).innerHTML = newHtml;
	  }else if(action=='add' && location=='display'){
		document.getElementById(otherID).innerHTML = 'File added to favorites.';
		document.getElementById(otherID).style.color = '#090';
	  }else if(location=='favorite' && otherID){
		vanish(otherID);
	  }
	  
	}
	return msg;
   }
  });
	
}
function playSlideshow(albumID, orderby){
  /*
 var arVersion = navigator.appVersion.split("MSIE");
 var version = parseFloat(arVersion[1]);
 if(version > 5 && version < 7){
	javascript:popup('/member/slideshow.php?albumID=' + albumID + '&orderby=' + orderby,'slideshow','titlebar=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=610,height=530');
 }else{
}
*/ 
	if(document.getElementById('flashcontent')){
		document.getElementById('flashcontent').style.display = 'none';
	}
	$('<div id="dimmer"></div>').appendTo("body");
	$('<div id="slideShowContainer"></div>').appendTo("body");
	$('<div id="slideShow"></div>').appendTo("#slideShowContainer");
	document.getElementById('bodyTag').style.overflow = 'hidden';
	saveScrollPos(); //save scroll position
	window.scroll(0,0);
	$("#slideShow").animate({width:"80%"}, 1000, '', 
		function(){ $("#slideShow").animate( { height:"100%", top:"0%" }, 1000, '', 
			function(){ 
				var divHtml = '<div id="slideShowClose" onclick="closeSlideshow();">X</a></div>\n';
				divHtml = divHtml + '<div id="slideshowPlayer"><iframe src="/member/slideshow.php?albumID=' + albumID  + '&orderby=' + orderby  + '" style="height: 100%; width: 100%;" frameborder="0"></iframe></div>\n';	
				$(divHtml).appendTo("#slideShow");
				$("#mainBody,#content,#contentFull").css("display","none");
			}
		); 
	});
}
function closeSlideshow(){
	$("#mainBody,#content,#contentFull").css("display","block");
	$("#slideShowContainer").empty();
	$("#slideShowContainer").remove();
	$("#dimmer").empty();
	$("#dimmer").remove();
	document.getElementById('bodyTag').style.overflow = '';
	window.scroll(0,scrollSave);
	if(document.getElementById('flashcontent')){
		document.getElementById('flashcontent').style.display = 'block';
	}

}
function localEditor(mediaID, app){ 
	document.getElementById('bodyTag').style.overflow = 'hidden';
	saveScrollPos(); //save scroll position
	window.scroll(0,0);
	$("body").append('<div id="dimmer"></div>');
	$("body").append('<div id="editorContainer"></div>');
	$("#editorContainer").append('<div id="editorWindow"></div>');
	$("#editorWindow").animate({width:"750px"}, 1000, '', 
		function(){ $("#editorWindow").animate( { height:"600px", top:"0px" }, 1000, '', 
			function(){ 
				$("#editorWindow").append('<div class="closeX" style="position: absolute; top: 0px; right: 0px;" onclick="closeEditor();">X</div>\n');
				$("#editorWindow").append('<iframe src="/photoeditor/index.php?photoID=' + mediaID + '&action=' + app + '" frameborder="0" width="100%" height="100%"></iframe>\n');
			}
		); 
	});
}
function picnikEditor(mediaID){
	$.ajax({
	   type: "POST",
	   url: "/member/ajax/media.picnikUrl.php",
	   data: "mediaID=" + mediaID,
	   success: function(url){
		if(url!='FAILED'){
			loadEditor(url);
		}else{
			alert("Photo editor could not load.");
		}
	   }
	});
}
function loadEditor(url){
	document.getElementById('bodyTag').style.overflow = 'hidden';
	saveScrollPos(); //save scroll position
	window.scroll(0,0);
	$("body").append('<div id="dimmer"></div>');
	$("body").append('<div id="editorContainer"></div>');
	$("#editorContainer").append('<div id="editorWindow"></div>');
	$("#editorWindow").animate({width:"90%"}, 800, '', 
		function(){ $("#editorWindow").animate( { height:"100%", top:"0%" },800, '', 
			function(){ 
				$("#mainBody,#content,#contentFull").css("display","none");
				var divHtml = '<div style="position: absolute; width: 10%; height: 24px; left: 90%; top: -25px;"><div style="position: relative; left: 1px; top: -1px; height: 24px; background: #54700f; border: 1px solid #999; border-bottom: 0px; color: #fff; cursor: pointer;" onclick="closeEditor();"><b>Close X</b></div></div>\n';
				divHtml = divHtml + '<iframe src="' + url + '" frameborder="0" width="100%" height="100%"></iframe>\n';
				$(divHtml).appendTo("#editorWindow");
			}
		); 
	});
}
function closeEditor(){
	$('#dimmer').remove();
	$('#editorContainer').empty();
	$('#editorContainer').remove();
	$("#mainBody,#content,#contentFull").css("display","block");
}
function doneEditing(url){
	$("#slideShowContainer").empty();
	$("#slideShowContainer").remove();
	$("#dimmer").empty();
	$("#dimmer").remove();
	window.location.href = url;

}
function collectInfoValues(){
	var fieldValues = document.forms['infoForm'].elements;
	var returnValue = '';
	for(i=0; i<fieldValues.length; i++){
		if (fieldValues[i].type=='text' || fieldValues[i].type=='hidden' || fieldValues[i].type=='textarea') {
			if(returnValue == ''){
				returnValue = fieldValues[i].value;
			} else {
				returnValue = returnValue + '|~|' + fieldValues[i].value;
			}
		}
	}
	return returnValue;
}
function collectInfo(collection,returnTo){
	var returnValue = '';
	saveScrollPos(); //save scroll position
	window.scroll(0,0);
	$('<div id="dimmer"></div>').appendTo("#bodyTag");
	$('<div id="infoContainer" style="z-index: 101; position: absolute; top: 25%; width:100%; text-align: center;"></div>').appendTo("#bodyTag");
	$('<div id="infoCollector" style="font-size: 12px; font-weight: bold; padding: 5px; text-align:center; border: 2px solid #999; background:#fff; margin: 0px auto 0px auto; width:350px;"></div>').appendTo("#infoContainer");
	var infoHtml = '<form name="infoForm" onsubmit="' + returnTo + '( collectInfoValues() );return false;" >';
	var inputFields = collection.split(',');
	for(i=0; i<inputFields.length; i++){
		var field = inputFields[i].split('|');
		if(!field[2]){ field[2] = ''; }
		
		if(field[1] != 'hidden'){infoHtml = infoHtml + '<div style="margin: 2px;">' + field[0] + '</div><div style="margin: 2px;">';}
		if(field[1]=='textarea'){
			infoHtml = infoHtml + '<textarea cols="30" rows="4" name="fieldName[]">'+ field[2] +'</textarea>';
		}else if(field[1]=='hidden'){
			infoHtml = infoHtml + '<input type="hidden" name="fieldName[]" value="'+ field[2] +'" />';
		}else{
			infoHtml = infoHtml + '<input type="text" size="40" name="fieldName[]" value="'+ field[2] +'" />';
		}
	}
	infoHtml = infoHtml + '</div><div style="margin: 2px;"><input type="button" value="Continue" ';
	infoHtml = infoHtml + 'onclick="' + returnTo + '( collectInfoValues() );" ';
	infoHtml = infoHtml + '/>';
	infoHtml = infoHtml + '<input type="button" value="Cancel" onclick="closeCollector();" /></div></form>';
	$(infoHtml).appendTo("#infoCollector");
	pageSetup();
	var fieldValues = document.forms['infoForm'].elements;
	fieldValues[0].focus();
	
}
function closeCollector(){
	$("#infoContainer").empty();
	$("#infoContainer").remove();
	$("#dimmer").empty();
	$("#dimmer").remove();
	window.scroll(0,scrollSave);
}

function createAlbum(fieldValues){
	var albumInfo = fieldValues.split('|~|');
	if(albumInfo[0].length > 2){
		 $.ajax({
		   type: "POST",
		   url: "/member/ajax/media.createAlbum.php",
		   data: "albumName=" + albumInfo[0] + "&albumDes=" + albumInfo[1],
		   success: function(newID){
			if(newID != 'FAILED'){
				$(newID).insertAfter("#albumDivider");
				$("#noAlbum").remove();
				createMoveAbleThumbs();
				return newID;
			}else{
				alert('Failed To Create Album For an Unknow Reason');
			}
		   }
		});
		closeCollector();
	}else{
		alert('Error you must provide an album name with 3 or more characters.');
	}
}
function createSubAlbum(fieldValues){
	var albumInfo = fieldValues.split('|~|');
	var albumID = document.getElementById('albumID').value;
	if(albumInfo[0].length > 2){
		 $.ajax({
		   type: "POST",
		   url: "/member/ajax/media.createAlbum.php",
		   data: "albumName=" + albumInfo[0] + "&albumDes=" + albumInfo[1] + "&inAlbum=" + albumID,
		   success: function(newID){
			if(newID != 'FAILED'){
				$(newID).insertAfter(".parentDir,.homeDir");
				$("#noAlbum").remove();
				createMoveAbleThumbs();
			}
			return newID;
		   }
		});
		closeCollector();
	}else{
		alert('Error you must provide an album name with 3 or more characters.');
	}
}
function moveFileToAlbum(mediaID,albumID){
	$.ajax({
		type: "POST",
		url: "/member/ajax/media.moveFileTo.php",
		data: "albumID=" + albumID + '&mediaID=' + mediaID,
		success: function(msg){
			if(msg == 'SUCCESS'){
				//alert('file has been moved');
			}
			return;
		}
	});
}
function moveAlbumToAlbum(albumID,targetID){
	$.ajax({
		type: "POST",
		url: "/member/ajax/media.moveAlbumTo.php",
		data: "albumID=" + albumID + '&targetID=' + targetID,
		success: function(msg){
			if(msg == 'SUCCESS'){
				//alert('album has been moved');
			}
			return;
		}
	});
}
function moveSelectedFiles(){
	var pageFiles = getFileSelection('media[]',true);
	if(pageFiles==false){ pageFiles =''; }
	
	if($("#saveSelectionOption").attr("checked") == true){
		$.ajax({
			url: "/member/ajax/media.sessionSelection.php?action=show",
			cache: false,
			success: function(sessionFiles){
				var curSelection = '';
				if(pageFiles){ curSelection = pageFiles; }
				if(sessionFiles){
					if(curSelection != ''){
						curSelection = curSelection + ',' + sessionFiles;
					}else{
						curSelection = sessionFiles;
					}
				}
				//check for dups
				if(curSelection != ''){
					curSelection = curSelection.toString();
					var ids = curSelection.split(',');
					curSelection = uniqueArray(ids);
				}
				GetToIt(curSelection);
			}
		});

	}else{
		GetToIt(pageFiles);
	}
	
	function GetToIt(files){
		//alert('Move IDs: ' + files);
		if(files){
			saveScrollPos(); //save scroll position
			window.scroll(0,0);
			$('<div id="dimmer"></div>').appendTo("#bodyTag");
			$('<div id="infoContainer" style="z-index: 101; position: absolute; top: 15%; width:100%; text-align: center;"></div>').appendTo("#bodyTag");
			$('<div id="infoCollector" style="font-size: 12px; font-weight: bold; padding: 5px; text-align: left; border: 2px solid #999; background:#fff; margin: 0px auto 0px auto; width:410px;"></div>').appendTo("#infoContainer");
			$.ajax({
				type: "POST",
				url: "/member/ajax/media.move.form.php",
				data: "files=" + files + '&albumID=' + document.getElementById('albumID').value,
				success: function(albumList){
					if(albumList != 'FAILED'){
						$(albumList).appendTo("#infoCollector");
						pageSetup();
					}else{
						alert('An UnExpected Error has occured');
					}
				}
			});
		}else{
			alert('No files were selected to move.');
		}
	}
}

function moveFilesTo(albumID, files){
	closeMover();
	if(document.getElementById('albumID')){
		if(document.getElementById('albumID').value == albumID){
			return;
		}
	}
	var amount = 0;
	files = files.split(',');
	var timeDown = 0;
	if(albumID && files){
		for(var i = 0; i < files.length; i++){
			var mediaID = files[i];
			$.ajax({
				type: "POST",
				url: "/member/ajax/media.moveFileTo.php",
				data: "albumID=" + albumID + '&mediaID=' + mediaID,
				success: function(msg){
					if(msg == 'SUCCESS'){
						amount++;
					}
					return;
				}
			});
			timeDown = timeDown +150;
			setTimeout("flyAway('" + mediaID + "')",timeDown);
		}
		
	}else{
		alert('An error has occured');
	}
}
function closeMover(){
	$("#infoContainer").empty();
	$("#infoContainer").remove();
	$("#dimmer").empty();
	$("#dimmer").remove();
	window.scroll(0,scrollSave);
}
function flyAway(id){
	var divID = "#" + id;
	if(document.getElementById(id)){
		document.getElementById(id).style.position = 'relative';
		$(divID).animate( {top:"-750px", left:"-750px", height:"10px", width:"10px" }, 600, '', 
			function(){ 
				$(divID).remove();
			}
		);
	}
}
function vanish(id){
	var divID = "#" + id;
	if(document.getElementById(id)){
		$(divID).animate( { height:"5px", width:"5px", opacity:"0" }, 600, '', 
			function(){ 
				$(divID).remove();
			}
		);
	}
}
function createMoveAbleThumbs(){
  if(($(".thumbnail").length > 0 || $(".album").length > 0) 
	&& ($(".album").length > 0 || $(".parentDir").length > 0 || $(".homeDir").length > 0) ){
	
	$(".thumbnail,.album,.albumDisplay").draggable( "destroy" );
	$(".album,.homeDir,.parentDir").droppable( "destroy" );
	
	//ALBUM DRAG ABLE
	$(".albumDisplay").hover(
      function () {
        $(this).parent().draggable({
			helper: 'clone',
			opacity: 0.5,
			zIndex: 105
		});
      }, 
      function () {
        $(this).parent().draggable( "destroy" );
      }
    );

	//thumbnail draggable
	$(".thumbnail").hover(
      function () {
		//set draggable function
		$(this).draggable({
			helper: 'clone',
			opacity: 0.5,
			zIndex: 105
		});
      }, 
      function () {
        $(this).draggable( "destroy" );
      }
    );
	
	//ALBUM DROPABLE
	$(".album,.parentDir").droppable({
		accept: ".thumbnail, .album",
		activeClass: 'dropActive',
		hoverClass: 'dropHover',
		drop: function(ev, ui) { 
			var elemClass = ui.draggable[0].className;
			if(elemClass.search('album')>-1){
				dropAlbumInAlbum(ui,this); //albumID, targetID
			} else {
				dropFileInAlbum(this, ui);	
			}	   
		} 
	});

	//HOMEDIR DROPABLE
	$(".homeDir").droppable({
		accept: ".album",
		activeClass: 'dropActive',
		hoverClass: 'dropHover',
		drop: function(ev, ui) { 
			var elemClass = ui.draggable[0].className;
			if(elemClass.search('album')>-1){
				dropAlbumInAlbum(ui,this); //albumID, targetID
			} else {
				//dropFileInAlbum(this, ui);	
			}	   
			//dropAlbumInAlbum(ui,this); //albumID, targetID
	    } 
	});
  }
}
//send a comment
function sendComment(fieldValues){
	if(fieldValues){
		fields = fieldValues.split('|~|');
		if(fields[0] && fields[1] && fields[2] && fields[3]){
			closeCollector();
			startProgressBar('Please Wait Copying File');
			$.ajax({
				type: "POST",
				url: "/member/ajax/sendMessage.php",
				data: "sendTo=" + fields[0] + '&subject=' + fields[1] + '&message=' + fields[2] + '&from=' + fields[3],
				success: function(result){
					if(result == 'SUCCESS'){
						closeProgressBar('<span style="color: #090;">Message has been Sent.</span>');
					}else{
						closeProgressBar('<span style="color: #900;">Failed to send message technical errors.</span>');
					}
				}
			});
		}else{
			alert("Connot send message. Please fill in the subject, message and from fields.");
		}
	}else{
		alert('Error processing request.');
	}
	return;
}
//send to mobile phone function
function sendToPhone(mediaID){
	$.ajax({
		url: "/member/ajax/media.sendToPhone.form.php?mediaID=" + mediaID,
		success: function(html){
			if(html.search('FAILED') > -1){
				alert('Unexpected Error');
			}else{
				saveScrollPos(); //save scroll position
				window.scroll(0,0);
				$("body").append('<div id="dimmer"></div>');
				$("body").append('<div id="infoContainer" style="z-index: 101; position: absolute; top: 15%; width:100%; text-align: center;"></div>');
				$("#infoContainer").append('<div id="messageBox" style="font-size: 14px; padding: 5px; text-align: left; border: 2px solid #999; background:#fff; margin: 0px auto 0px auto; width: 500px;"></div>');
				$("#messageBox").append(html);
				pageSetup();
			}
		}
	});
}
function sendPhoto(){
	//validate form
	var mediaID = document.forms['phoneForm'].elements['mediaID'].value;
	var mobileID = document.forms['phoneForm'].elements['mobileID'].value;
	var message = document.forms['phoneForm'].elements['message'].value;
	if(mediaID && mobileID){
		$("#infoContainer").empty();
		$("#infoContainer").remove();
		startProgressBar('Sending Photo Please Wait!');
		$.ajax({
			type: "POST",
			url: "/member/ajax/media.sendToPhone.php",
			data: "mediaID=" + mediaID + '&mobileID=' + mobileID + '&message=' + message,
			success: function(result){
				if(result.search('FAILED') > -1){
					closeProgressBar('<span style="color: #900;">Failed to send photo technical errors.</span>');
				}else{
					closeProgressBar('<span style="color: #090;">Photo has been Sent.</span>');
				}
			}
		});
	}else{
		alert('An unexpected error happened');
		cancelSendPhoto();
	}

}
function cancelSendPhoto(){
	$("#infoContainer").empty();
	$("#infoContainer").remove();
	$("#dimmer").empty();
	$("#dimmer").remove();
	window.scroll(0,scrollSave);
}
//order prints
function orderPrints(){
	$.ajax({
		url: "/member/ajax/media.sessionSelection.php?action=show",
		success: function(curSelection){
			var selection = curSelection.split(',');
			if(selection.length>0){
				window.location = '/shop/prints.php?orderPrints=true&fromAlbum=true';
			}else{
				alert('No files selected.');
			}
			
		}
	});
}

$(document).ready(function(){
   createMoveAbleThumbs();
   
   if(document.getElementById('saveSelectionOption')){
	   $(":checkbox").click(function () { 
	      saveSelection('null'); 
	    });
		$("#saveSelectionOption").unbind();
	}
});

