﻿$(function () {

	// SEO Content Move
	$('.phe-ui-moveto').each(function (i) {
		var m = $(this);
		$(m.attr('moveto')).html(m.html());
		m.empty();
	});

	// Hero Ad Slider
	var MainBannerAdsUrl = $('base').attr('href') + 'handlers/legacyadvertisements.ashx?adType=MAINBANNERS';
	$('#HeroAds div.items').load(MainBannerAdsUrl, function () {
		$('#HeroAds').scrollable({ circular: true }).autoscroll({ interval: 12000 }).navigator({ indexed: true, naviItem: 'span' });
	});

	
	// RoundedCornerDiv
	$('#SideMenu').wrap('<div style="background-color:#eaeaea"></div>').corner("round 8px").parent().css('padding', '2px').corner("round 10px");

	// Tab Style Content Wrapper
	$('.tabheadline', 'div.phe-ui-tabcontent').wrap('<div class="thl"></div>');
	$('.tabcontent', 'div.phe-ui-tabcontent').after('<div class="ft"><div></div></div>');
	$('.tabcontent', 'div.phe-ui-tabcontent').wrap('<div class="bd"></div>');
    
});


//TOP TABS 11/15/2011**********************
$(function () {
	var MainMenuSubContent = '/handlers/legacyadvertisements.ashx?adType=Main_Menu_DropDowns';
	
	//temp load all submenus at once //then bind hover event
	$('#TopTabSubMenuHtml').load(MainMenuSubContent,
	function () {
		$(".ttsubmenucontainer").hover(function () {
			showMenuParts($(this).attr('id').replace('DDMenu', ''));
		}, function () {
			hideMenuParts($(this).attr('id').replace('DDMenu', ''));
		});
	});
});


$(function () {
	$(".tt").hover(function () {
		showMenuParts($(this).attr('id').replace('TTMenu', ''));
	}, function () {
		hideMenuParts($(this).attr('id').replace('TTMenu', ''));
	});
});

function showMenuParts(id) {
	
	//some things need to happen regardless of whether there is a menu or not
	var $ttmenu = $('#TTMenu' + id);
	$ttmenu.addClass('TopNavHover');

	for (i = 0; i <= 20; i++) {
			if (i != id) {
               toptab_off(i);
			}
		}

	if ($('#DDMenu' + id).length) {
		var $ddmenu = $('#DDMenu' + id);
		var $connectdd = $('#DDMenu' + id + ' .dvConnector');
		var $windowwidth = $(window).width();
		var buffer = 10
		var moveLeft = 0;
		var pos = $ttmenu.offset();
		var width = $ttmenu.width() + 20;
		var height = 26;
		var ddmenuwidth = 200

		//account for missing right side of menu
		if ($('#DDMenu' + id + ' .ttsubmenuright').length) { ddmenuwidth += 200; }
		else {
			$ddmenu.addClass('ttsubmenuSlim');
			$('#DDMenu' + id + ' .ttsubmenuleft').addClass('ttsubmenuleftSlim');
		}

		//move menu left if it's outside the window					
		if ($windowwidth < (pos.left + ddmenuwidth)) { moveLeft = ((pos.left + ddmenuwidth + buffer) - $windowwidth); }

		$ddmenu.css({ "left": (pos.left - moveLeft - 1) + "px", "top": (pos.top + height) + "px", "visibility": "visible" }).show();
		$connectdd.css({ "left": moveLeft + "px", "width": (width) + "px" }).show();
		
		//make sure sub menu is on highest z-index
		$ddmenu.maxZIndex({ inc: 5 });
	}
}

function toptab_off(id) { 
   $('#TTMenu' + id).removeClass('TopNavHover');
}

function hideMenuParts(id) {
    setTimeout(function () { //adds a delay (ms) so that menu doesn't flicker on every mouse out
        if ($('#DDMenu' + id).length && $('#TTMenu' + id).ismouseover() == false && $('#DDMenu' + id).ismouseover() == false) {
            toptab_off(id);
            $('#DDMenu' + id).css('visibility', 'hidden');
        }
        //seperated for no drop downs version
        if (!$('#DDMenu' + id).length && $('#TTMenu' + id).ismouseover() == false) {
            toptab_off(id);
        }
    }, 200);
}

