// JavaScript Document
var currentThumb = 1; 
var albumThumb = 1;
var currentPic;
window.onload = function(){
	currentPic = document.getElementById('currentPic').value;
};

var zoomState = false;
function zoom(){
	var h; var w;
	if(zoomState){
		h = document.getElementById("theImage").height;
		w = document.getElementById("theImage").width;
		h = Number(h)/2;
		w = Number(w)/2;
		document.getElementById("theImage").height=h;
		document.getElementById("theImage").width=w;
		document.getElementById('zoomer').innerHTML = "+ Zoom in";
		zoomState = false;
	}else{
		h = document.getElementById("theImage").height;
		w = document.getElementById("theImage").width;
		h = Number(h)*2;
		w = Number(w)*2;
		document.getElementById("theImage").height=h;
		document.getElementById("theImage").width=w;
		document.getElementById('zoomer').innerHTML = "- Zoom out";
		zoomState = true;
	}
}	

//changes the currently displayed photo to the one corresponding to the thumbnail that was just clicked on.
function change(elmnt, des){
	document.getElementById('imgBox').innerHTML = "<img src=\"photoGallery/"+elmnt+"\" id=\"theImage\" alt=\"\" />";	
	document.getElementById('picDescript').innerHTML = des;
	currentPic = elmnt;
}
//changes the album to another one
function changeAlbum(album){
	document.getElementById('albumNameField').value = album;
	document.getElementById('albumTitle').innerHTML = album;
	
	pg.switchAlbumPictures(album, function (result) { 
		try{
			var info = YAHOO.lang.JSON.parse(result);
		}catch(e){
			alert('Invalid album info');
		}
		if(info.thumbs != "null"){
			$get('thumbs').innerHTML = info.thumbs; 

			if (info.pic != null){
				$get('imgBox').innerHTML = '<img src="photoGallery/'+info.pic+'" id="theImage" alt="'+info.description+'" />'; 
			}else{
				$get('imgBox').innerHTML = '&nbsp;'; 
			}
			$get('picDescript').innerHTML = info.description;
			currentPic = info.pic;
			currentThumb = 1;
		}
	});
}
//skips the currently displayed picture in the specified direction.
function skip_click(dir) {
	var albumName = document.getElementById('albumNameField').value;
    skip.skipFunc(dir, currentPic, albumName, function (result) { 
		try{
			var info = YAHOO.lang.JSON.parse(result);
		}catch(e){
			alert('Invalid Picture Date!');
		}
		if(info.pic != "null"){
			$get('imgBox').innerHTML = '<img src="photoGallery/'+info.pic+'" id="theImage" alt="'+info.description+'" />'; 
			$get('picDescript').innerHTML = info.description;
			currentPic = info.pic;
		}
	});
}

//Skips the currently displayed thumbnails in the specified direction in groups of 5
function pg_click(dir) {
	var albumName = document.getElementById('albumNameField').value;
	pg.pgFunc(dir, currentThumb, albumName, function (result) { 
		if(result != "null"){
			$get('thumbs').innerHTML = result; 
			if(dir == "left"){
				currentThumb = currentThumb - 1;
			}else{
				currentThumb = currentThumb + 1;
			}
		}
	});
}
//Skips the currently displayed album thumbnails in the specified direction in groups of 5
function ab_click(dir) {	
	pg.abFunc(dir, albumThumb, function (result) { 
		if(result != "null"){
			$get('albums').innerHTML = result; 
			if(dir == "left"){
				currentThumb = albumThumb - 1;
			}else{
				currentThumb = albumThumb + 1;
			}
		}
	});
}
function checkPhotoFile(){
	var ext = document.getElementById('photo').value.split('.'); 
	if((ext[1] != 'jpg') && (ext[1] != 'jpeg') && (ext[1] != 'JPEG') && (ext[1] != 'JPG')){
		alert('Only files of type JPEG are allowed.');
		document.getElementById('photo').value='';
	}
}
function checkPhotoSubmit(){
	var ext = document.getElementById('photo').value.split('.'); 
	if(document.getElementById('photo').value == ''){
		alert('Please select a photo before submitting');
		return false;
	}else if((ext[1] != 'jpg') && (ext[1] != 'jpeg') && (ext[1] != 'JPEG') && (ext[1] != 'JPG')){
		alert('Only files of type JPEG are allowed.');
		document.getElementById('photo').value='';
		return false;
	}
}
function selectEdit(){	
	var pick = document.getElementById('editPhotoSelect').value;
	var albumpicker = document.getElementById('editPhotoAlbum');
	var editbutton = document.getElementById('editButton');
	var descript = document.getElementById('editDescription');
	var preview = document.getElementById('editPhotoPreview');			
	
	if (pick != ''){
		albumpicker.disabled = false;
		editbutton.disabled = false;
		YAHOO.util.Dom.removeClass('editPhotoAlbum','disabled');
		YAHOO.util.Dom.removeClass('editButton','disabled');		
		preview.innerHTML = '<img src="photoGallery/thumbs/'+pick+'" />';
		pg.editChoice(pick, function (result){
			//alert(result);
			try { 				
			    var choice = YAHOO.lang.JSON.parse(result);
			} 
			catch (e) { 
			    alert("Invalid data"); 
			}
			albumpicker.value = choice.album;
			
			//set textarea which is a tinymce editor
			tinyMCE.execCommand('mceFocus',false,descript.id);
			tinyMCE.activeEditor.setContent(stripslashes(choice.description));
			descript.value = stripslashes(choice.description);
		});
		
	}else{
		albumpicker.disabled = true;
		editbutton.disabled = true;
		YAHOO.util.Dom.addClass('editPhotoAlbum','disabled');
		YAHOO.util.Dom.addClass('editButton','disabled');
		preview.innerHTML = '';
	}	
	document.getElementById('editChoice').value = pick;
}
function deleting(){
	if (document.getElementById('editPhotoAlbum').value == '') return false;
	return confirm('Do you want to delete this?');
}
function editing(){
	if (document.getElementById('editChoice').value == '') return false;
	return confirm('Save changes to this photo\'s info?');
}
function addNewAlbum(){
	if (document.getElementById('newAlbum').value == '') return false;
	return confirm('Create New Album?');
}
function selectAlbum(){
	var pick = document.getElementById('albumSelect').value;
	var albumfield = document.getElementById('editAlbumChoice');
	var preview = document.getElementById('oldThumbPreview');
	var albumtext = document.getElementById('changedAlbumName');
	var thumbChoices = document.getElementById('editAlbumThumb');
	var newpreview = document.getElementById('newThumbPreview');
	var editbutton = document.getElementById('editAlbumButton');
	
	if (pick != ''){
		albumfield.value = pick;
		albumtext.value = pick;
		pg.selectAlbum(pick, function(result){
			try { 				
			    var album = YAHOO.lang.JSON.parse(result);
			} 
			catch (e) { 
			    alert("Invalid data"); 
			}
			preview.innerHTML = album.thumb;
			thumbChoices.innerHTML = album.choices;
			newpreview.innerHTML = '';
			editbutton.disabled = false;
			YAHOO.util.Dom.removeClass(editbutton.id, 'disabled');
		});
		//preview.innerHTML = 'Old Thumb'
	}else{
		albumfield.value = '';
		albumtext.value = '';
		preview.innerHTML = '';
		newpreview.innerHTML = '';
		editbutton.disabled = true;
		YAHOO.util.Dom.addClass(editbutton.id, 'disabled');
	}
}
function selectNewThumb(){
	var preview = document.getElementById('newThumbPreview');
	var pick = document.getElementById('editAlbumThumb').value;
	
	if (pick != ''){
		preview.innerHTML = 'New Thumb<br /><img src="photoGallery/thumbs/'+pick+'" alt="new thumb" />';
	}else{
		preview.innerHTML = '';
	}
}
function deletingAlbum(){
	var pick = document.getElementById('albumSelect').value;
	if (pick == ''){
		return false;		
	}else{
		pg.checkDeleteAlbum(pick, function(result){			
			if (result!='0'){				
				alert("You can't delete albums that still have photos in them!");
				return false;
			}else{
				if (confirm('Do You Want To Delete This Album?')){
					document.deleteAlbumForm.submit();
				}else{
					return false;
				}
			}
		});		
	}
}
function editingAlbum(){
	var albumtext = document.getElementById('changedAlbumName').value;
	var pick = document.getElementById('editAlbumChoice').value;
	
	if (albumtext == '') return false;
	pg.checkEditAlbum(albumtext, function(result){
		if ((result=='false')&&(albumtext != pick)){
			alert ('Album with that name already exists.');
		}else{
			if (confirm('Do you want to save these changes?')){
				document.editAlbumForm.submit();
			}else{
				return false;
			}
		}
	});
}