	
	$(document).ready( function() {

		var devid = 0;
		var respid = 0;
		var popup_show = false;

		function centerPopup( type ) {
			var windowWidth = document.documentElement.clientWidth;
			var windowHeight = document.documentElement.clientHeight;
			var popupHeight = 0;
			var popupWidth = 0;
			if( type === 'comment' ) { 
				popupHeight = $("div.cal_com_popup").height();
				popupWidth = $("div.cal_com_popup").width();

				$("div.cal_com_popup").css({
					"position": "fixed",
					"top": windowHeight/2-popupHeight/2,
					"left": windowWidth/2-popupWidth/2
				});
	
				$("div.cal_com_popup_bk").css({
					"height": windowHeight
				});
			}
			else if( type === 'yesno' ) {
				popupHeight = $("div.cal_com_popup_yesno").height();
				popupWidth = $("div.cal_com_popup_yesno").width();

				$("div.cal_com_popup_yesno").css({
					"position": "fixed",
					"top": windowHeight/2-popupHeight/2,
					"left": windowWidth/2-popupWidth/2
				});
			}
			else if ( type === 'help' ) {
				popupHeight = $('div.form_help_popup').height();
				popupWidth = $('div.form_help_popup').width();

				$("div.form_help_popup").css({
					"position": "fixed",
					"top": windowHeight/2-popupHeight/2,
					"left": windowWidth/2-popupWidth/2
				});

	
				$("div.form_help_bk").css({
					"height": windowHeight
				});
			
			}
			
	
		}

		function hidePopup() {
			if( popup_show === true ) {
				$('div.cal_com_popup_bk').hide();
				$('div.cal_com_popup').hide();
				$('div.cal_com_popup_yesno').hide();
				$('div.form_help_bk').hide();
				$('div.form_help_popup').hide();
				popup_show = false;
			}
		}

		function showPopup(  type ) {
			centerPopup( type );
			popup_show = true;
			
			if ( type === 'yesno' ) {
				$('div.cal_com_popup_bk').show();
				$('div.cal_com_popup_yesno').show();
			}

			else if( type === "comment" ) {
				$('input.cal_com_name').val('');
				$('input.cal_com_email').val('');
				$('textarea.cal_com_message').val('');
	
				$('div.cal_com_popup_bk').show();
				$('div.cal_com_popup').show();
			}
			else if( type === "help" ) {
				$('input.form_help_name').val('');
				$('input.form_help_email').val('');
				$('textarea.form_help_message').val('');
			
				$('div.form_help_bk').show();
				$('div.form_help_popup').show();
			}
		}

		$('a.help_form').live("click", function() { 
				showPopup( 'help' );
		});
		
		$('span.form_help_close_popup').live("click", function() { hidePopup(); });
		$('input.form_help_cancel').live("click", function() { hidePopup(); });
		

		function comment_refresh( dev_id ) {
			$.post('new_cal.php', { ACT:'CLK', 	DEVID:dev_id },
						function(data) {
							$('div.cal_comment_body').html( data );
						}, 'html' );

		}

		$('a.showsupport').live("click", function() {
				hidePopup();
				showPopup( 'help' );
		});

		$('span.cal_comment_del_id').live("click", function() { 
				respid = $(this).attr('title');
				showPopup( 'yesno' ); 
		});

		$('input.form_help_submit').live("click", function() {
			var help_name = $('.form_help_name').val();
			var help_email = $('.form_help_email').val();
			var help_msg = $('.form_help_message').val();
			var help_subnum = $('.form_help_subnum').val();

			if( help_name.length > 1 && help_email.length > 1 && help_msg.length > 1 ) {
				$.post( 'help_form.php', { NAME:help_name,EMAIL:help_email,MSG:help_msg,SUB:help_subnum },
							function( data ) {
								if( data.err ) {
									alert( "Error : "+data.msg );
								}
								else {
									alert( data.msg );
									hidePopup();
								}
							}, 'json' );

			}
			else {
				if ( help_name.length  < 1 ) {
					alert("Please type in your name");
				}
				if ( help_email.length  < 1 ) {
					alert("Please type in your email address");
				}
				if ( help_msg.length  < 1 ) {
					alert("Please type in a messsage");
				}
			}
				

		});

		$('input.cal_com_cancel').live("click", function() { hidePopup(); });
		$('span.cal_close_popup').live("click", function() { hidePopup(); });
		$('.cal_com_add_comment').live("click", function() { showPopup( 'comment' ); });
		$('.cal_com_yesno_cancel').live("click", function() { hidePopup(); });

		$('input.cal_del_com').live("click", function() { 

				$.post('new_cal.php', { ACT:'DCOM', RESID:respid },
							function( data ) {
								if ( data.ERROR ) {
									alert( "Error : "+data.ERROR_MSG );
								
								}
								comment_refresh( data.DEVID );
								hidePopup();
								},'json');
		});

		$('span.cal_comment_resp_id').live("click", function() {
				respid = parseInt( $(this).attr('title') );
				showPopup( 'comment' );
		});

		$('td.cal_click').live("click", function() {

			var jday = $(this).attr('title');

			$.post('new_cal.php',
						{ 
							//CACT:'CLICK',
							ACT:'GDEV',
							Y:$('span.cal_cur_year').attr('title'),
							M:$('span.cal_cur_month').attr('title'),
							D:jday
						},
						function(data) {
							$('div.dev_month').html( data.month );
							$('div.dev_day').html( data.day );
							$('span.dev_heading').html("'"+data.heading+"'" );
							$('span.dev_feast').html( data.feast );
							$('span.dev_scripture').html( data.scripture );
							$('span.dev_citation').html( data.citation );
							$('div.cal_devid').attr('title', parseInt(data.devotionid) );
							$('a.cal_show_comment').attr('href', ('/devotion-'+data.nmonth+'-'+data.day+'-'+data.year));
							devid = data.devotionid;

							if( $('div.cal_comment_body').length ) { 
								comment_refresh( data.devotionid ); 
							}

						},"json");


			

		});

		$('td.cal_switch').live("click", function() {
			var jYear;
			var jMonth;
			if( $(this).attr('title') === 'prev' ) {
				jYear = $('span.cal_prev_year').attr('title');
				jMonth = $('span.cal_prev_month').attr('title');
				
			}
			else if ( $(this).attr('title') === 'next' ) {
				jYear = $('span.cal_next_year').attr('title');
				jMonth = $('span.cal_next_month').attr('title');
			}
			$.post('new_cal.php', { ACT:'CMTH',Y:jYear,M:jMonth }, 
				function(data) {
					$('div.cal_body').html( data );
				},"html");
	
		});
	
		
		$('input.cal_com_submit').live("click", function() {
			var Com_Name = $('input.cal_com_name').val();
			var Com_Msg  = $('textarea.cal_com_message').val();
			var Com_Email = $('input.cal_com_email').val();


			var Com_DevID = parseInt( $('div.cal_devid').attr('title'));

			if ( Com_Name.length > 0 && Com_Msg.length > 0 ) {
				$.post( 'new_cal.php',
				       			{ ACT:'ICOM', NAME:Com_Name, MSG:Com_Msg,EMAIL:Com_Email,DEVID:Com_DevID,RESID:respid },
						       	function( data ) {
	
								if( data.ERROR ) {
									alert( "ERROR : "+data.ERROR_MSG );
								}
								else {
									comment_refresh( Com_DevID );
								}
								if ( respid != 0 ) { respid = 0; }
								hidePopup();
				},'json');
					
			}
			else {
				if ( Com_Name.length == 0 )
					alert("Please enter a name");
				else if ( Com_Msg.length == 0 )
					alert("Please enter a message");

			}
	
		});

		// NONE CALENDAR RELATED ITEMS 

		$('li.tabs_items').live("click", function() {
			var jpage = $(this).attr('id');
			$.post('/content.php', { PAGE:jpage },
				function(data) {
					$('div.tabs_contents').html( data );
				},"html");
		});

		$('p.faq_reveal').live("click", function() {
				if ( $('div.'+$(this).attr('id')).is(':hidden') ){
					$('div.'+$(this).attr('id')).show();
					$('img.'+$(this).attr('id')).attr('src','structure/minus_sign.gif');
				}
				else { 
					$('div.'+$(this).attr('id')).hide(); 
					$('img.'+$(this).attr('id')).attr('src','structure/plus_sign.gif');					
				}
		});

		$('li.tabs_item_about').trigger('click');	

		var pName = window.location.pathname;

		if( pName.length > 4 ) {
			var rPage = pName.match(/\/(.*)\// );
			if ( rPage != null || rPage != 'sample' ) {
				$('li.tabs_item_'+rPage[1] ).trigger('click');
			}
		}

	});

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

