var thedom = "http://www.ajesuschurch.org/";
var cntIMGs = 0;

function dopage(thepage,params,resdiv,restxt,noFade) {	
	$.ajax({
	   type: "POST",
	   url: thepage,
	   data: params,
	   success: function(res){
		  if (resdiv!=""){
			  if (restxt==""){restxt=res;}			  			  
			  if (noFade=="1"){updDiv(resdiv,restxt);}else{updDiv(resdiv,restxt,1);}
		  }
	   }
	});
}

function dopageCh(deltxt,thepage,params,resdiv,restxt) {
	var answer = confirm("Are you sure you want to\npermanently Delete this "+deltxt+"?")
	if (answer){dopage(thepage,params,resdiv,restxt);}
}

function goCH(deltxt,thepage) {
	var answer = confirm("Are you sure you want to\npermanently Delete this "+deltxt+"?")
	if (answer){window.location = thepage;}
}

function goCH2(deltxt,thepage) {
	var answer = confirm("Are you sure you want to\npermanently "+deltxt+"?")
	if (answer){window.location = thepage;}
}

function theNav(thediv,thefile,params,cir,updr) { 
	if (cir!="1" && cir!= undefined){updDiv(thediv,'999');}
	if (updr!="" && updr!= undefined){updDiv(updr,'999');}
	$.ajax({
	   type: "get",
	   url: thedom+'we/ajax/'+thefile+'.php',
	   data: params,
	   success: function(res){
		  $('#'+thediv).html(res);
		  if (updr!=""){updDiv(updr,'');}
	   }
	});
}

function theNav2(thediv,thefile,params,cir,updr) {
	if (cir!="1" && cir!= undefined){updDiv(thediv,'999');}
	if (updr!="" && updr!= undefined){updDiv(updr,'999');}
	$.ajax({
	   type: "POST",
	   url: thedom+thefile+'.php',
	   data: params,
	   success: function(res){
		  $('#'+thediv).html(res);
		  if (updr!=""){updDiv(updr,'');}
	   }
	});
}
function theNavApp(thediv,thefile,params,cir,updr) {
	if (cir!="1" && cir!= undefined){updDiv(thediv,'999');}
	if (updr!="" && updr!= undefined){updDiv(updr,'999');}
	$.ajax({
	   type: "POST",
	   url: thedom+thefile+'.php',
	   data: params,
	   success: function(res){
		  $('#'+thediv).append(res);
		  if (updr!=""){updDiv(updr,'');}
	   }
	});
}

function theNavCH(thediv,thefile,params,deltxt,cir,updr) { 
	if (cir!="1" && cir!= undefined){updDiv(thediv,'999');}
	if (updr!="" && updr!= undefined){updDiv(updr,'999');}
	
	var answer = confirm("Are you sure you want to\npermanently Delete this "+deltxt+"?")
	if (answer){
		$.ajax({
		   type: "get",
		   url: thedom+'we/ajax/'+thefile+'.php',
		   data: params,
		   success: function(res){
			  $('#'+thediv).html(res);
			  if (updr!=""){updDiv(updr,'');}
		   }
		});
	}
}

function addDiv(xdiv,thetxt,theid){
	if (thetxt==999){thetxt="<img src='"+thedom+"/images/loading.gif'/>";}
	$('#'+xdiv).html('<div>'+thetxt+'</div>');
}

function updDiv(xdiv,thetxt,doFade){
	if (doFade==undefined){doFade=0;}
	if ($('#'+xdiv)!=null){
		if (thetxt==999){thetxt="<img src='"+thedom+"/images/loading.gif'/>";}
		if (thetxt==998){thetxt="<div class='loading'></div>";}
		
		$('#'+xdiv).html(thetxt);		
		
		if (doFade==1){
			$('#'+xdiv).show().delay(4000);
			$('#'+xdiv).fadeOut('slow');
			$('#'+xdiv).css("display",""); $('#'+xdiv).show();
		}
		newdiv = null; thediv = null; 
	}
}
function delDiv(xdiv,theid){
	$('#'+xdiv+' > #'+theid).fadeOut('slow').delay(500).remove();
}

function chasect(xdiv,theurl,params){
	var thediv = "section_" + xdiv + "C";
	$.ajax({
	   type: "POST",
	   url: theurl,
	   data: params,
	   success: function(res){
		  $('#'+thediv).html(res);
	   }
	});
}

function TView(tid,ttype) {		
	$.ajax({
	   type: "POST",
	   url: thedom+'add_TViews.php',
	   data: "tid="+tid+"&ttype="+ttype
	});			
}


function goScroll(id){
     	$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}

function divTog(thediv) {
	if ($('#'+thediv).css("display") == 'none') {
		$('#'+thediv).slideDown("slow");	
	}else{
		$('#'+thediv).slideUp("slow");
	}
}
function divTogF(thediv,chTxt,thetxt) {
	if ($('#'+thediv).css("display") == 'none') {
		$('#'+thediv).slideDown("fast");
		if (chTxt){updDiv(chTxt,'hide '+thetxt);}	
	}else{
		$('#'+thediv).slideUp("fast");
		if (chTxt){updDiv(chTxt,'show '+thetxt);}	
	}
}
function divTogPM(thediv,speed) {
	var thespeed = "fast";
	if (speed=="s") thespeed = "slow";
	if ($('#'+thediv).css("display") == 'none') {
		$('#'+thediv+'PM').attr('class', 'img_min');
		$('#'+thediv).slideDown(thespeed);		
	}else{
		$('#'+thediv+'PM').attr('class', 'img_add');
		$('#'+thediv).slideUp(thespeed);
	}
}

function divSlide(thediv) {
	$('#'+thediv).animate({width:'toggle'});
}

function divFad(thediv,xdiv) {	
	$('#'+thediv).delay(4000).fadeOut(1000,0);
	$('#'+xdiv).show().delay(500);		
}

function goFade(thediv) {
	$('#'+thediv).delay(4000).fadeOut(1000,0);
}
function goFade2(thediv) {
	$('#'+thediv).delay().fadeOut(500,0);
}

function divSH(thediv,chTxt,thetxt) {
	if ($('#'+thediv).css("display") == 'none') {
		$('#'+thediv).css("display",""); 
		if (chTxt){updDiv(chTxt,'hide '+thetxt);}	
	}else{
		$('#'+thediv).css("display","none"); 
		if (chTxt){updDiv(chTxt,'show '+thetxt);}
	}
}

function div_S(thediv) { $('#'+thediv).css("display","");  }
function div_H(thediv) { $('#'+thediv).css("display","none");  }

function divSH2(thediv,chTxt) {	
	if ($('#'+thediv).css("display") == 'none') {
		$('#'+thediv).css("display",""); 
		if (chTxt){updDiv(chTxt,'(less info...)');}	
	}else{
		$('#'+thediv).css("display","none"); 
		if (chTxt){updDiv(chTxt,'(more info...)');}
	}
}

function ToggleAll(theElement) {
	 var theForm = theElement.form, z = 0;
	 for(z=0; z<theForm.length;z++){
	  if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
	  theForm[z].checked = theElement.checked;
	  }
	 }
}

function getCheckedValue(radioObj) {
	if(!radioObj) {return "";}
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked){
			return radioObj.value;
		}else{
			return "";}
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {return radioObj[i].value;}
	}
	return "";
}

function updRes(thediv,thetxt){
	div_S(thediv);
	updDiv(thediv,thetxt);
	goFade(thediv);
}

function stripHTML(thestr) {     
	return thestr.replace(/<\/?[^>]+>/gi, ""); 
}
function escapeHTML(thestr) {     
	return thestr.escapeHTML(); 
}


function chkImg(file) {
	extArray = new Array(".jpg", ".jpeg", ".gif");
    allowSubmit = false;
    if (!file){ return;}
    while (file.indexOf("\\") != -1)
    file = file.slice(file.lastIndexOf("\\") + 1);
    ext = file.slice(file.lastIndexOf(".")).toLowerCase();
    for (var i = 0; i < extArray.length; i++) {
    	if (extArray[i] == ext) { allowSubmit = true; break; }
    }
    if (allowSubmit){
		return true;
	}else{
		alert("Please only upload files that end in types:  "
		+ (extArray.join("  ")) + "\nPlease select a new "
		+ "file to upload and submit again.");
		return false;
	}
}

function delgroup(gourl,deltxt) {
	var answer = confirm("Are you sure you want to\npermanently Delete this "+deltxt)
	if (answer){ window.location = gourl;}
		
}

function load_Editor(){
	tinyMCE_GZ.init({themes : 'advanced',languages : 'en',disk_cache : true,debug : false});
	
	tinyMCE.init({mode : "exact",theme : "advanced",skin : "o2k7", skin_variant : "silver",elements : "details",
		plugins : "safari,style,table,spellchecker,layer,advhr,advimage,advlink,emotions,inlinepopups,insertdatetime,preview,contextmenu,paste,fullscreen,noneditable,",
		theme_advanced_buttons1 : "fontselect,fontsizeselect,forecolor,backcolor,|,bold,italic,underline,strikethrough,|,sub,sup,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,outdent,indent",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,link,unlink,|,advhr,charmap,emotions,image,|,insertdate,inserttime,|,insertlayer,moveforward,movebackward,absolute,|,spellchecker,cleanup,fullscreen,preview,code,|,undo,redo",
		theme_advanced_buttons3 : "",
		theme_advanced_buttons4 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		content_css : thedom+"/css/css.css",
		external_image_list_url : thedom+"/scripts/editor/image_list.js"	
	});	
}

var t = null;
function load_suggest(thediv,theval){	
	if (t) {window.clearTimeout(t);}
	if (theval.length>2) {
		t = window.setTimeout("end_suggest('"+thediv+"','"+theval+"')",300);		
	}
}
function end_suggest(thediv,theval){	
	updDiv(thediv + '2',999);
	$.ajax({
	   type: "POST",
	   url: thedom+'we/scripts/' + thediv + '_s.php',
	   data: theval,
	   success: function(res){
		  $('#'+thediv+'2').show();	
		  updDiv(thediv + '2',res);
	   }
	});
	
}
function suggest_it(thediv,theform,theid,theval){
	var theform = document.forms[theform];
	theform.elements[thediv + 'id'].value = theid; 
	theform.elements[thediv].value = theval;
	$('#'+thediv+'2').fadeOut('slow');
}

var reEscape = new RegExp('(\\' + ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'].join('|\\') + ')', 'g');

function fnFormatResult(value, data, currentValue) {
  var pattern = '(' + currentValue.replace(reEscape, '\\$1') + ')';
  return value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>');
}




/* Custom Dropdown menu */
$(document).ready(function(){

	//$("ul.sdnav").parent().append("<span></span>"); Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

	$("ul.ssdd li").click(function() { 

		$(this).parent().find("ul.sdnav").slideDown('fast').show(); 

		$(this).parent().hover(function() {
		}, function(){
			$(this).parent().find("ul.sdnav").slideUp('slow'); 
		});

		}).hover(function() {
			$(this).addClass("subhover"); 
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); 
	});
	
	//fix the top paragraph margin of a page
	$(".main_page p:first").css("margin-top", "0");

});

function updateDD(val,txt){
	updDiv('STvalue',txt); 
	document.STForm.Stype.value=val; 
}

function jspopup(url,name) {
	newwindow=window.open(url,name,'toolbar=0,location=0,scrollbars=0,statusbar=0,status=0,titlebar=0,menubar=0,resizable=1,width=700,height=200,left=265,top= 262');
}


(function ($) {
	$.fn.center = function() {
		this.css("position","absolute");
		this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
		this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
		return this;
	};
})(jQuery);

