$(document).ready(function(){
	$('#nav li a.regular').parent().mouseenter(function(){
		$(this).children().addClass('hover');
		Cufon.refresh('#nav li a.regular.hover, #nav li a.regular-b.hover');
	}).mouseleave(function(){
		$(this).children().removeClass('hover');
		Cufon.refresh('#nav li a.regular');
	});
	
	$('#nav li a.regular-b').parent().mouseenter(function(){
		$(this).children().addClass('hover');
		Cufon.refresh('#nav li a.regular.hover, #nav li a.regular-b.hover');
	}).mouseleave(function(){
		$(this).children().removeClass('hover');
		Cufon.refresh('#nav li a.regular-b');
	});
	
	$('table.list tr:even').addClass('even');
	
	$('#email-signup-form').submit(function(e){
		e.preventDefault();
		$.post('/ajax/email-signup.php', $('#email-signup-form').serialize() + '&source=js', 
			function(data){
				if(data.result == 'ok'){
					$('#email-signup-message').html(data.message).fadeIn();
					setTimeout(function(){
						$('#email-signup-message').fadeOut();
					}, 3500);
				} else {
					$('#email-signup-error').html(data.error).fadeIn();
					setTimeout(function(){
						$('#email-signup-error').fadeOut();
					}, 3500);
				}
			}, 'json'
		);
	});
	
	$('#email-signup-form-b').submit(function(e){
		e.preventDefault();
		$.post('/ajax/email-signup.php', $('#email-signup-form-b').serialize() + '&source=js', 
			function(data){
				if(data.result == 'ok'){
					$('#email-signup-b-message').html(data.message).fadeIn();
					setTimeout(function(){
						$('#email-signup-b-message').fadeOut();
					}, 3500);
				} else {
					$('#email-signup-b-error').html(data.error).fadeIn();
					setTimeout(function(){
						$('#email-signup-b-error').fadeOut();
					}, 3500);
				}
			}, 'json'
		);
	});
	
	$('body').append('<div id="overlay" style="display:none;position:fixed;top:0px;left:0px;height:'+$(window).height()+'px;width:'+$(window).width()+'px;z-index:750;background:url(\'/wp-content/themes/dsw/images/pixel.png\') repeat;" onclick="closeModal();"></div><div id="modal" style="position:fixed;display:none;top:50%;left:50%;height:300px;margin-top:-170px;width:840px;margin-left:-440px;padding:20px;z-index:756;background:#404040;"></div><a id="close_modal" onclick="closeModal(); return false;" style="display:none;position:fixed;top:50%;margin-top:-190px;left:50%;margin-left:420px;z-index:765;"><img src="/wp-content/themes/dsw/images/close.png" /></a>');
	
	$(document).keyup(function(e){
		if(e.keyCode == 27){
			closeModal();
		}
	});
});

var current_url = '';
var modal_open = 'no';

function openModal(url){
	if(current_url != url){
		$('#modal').load(url, function(){
			Cufon.refresh('.cufon-text-30');
			$('#overlay').fadeIn();
			$('#modal').fadeIn();
			$('#close_modal').fadeIn();
		});
		current_url = url;
	} else {
		$('#overlay').fadeIn();
		$('#modal').fadeIn();
		$('#close_modal').fadeIn();
	}
	modal_open = 'yes';
}

function closeModal(){
	if(modal_open == 'yes'){
		$('#overlay').fadeOut();
		$('#modal').fadeOut();
		$('#close_modal').fadeOut();
		modal_open = 'no';
	}
}

function confirmDelete(txt,form){
	var answer = confirm(txt);
	if(answer){
		if(form != undefined){
			$(form).submit();
		} else {
			return true;
		}
	} else {
		return false;
	}
}


// Added by Nick T. Use the Jquery Input Default plugin to set default values for the appropriate input fields
$(document).ready(function() {
	$(':input[@default]').inputdefault();
});

// Added by Nick T to allow for clicking on the Q&A Forum questions
$(document).ready(function() {
	$('.faq li h3').toggle(
		function() { $(this).siblings('.answer').show(); },
		function() { $(this).siblings('.answer').hide(); }
	);
});


// Mogiv JS
var popupStatus = 0; 
var popupname = '';
var popupCurrent = '';

function updatePopup(source, campaignId, ruri){
	source.replace("../","");
	if(popupCurrent != source || source == 'preview-post'){
		$('#js-loadin').show;
		if(campaignId) uri = '/church-network/ajax/'+source+'.php?cid='+campaignId+'&ruri='+ruri
		else uri = '/church-network/ajax/'+source+'.php?ruri='+ruri;
		$.ajax({
			url: uri,
			success: function(data) {
				$('#js-popup-data').html(data);
				showPopup('#js-popup');
				popupCurrent = source;
				$('#js-loadin').hide;
			//	document.popupForm.campaign.value = campaignId;
			}
		});
	} else {
		showPopup('#js-popup');
	}
}

function updatePopupAbs(source, campaignId, ruri){
	source.replace("../","");
	$('#js-loadin').show;
	if(campaignId) uri = '/church-network/ajax/'+source+'.php?cid='+campaignId+'&ruri='+ruri
	else uri = '/church-network/ajax/'+source+'.php?ruri='+ruri;
	$.ajax({
		url: uri,
		success: function(data) {
			$('#js-popup-data').html(data);
			showPopupAbs('#js-popup');
			popupCurrent = source;
			$('#js-loadin').hide;
			centerPopupAbs();
		//	document.popupForm.campaign.value = campaignId;
		}
	});
}

function newPopup(source, arg1, arg2, arg3, width){
	source.replace("../","");
	$('#js-loadin').show;
	if(arg3 != undefined){
		uri = '/church-network/ajax/'+source+'.php?arg1='+arg1+'&arg2='+arg2+'&arg3='+arg3;
	} else if (arg2 != undefined){
		uri = '/church-network/ajax/'+source+'.php?arg1='+arg1+'&arg2='+arg2;
	} else {
		uri = '/church-network/ajax/'+source+'.php?arg1='+arg1;
	}
//	alert(uri);
	$.ajax({
		url: uri,
		success: function(data) {
			$('#js-popup-data').html(data);
			showPopup('#js-popup');
			popupCurrent = source;
			$('#js-loadin').hide;
			if(width){
				document.getElementById('#js-popup').style.width = '600px';
			}
		//	document.popupForm.campaign.value = campaignId;
		}
	});
}

function showPopup(target){
	//loads popup only if it is disabled
	if(!target) target = '#js-popup';
	popupname = target;
	if(popupStatus==0){
		centerPopup();
		$("#backgroundShadow").css({
			"opacity": "0.7"
		});
		$("#backgroundShadow").fadeIn("slow");
		//$(target).addClass('js-popup');
		$(target).fadeIn("slow");
		popupStatus = 1;
	}
}

function showPopupAbs(target){
	//loads popup only if it is disabled
	if(!target) target = '#js-popup';
	popupname = target;
	if(popupStatus==0){
		centerPopupAbs();
		$("#backgroundShadow").css({
			"opacity": "0.7"
		});
		$("#backgroundShadow").fadeIn("slow");
		//$(target).addClass('js-popup');
		$(target).fadeIn("slow");
		popupStatus = 1;
	}
}

function closePopup(){
//disables popup only if it is enabled
	if(popupname && popupStatus==1){
		$("#backgroundShadow").fadeOut("slow");
		$(popupname).fadeOut("slow");
		popupStatus = 0;
		popupname = ''
	}
}

//centering popup
function centerPopup(){
//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(popupname).height();
	var popupWidth = $(popupname).width();
	//centering
	
	$(popupname).css({
		"position": "fixed",
		"top" : "50%",
		"left" : "50%",
		"margin-top" : -popupHeight/2,
		"margin-left" : -popupWidth/2
		/*"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2*/
	});
	/*
	//only need force for IE6 
	$("#backgroundShadow").css({
		"height": windowHeight
	});
	
	$("#backgroundShadow").css({
		"height": windowHeight
	});
*/
}

//centering popup
function centerPopupAbs(){
//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = ($(popupname).height()+38);
	var popupWidth = $(popupname).width();
	//centering
	$(popupname).css({
		"position": "fixed",
		"top" : "50%",
		"left" : "50%",
		"max-height" : (windowHeight - 100),
		"width" : popupWidth,
		"margin-top" : -popupHeight/2,
		"margin-left" : -popupWidth/2,
		"overflow" : "auto"
	});
}

function cS(pn, action){
	if(!action){ action = 'continue'}
var agree=confirm("Are you sure you wish to "+action+"?");
if (agree)
	pn.submit();
	//return true ;
else
	return false ;
}

var Xpos, Ypos, Xlock, Ylock, noteClosed, queued_file, queued_arg;
noteClosed = true;

function updateNote(file,arg){
//	alert('updating'); 
	if(noteClosed){
		if(file){
			if(arg){
				url = '/'+file+'.php?p='+arg;
			} else {
				url = '/'+file+'.php';
			}
			$('#js-note-data').load(url); 
		} else {
			$('#js-note-data').html(arg);
		}
		queued_file = '';
		queued_arg = '';
	} else {
		queued_file = file;
		queued_arg = arg;
	}
}

function showNote(){
	if(noteClosed){
		document.getElementById('js-note').style.top = Ypos+'px';
		document.getElementById('js-note').style.left = Xpos+'px';
		Xlock = Xpos;
		Ylock = Ypos;
		$('#xlock-test').html(Xlock);
		$('#ylock-test').html(Ylock);
		$('#js-note').fadeIn('normal');
		noteClosed = false;
		queued_file = '';
		queued_arg = '';
	} else {
		closeNote();
		if(queued_file && queued_arg){
			updateNote(queued_file,queued_arg);
			queued_file = '';
			queued_arg = '';
		}
	}
}

function closeNote(){
	$('#js-note').fadeOut('normal');
	noteClosed = true;
}

$(document).mousemove(function(e){
	Xpos = e.pageX; 
	Ypos = e.pageY;
	$('#xpos-test').html(Xpos);
	$('#ypos-test').html(Ypos);
	if(Xpos < (Xlock - 150) || Xpos > (Xlock + 350) || Ypos < (Ylock - 90) || Ypos > (Ylock + 400)){
		closeNote();
	}
});
