﻿/*
INIT.JS 
--------
Functions should run only after the content has loaded.
SEE BOTTOM OF FILE FOR INIT CALLS.

Last modified: 
2009-05-01 sshepar3:
	Consolidated init.js with textpager.js.  
	Moved core.js fixes into here from masterpage.
----------------------------------------------------------------------
Description : Optimized code for the ford refresh
Last modified Date: 2011-10-21
Last modified By: slittl31
----------------------------------------------------------------------
Description : headline - html is null or not a object error fix 
Last modified Date: 2011-10-21
Last modified By: sshanth2
----------------------------------------------------------------------



/*
These functions are executed after the page loads.
*/
function initCDUPresentation(){
	addSearchWrapper();
	
	//Updates the copyright year.
	var todaysDate = new Date();
	var year = todaysDate.getFullYear();
	$("#lbl_Year").text(year);	

	/*
	// Set "All" as the default in the Search menu and then hide it. 
	$('#search_controls select[id$=SBScopesDDL]', thisHtml).val("").hide();

	// Adjust the widths of search functions
	$('#ctl00_PlaceHolderSearchArea_ctl01_SBScopesDDL', thisHtml).css('width','135px');
	$('#search_wrapper input[id*="InputKeywords"]', thisHtml).css('width', '130px');
	$('#ctl00_PlaceHolderSearchArea_ctl01_SBScopesDDL', thisHtml).appendTo($('#search_wrapper input[id*="InputKeywords"]', thisHtml).parent());
	*/
	
	// Create a "set home Page" link for IE users.
	if (document.all){
		var setHomePageEl = $('#setHomePage');
		setHomePageEl.html('<a href="javascript:;">Make This My Home Page</a>');
		$('a', setHomePageEl).click(function() { 
			this.style.behavior='url(#default#homepage)';
			this.setHomePage('http://www.at.ford.com');
		});
	}
	
	// fix table heights in IE6
	if (document.all){
		$('table.ms-cal-tweekitem, table.ms-cal-tdayitem').each(function(i, tt) {
			var h = $(tt.parentNode.parentNode).attr('scrollHeight') * $(tt.parentNode).attr('rowspan');
			$(tt).css('height', h);
		});
	}

	/* Check for Sharepoint logged-in user and set content appropriately. */
	var authorizedUser = $('input[id="isLocal"]').length == 0 ? false : true;
	if (authorizedUser) {
		$('.authShow').show();
		$('.authHide').hide();
	} else {
		$('.unAuthShow').show();
		$('.unAuthHide').hide();
	}
		
	/* Modified FBA hide and Show - Mahanth */
	var externalUser;
	/* -- Try looking for My Links	*/
	var welcomeHTML = $('#navglobal_primary').html();
	if(welcomeHTML.indexOf('text="My Links"') > -1) {
		// user is internal
		externalUser = false; 	   
		$('.authShow').show();
		$('.unAuthShow').hide();
		// Show internal search and hide external		
		$('#extSearch_wrapper').hide();	
		$('#search_wrapper').show();
	} else {
	// user is external - FBA User
		externalUser = true;   		
		/* hide internal search and show external search to everyone (anon + fbauser) */
		$('#search_wrapper').hide();
		$('#extSearch_wrapper').show();
		/*-------------------- Hide Banner  -------------------- */		
		$('#divBannerInternalOnly').hide();
		$('li[id="cds_li"]').hide();		
	}
	$('#google_search').hide();		

	if(externalUser != undefined){
		var fbaUser = externalUser && authorizedUser ? true : false;
		if (fbaUser) {
			// FBA - Logged-in User	
			$('.fbaHide').hide();
			$('.fbaShow').show();
			/*-------------------- Hide Banner  -------------------- */		
			$('#divBannerInternalOnly').hide();
			$('li[id="cds_li"]').hide();
			
		} else {
			// FBA - anonymous user
			$('.fbaShow').hide();
			if(externalUser){
				$('.authShow').hide();			
				$('.unAuthShow').show();			
			}
			
		}
	}
	
	// soft-insert the Textpager Form so Sharepoint doesn't fail when saving the masterpage
	//$('#sendTextForm').wrap('<form action="http://vm4.dearborn.ford.com/cgi/textpage" target="_blank" method="post" name="page_754" id="textPagerForm" />');
		
	// Activate drop-down menu
	$('.nav_primary').navMenu();
	

	//Hide all empty areas
	var landingPage = $('#landingPageWithImages, #landingPageNoImage');
	if(landingPage.length > 0) {

		var topics = $('.topic', landingPage);
		if(topics.length > 0) {
			var maxHeight = 0;
			topics.each(function(i, topic){
				// Remove all topics that do not have a title when not in edit mode
				if (!window.editMode){
//					if ($('a', $('h6', topic)).length == 0) {
					if ($('h6 a', topic).length == 0 && ($('p', topic).length < 10)) {
						$(topic).remove();
					} else {
						//Remove all elements with class photo that don't have an image element
						var photoContainer = $('.photo', topic);
						if(photoContainer.find('img').attr('src') == undefined) {
							$(photoContainer).remove();
						}

						//Grab all the children of the element with class topic including Text Nodes
						//If any of the children are empty, then remove them.
						//NOTE: Need to replace &nbsp; as the jQuery trim method is not catching it when grabbing the HTML
						var fullTopic = $(topic).contents();
						fullTopic.each(function(index, element) {
							if($(element).html() != null) {
								$(element).html($.trim($(element).html().replace('&nbsp;', ' ').replace(/&nbsp;|\n|$/, ' ')));
							}
							if($.trim($(element).text()).length == 0) {
								$(element).remove();
							}
						});						
					}
				}

				//Adjust the height of all topics to be the same
				var height = parseInt($(topic).css('height').replace('px', ''));
				if(maxHeight < height) {
					maxHeight = height;
				}

			});
			topics.css('height', maxHeight);	
		}
		
		//Grab all the children of the element with class pageDescription including Text Nodes
		//If any of the children are empty, then remove them.
		//NOTE: Need to replace &nbsp; as the jQuery trim method is not catching it when grabbing the HTML
		var fullPageDescription = $('.pageDescription', landingPage).contents();
		fullPageDescription.each(function(index, element) {
			if($(element).html() != null) {
				$(element).html($.trim($(element).html().replace('&nbsp;', ' ')));
			}
			if($.trim($(element).text()).length == 0) {
				$(element).remove();
			}
		});						
	}

	//Remove unwanted &nbsp; that sharepoint designer adds randomly, if a headline exists
	var headline = $('#heading h1, h1.title');	
	if(headline.html())	{
		headline.html($.trim(headline.html().replace('&nbsp;', ' ')));
	}


	//Adjust the height of all stories to be the same
	var publicationsList =  $('#publicationsList');
	if(publicationsList.length > 0) {

		var storyGroups = $('.storyGroup', publicationsList);
		if(storyGroups.length > 0) {
			storyGroups.each(function(i, storyGroup){
				var maxHeight = 0;
				var stories = $('.story', storyGroup);
				var hasImage = $(storyGroup).find('img').length > 0;
				if(hasImage) {
					$(storyGroup).find('dd').css('min-height', '66px');
				}
				stories.each(function(j, story){			
					var height = parseInt($(story).css('height').replace('px', ''));
					if(maxHeight < height) {
						maxHeight = height;
					}
					
				});
				stories.css('height', maxHeight);
			});
		}
	}
}

function toggleRibbon(state) {
/*
	if(state == 'show' || window.location.href.indexOf('Tools/EventsCalendar/Pages/default.aspx') != -1) {
		$('#s4-ribbonrow').show();
	} else if(state == 'hide') {
		$('#s4-ribbonrow').hide();
	}
*/
}

