﻿
/*
	side bar grow to bottom
*/
/*
$(document).ready(function() {
	var $contentHeight = $('#hhnArticleContent').outerHeight();
	var $sideHeight = $('#hhnArticlePageSideNav').outerHeight();
	
	if ($contentHeight > $sideHeight)
	{
		$('#hhnArticlePageSideNav').css('height', $contentHeight + 'px');
	}
});
*/

/*
	Builds article links
*/
var articleLinkBuilder = function(where, what) {
	// 1. where to inject
	// 2. what to inject
	// 3. condition for injection
};

/*
	build navigation items (side bar and header)
*/
$(document).ready(function() {
	$('#hhnArticlePageSectionNavLinks').append("<ul></ul>")					   
    var $sideBarList = $('#hhnArticlePageSectionNavLinks ul');
    $sideBarList.empty();
    var $sectionIndex = 0;
    $('.hhnArticleSection').each(function() {
        // for reach article section 
        // 1. add <li><a href=#{id}>{'hhnArticleSectionNavTitle'.text}</a></li>
        var $child = $(this).find('h1.hhnArticleSectionNavTitle');
        var $navText = $child.text();
        //var $navId = $(this).attr('id');

        var $navId = 'hhnArticle-Segment-' + $sectionIndex;
        $(this).attr('id', $navId);

        var $li = '<li><a href="#' + $navId + '">' + $navText + '</a></li>';
        $sideBarList.append($li);

        $sectionIndex++;
    });
	
	//$('.hhnArticlePageDownloadArticle').prepend('<img src="/images/PdfDoc.png" alt="Pdf" />&nbsp;');
	$('#hhnArticleFeedback').prepend('<img src="/images/EmailIcon.png" alt="Feedback" />&nbsp;');
	$('#hhnArticlePageSideNav a[href$=pdf]').prepend('<img src="/images/PdfDoc.png" alt="Pdf" />&nbsp;');
	$('.ArticleTxt a[href$=pdf]').prepend('<img src="/images/PdfDoc.png" alt="Pdf" />&nbsp;');
});

/* 
	build other navigation items
*/
$(document).ready(function() {
	/*
	$('#hhnArticleRecommendedLinks h1').append("<ul></ul>");						   
	var $listTag = $('#hhnArticleRecommendedLinks ul');
	$listTag.empty();
	
	$('a.hhnFurtherReading').each(function() {
		var $li = '<li><a href="' + $(this).attr('href') + '">' + $(this).text() + '</a></li>';
		$listTag.append($li);
	});
	*/
});

$(document).ready(function() {
    var $sections = [];
    $('.hhnArticleSection').each(function() {
        // for reach article section 
        /*
        <div class="hhnArticleSectionNav">
        <ul>
        <li class="hhnNavFirst"><a href="#"><img src="images/Player fastrwd.png" /><span>First</span></a></li>
        <li class="hhnNavNext"><a href="#"><img src="images/Player next.png" /><span>Next</span></a></li>
        <li class="hhnNavPrev"><a href="#"><img src="images/Player previous.png" /><span>Prev</span></a></li>
        <li class="hhnNavLast"><a href="#"><img src="images/Player fastfwd.png" /><span>Last</span></a></li>
        </ul>
        </div>
        */
        var $header = $(this).find('.hhnArticleSectionHeader');
        var $navId = $(this).attr('id');

        $sections.push({ id: $navId, header: $header });
    });

    for (var $i = 0; $i < $sections.length; $i++) {
        var $navFirst = ($i != 0) ?
			'<li class="hhnNavFirst"><a href="#">Top</a></li>\n' :
			'';

/*
        var $navNext = ($i != $sections.length - 1) ?
			'<li class="hhnNavNext"><a href="#' + $sections[$i + 1].id + '"><img src="/images/Player next.png" /><span>Next</span></a></li>\n' :
			'';

        var $navPrev = ($i != 0) ?
			'<li class="hhnNavPrev"><a href="#' + $sections[$i - 1].id + '"><img src="/images/Player previous.png" /><span>Prev</span></a></li>\n' :
			'';

        $navPrev = ($i == 1) ?
			'<li class="hhnNavPrev"><a href="#"><img src="/images/Player previous.png" /><span>Prev</span></a></li>\n' :
            $navPrev;

        var $navLast = ($i != $sections.length - 1) ?
			'<li class="hhnNavLast"><a href="#' + $sections[$sections.length - 1].id + '"><img src="/images/Player fastfwd.png" /><span>Last</span></a></li>\n' :
			'';
*/

		if ($navFirst != '')
		{
			var $navText = '<div class="hhnArticleSectionNav">\n' +
						   '   <ul>\n' +
						   //$navFirst + $navNext + $navPrev + $navLast +
						   $navFirst
						   '   </ul>\n' +
						   '</div>';
	
			$sections[$i].header.append($navText);
		}
    }
});

/*
drop shadow effects
*/
$(document).ready(function() {
    var opts = {
        left: 2,
        top: 2,
        blur: 2,
        opacity: 0.5,
        color: '#333',
        swap: false
    }
	
	var textOpts = {
        left: 0,
        top: 0,
        blur: 4,
        opacity: 0.5,
        color: '#000',
        swap: false
	}

	if (!$isIE6OrBelow)
		$('#hhnArticlePageBannerText img').dropShadow(textOpts);
    //$('.hhnArticleSectionHeader h1').dropShadow(textOpts);
	//$('#hhnArticlePageSideNav h1').dropShadow(textOpts);
	$('#hhnArticlePhoto').dropShadow(opts);
    //$('#hhnArticlePageSideNav a').dropShadow(textOpts);
	
});

/*
    Load Handbook Sidebar
*/
$(document).ready(function() {
	$('#hhnHandbookSideBarContent').load('/snipets/handbookSidebar.htm');
});

$(document).ready(function() {
	// click the a opens the panel
	// move off the a starts a timer
	//     if within timeperiod panel receives mouse over, timeperiod is canceled and panel remains open.
	//     else timeperiod expires and the panel is closed.
	// mouseout on the panel closes the panel.
	// Panel (panel, parent, open(), close(), setTimer(), endTimer()
	var panel = {
		panel: null,
		parent: $('#hhnArticleFeedback'),
		timer: null,
		isOpen: false,
		panelWidth: null,
		panelHeight: null,
		stayOpen: false,
		
		showPanel: function() {
			if (this.panel == null)
				this.init();
				
			// position panel
			var offset = this.parent.offset();
			
			// where the element is in the viewport.
			//alert(offset.top);
			// where the top of the page is.
			//alert(window.pageYOffset);
			var toTopLength = offset.top - window.pageYOffset;
			var toBottomLength = window.innerHeight - toTopLength - this.parent.height();
			
			this.panel.height(280);
			this.panel.width(440);
			var leftPos = offset.left - this.panel.outerWidth();
			var centerLine = offset.top - (this.parent.outerHeight() / 2);
			var topForCentered = centerLine - (this.panel.height() / 2);
			
			// enough room?
			// how much room do i need towards top
			//var parentToTopPanelLength = topForCentered - offset.top;
			//var okToTop = toTopLength > parentToTopPanelLength;
			//var okToBot = toBottomLength > this.panel.height();
			//if (okToTop && okToBot || (!okToTop && !okToBot))
				this.panel.offset({ top: topForCentered, left: leftPos });
			//else if (okToTop) // implies !okToBot
			//{ // bottoms should line up.
			//	var parentBot = offset.top - this.parent.height();
			//	var useTop = parentBot - this.panel.height();
			//	this.panel.offset({ top: useTop, left: leftPos });
			//}
			//else if (okToBot) // imples !okToTop
			//{ // align tops
			//	this.panel.offset({ top: offset.top, left: leftPos });
			//}

			this.panel.css('border', 'solid 1px #000');
			this.panel.show();
			this.panel.dropShadow();
			
			this.isOpen = true;
		},
		hidePanel: function() {
			this.isOpen = false;
			this.stayOpen = false;
			window.clearTimeout(this.timer);
			this.panel.removeShadow();
			this.panel.hide();
		},
		togglePanel: function() 
		{
			if (this.isOpen)
				this.hidePanel();
			else
				this.showPanel();
		},
		init: function() {
			// grab panel from contact us
			this.panel = $('#hhnContactUsPanel').clone();
			this.panel.removeAttr('id');
			this.panel.attr('id', 'hhnFeedbackPanel');
			this.panel.css('display', 'none');
			this.panel.appendTo('#hhnArticlePageSideNav');
			this.panelHeight = this.panel.height();
			this.panelWidth = this.panel.width();
			
			var thePanel = this;
			this.parent.hover( function() {}, function() {
				// start timer.
				thePanel.timer = window.setTimeout(function() 
				{ 
					// timer is done
					// if requested no request to stay open then close
					if (!thePanel.stayOpen)
						thePanel.hidePanel();
				}, 500);
			});
			
			this.panel.hover(function() 
			{
				thePanel.stayOpen = true;
				window.clearTimeout(thePanel.timer);
			},
			function() 
			{
				thePanel.stayOpen = false;
				window.clearTimeout(thePanel.timer);
				thePanel.hidePanel();
			});
		}
	};
	
	panel.parent.click(function($e) {
		$e.preventDefault();
		panel.togglePanel();
	});
});

