/**
 * @desc Page manipulations that need to be made before dom ready.
 * @author Shawn Melton <shawn.melton@webteks.com>
 * @version $id$ 
 */
(function($){
	/**
	 * @desc Tear up the url to get the equivalent to the request URI.
	 * @return String containing request uri.
	 */
	var parseHref = (function() {
		var hrefParts = location.href.split("/");
		
		// 0, 1 & 2 make up the domain.  3 is first folder.
		var hrefStr = "";
		var stop = hrefParts.length - 2;
		if( hrefParts.length < 5 ) {
			stop = hrefParts.length - 1;
		}
		
		for( var i = 3; i <= stop; i++ ) {
			if( hrefParts[i] && hrefParts[i] != "" ) {
				hrefStr += "/"+ hrefParts[i];
			}
		}
		
		return hrefStr;
	});
	

	/**
	 * @desc Navigation Trails - keep parent link of current page highlighted if there is not an active link set.
	 * Make sure we update section titles if we are able to as well.
	 */
	if( !$("#block-system-main-menu .menu .active").length ) { // There are no active links.
		var parentHref = parseHref();
		switch( parentHref ) {
			case "/news": parentHref = "/news-and-announcements"; break;
			case "/about": parentHref = "/do-not-change-this"; break;
			case "/users": parentHref = "/members"; break;
			case "/articles": 
				parentHref = "/news-and-announcements";
				
				$(".field-type-taxonomy-term-reference a").each(function(){ // If any of the issue and updates taxonomy terms are found, then mark Issues and Updates active
					if( $(this).html() == "Action Alerts" || $(this).html() == "Member Communications" || $(this).html() == "Issue Updates" ) {
						parentHref = "/issues-and-alerts";
					} 
				});
				break;
		}

		var newTitle = false;
		$("#block-system-main-menu .menu a").each(function(){
			if( $(this).attr("href") == parentHref ) {
				$(this).addClass("active");
				
				newTitle = $(this).html();
			}
		});
		
		if( newTitle == "Meetings" ) {
			newTitle = "Meeting Details";
		}
		
		if( newTitle !== false && $("h1").html() != newTitle ) { // Don't update if there is no need to.
			$("h1").html(newTitle);
		}
		
	} else if( $("h3.subHeading").length ) { // Sub pages repairs to title of page.
		var subHeading = $("h1").html();
		$("h1").html($("h3.subHeading").html());
		$("h3.subHeading").html(subHeading);
	}
	
	// Move read more link
	/*if( $(".field-name-field-article-teaser").length || $(".field-name-field-mtg-teaser").length || $(".field-type-text-with-summary").length ) {
		if( $(".links .node-readmore").length ) {
			var link = $(".links .node-readmore").html();
			$(".field-name-field-article-teaser .field-item, .field-name-field-mtg-teaser .field-item, .field-type-text-with-summary .field-item").append('<br class="readMoreClear" />'+ link);
			$(".field-name-field-article-teaser .field-item, .field-name-field-mtg-teaser .field-item, .field-type-text-with-summary .field-item").each(function(){
				$(this).find("a:last").addClass("readMore");
			});
			$(".links, .field-type-text-with-summary .readMoreClear").hide();
		}
	}*/
	
	
	/**
	 * @desc Update the copyright date in the site footer.
	 */
	var dt = new Date();
	$("#CopyrightYear").html(dt.getFullYear());
	

	/**
	 * @desc Add in Blue side bar that drifts into secondary navigation.
	 * This item will be added after the page has rendered and only on the secondary template.
	 */
	if( $(".region-sidebar-second").length ) {
		$("#content").append('<div id="BlueSidebar"></div>');
	}
	

	/**
	 * @desc Add login and logout links to site.
	 */
	if( $("#secondary-menu").length ) {
		$("#name-and-slogan").after('<a href="/user/logout" id="LogOutLink"><span>Log-Out</span></a>');
	} else {
		$("#name-and-slogan").after('<a href="/user/login" id="LogInLink"><span>Member Log-in</span></a>');
	}
	

	/**
	 * @desc On the dashboard page, the user name is a placeholder.
	 * Swap out placeholder with actual text.
	 */
	if( $("#dashboard-user-name").length ) {
		$.getJSON("/ajx/user/current", function(output){
			if( output && output.status == "Success" ) {
				if( output.response.realname != "" ) {
					$("#dashboard-user-name").html(output.response.realname);
				} else {
					$("#dashboard-user-name").html(output.response.name);
				}
			}
		});
	}
	
	
	// If there is a pager, make sure its on the top and bottom of the page.
	if( $(".pager").length ) {
		$(".pager").parent().parent().find(".view-content").before('<div class="item-list">'+ $(".pager").parent().html() +'</div>');
	}
	
	
	/**
	 * @desc Correct webforms that show title twice
	 */
	if( $("body.node-type-webform").length && $("h2.node-title").html() == $("h1").html() ) {
		$("h2.node-title").hide();
	}
	
	
	/**
	 * @desc IE 7 repair where menu was pushed down 150 px.  So we are adding a 
	 * clear to pull up main menu.
	 */
	if( $.browser.msie && $.browser.version < 8 ) {
		$("#search-block-form .form-submit").after('<br class="clear">');
	}
	
	
	/**
	 * @desc Add a placeholder for IE browsers on search form.
	 * Make sure we handle the on focus, blur and submission clauses.
	 */
	if( $.browser.msie ) {
		var input = $("#search-block-form .form-text");
		input.css("line-height", "22px");
		if( input.val() == "" ) {
			input.val("SEARCH");
		}
		
		input.focus(function() {
			if( $(this).val() == "SEARCH" ) {
				$(this).val("");
			}
		});
		
		input.blur(function() {
			if( $(this).val() == "" ) {
				$(this).val("SEARCH");
			}
		});
		
		$("#search-block-form").submit(function(){
			if( $(this).find(".form-text").val() == "SEARCH" ) {
				$(this).find(".form-text").val("");
				return false;
			}
				
			return true;
		});
	}
})(jQuery);
