﻿//var $j = jQuery.noConflict();

$j(document).ready(function()
{

	var case_str = '';
	var top = 0;


	var type = '';
	
	
	var isOverlayOpen = false;
	var prevNavItem;

	// Init Function
	function initjQuery()
	{
		//  ------------------
		//  Add overlay holder
			$j('body').append('<div class="overlay"></div>');
			$j('body').append('<div class="overlay-portal"></div>');
						
		//  ------------------------------------------
		//  Setup all input text box to clear on focus

		
		//  --------------------------------------------------------
		//  Setup the required function for the quick link drop down
			if ($j('.quickLink').length > 0) 
			{
				$j('select', '.quickLink').change(function(num)
				{
					
					$j('select', '.quickLink').removeAttr('onchange');
					var str = $j('select', '.quickLink').attr('value').toString();
					
					if (str != '')
					{
						if (str.indexOf('http://') >= 0)
						{
							window.open(str);
						}
						else
						{
							window.location = str;
						}
					}
					
					return false;
				});
			}
		
		
		
		//  ----------------------
		//  Setup Font Replacement
			
			Cufon.replace('.contact-closeBtn', {fontFamily: 'tradeGothic', fontSize: '12px', letterSpacing: '.6px'});
			Cufon.replace('.overlay-closeBtn', {fontFamily: 'tradeGothic', fontSize: '12px', letterSpacing: '.6px'});
		
		/*	
			$j('.mainnav').removeClass('scriptHideOnLoad');
			
			
			//$j('.mainnav').parent().append('<ul class="mainnav mainnavhover">' + $j('.mainnav').html() + '</ul>');
			
			Cufon.replace('.mainnav a', {fontFamily: 'tradeGothic', hover: false, fontSize: '14px', letterSpacing: '.6px'});
			//$j('alpha50 li').css({opacity: .1});
			$j('.mainnav li').css({margin: '0 15px'});
		*/	
			// Slideshow heading
			/*
			Cufon.replace('.ss-ctn h1', {fontFamily: 'tradeGothicBold', fontSize: '23px'});
			Cufon.replace('.ss-ctn h2', {fontFamily: 'tradeGothicBold', fontSize: '23px'});
			Cufon.replace('.ss-ctn h3', {fontFamily: 'tradeGothicBold', fontSize: '23px'});
			Cufon.replace('.ss-ctn h4', {fontFamily: 'tradeGothicBold', fontSize: '23px'});
			Cufon.replace('.ss-ctn h5', {fontFamily: 'tradeGothicBold', fontSize: '23px'});
			Cufon.replace('.ss-ctn h6', {fontFamily: 'tradeGothicBold', fontSize: '23px'});
			*/
		//  -------------
		//  Popup - Click
			$j('a[rel=popup]').each(function()
			{
				var id = $j(this).attr('id');
            	$j(this).initOverlay({ cID: id });
			});
			
		//  -----------------
		//  Popup - Roll Over
			$j('a[rel=popup-over]').each(function()
			{
				//$j(this).initOverlay({mouseEvent: 'mouseover'});
				$j(this).bind('mouseover', {obj: $j(this)}, setPopupTimer); 
				//$j(this).bind('click', {obj: $j(this)}, popupOver); 
			});
		
		//  ---------------------
		//  Main navigation popup
			$j('.mainnavhover a').each(function()
			{
				//$j(this).initOverlay({mouseEvent: 'mouseover'});
				$j(this).bind('mouseover', {obj: $j(this)}, createSubNav);	
			});
		
		//  -----------------------------
		//  Setup the ticker if it exists
			if ($j('.ticker').length > 0)	{ $j('.ticker').initTicker(); }
			
		//  --------------------
		//  Horizontal Scrollers
			if ($j('.sliderHor').length > 0)
			{
				$j('.sliderHor').each(function()
				{					
					//$j(this).slider();
						$j(this).slider({mouseDisable: false, autoSlide: true, autoSlideDelay: 8000, timeDelay: 1000, btnOpacityOver: 1, btnOpacityOff: 1});
				});

            Cufon.refresh();
			}
			
			
			
			$j('dl', '.subnav').each(function()
			{
				$j(this).addClass('img-link');
				
				$j(this).click(function()
				{
					var url = $j('a', this).attr('href');
					window.location = url;
						
					return false;
				});
			});
			
			
		//  -----
		//  Accordion
			if ($j('.accordion').length > 0)
			{
				$j('.accordion').each(function()
				{
					$j('.acc-item', this).each(function()
					{
						$j(this).accordion({hasButtons: true});
					});
				});
			}
				
				
		//  -----
			if ($j('#emailForm').length > 0)
			{
			    $j('#emailFormContent').css({ position: 'relative' }).removeClass('hide');
			    if ($j('#emailFormContent').attr('class').indexOf('postbacked') < 0)
			    {
			    	$j('#emailFormContent').toggle();
			    }


				$j('a.contact-closeBtn', '#emailFormContent').bind('click', closeForm);
				
				$j('#emailForm').click(function()
				{
					
						var form = $j('#emailFormContent');
						form.css({ opacity: 1 });

						//form.fadeIn().slideDown();
						form.slideDown('slow');
					
					return false;
				});
			}
				
			function closeForm()
			{
				var form = $j('#emailFormContent');
				//form.fadeOut().slideUp()
				form.slideUp('slow');
				//prevItem.removeClass('selected');
				return false;
			}
			
		//  -----------
		//  Create Tabs
			if ($j('.tabs-holder').length > 0)
			{
				$j('.tabs-holder').each(function()
				{
					var id = $j(this).attr('id');
					
					var fixedTab = ($j('.tabs-holder[class*=fixed]').length > 0) ? true : false;
					if (id == "cTabs")
					{	
						$j(this).CreateTabs({isFixed: fixedTab, tabsID: id, toggleContent: true});
					}
					else
					{
						$j(this).CreateTabs({isFixed: fixedTab, tabsID: id});
					}
				});
			}
				
			/*
			if (!$j.browser.msie)
			{
				$j('.js-img-overlay').each(function()
				{
					$j(this).append('<div class="img-overlay"><img src="/common/images/feature-overlay.png" width="212" height="76" alt="" /></div>');
									
					$j('.img-overlay').css({opacity: 0});
					
					var parent = $j(this).parent();
					parent.css({cursor: 'pointer'});
					
					$j(this).mouseover(function()
					{
						$j('.img-overlay', this).animate({opacity: 1});
					}).mouseout(function()
					{
						$j('.img-overlay', this).animate({opacity: 0});
					}).click(function()
					{
						var url = $j('a', parent).attr('href');
						
						window.location = url;
						return false;
					});
				});
			}
			*/
	}
	
	var helpTimer;
	
	function setPopupTimer(event)
	{
		clearInterval(helpTimer);
		//timer = setInterval(openSubNav, 250, event);		
		helpTimer = setInterval(function() { popupOver(event); }, 50 );
		event.data.obj.bind('mouseout', deleteTimer);
	}
	
	function popupOver(event)
	{
		clearInterval(helpTimer);
		var elem = event.data.obj;
		
		//elem.unbind('mouseover', popupOver);
		elem.unbind('mouseover', popupOver);
		
		elem.addClass('selected');
		var parent = $j(elem).parent();
		case_str = parent.attr('class');
					
		//alert(case_str);
					
		//switch (case_str)
		//{
			//case 'help-select-dd':
				var h = parent.outerHeight(true);
				
				//$j('.help-closeBtn').bind('click', {obj: elem}, closeOverlay);
				
				$j('.' + case_str + '-position').css({position: 'relative', 'z-index': 100});
				$j('.' + case_str + '-content').removeClass('scriptHideOnLoad').css({clear: 'none', opacity: 0, position: 'absolute', left: 0, bottom: h + 'px', display: 'block'}).animate({opacity: 1});
				/*
				$j('.' + case_str + '-content').removeClass('scriptHideOnLoad').css({clear: 'none', opacity: 0, position: 'absolute', left: 0, bottom: '-75px'});
				$j('.' + case_str + '-content').css({display: 'block'}).animate({opacity: 1, bottom: (h + 15) + 'px'}, 'normal').animate({bottom: h + 'px'}, 'fast');
				*/
			//	break;
		//}
		
		$j('.mainnavholder').css({ 'z-index': 5 });
		//$j('.mainnavholder').css({opacity: .2});
		
		if (!isOverlayOpen)
		{
			showOverlay(elem, false);
		}
		
		return false;
	}
	
	var prevNavSelected = false;
	
	var timer;
	
	function createSubNav(event)
	{
		clearInterval(timer);
		//timer = setInterval(openSubNav, 250, event);		
		timer = setInterval(function() { openSubNav(event); }, 50 );
		event.data.obj.bind('mouseout', deleteTimer);
	}
	
	function deleteTimer()
	{
		clearInterval(timer);
		clearInterval(helpTimer);
	}
	
	function openSubNav(event)
	{
		clearInterval(timer);
		
		hidePrevNavItem();
		
		case_str = '';
		var elem = event.data.obj;
		prevNavItem = elem;
		elem.unbind('mouseover', createSubNav);
		
		prevNavSelected = (elem.attr('class') == 'selected') ? true : false;
		
		var id = elem.attr('id');
		$j('.' + id).addClass('selected');
		Cufon.refresh();
		
		//elem.addClass('selected');
		
		if (!isOverlayOpen)
		{
			$j('.subnav').animate({opacity: 0}, 50, function() { $j('.subnav').css({opacity: 1, display: 'block'}) });
			showOverlay(elem, false);
			//$j('.subnav').css({opacity: 0, display: 'block'}).stop().animate({opacity: 1}, 250);
		
		
			$j('#' + elem.attr('id') + '-sub').animate({opacity: 0}, 50, function()
			{
				$j('#' + elem.attr('id') + '-sub').css({opacity: 1}).removeClass('hide').addClass('show');
			});//.animate({opacity: 1});
		}
		
		$j('#' + elem.attr('id') + '-sub').css({opacity: 1}).removeClass('hide').addClass('show');
		//$j('#' + elem.attr('id') + '-sub').css({opacity: 0}).removeClass('hide').addClass('show').animate({opacity: 1}, 250);
	}
	
	function hidePrevNavItem()
	{
		if (prevNavItem != null)
		{
			$j('#' + prevNavItem.attr('id') + '-sub').removeClass('show').addClass('hide');
			if (!prevNavSelected)
			{
				var id = prevNavItem.attr('id');
				$j('.' + id).removeClass('selected');
			
				
				//prevNavItem.removeClass('selected');
				Cufon.refresh();
				//Cufon.replace(prevNavItem, {fontFamily: 'tradeGothic', hover: true, fontSize: '14px', letterSpacing: '.6px', color: '#FFFFFF'});
			}
			prevNavItem.bind('mouseover', {obj: prevNavItem}, createSubNav);
		}
	}
	
	function closeOverlay(event)
	{
		hidePrevNavItem();
		var elem = event.data.obj;
				
		if (case_str != '')
		{
			elem.removeClass('selected');
			$j('.mainnavholder').css({ 'z-index': 30 });
			switch (case_str)
			{
				case 'help-select-dd':
					//$j('.' + case_str + '-content').animate({bottom: '45px'}, 'fast').animate({opacity: 0, bottom: '-75px'}, 'normal', 
					$j('.' + case_str + '-content').animate({opacity: 0}, 
													function()
													{
														$j(this).css({display: 'none'});
														$j('.' + case_str + '-position').css({'z-index': 0});
														//elem.bind('mouseover', {obj: elem}, popupOver);
														elem.bind('click', {obj: elem}, popupOver);
													});
					break;
			}
		}
		else
		{
			$j('.subnav').css({display: 'none'});
			//$j('.hdr-logo img').stop().animate({opacity: 1}, 250);
			//$j('.hdr-search').stop().animate({opacity: 1}, 250);
		}
		
		hideOverlay();
		return false;
	}
	
	function showOverlay(elem, isClick)
	{		
		isOverlayOpen = true;
		var bodyHeight = $j(document).height();
		$j('.overlay').css({backgroundColor: '#000000', opacity: 0, display: 'block', height: bodyHeight + 'px'}).stop().animate({opacity: .7}, 250,  function()
																						   {
																							   if (!isClick)
																							   {
																								   $j('.overlay').bind('mouseover', {obj: elem}, closeOverlay);
																							   }
																							   else
																							   {
																								   $j('.overlay').bind('click', {obj: elem}, closeOverlay);
																							   }
																						   });
	}
	
	function hideOverlay()
	{
		//$j('.mainnavholder').css({opacity: 1});
		isOverlayOpen = false;
		$j('.overlay').unbind('mouseover', closeOverlay);
		/*
		$j('.overlay').animate({opacity: 0}, 250, function()
											{
												$j(this).css({display: 'none'});
												//$j('.header').css({'z-index': 5});
											});
		*/
		$j('.overlay').css({opacity: 0, display: 'none'});
	}
	
    // Init Function
    function initPortal() 
	{
        $j.ajax({
            url: "portal.html",
            cache: false,
            success: function(portal) 
			{
				var id = 'portal'//$j(this).attr('id');
				$j('#' + id + '-overlay').append(portal);
				$j('body').initOverlay({cID: id, mouseEvent: 'null', overlayColor: '#FFFFFF', overlayOpacity: 1});
								
				Cufon.replace('h1', {fontFamily: 'tradeGothicBold', fontSize: '23px', letterSpacing: '-.1px'});
				//$j('body').append(portal);
            }
        });
    }
    
    //initPortal();
	initjQuery();
});
