// Javascript Rules

var myrules = {
	'img.dragme' : function(element){
		new Draggable($(element), {revert:true});
		element.onmousedown = function() {
			this._x = this.offsetLeft;
			this._y = this.offsetTop;
		}
		element.onclick = function() {
			check_x = this.offsetLeft;
			check_y = this.offsetTop;
			if(check_x != this._x || check_y != this._y) {
				//alert('was dragging (x: '+ this._x +',y: '+ this._y + ')');
			} else {
				//if (this.alt) showLightbox(this);
				if (this.alt) myLightbox.start(this);
			}
		}
	},
	'div.draghere' : function(element){
		Droppables.add($(element), {
						hoverclass:'draghover',
						onDrop:function(element, dropon, event){
							if (dropon.id == currentFolder || element.className.match(/draghere/)) {
								//new Effect.Highlight(dropon, {startcolor:'#FF0000', endcolor:'#000000'});
							} else {
								makeDrop(element,dropon,event);
							}
						}
						});
		new Draggable($(element), {revert:true});
		element.onmousedown = function() {
			this._x = this.offsetLeft;
			this._y = this.offsetTop;
		}
		element.onclick = function() {
			check_x = this.offsetLeft;
			check_y = this.offsetTop;
			if(check_x == this._x && check_y == this._y) {	
				// Close all other folder icons
				pNode = $(this.parentNode);
				foldersNum = pNode.childNodes.length;
				for (i=0; i<foldersNum; i++) {
					classTest = pNode.childNodes[i].className;
					if (classTest == 'folder draghere folderOpen' || classTest == 'folder draghere folderOpenEmpty') {
						totalPhotos = pNode.childNodes[i].title.replace(" Photos","");
						if (totalPhotos != 0) {	
							pNode.childNodes[i].className = 'folder draghere folderFull';
						} else {
							pNode.childNodes[i].className = 'folder draghere folderEmpty';
						}
					} else 
					if (classTest == 'folder draghere folderOpenSecure' || classTest == 'folder draghere folderOpenEmptySecure') {
						totalPhotos = pNode.childNodes[i].title.replace(" Photos","");
						if (totalPhotos != 0) {	
							pNode.childNodes[i].className = 'folder draghere folderFullSecure';
						} else {
							pNode.childNodes[i].className = 'folder draghere folderEmptySecure';
						}
					}
				}
				var secure = "";
				var re = new RegExp('Secure');
				if (this.className.match(re)) {
					secure = "Secure";
				}
				album_id = this.id;
				currentFolder = this.id;
				totalPhotos = this.title.replace(" Photos","");
				if (totalPhotos != 0) {
					this.className = 'folder draghere folderOpen'+secure;
				} else {
					this.className = 'folder draghere folderOpenEmpty'+secure;
				}
				loadThumbs("username="+username+"&album_id="+this.id+"&numThumbs="+numThumbs);
			}
		}
	},
	'div#trash' : function(element){
		Droppables.add($(element), {
						hoverclass:'trashfull',
						onDrop:function(element, dropon, event){
							if(element.className.match(/draghere/)) {
								// This is a folder
								if(confirm('Are you sure you want to delete the folder '+element.rel+' which contains '+element.title.toLowerCase()+'?\n\nAll photos in this folder will be deleted as well.\n\nPlease note this action is permanent.')) {
									deleteFolder(element,dropon,event);
								}
							} else {
								// This is an image
								if(confirm('Are you sure you want to delete the photo '+element.title+'?\n\nPlease note this action is permanent.')) {
									deletePhoto(element,dropon,event);
								}
							}
						}
						});
		element.onclick = function() {
			alert('I am the almighty trash can!  Feed me your unwanted photos and delicious folders or I shall grow angry!');
		}
	},
	'div#photo_search' : function(element){
		var searchInput = $('photoLivesearch');
		//var searchLoading = $('photoSearchLoading');
		
		//searchLoading.style.display = 'none';
		if (searchInput.value == '') searchInput.value = searchInputMsg;
		
		addEvent(searchInput, 'keyup', liveSearchStart, false);
	},
	'input#clearLivesearch' : function(element){
		element.onclick = function() {
			resetEverything();
		}
	},
	'input#photoLivesearch' : function(element){
		element.onfocus = function() {
			clearSearchText();
		}
		element.onblur = function() {
			resetOnBlur();
		}
	},
	'div#createFolder' : function(element){
		var searchInput = $('albumTitle');
		addEvent(searchInput, 'keyup', albumSearchStart, false);
	},
	'input#addFolder' : function(element){
		element.onclick = function() {
			var myInput = $('albumTitle').value;
			var albumPassword = $('albumPass').value;
			var testi = myInput.replace(/^\s+|\s+$/g, '');
			if (testi != '') {
				addFolder("username="+username+"&title="+myInput+"&password="+albumPassword);
			} else {
				$('albumMessage').innerHTML = 'Album titles cannot be empty';
				$('albumTitle').value = '';
			}
		}
	}
};
	
Behaviour.register(myrules);