/* ---------- jQuery Events ---------- */

/*
 * Whenever the user navigates in the 'siteContent' frame, the portal_nav close link (in the other frame) has its 'href' attribute updated to reflect the actual page being viewed.
 * This is necessary because the <frame src> attribute is NOT updated when the user navigates within that frame.
 * Then, when the portal_nav close link is clicked, it must be handled specially to "break out" of the frame.
 */
jQuery(document).ready(function() {

	// init site content frame
	jQuery('#portalframe_siteContent').load(function() {
		// register on-load of site content
		var currentLocation;
		try{
			if(jQuery.browser.msie) {
				currentLocation = jQuery('#portalframe_siteContent').get(0).contentWindow.location.href;
			} else {
				currentLocation = jQuery('#portalframe_siteContent').get(0).contentDocument.location.href;
			}
		} catch(e) {
			// do not allow 
			currentLocation = jQuery('#portalframe_siteContent').attr('src');
		}

		// hard-coded list of pages that hide the nav bar. all others show the nav bar.
		if (
			currentLocation.indexOf('/ma/index.jsp') != -1
			|| currentLocation.indexOf('http://blog.ma.collegeview.com') != -1
			) {
			setVisiblePortalNav(false);
		} else {
			setVisiblePortalNav(true);
		}

		// now change the 'href' attribute of the close button in the header
		jQuery('#portalframe_nav').each(function() {
			var docPortalNav;
			if(jQuery.browser.msie) {		
				docPortalNav = jQuery(this.contentWindow.document);
			} else {
				docPortalNav = jQuery(this.contentDocument);
			}

			docPortalNav.find('a#portal_close').attr('href', currentLocation);
		}); // end frame-each
	}); // end frame-load

	// init nav frame
	jQuery('#portalframe_nav').load(function() {
		// hijack the portal_nav close link action so that when clicked, it will: 1) abandon frames and 2) navigate to the content frame address
		var docPortalNav;
		if(jQuery.browser.msie) {		
			docPortalNav = jQuery(this.contentWindow.document);
		} else {
			docPortalNav = jQuery(this.contentDocument);
		}

		docPortalNav.find('a#portal_close').each(breakoutFrame);

		docPortalNav.find('a#portal_logo_state').each(openInOtherFrame);
		docPortalNav.find('a#portallink_home').each(openInOtherFrame);
		docPortalNav.find('a#portallink_findcollege').each(openInOtherFrame);
		docPortalNav.find('a#portallink_contact').each(openInOtherFrame); // took easy way out - page does NOT use form. instead uses URL which allows us to treat it normally here
		docPortalNav.find('a#portallink_learnabout').each(openInOtherFrame); // note that all blog links use the sub-domain, and the frame solution keeps that sub-domain out of the addr bar

	}); // end frame-load

}); // end doc-ready


/* ---------- Functions ---------- */

/*
 * Callable by jQuery on a <a> tag to cause the target page to be loaded without frames.
 */
function breakoutFrame() {
	jQuery(this).click(function(e) {
		e.preventDefault(); // suppress normal navigation
		window.location = this.href;
	});
}

/*
 * Callable by jQuery on a <a> tag to cause the target page to be loaded in the 'siteContent' frame.
 */
function openInOtherFrame() {
	jQuery(this).click(function(e) {
		e.preventDefault(); // suppress normal navigation
		jQuery('#portalframe_siteContent').attr('src', this.href);
	});
}

/*
 * Hides/shows the nav frame as indicated by 'hideNav'.
 * This hide/show is controlled by maniuplating the 'rows' attribute of the parent <frameset>.
 */
function setVisiblePortalNav(visible) {
	// must show/hide by height, due to limitation of frame/frameset API. height=44 MUST match the height of the /ma/nav_header.jsp
	jQuery('#portalframeset').attr('rows', visible ? '44, *' : '0, *');
}
