function addTextSizer(){
    $('#PageToolsWrapper').after('<div id="TextSizer" class="ClearFix"><h2>Text Size</h2><ul>' +
				'<li><a class="NormalFont" href="javascript:;" title="Small Font Size"></a></li>' +
				'<li><a class="LargeFont" href="javascript:;" title="Medium Font Size"></a></li>' +
				'<li><a class="X-LargeFont" href="javascript:;" title="Large Font Size"></a></li>' +
			'</ul></div>');
	$('a.NormalFont').click(function() {$('#Content').removeClass('textLarge').removeClass('textXLarge');$.cookie('text_sizer', null, { path: '/' });});
	$('a.LargeFont').click(function() {$('#Content').addClass('textLarge').removeClass('textXLarge');$.cookie('text_sizer', 'large', { path: '/' });});
	$('a.X-LargeFont').click(function() {$('#Content').removeClass('textLarge').addClass('textXLarge');$.cookie('text_sizer', 'xlarge', { path: '/' });});
	$('#TextSizer a').click(function() {$('#TextSizer a').removeClass('active');$(this).addClass('active');});
}
function text_size_checker() {
	var text_sizer_cookie = $.cookie('text_sizer');
	if (text_sizer_cookie==null){$('a.NormalFont').addClass('active')};
	if (text_sizer_cookie=='large'){ $('#PageToolsWrapper').addClass('textLarge');$("a.LargeFont").addClass('active') };
	if (text_sizer_cookie=='xlarge'){ $('#PageToolsWrapper').addClass('textXLarge');$("a.X-LargeFont").addClass('active') };
}
function addPageTools(){
	var clientDomainName = 'genesishcs.org',
		clientSiteName = 'Genesis HealthCare System (Ohio)',
		currentPageUrl = document.location,
		currentPageTitle = $.trim($('title').text()),
		emailSubject = clientDomainName + ': ' + currentPageTitle,
		emailHref = 'mailto:?subject=' + escape(emailSubject) + '&body=I thought you would be interested in a page on the ' + escape(clientSiteName) + ' Web site: ' + escape(currentPageTitle) + ' - ' + escape(currentPageUrl);
	var urlToUse = (('https:' == document.location.protocol) ? 'https://secure.addthis.com/js/250/addthis_widget.js?pub=genesisoh' : 'http://s7.addthis.com/js/250/addthis_widget.js?pub=genesisoh');
	$('#PageToolsWrapper').append('<ul>' +
            '<li class="Email"><a href="' + emailHref + '">Email</a></li>' +
			'<li class="Print"><a href="javascript:;" onclick="print();">Print</a></li>' +
			'<li class="AddThis">' +
				'<script type="text/javascript">var addthis_pub = "genesishealth";</script>' +
					'<a href="http://www.addthis.com/bookmark.php?v=250&pub=genesisoh" onclick="return addthis_open(this, \'\', \'[URL]\', \'[TITLE]\')" onmouseout="addthis_close()"onclick="return addthis_sendto()" onclick="pageTracker._trackEvent(\'AddThis\', \'Open\');">Share</a>' +
					'<script type="text/javascript" src="' + urlToUse + '"></script>' +
			'</li>' +
		'</ul>');
}



function tabGallery() {

    var timeout = 5000,
		navTabsAllowed = 6;

    var $g = $('.TabContainer.Gallery'),
		$tabs = $g.find('.Tab'),
		$nav = $g.find('.Tabnav').wrap('<div class="TabnavWrapper"></div>'),
		$navLI = $nav.find('li'),
		$tabs = $g.find('.Tab'),
		$prev = $('<img class="Left Prev" title="Previous" src="/imgs/gallery-prev.png" alt="Previous"/>'),
		$next = $('<img class="Right Next" title="Next" src="/imgs/gallery-next.png" alt="Next"/>'),
		$navwrap = $g.find('.TabnavWrapper').before($prev).after($next),
		navLIWidth = parseInt($navLI.css('width')),
		navCount = $navLI.size();
        navHeight = $(".TabnavWrapper").height();

        //$(".Tab").css('margin-bottom', navHeight);

    // set nav width so the tabnav li are on the same line
    //$nav.width(navCount * navLIWidth)

    // --- RANDOMIZE THE NAV AND TABS TOGETHER ---
   /*
    var randomIndexes = [];
    for (var i = 0; i < navCount; i++) {
        randomIndexes[i] = i;
    }
    randomIndexes.sort(function () { return (Math.round(Math.random()) - 0.5); });
   
    for (var i = 0; i < navCount; i++) {
        $nav.append($navLI.eq(randomIndexes[i]).remove());
        $g.append($tabs.eq(randomIndexes[i]).remove());
    }
    
    var $navLI = $nav.find('li'),
			$tabs = $g.find('.Tab');
    */
    // --- END RANDOM ---


    // if loading page with #hash to show specific tab
    // if no #hash then make first tab active
    if ($.url.attr('anchor') == null) {
        $navLI.removeClass('Tabactive').eq(0).addClass('Tabactive');
        $tabs.addClass('Tabhide').eq(0).removeClass('Tabhide');
    }

    // AT PAGELOAD, SHOW THE RIGHT TAB
    animateNav($navLI.filter('.Tabactive').getIndex());
    $('.Tabnav li:last-child').addClass('Last');

    // --- CLICK EVENTS ---	
    // click next/prev
    $next.click(function () { animateNav($navLI.filter('.Tabactive').getIndex() + 1) });
    $prev.click(function () { animateNav($navLI.filter('.Tabactive').getIndex() - 1) });
    // click on tabnav
    $navLI.bind('mouseup', function () { animateNav($(this).getIndex()) });

    // --- AUTO ROTATOR ---
    // init AutoRotator
    var AutoSlide = setInterval(function () { animateNav() }, timeout);
    // pause AutoRotator on hover
    $g.hover(
				function () { $(this).addClass('Paused'); clearInterval(AutoSlide); },
				function () { $(this).removeClass('Paused'); AutoSlide = setInterval(function () { animateNav() }, timeout); }
				);

    // --- DO THE ANIMATION ----

    function animateNav(x) {

        // current slide
        var ActiveLI = $navLI.filter('.Tabactive').getIndex();

        if (x == null) { // then it's using the Rotator - find next slide
            x = ActiveLI + 1;
            if (x > navCount - 1) { x = 0; }
        }
        if (x >= 0 && x < navCount) { // if we're on #1 or last one, so don't go any farther

            // get the next slide
            var newActive = $navLI.removeClass('Tabactive').eq(x).addClass('Tabactive').getIndex();

            if (x !== ActiveLI) { // if the current slide !== the new slide
                $navLI.filter('.Tabactive').find('a').trigger('click');

                // make the previous tab fade out over the new one...
                $tabs.eq(ActiveLI)
							.css({ zIndex: '2', position: 'absolute' })
							.show()
							.fadeOut('slow', function () {
							    $tabs.css({ zIndex: '', position: '', display: '' })
							});
            }
            // move the tabnav into place
            /*
            if (x < navCount - 2) {
                $nav.animate({
                    left: -(navLIWidth * newActive)
                }, 500, function () {
                    //animation complete
                });
            }*/

        } // end if the current slide !== the new slide

        var CurrentLI = $navLI.filter('.Tabactive').getIndex();
        // fade the navigation buttons
        if (CurrentLI == 0) { fade($prev) }
        if (CurrentLI == navCount - 1) { fade($next) }

        if (CurrentLI > 0 && CurrentLI < navCount - 1) {
            unfade($prev);
            unfade($next);
        }

    } // end function animateNav

    function fade(obj) {
        obj.css('display', 'none');
    }
    function unfade(obj) {
        obj.css('display', 'block');
    }
}
$.fn.getIndex = function () {
    var $p = $(this).parent().children();
    return $p.index(this);
}

$(document).ready(function () {
    addTextSizer();
    addPageTools();
    tabGallery();

    // ----------- TEMPLATE EXTRAS ------------------//
    
    //Login
    $('#LoginDropdown ul').hide();
    $('#LoginDropdown').hover(
		function () {
		    $('#LoginDropdown ul').removeClass('Remove');
		    $('#LoginDropdown a.LoginArrow').addClass('over')
		    $('#LoginDropdown ul').slideDown();
		},
		function () {
		    $('#LoginDropdown ul').addClass('Remove');
		    $('#LoginDropdown a.LoginArrow').removeClass('over');
	
		}
	);

    //Remove quicklinks if empty
		$("#Content .cmspage .QuickLinksPanel:empty").remove();
		if ($.trim($(".QuickLinksPanel").text()) == "") {
		    $(".QuickLinksPanel").remove();
		}

    // ----------- HACK LIST START ------------------//
	
	/* Adding link to Dr designation */
	$('.doc8ee5da0d73bce011b8c300505682182a strong').replaceWith('<a href="/services/womens/spirit-of-women/">Spirit of Women</a>');

    // ----------- HACK LIST END ------------------//



});
