﻿$(document).ready(function(){
	try{		
		
		if($('.banner-item').size() > 0){
			initBannerImages();
		}
	}
	catch(e) {}
	
	//HideRibbon();
	FooterLinks();
	StyleCustomWebParts();
	StyleCalendar();
	StyleBlogPages();
	StyleWelcomeControl();
	StyleSearchPage();
	ShowHideEventFilter()
	

});

function StyleSearchPage() {
	try {
		//hide refinements if no refinements available
		if($('span.ms-searchref-msg').text() == 'No refinements available') {
			$('div.ms-searchref-main').parents('table.s4-wpTopTable').hide();	
		}
	}
	catch(e) {}
}

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function ShowHideEventFilter() {
	try {
		var title = $("td.ms-WPHeaderTd[title*='AOA Calendar Filter']").find('.ms-WPTitle span').text();
		var showhide = '<a name="cal" id="cal"></a><div id="eventsFilterShowHide">Show Filter</div>';
		$("td.ms-WPHeaderTd[title*='AOA Calendar Filter']").first('.ms-WPTitle h3 span').html('<h3 class="ms-standardheader ms-WPTitle" style="text-align: justify;"><nobr><span><div id="eventFilterTitle">'+title+'</div>'+showhide+'</span></nobr></h3>');
		
		var vars = getUrlVars();
		if (vars["cfilter"] != '1')
			$("td.ms-WPHeaderTd[title*='AOA Calendar Filter']").parents('.s4-wpTopTable').find('.ms-WPBorder').hide();		
		
		$("td.ms-WPHeaderTd[title*='AOA Calendar Filter']").first('.ms-WPTitle h3 span').click(function() {

			if($(this).find('#eventsFilterShowHide').text() == "Show Filter") {
				$(this).find('#eventsFilterShowHide').text('Hide Filter');
				$(this).parents('.s4-wpTopTable').find('.ms-WPBorder').show();
			}
			else {
				$(this).find('#eventsFilterShowHide').text('Show Filter');
				$(this).parents('.s4-wpTopTable').find('.ms-WPBorder').hide();			
			}
		});
		
		//if (vars["CalendarDate"])
		//{
			//setTimeout("location.href = location.href + '#cal'", 1000);
			//setTimeout("ScrollToElement(document.getElementById('cal'));", 1000);
		//}
		//		$("td.ms-WPHeaderTd[title*='AOA Calendar Filter']").parent
//		$('h3.ms-WPTitle span').each(function()).text('AOA Calendar Filter')
	}
	catch(e) { }
}

function ScrollToElement(theElement){

  var selectedPosX = 0;
  var selectedPosY = 0;
              
  while(theElement != null){
    selectedPosX += theElement.offsetLeft;
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }
                        		      
 window.scrollTo(selectedPosX,selectedPosY);

}


function HideRibbon() {

	try {
		/*if($('a.s4-signInLink').size() == 0){
			$('#s4-ribbonrow').show();
		}*/
		
		if($('#s4-ribbonrow').size() == 0) {
			$('#s4-workspace').css('overflow-y','auto');
			$('body').css('overflow-y','scroll');
		}
		
	}	
	catch(e) {}
}

function DropdownMenu() {
	var menuStatus = readCookie('welcomeMenuOpened');
            if(menuStatus == null){
                  $('.welcomeUserMenu').slideDown();
                  createCookie('welcomeMenuOpened', 'true');

            var welcomeMenuTimer = setTimeout("$('.welcomeUserMenu').slideUp();", 10000);
            $('.welcomeUserDisplayName').mouseenter(function(){
                  clearTimeout(welcomeMenuTimer);
                  $('.welcomeUserMenu').stop(true, true);
                  $('.welcomeUserMenu').slideDown();
            });

            $('.welcomeUserMenu').mouseenter(function(){
                  clearTimeout(welcomeMenuTimer);
                  $('.welcomeUserMenu').stop(true, true);
                  $('.welcomeUserMenu').show();
            });

            }
}

function StyleWelcomeControl() {
	try {
		/*$('.welcomeUserDisplayName').attr('title','AOA User Menu');
		$('.welcomeUserDisplayName').mouseenter(function() {
			$('.welcomeUserMenu').show(300);
		});
		$('.welcomeUserMenu').mouseenter(function() {
			$(this).show();
		});
		$('.welcomeUserMenu').mouseleave(function() {
			$(this).hide(300);
		});*/
		
		$('.welcomeUserDisplayName').mouseenter(function(){
			clearTimeout(welcomeMenuTimer);
			$('.welcomeUserMenu').stop(true, true);
			$('.welcomeUserMenu').slideDown();
		});
		$('.welcomeUserMenu').mouseenter(function(){
			clearTimeout(welcomeMenuTimer);
			$('.welcomeUserMenu').stop(true, true);
			$('.welcomeUserMenu').show();
		});
		$('.welcomeUserDisplayName,.welcomeUserMenu').mouseleave(function(){
			welcomeMenuTimer = setTimeout("$('.welcomeUserMenu').slideUp();", 1000);
		});
	}
	catch(e) {}
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}

function StyleBlogPages() {
	
	try {
		// add header image to blog archive sites
		var blogpath = '/inside-aoa/news-and-publications/blogs/';
		var pathname = window.location.pathname;
		if(pathname.search(blogpath) >= 0) {
			// we are in the blog site
		    var blogArchivePath = '/inside-aoa/news-and-publications/blogs/archive/';
			if(pathname.search(blogArchivePath) >= 0) {
				// we are in the blog archive site
				blogpath = blogArchivePath;
			}
			var blogsite = pathname.replace(blogpath,'');
			var pathnodes = blogsite.split('/');
			if(pathnodes.length > 1) {
				var blog = pathnodes[0];			
				var page = pathnodes[1];
				
				var imageSource = blogpath + blog + '/PublishingImages/header.jpg';
				if(blog != "Pages")
				{
					// add header image to page
					var imageSource = blogpath + blog + '/PublishingImages/header.jpg';
					$('#masterColumnRight').prepend('<img style="margin-top:-10px;margin-bottom:6px;" src="' + imageSource + '" />');
				}
			}		
		}
	}
	catch(e) {}

}

function StyleCalendar() {
	try {
		// add icons to select 
		$('select[id$=_ddlEventTypes] option').each(function() {
			switch($(this).text()) {
				case "CME":
					$(this).addClass('cme-filter');
					break;
				case "AOA Event":
					$(this).addClass('aoaevent-filter');
					break;
				case "Other Osteopathic Business":
					$(this).addClass('certificationexam-filter');
					break;
				case "Certification Exam":
					$(this).addClass('otherosteopathicbusiness-filter');
					break;
				case "COM Activities":
					$(this).addClass('comactivities-filter');
					break;
				case "Promotion/Advocacy":
					$(this).addClass('promotionadvocacy-filter');
					break;				
			}		
		});
		// style filter button
		$('input[id$=_btnFilter]').addClass('button');
		$('input[id$=_btnFilter]').addClass('filter-button');
		$('input[id$=_btnClear]').addClass('button');
		
		//event edit
		$('.editEventSection select[id$=_ddlOrganizations]').addClass('orgSelect');
		
		$('#MontlyViewDefault_CalendarView').css('overflow','');
	}
	catch(e) {}

}

function StyleCustomWebParts() {
	try {
		$('.relatedContent:even').addClass('alt');
		$('table.grid tr:odd td').addClass('alt');
	}
	catch(e) {}
		
}
function FooterLinks() {
	try {

		$('#aoaTabs li.dynamic-children').each(function() {
			var linkText = $(this).find('a.static .menu-item-text').text();

			if(linkText == 'Inside the AOA') {
				$('#footer .insideLinks .links').html($(this).find('ul.dynamic').html());		
			}
			else if(linkText  == 'Osteopathic Medicine and Your Health') {
				$('#footer .osteoLinks .links').html($(this).find('ul.dynamic').html());
			}		
		});
	}
	catch(e) {}

}

// banner image slider
var bannerTransitionTime = 500;
var bannerDisplayTime = 8000;
var bannerSlideTimer = null;
var allowTransition = true;

function initBannerImages(){
	try {
		$('.banner-item:not(.banner-item:eq(0))').hide();
		$('.banner-item:eq(0)').parent().parent().wrap('<div id="banner-item-wrapper"/>');
		
		// build banner item navigation
		var navList = $('<ul id="banner-item-nav" />');
		$('.banner-item').each(function(i){
			$(navList).append('<li><a href="javascript:nextBannerSlide(' + i + ');">' + (i + 1) + '</a></li>');
		});
		$(navList).find('li:eq(0)').addClass('active');
		$('#banner-item-wrapper').append(navList);
		
		bannerSlideTimer = setTimeout('nextBannerSlide();', bannerDisplayTime);
	}
	catch(e) {}
}

function nextBannerSlide(bannerNo){
	try {
		clearTimeout(bannerSlideTimer);
		if(allowTransition){
			allowTransition = false;
			var nextBanner = $('.banner-item:visible').parent().next('li').children('.banner-item');
			if($(nextBanner).size() == 0){
				nextBanner = $('.banner-item:eq(0)');
			}
			if(bannerNo != null){
				nextBanner = $('.banner-item:eq(' + bannerNo + ')');
			}
			if($(nextBanner).css('display') == 'none'){
				$('.banner-item:visible').fadeOut(bannerTransitionTime);
				$(nextBanner).fadeIn(bannerTransitionTime, function(){
					bannerSlideTimer = setTimeout('nextBannerSlide();', bannerDisplayTime);
					var nextNav = $('#banner-item-nav li.active').next('li');
					if($(nextNav).size() == 0){
						nextNav = $('#banner-item-nav li:eq(0)');
					}
					if(bannerNo != null){
						nextNav = $('#banner-item-nav li:eq(' + bannerNo + ')');
					}
					$('#banner-item-nav li').removeClass('active');
					$(nextNav).addClass('active');
					allowTransition = true;
				});
			}
			else{
				allowTransition = true;
				bannerSlideTimer = setTimeout('nextBannerSlide();', bannerDisplayTime);
			}
		}
	}
	catch(e) {}		
}

// override sharepoint scrolling behavior
function FixRibbonAndWorkspaceDimensions(){
  ULSxSy:;
  g_frl = true;
  var elmRibbon = GetCachedElement("s4-ribbonrow");
  var elmWorkspace = GetCachedElement("s4-workspace");
  var elmTitleArea = GetCachedElement("s4-titlerow");
  var elmBodyTable = GetCachedElement("s4-bodyContainer");
  if(!elmRibbon || !elmWorkspace || !elmBodyTable){
    return;
  }
  if (!g_setWidthInited){
    var setWidth = true;
    if (elmWorkspace.className.indexOf("s4-nosetwidth") > -1)
      setWidth = false;
    g_setWidth = setWidth;
    g_setWidthInited = true;
  }
  else{
    var setWidth = g_setWidth;
  }
  var baseRibbonHeight = RibbonIsMinimized() ? 44 : 135;
  var ribbonHeight = baseRibbonHeight + g_wpadderHeight;
  if(GetCurrentEltStyle(elmRibbon, "visibility") == "hidden"){
    ribbonHeight = 0;
  }

  // Override default resizing behavior
  // -- adds padding to the top of the "s4-workspace" <div> if the ribbon exists and has content
  // -- allows the ribbon to be positioned using CSS instead of JavaScript (more accessible)
  // -- checks to see if the page is inside a "no-ribbon" dialog
  if(elmRibbon.children.length > 0 && document.getElementsByTagName("html")[0].className.indexOf('ms-dialog-nr') == -1){
    elmWorkspace.style.paddingTop = ribbonHeight + 'px';
  }
}
