//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	URL popup functionality
//-------------------------------------------------------------------------------------------------------
    var _popupTracker = {}
    var _popupCounter = 0;

	var	SHpopup	= null;

    $.fn.openPopupWindow = function(options)
    {
		var SHpopup;

        var defaults = {
            height: 650, // sets the height in pixels of the window.
            width: 960, // sets the width in pixels of the window.
            toolbar: 0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
            scrollbars: 0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
            status: 0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
            resizable: 1, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
            left: 0, // left position when the window appears.
            top: 0, // top position when the window appears.
            center: 1, // should we center the window? {1 (YES) or 0 (NO)}. overrides top and left
            createnew: 0, // should we create a new window for each occurance {1 (YES) or 0 (NO)}.
            location: 0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
            menubar: 0 // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
        };

        var options = $.extend(defaults, options);

        var obj = this;

        // center the window
        if (options.center == 1)
        {
            options.top = (screen.height - (options.height + 110)) / 2;
            options.left = (screen.width - options.width) / 2;
        }

        var parameters = "location=" + options.location +
                         ",menubar=" + options.menubar +
                         ",height=" + options.height +
                         ",width=" + options.width +
                         ",toolbar=" + options.toolbar +
                         ",scrollbars=" + options.scrollbars +
                         ",status=" + options.status +
                         ",resizable=" + options.resizable +
                         ",left=" + options.left +
                         ",screenX=" + options.left +
                         ",top=" + options.top +
                         ",screenY=" + options.top;

        // target url
        var target = obj.attr("href");       

		var windowname	= "sacredheart";

        // test if popup window is already open, if it is, just give it fokus.        
        if(!SHpopup || SHpopup.closed)
        {
			// open window
			SHpopup = window.open(target, windowname, parameters);
  			SHpopup.focus();
        } 
        else
        {
			SHpopup.location=target;
            SHpopup.focus();
        }

//        return false;
    };        

jQuery.fn.HideEmpty = function() {
  return this.each(function(){
	var itemcount	= $(this).find('li').length;
	if( !itemcount || itemcount == 0 )
	  {
		$(this).css( "visibility", "hidden" );
	  }
  });
};


function expand(param)
{
		param.style.display=(param.style.display=="none")?"block":"none";
}

function expandById(id)
{
	expand(document.getElementById(id));
}

function toggle_primer_form()
{
	$("#site_dim").toggle();
	$("#primer_form_container").toggle();
}

function toggle_become_a_member_form()
{
	$("#site_dim").toggle();
	$("#become_a_member_form_container").toggle();
	return false;
}

function toggle_rsp_application_form()
{
	$("#site_dim").toggle();
	$("#rsp_application_form_container").toggle();
}

function toggle_volunteer_form()
{
	$("#site_dim").toggle();
	$("#volunteer_form_container").toggle();
}


function toggle_video( id )
{
	if( !id)
	{
		return;
	}

	var div_id = '#video_'+id;
	$("#site_dim").toggle();
	$(div_id).toggle();
}

//*********************************************************************************************************
//		Function:	is_digit
//---------------------------------------------------------------------------------------------------------
//	What it does:	validate if the digit put in is a number
//*********************************************************************************************************
function checkCommentLength( obj, e )
{
	var obj = $('#'+obj.id);

	var max_chars = obj.attr('max_chars');
	
	var keynum;
	var keychar;

//-----------------------------------------------------------------------------------------------------
// get the value of the character that was pressed
//-----------------------------------------------------------------------------------------------------
	if(window.event) // IE
	{
	  keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
	  keynum = e.which;
	}


	keychar = String.fromCharCode(keynum);
//-----------------------------------------------------------------------------------------------------
// what type of char
//-----------------------------------------------------------------------------------------------------
	if((keynum == 8) || (keynum == 0) || (keynum == 46) || (keynum == 9 ) ||  (keynum == 37 )|| (keynum == 39 ) || (keynum == 17 ) )
	{
		return true;
	}

//-----------------------------------------------------------------------------------------------------
// validate
//-----------------------------------------------------------------------------------------------------
	//alert(obj.value.length);
	var comment_length =  obj.val().length;
	$('#comments_length').html(  comment_length + '/'+ max_chars );
	if( comment_length >= max_chars )
	{
		//$('#comments_length').html('Comments are limited to '+ max_chars+' characters ');
		return false
	}

	return true;
}
// debug func to show props of obj
function  printProps(obj, objName) {
  var output = "" ;
  for (var prop in obj) {
    output += objName + "." + prop + " = " + obj[prop] + "\n</br>" ;
  //  output += objName + "." + prop + " = " + obj[prop] + "\n" ;
  }

  return output ;
}

//*********************************************************************************************************
//		document.ready:::::::
//---------------------------------------------------------------------------------------------------------
//*********************************************************************************************************
$(document).ready(function(){

		$('ul.m2').HideEmpty();

	//---------------------------------------------------------------------------------------------------------
	//		remove text from the textboxes when one focuses on the textbox
	//---------------------------------------------------------------------------------------------------------
    $('.label_in_textbox').each(function(){
		$(this).attr('label',$(this).val());

		$(this).focus(function(){
			var label = $(this).attr('label');
			var value = $(this).val();
			
			//alert('l='+label +' v='+value);
			if( label == value )
			{
				$(this).val('');
			}
			
		});

		$(this).blur(function(){
			var label = $(this).attr('label');
			var value = $(this).val();

			if( !value )
			{
				$(this).val(label);
			}
			
		});

    });


	
	$('.bodytext img').each( function(){
		var caption = $(this).attr('alt');
		
		if (caption) {
			var align = $(this).css('float');
			var width = $(this).outerWidth(true);

			$(this).wrap('<div class="img-caption-'+align+'" style="width:'+width+'px">');
			$(this).parent().append(caption);
		};
	});
	
	
	//	RSP Chart - open new window
	$("a#rspchart").click(function (event)
	{
		event.preventDefault();
		$(this).openPopupWindow({
				scrollbars:1
			});
 	});    
	
		
});
