// Use ShareThis new widget
var switchTo5x = true;

// ONLOAD EVENTS - PUBLIC AREAS
$(document).ready(function() {
	var isDesktopBrowser = ($(window).width() > 480);

	// Open login pull-down if page loads with a login error
	if ($("#loginMessage .wlpeMessage").swrPopulatedTextNodeDescendents().length != 0 || $('#wlpeReset').length != 0 || $('#wlpeActivate').length != 0) {
			$('#masthead .memberLogin .toggleDialog a').text('Close Login Box');
			$("#masthead .memberLogin .loginDialog").slideToggle("fast");
			$('#masthead .memberLogin .loginDialog input:first').focus();
	}
	
	// Prep member login pull-down
	$('#masthead .memberLogin .toggleDialog').click(function(event) {
		event.preventDefault();
		$toggleDialogLink = $('#masthead .memberLogin .toggleDialog a');
		if ($toggleDialogLink.text() == 'Member Login') {
			$toggleDialogLink.text('Close Login Box');
		}
		else {
			$toggleDialogLink.text('Member Login');
		}
		$('#masthead .memberLogin .loginDialog').slideToggle('slow', function() {
			$('#masthead .memberLogin .loginDialog input:first').focus();
		});
	});
	
	// If logged-in cause toggle dialog to trigger logout button
	if ($('#wlpeLogoutButton').length != 0) {
		$('#masthead .memberLogin .toggleDialog').unbind('click')
												 .click(function(event) {
			event.preventDefault();
			$('#wlpeLogoutButton').trigger('click');
		});
	}
	
	// If open, close member login pull-down on click anywhere but in pull-down
	$('body').click(function(event) {
		if ($(event.target).closest('#masthead .memberLogin').length == 0) {
			if ($('#masthead .memberLogin .loginDialog').is(':visible')) {
					$('#masthead .memberLogin .loginDialog').slideUp();
					$('#masthead .memberLogin .toggleDialog a').text('Member Login');
			}
		}
	});
	
	// Place togglable default text in site search box
	$('#ajaxSearch_input').swrInputDefaultText();

	// Change event links to alternate url with output suitable for modal display
	if (isDesktopBrowser) {
		$('#contentPrimary .modal h4 a, #contentSecondary .eventList h4 a').swrChangeHrefByPageId({
				pageIdClassPrefix:		'pageid',
				pageIdClassIdRegex:		'[0-9]+',
				replacementHrefPrefix:	'/about-us/events/modal-display?pageid=',
				replacementHrefSuffix:	''
		});
	}
	
	// Add modal behavior to event lists
	if (isDesktopBrowser) {
		$('#contentPrimary .modal h4 a, #contentSecondary .modal h4 a').colorbox({initialWidth: '100px', initialHeight: '100px', width:'50%', height:'70%', title:'Event Details', iframe:true});
	}
		
	// Add modal behavior to links to videos
	if (isDesktopBrowser) {
		$('#contentPrimary a.video, #contentSecondary a.video').colorbox({initialWidth: '100px', initialHeight: '100px', innerWidth:640, innerHeight:385, iframe:true});
	}

	// Build 'print this' links
	$('#container p.printPage').swrBuildPrintLink({linkText: 'Print this'});
	
	// Add formatted tooltip containing full text to truncated news feed entries
	if (isDesktopBrowser) {
		$('#contentPrimary .newsfeed li[title], #contentSecondary .newsfeed li[title]').hover(
			function() {
				$(this).css('cursor','help');
			},
			function() {
				$(this).css('cursor','auto');
			}
			).tinyTips('light', 'title');
	}

	//IE < 8, Safari, Chrome -- sheesh. Replaces iebuttonfix.js		
/*	jQuery(function() { 
		$('form button').click(function() {
		var obj = $(this);
		$('button').attr('disabled', 'disabled'); 
		var label = obj.text(); 
		obj.text('');
		obj.after($("<input/>").attr('type', 'hidden').attr('name', obj.attr('name')).val(obj.attr('value'))); 
		obj.text(label); 
		obj.closest('form').submit(); 
		});
	});*/
	
	// Make <button> elements behave cross-browser
	$('form button').swrButtonFix();
	
	// Hide select forms on submit - prevent re-submit & show wait message(s), if any
	$(document).ready(function() {
		$('form.hideOnSubmit').submit(function() {
			$(this).hide();
			$('body').css('cursor', 'progress');
			$('.showOnSubmit').show();
		});
	});
	
	// Enable ShareThis functionality - if the ShareThis script has been loaded for this page
	if (typeof(stLight) != "undefined") {
	// (CSS/js DOM manipulation needed to keep st's onload creation of it's wrapper followed by hide from flashing scroll bars on short pages)
	/* Disabled - ST seems to have fixed this
		$('#socialShare').bind('mouseenter.displayShareThisWrapper', function() {
			$('#stwrapper').css('display', 'block');
			$(this).unbind('mouseenter.displayShareThisWrapper');
		});
	*/
		stLight.options({publisher: '82ea6ecb-d246-453e-8399-d7960f5c5233', theme: 3, shareButtonColor: '#339733', onhover: false, headerTitle:'Oshkosh Rotary Southwest', tracking: 'google'});
	}
	
	// Display Google map if map canvas element present
	if ($("#gmapCanvas").length != 0) {
			swrInitializeLocationMap();
	}

	// Add password strength meter
	$('#wlpeUserProfilePassword, #wlpeNewPassword').passStrength({
		shortPass: 		'shortPassword',
		badPass:		'badPassword',
		goodPass:		'goodPassword',
		strongPass:		'strongPassword',
		baseStyle:		'passwordTest',
		userid:			'#wlpeUserProfileUsername'
	});
	
	// Event list grid calendar
	if (isDesktopBrowser) {
		$('#showEventList, #showEventGrid').change(function() {
			if ($(this).is('#showEventGrid') && $(this).is(':checked')) {
				swrUtility.showEventCalendarGrid();
			}
			else {
				swrUtility.hideEventCalendarGrid();
			}
		});
		
		$('#contentPrimary .eventCalendarGridSelectYear, #contentPrimary .eventCalendarGridSelectMonth').change(function() {
			swrUtility.showEventCalendarGrid();
		});
		
		$('#contentPrimary .eventCalendarGridNextMonth').click(function(event) {
			event.preventDefault();
			var $displayMonth = $('#contentPrimary .eventCalendarGridSelectMonth');
			var currentMonth = parseInt($displayMonth.val());
			if (currentMonth == 12) {
				$displayYear = $('#contentPrimary .eventCalendarGridSelectYear');
				currentYear = parseInt($displayYear.val());
				$displayYear.val(currentYear + 1);
				newMonth = 1;
			}
			else {
				newMonth = currentMonth + 1;
			}
			$displayMonth.val(newMonth);
			swrUtility.showEventCalendarGrid();
		});
		
		$('#contentPrimary .eventCalendarGridPrevMonth').click(function(event) {
			event.preventDefault();
			if ($(this).data('disabled')) { return false; }
			var $displayMonth = $('#contentPrimary .eventCalendarGridSelectMonth');
			var currentMonth = parseInt($displayMonth.val());
			if (currentMonth == 1) {
				$displayYear = $('#contentPrimary .eventCalendarGridSelectYear');
				currentYear = parseInt($displayYear.val());
				$displayYear.val(currentYear - 1);
				newMonth = 12;
			}
			else {
				newMonth = currentMonth - 1;
			}
			$displayMonth.val(newMonth);
			swrUtility.showEventCalendarGrid();
		});
	}
    
    // Generates footnote list of link destinations
    var $footnotableLinks = $('#contentPrimary a[href], #contentSecondary a[href]');
    if ($footnotableLinks.length > 0) {
        $('#footer').prepend('<div id="linkFootnotes"><h4>Link Addresses</h4><ul id="linkFootnoteList" class="footnotes"></ul></div>');

        $('#contentPrimary a[href], #contentSecondary a[href]').each(function(index) {
            var footnoteIndex = index + 1;
            var $linkElement = $(this);
            var linkElementHref = $linkElement.attr('href');
            
            if (linkElementHref.toLowerCase().indexOf('http:') < 0 && linkElementHref.toLowerCase().indexOf('mailto:') < 0) {
                if (linkElementHref.indexOf('/') == 0) {
                    linkElementHref = linkElementHref.substr(1);
                }
                linkElementHref = 'http://www.OshkoshRotarySouthwest.org/' + linkElementHref;
            }
            if (linkElementHref.toLowerCase().indexOf('mailto:') == 0) {
                linkElementHref = linkElementHref.substr(7);
            }
            
            $linkElement.after('<sup class="linkFootnote">' + footnoteIndex + '</sup>');
            $('#linkFootnoteList').append('<li><sup>' + footnoteIndex + '</sup> ' + linkElementHref + '</li>');
        });
    }

	// Language translation
	if (swrUtility.getUserLanguage() != 'en') {	// If visitor's computer reports a default language other than English
		var savedTranslationLanguage = swrCookie.read('translationlanguage');
		if (savedTranslationLanguage != 'en') {		// If the user hasn't previously declined translation (cookie value will be 'en' if translation declined)
			$.getScript('scripts/jquery.translate-1.4.7.js', function() {	// Lazy-load translation plugin & do translation in the lazy-load's callback
				$.translate.ready(function() {
					var savedTranslationLanguage = swrCookie.read('translationLanguage');
					
					// If a translation language (other than 'en') was saved, use that, otherwise get from browser
					if (savedTranslationLanguage) {
						var toLanguageCode = savedTranslationLanguage;
					}
					else {
						var toLanguageCode = swrUtility.getUserLanguage();
					}
					
					// If not already seen, build & display translation notice in user's language in modal box
					if (! swrCookie.read('translationmessageseen')) {
						var toLanguageName = $.translate.toLanguage(toLanguageCode, 'capitalize');
						var translationNotice = '<div class="modalBody" style="width: 100%; height: 100%; background-color: #6f9cd8; padding: 10px; text-align: left;"><h1>Translate this Web Site?</h1><p style="margin-bottom: 1.3em;">This Web site is written in English. It appears that you usually browse the Web in ' + toLanguageName + '.</p><p style="margin-bottom: 1.3em;">Would you like to have the pages of this site "machine-translated" as you browse the site?</p><p style="margin-bottom: 1.3em;"><button onclick="swrCookie.create(\'translationlanguage\', \'' + toLanguageCode + '\', 3650); swrCookie.create(\'translationmessageseen\', \'true\', 3650); $.colorbox.close()">Yes, translate the site</button></p><p style="margin-bottom: 1.3em;"><button onclick="swrCookie.create(\'translationlanguage\', \'en\', 3650); swrCookie.create(\'translationmessageseen\', \'true\', 3650); location.reload(true);">No, do not translate the site</button></p></div>';

						$.translate(translationNotice, 'en', toLanguageCode, {complete: function(translation) {
							$.colorbox({initialWidth: '100px', initialHeight: '100px', width: '50%', height: '50%', html: translation});
						}});
					}
					
					// Translate page contents
					$('#container').translate('en', toLanguageCode, {toggle: true});
					$('#container *[title]').translate('en', toLanguageCode, {toggle: true, subject: 'title'});	// Have to do this separately; translate() method does not recursively find title attributes
				});
			});
		}
	}

});


