 jQuery(document).ready(
	function(){
		
		// link sidebar student image to expanded view
		
		jQuery('.ngg-widget a.thickbox').each(
			function(){
					var url = jQuery(this).attr('href');
					
					var newUrl = url.replace( 'students-initial-view', 'students-expanded-view');
					newUrl = newUrl.replace( '_initial', '');
					
					jQuery(this).attr('href', newUrl); 
				}
		);
		
		// supress image gallery image alt and title
		jQuery('.ngg-widget img').removeAttr('alt');
		jQuery('.ngg-widget img').removeAttr('title');
		jQuery('.ngg-widget a').removeAttr('title');
		jQuery('.ngg-gallery-thumbnail img').removeAttr('alt');
		jQuery('.ngg-gallery-thumbnail img').removeAttr('title');
		jQuery('.ngg-gallery-thumbnail a').removeAttr('title');
		
		
		// adjust margin left for last menu item
		 jQuery('#access>div.menu-header>ul>li:last').addClass('giving');
		
		// adjust margin and widht for last widget
		 jQuery('#homepage_widgets>.home_widget_container:last').css({
				'margin-left':'5px',
				'width':'195px'
			});
		
		
		// adjust footer widgets padding and borders
		 jQuery('.footer_widget_container:eq(0)').css('padding-left','0');
		 jQuery('.footer_widget_container:eq(1)').css({
				'border-right':'1px solid #666666',
				'padding-left':10+'px'
				});
		 jQuery('.footer_widget_container:eq(2)').css('border-right','1px solid #666666');
		 jQuery('.footer_widget_container:last').css('padding-right','0');
		
		
		// Subpages dynamic columns
		noChildPages = jQuery('.child_pages>li').size();
		if( noChildPages > 4 ){
				remainder = noChildPages % 3;
				rows = Math.floor( noChildPages / 3 );
				ctr = 1;
				i = 0;
				for( i = 0; i < noChildPages ; ){
					
					 for( j = 0; j < rows; j++ ){
							jQuery('.child_pages>li:eq('+i+')').addClass('col'+ctr+'');
							i++;
						}
					if( remainder == 1 ){
							jQuery('.child_pages>li:eq('+i+')').addClass('col'+ctr+'');
							remainder = 0; 
							i++;
						}
					else if( remainder == 2 ){
							jQuery('.child_pages>li:eq('+i+')').addClass('col'+ctr+'');
							remainder = 1; 
							i++;
						}
				
					ctr++;
					if( ctr > 3 ){
							
							break;
						}
				}
			
			}		
		if( noChildPages == 4 ){
				jQuery('.child_pages>li:eq(0)').addClass('col1');
				jQuery('.child_pages>li:eq(1)').addClass('col1');
				jQuery('.child_pages>li:eq(2)').addClass('col2');
				jQuery('.child_pages>li:eq(3)').addClass('col2');
			}
		
			// wrap list item / subpages which have same class by div so we can have column layout
					jQuery('.child_pages>li.col1').wrapAll('<div class="col1_wrapper col_wrapper">');
					jQuery('.child_pages>li.col2').wrapAll('<div class="col2_wrapper col_wrapper">');
					jQuery('.child_pages>li.col3').wrapAll('<div class="col3_wrapper col_wrapper">');

		// add class to event widget dynamically
		 jQuery('.widget_em_widget ul').addClass('eventScroll');
		
		// insert scroller controls ( up & down ) after event widget title
		 jQuery('.widget_em_widget h3.home_widget_title').after('<span id="eup" class="up"></span><span id="edown" class="down" style="display:none"></span>');
		
		
		
		// News widget content scroll down and scroll up script
		var nscrollCtr = 0;
		var nmaxScrollCtr = Math.ceil(  jQuery('ul#newsScroll').height() / 100 );
		 jQuery('#ndown').click( function(){
			if( nscrollCtr < nmaxScrollCtr - 2 ){
					nscrollCtr++;  jQuery('ul#newsScroll').animate({'top':'-=100px'},'slow',
					function(){ jQuery('#nup').css('display','block');});									 				
					}
			});	
		 jQuery('#nup').click(function(){
			if( nscrollCtr != 0 ){
				nscrollCtr--;  jQuery('ul#newsScroll').animate({'top':'+=100px'},'slow',
				function(){if( nscrollCtr == 0 ){ jQuery('#nup').css('display','none');}});
				}
				
			});	
			
			
		
        
        // fix Datepicker field of Gravity Form with attribute readonly
        jQuery('.gform_page_fields ul li.gfield .ginput_container input.datepicker').attr('readonly', true);
        
        // fix Social Security Number field
        jQuery('.gform_page_fields ul li.social-security-number input').blur(function(){
            var socialnumber = $(this).val().toString();            
            
            if(socialnumber.length != 9) {
                alert('This field has 9 characters!');
                $(this).val('');
            } else {
                var newnumber = '';                
                newnumber = socialnumber.substr(0, 3) + '-' + socialnumber.substring(3, socialnumber.length-4) + '-' + socialnumber.substr(5, 4);
                $(this).val(newnumber); 
            }
        });
        
        // fix Phone field
        jQuery('.gform_page_fields ul li.gf_phone input').blur(function(){
            var phonenumber = $(this).val().toString();
            if(phonenumber != '' && phonenumber[0] != '(' && phonenumber[3] != ')') {
                var newnumber = '';                
                newnumber = '(' + phonenumber.substr(0, 3) + ')' + phonenumber.substring(3, phonenumber.length-4) + '-' + phonenumber.substring(phonenumber.length-4, phonenumber.length);
                $(this).val(newnumber);
            }
        });
        
        //add auto save form
        //jQuery('.gform_wrapper form#gform_4 *').autosave();
	
	}  // end document ready function
);

// Twitter widget content scroll down and scroll up script
function dynamicStyles(){
	
		// twitter widget scroll
		var tscrollCtr = 0;
		var tmaxScrollCtr = Math.ceil(  jQuery('ul.tweet_list').height() / 100 );
		 jQuery('#tdown').click( function(){				
			if( tscrollCtr < tmaxScrollCtr - 2 ){
					tscrollCtr++;  jQuery('ul.tweet_list').animate({'top':'-=100px'},'slow',
					function(){ jQuery('#tup').css('display','block');});									 				
					}
			});	
		 jQuery('#tup').click(function(){
			if( tscrollCtr != 0 ){
				tscrollCtr--;  jQuery('ul.tweet_list').animate({'top':'+=100px'},'slow',
				function(){ if( tscrollCtr == 0 ){ jQuery('#tup').css('display','none'); }});
				}
			});	
			
		
			// Event widget content scroll down and scroll up script
		var escrollCtr = 0;
		var emaxScrollCtr = Math.ceil(  jQuery('ul.eventScroll').height() / 100 );
		 jQuery('#edown').click( function(){				
			if( escrollCtr < emaxScrollCtr - 2 ){
					escrollCtr++;  jQuery('ul.eventScroll').animate({'top':'-=100px'},'slow',
					function(){ jQuery('#eup').css('display','block');});									 			
					}
			});	
		 jQuery('#eup').click(function(){
			if( escrollCtr != 0 ){
				escrollCtr--;  jQuery('ul.eventScroll').animate({'top':'+=100px'},'slow',
				function(){ if( escrollCtr == 0 ){ jQuery('#eup').css('display','none');}});
				}
				
			});	
			
			
	
		// Equal height for #container and #content 
		contentHeight =  jQuery('#container').height();
		 jQuery('#content').css('height', contentHeight+'px');
		 
		 
		 // content vertical scroller script
	
		if(  jQuery('#content-scroll').height() < 600 ){
				 jQuery('.cdown').css('display', 'none');
			}
			
		scrollHeight =  jQuery('#content-scroll').height() - 600;
		maxContenScrollCtr = Math.ceil( scrollHeight ) / 150 ;
		cScrollCtr = 0;
		 jQuery('.cdown').click( function(){
				if( cScrollCtr < maxContenScrollCtr  ){
					cScrollCtr++;  jQuery('#content-scroll').animate({'top':'-=150px'},'slow',
					function(){ jQuery('.cup').css('display','block');});									 				
					}
				
			});
		 jQuery('.cup').click(function(){
			if( cScrollCtr != 0 ){
				cScrollCtr--;  jQuery('#content-scroll').animate({'top':'+=150px'},'slow',
				function(){if( cScrollCtr == 0 ){ jQuery('.cup').css('display','none');}});
				}
				
			});	
		 
		 
		 	// dynamic widht of subpages 	
		title_width = 638 - ( $('.cur_page_title').width() + 42 );
		//alert( title_width );
		$('.child_page_wrapper').css('width', title_width + 'px');
		
	
		/*maxWidth = 0;
		 jQuery('.child_pages li').each(function(){
				width =  jQuery( this ).width();
				if( width > maxWidth)
					maxWidth = width;
			}); */
		 //jQuery('.child_pages li').css('width', maxWidth+'px'); 
		
		// add dynamic height to separator bettwen page title and subpages
		if( $('.child_page_wrapper > ul.child_pages').text() != ''){
			 height = $('.child_page_wrapper').height()+13;
			$('.page_vertical_sep').css({
				'height': height+'px',
				'min-height': 82+'px' 
				});
		}
	}   

