// JavaScript Document
Spry.Utils.addLoadListener(InitPage);

function InitPage() {
	if(Spry.$("gallery")) {
		initGallery();
	}
	if(Spry.$('map_canvas')) {
		initialize();	
	}
	if(Spry.$('content')) {
		var notifications = Spry.$('content').getElementsByTagName('img');
		for(i=0;i<notifications.length;i++) {
			if(notifications[i].id.match(/notify/)) {
				if(notifications[i].src.match(/del/)) {
					Spry.Utils.addEventListener(notifications[i],'click',deleteNotification,false);						  
				}
				if(notifications[i].src.match(/add/)) {
					Spry.Utils.addEventListener(notifications[i],'click',addNotification,false);						  
				}
					
			}
		}
	}
	if(Spry.$('signup-form')) {
		Spry.Utils.addEventListener('email','blur',checkEmail,false);	
		Spry.Utils.addEventListener('confirm_password','blur',checkPassword,false);
		Spry.Utils.addEventListener('password','blur',checkPassword,false);
	}
}
function removeListeners() {
	var notifications = Spry.$('content').getElementsByTagName('img');
	for(i=0;i<notifications.length;i++) {
		if(notifications[i].id.match(/notify/)) {
			if(notifications[i].src.match(/del/)) {
				Spry.Utils.removeEventListener(notifications[i],'click',deleteNotification,false);						  
			}
			if(notifications[i].src.match(/add/)) {
				Spry.Utils.removeEventListener(notifications[i],'click',addNotification,false);						  
			}
				
		}
	}
}
function splitString(ele,pos) {
	var stringArray = ele.split('_');
	return stringArray[pos];
}
function addNotification() {
	removeListeners();
	var entry	=	splitString(this.id,1);
	var type	=	splitString(this.id,2);
	var user 	= 	splitString(this.id,3);	
	
	var idObj 	= 	new Object;
	idObj.id 	= 	this;
	idObj.dir	=	'add';
	
	Spry.Utils.loadURL('POST','admin/com/notifications.cfc',false,fadeOutNotification, {postData: "method=addNotification&entryID="+entry+'&type='+type+'&userID='+user, headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" },userData:idObj});
}
function deleteNotification() {
	removeListeners();
	var entry	=	splitString(this.id,1);
	var type	=	splitString(this.id,2);
	var user 	= 	splitString(this.id,3);	
	
	var idObj 	= 	new Object;
	idObj.id 	= 	this;
	idObj.dir	=	'del';
	
	Spry.Utils.loadURL('POST','admin/com/notifications.cfc',false,fadeOutNotification, {postData: "method=deleteNotification&entryID="+entry+'&type='+type+'&userID='+user, headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" },userData:idObj});
}
function fadeOutNotification(req) {
	var func = function() {swapImage(req.userData.id,req.userData.dir)};
	var img = new Spry.Effect.Fade(req.userData.id,{duration: 500,from: 100, to: 0,toggle: false, finish:func});
	img.start();		
}
function fadeInNotification(req) {
	/*var func = function() {swapImage(req.userData.id,req.userData.dir)};*/
	var img = new Spry.Effect.Fade(req,{duration: 500,from: 0, to: 100,toggle: false, finish:InitPage});
	img.start();		
}
function swapImage(req,dir) {
	var child = req;
	var parent = child.parentNode;
	
	if(dir=='add') {
		child.setAttribute('src','images/del_notify.png');
		child.setAttribute('title','Un-notify me of this, please!');
		fadeInNotification(req);
	}
	if(dir=='del') {
		child.setAttribute('src','images/add_notify.png');
		child.setAttribute('title','Notify me of this');
		fadeInNotification(req);
	}
	/*var confirmation = document.createElement('span');
	confirmation.setAttribute('class','saved');
	confirmation.innerHTML = 'Saved!';
	parent.removeChild(child);	
	parent.appendChild(confirmation);*/	
}
