
$(document).ready(function() {

	WelcomeControl();
	StyleWebParts();
	StyleSocialIcons();
	TabWebParts();		
	EditModePanel();
	homepagefeature();
	InformationForMenu();
	FooterNavigation();
	ProcessAds('small');
	ProcessAds('large');
	StyleAppPages();	
	getRandomImage();	
	
});

function StyleAppPages() {
	try {
		
		var pathname = window.location.pathname;	
		if(pathname.indexOf('/_layouts/') >= 0 || pathname.indexOf('/_catalogs/') >= 0 ) { 		
			$('#s4-workspace').addClass('app-page');
		}
		
	} catch(e) { }
}

function WelcomeControl() {
	try {
		
		$('.welcomeUserDisplayName').attr('title','NGWA User Menu');
		$('.welcomeUserDisplayName').mouseenter(function() {
			$('.welcomeUserMenu').show(300);
		});
		$('.welcomeUserMenu').mouseenter(function() {
			$(this).show();
		});
		$('.welcomeUserMenu').mouseleave(function() {
			$(this).hide(300);
		});
		
		$('div.welcomeUserMenu').corner();
		
	} catch(e) { }
}


function InformationForMenu() {
	try {
		$('#informationForMenu').corner("bottom keep");
		
		$('#informationFor a.info-menu-link').click(function() {
			$('#informationForMenu').toggle(300);
		}); 
		
		/*$('#informationForMenu').mouseenter(function() {
			$(this).show();
		});
		$('#informationForMenu').mouseleave(function() {
			$(this).hide(300);
		});*/
		
	} catch(e) { }
}

function EditModePanel() {
	try {
		$('#edit-mode-panel').tabs();
	} catch(e) {}
}

function StyleWebParts() {
	try {
		// only style web parts if they are not in edit mode
		if($('.ms-WPAddButton').size() <= 0) {
			var tableBefore = '<table class="wp-wrapper-table" cellspacing="0" cellpadding="0">'+
				'<tr>'+
					'<td class="wp-wrapper-ul"></td>'+
					'<td class="wp-wrapper-um"></td>'+
					'<td class="wp-wrapper-ur"></td>'+
				'</tr>'+
				'<tr>'+
					'<td class="wp-wrapper-ml"></td>'+
					'<td class="wp-wrapper-mm">';					
			var tableAfter = '</td>'+
					'<td class="wp-wrapper-mr"></td>'+
				'</tr>'+
				'<tr>'+
					'<td class="wp-wrapper-bl"></td>'+
					'<td class="wp-wrapper-bm"></td>'+
					'<td class="wp-wrapper-br"></td>'+
				'</tr>'+
			'</table>';
	
			// only wrap the web parts with chrome border
			$('table.s4-wpTopTable td.ms-WPBorder').each(function() {
			
				$(this).parents('table.s4-wpTopTable').wrap('<div class="wp-wrapper-inner">');
				$(this).parents('.wp-wrapper-inner').html(tableBefore+$(this).parents('.wp-wrapper-inner').html()+tableAfter);
			});
			$('table.s4-wpTopTable td.ms-WPBorderBorderOnly').each(function() {
			
				$(this).parents('table.s4-wpTopTable').wrap('<div class="wp-wrapper-inner">');
				$(this).parents('.wp-wrapper-inner').html(tableBefore+$(this).parents('.wp-wrapper-inner').html()+tableAfter);
			});
	
			// round headers
	
			$('table.s4-wpTopTable tr.ms-WPHeader').each(function() {
				$(this).parent().parent().wrap('<div class="wp-header">');
			});
		}
	}
	catch (e) {}
}

function emailFriend() {
	try {
		var pageTitle = escape(document.title);
		var pageUrl = escape(location.href);
		
		var email = "mailto:?subject=Sending You a Link From NGWA:  " + pageTitle;
		email += "&body=I thought you might be interested in this article on the National Ground Water Association's website: " + pageTitle;
		email += ". You can view it here:  " + pageUrl; 
		
		location.href = email;
	} catch(e) {}
	
}

function homepagefeature() {
	try {
		if($('#featureArea .dfwp-column.dfwp-list').size() > 0) {
			$('#featureArea .dfwp-column.dfwp-list').cycle({ 
					fx:     'fade', 
					speed:  1000, 
					timeout: 8000, 
					pager:  '#featureNav',
					pause:	1
			});
			
			$('.featureContainer').each(function() {
				var link = $(this).find('.featureTitle a').attr('href');
				if(link !== "") {
					// add link tag
					$(this).html('<a href="'+link+'" class="image-link">'+$(this).html()+'</a>');
				}
			});
		}
		
	} catch (e) {}
}

function TabWebParts() {
	try {		
	
		$('#tabbedZone').each(function(i) {																			
			
			//wrap it
			var tableBefore = '<table class="wp-wrapper-table" cellspacing="0" cellpadding="0">'+
				'<tr>'+
					'<td class="wp-wrapper-ul"></td>'+
					'<td class="wp-wrapper-um"></td>'+
					'<td class="wp-wrapper-ur"></td>'+
				'</tr>'+
				'<tr>'+
					'<td class="wp-wrapper-ml"></td>'+
					'<td class="wp-wrapper-mm">';					
			var tableAfter = '</td>'+
					'<td class="wp-wrapper-mr"></td>'+
				'</tr>'+
				'<tr>'+
					'<td class="wp-wrapper-bl"></td>'+
					'<td class="wp-wrapper-bm"></td>'+
					'<td class="wp-wrapper-br"></td>'+
				'</tr>'+
			'</table>';
	
			// only wrap the web parts with chrome border
			$(this).wrap('<div class="wp-wrapper-inner">');
			$(this).parents('.wp-wrapper-inner').html(tableBefore+$(this).parents('.wp-wrapper-inner').html()+tableAfter);
			//$('.wp-tabify .wp-wrapper-inner').hide();
		});
		
		
		if($('.ms-WPAddButton').size() == 0){
			$('#tabbedZone').each(function(i){
				var tabContent = "";				
				var tab = "";
			    $(this).find('.s4-wpTopTable,td[id^="MSOZoneCell_"] > table').each(function(j) {

			    	if($(this).find('.ms-WPTitle').text() != "" && $(this).find('.ms-WPBody').parent().html() != "") {
						tab += '<li><a href="#tabContent_'+i+'-'+j+'">'+$(this).find('.ms-WPTitle').text()+'</a></li>';
				    	tabContent += '<div id="tabContent_'+i+'-'+j+'">'+$(this).find('.ms-WPBody').parent().html()+'</div>';			    			    	
				    }
			    });

				
				$(this).html('<ul>'+tab+'</ul>'+tabContent);
				$(this).tabs();
			});						
			
		}						
	}
	catch (e) {} 
}

function FooterNavigation() {
	try {
		$('#footerNavigation').html('<ul></ul>');
		$('#mainNav .menu-horizontal ul.root li.static > a').each(function() {
			if($(this).hasClass('selected')) {
				$('#footerNavigation ul').append('<li>'+$(this).find('.menu-item-text').text()+'</li>');
			}
			else {
				$('#footerNavigation ul').append('<li><a href="'+$(this).attr('href')+'">'+$(this).find('.menu-item-text').text()+'</a></li>');
			}
		});				
	}
	catch(e) {}
}
function StyleSocialIcons() {
	try {
		
		$('img[id^=AddQuickTagImg]').attr('src','/Style Library/ngwa/images/social-tag.png');
		$('img[id^=AddQuickTagImg]').removeAttr('style');		
		$('img[id^=TagsAndNotesImg]').attr('src','/Style Library/ngwa/images/social-comments.png');				
		$('img[id^=TagsAndNotesImg]').removeAttr('style');
		

		$('a[id^=TagsAndNotes]').mouseover(function(e) { 		
			$('img[id^=TagsAndNotesImg]').attr('src','/Style Library/ngwa/images/social-comments-active.png');
			$('img[id^=TagsAndNotesImg]').removeAttr('style');		
			e.preventDefault();
		});
		$('a[id^=TagsAndNotes]').mouseout(function(e) { 			
			$('img[id^=TagsAndNotesImg]').attr('src','/Style Library/ngwa/images/social-comments.png');
			$('img[id^=TagsAndNotesImg]').removeAttr('style');		
			e.preventDefault();			
		});
		$('a[id^=AddQuickTag]').mouseover(function(e) { 
			$('img[id^=AddQuickTagImg]').attr('src','/Style Library/ngwa/images/social-tag-active.png');
			$('img[id^=AddQuickTagImg]').removeAttr('style');		
		});
		$('a[id^=AddQuickTag]').mouseout(function(e) { 			
			$('img[id^=AddQuickTagImg]').attr('src','/Style Library/ngwa/images/social-tag.png');
			$('img[id^=AddQuickTagImg]').removeAttr('style');		
		});
		
	}
	catch(e) {}
}

function ProcessAds(size) {

	try {

		if($('.ngwa-ad-'+size).size() > 0) {
			// store current ad in a cookie					
			var currentAd = 0;
			if($.cookie('ngwa-ad-'+size) !== null) {
				currentAd = parseInt($.cookie('ngwa-ad-'+size));
			}																
			
			// show current ad only
			$('.ngwa-ad-'+size).each(function(i) {
				if(i == currentAd) {
					$(this).show();
				}
			});
			
			var nextAd;
			if(currentAd+1 < $('.ngwa-ad-'+size).size()) {
				nextAd = currentAd+1;
			}
			else {
				nextAd = 0;
			}
			
			$.cookie('ngwa-ad-'+size,nextAd.toString());
		}		
	
	}
	catch(e) {}

}
function homePageAdSelection()
{

}
function getRandomImage() {
 var num = Math.round( Math.random() * $(".AdImageRandom").toArray().length -1 );
 $(".AdImageRandom:eq(" + num + ")").show();
 
}

