﻿jQuery.fn.fadeIn = function(speed, callback) { 
    return this.animate({opacity: 'show'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeOut = function(speed, callback) { 
    return this.animate({opacity: 'hide'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
};

var alreadyMemberTimeOut;


$(document).ready( function()
{
	// lightbox overlay bindings
	
	$(".openCalc").fancybox(
		{	'hideOnContentClick': false, 
			'frameWidth': 600, 
			'frameHeight': 600, 
			'centerOnScroll': false 
		} );
	
	$(".openQuestions").fancybox(
		{	'hideOnContentClick': false, 
			'frameWidth': 700, 
			'frameHeight': 500, 
			'centerOnScroll': false,
			'hideOnOverlayClick' : false
		} );
		
	$( ".openLocations" ).fancybox(
		{	'hideOnContentClick': false, 
			'frameWidth': 950, 
			'frameHeight': 500, 
			'centerOnScroll': false
		} );

	$( ".myHealthManager" ).fancybox(
		{	'hideOnContentClick': false, 
			'frameWidth': 1005, 
			'frameHeight': 685, 
			'centerOnScroll': false
		} );
		
	$( ".openEnrollmentKit" ).fancybox(
		{	'hideOnContentClick': false, 
			'frameWidth': 650, 
			'frameHeight': 450, 
			'centerOnScroll': false
		} );
		
	$( ".openExploreRegions" ).fancybox(
		{	'hideOnContentClick': false, 
			'frameWidth': 500, 
			'frameHeight': 400, 
			'centerOnScroll': false
		} );
		
	$( ".link-connected-video" ).fancybox(
		{	'hideOnContentClick': false, 
			'frameWidth': 620, 
			'frameHeight': 450, 
			'centerOnScroll': false
		} );	
		
		
	// questions and answers expanding/shrinking function
		
	$( ".question" ).click( function()
		{
			var num = $( this ).attr( "question" );
			toggleAnswer( num );
		} );
		
		
	$( "#divQA img" ).click( function()
		{
			var num = $( this ).attr( "question" );
			toggleAnswer( num );
			
		} );
		
	function toggleAnswer( num )
	{
		if ( $( "div[answer='" + num + "']" ).is( ":visible" ) )
		{
			$( "div[answer='" + num + "']" ).slideUp( "fast" );
			$( "img[question='" + num + "']" ).attr( 'src', '/assets/images/plus.jpg' );
		}
		else
		{
			$( "div[answer='" + num + "']" ).slideDown( "fast" );
			$( "img[question='" + num + "']" ).attr( 'src', '/assets/images/minus.jpg' );
		}
	}
		
	
	// Learn more popup
	
	$( ".alreadyMemberHover" ).mouseover( function()
		{
			var p = $( ".alreadyMemberHover" ).position();
//			$( ".alreadyMember" ).css( "left", p.left + 100 );
//			
			$( ".alreadyMember" ).show();
			clearAlreadyMemberTimeOut();
		});
		
	$( ".alreadyMember" ).mouseover( function()
		{
			clearAlreadyMemberTimeOut();
		});
		
	$( ".alreadyMemberHover" ).mouseout( function() 
		{
			startAlreadyMemberFadeOut();
		});

	$( ".alreadyMember" ).mouseout( function() 
		{
			startAlreadyMemberFadeOut();
		});		





	
	$( ".footerLink:last a" ).mouseover( function()
		{
			var p = $( ".footerLink:last" ).position();
			$( ".learnMore" ).css( "left", p.left - 20 );
			$( ".learnMore" ).css( "top", Number( p.top ) - 320 );

			$( ".learnMore" ).show();
		});

	$( ".footerLink:last a" ).mouseout( function() 
		{
			$( ".learnMore" ).fadeOut();
		});	
		
		
		
		
	$( ".openLocations" ).mouseover( function()
		{
			var p = $( ".openLocations" ).position();
			$( ".tooltip-text" ).html( $( ".openLocations" ).attr( "tooltip" ) );
			
			
			$( ".tooltip" ).css( "left", 475 );
			$( ".tooltip" ).css( "top", 37 );
			
			$( ".tooltip" ).show();
		});
		
	$( ".openLocations" ).mouseout( function() 
		{
			$( ".tooltip" ).fadeOut();
		});	



	$( ".openQuestions" ).mouseover( function()
		{
			var p = $( ".openQuestions" ).position();
			$( ".tooltip-text" ).html( $( ".openQuestions" ).attr( "tooltip" ) );
			
			
			$( ".tooltip" ).css( "left", 570 );
			$( ".tooltip" ).css( "top", 37 );
			
			$( ".tooltip" ).show();
		});
		
	$( ".openQuestions" ).mouseout( function() 
		{
			$( ".tooltip" ).fadeOut();
		});			
		
		
		
	$( ".chat_image" ).live( 'click', function()
		{
			trackPage( 'channel=BUYKP:,pagename=BUYKP: Chat Now,events=event14' );
		});
		
	startSlideShow();
	
	
	if ( typeof( openOverlay ) != "undefined" )
	{
		switch ( openOverlay )
		{
			case "locations":
				setTimeout( "$('.openLocations').click();", 500 );
				break;
			case "questions-and-answers":
				setTimeout( "$('.openQuestions').click();", 500 );
				break;
			case "my-health-manager":
				setTimeout( "$('.myHealthManager').click()", 500 );
				break;
			case "enrollment-kit":
				setTimeout( "$('.openEnrollmentKit').click()", 500 );
				break;
			case "explore-regions-select":
				setTimeout( "$('.openExploreRegions').click()", 500 );
				break;
		}
	}
	
});


function startAlreadyMemberFadeOut()
{
	alreadyMemberTimeOut = setTimeout( "alreadyMemberFadeOut()", 2000 );
}

function alreadyMemberFadeOut()
{
	$( ".alreadyMember" ).fadeOut();
}

function clearAlreadyMemberTimeOut()
{
	clearTimeout( alreadyMemberTimeOut );
}

function displayInfo(current) 
{
    if ($(current).next().is(":hidden")) {
        $(current).next().slideDown("slow");
    }
    else 
    {
        $(current).next().slideUp("slow");
    }
}


function toggleCoordinatedCare()
{
	if ( $( ".coordinateCareContent" ).is( ":visible" ) )
	{
		$( ".coordinateCareContent" ).slideUp( "fast" );
	}
	else
	{
		$( ".coordinateCareContent" ).slideDown( "fast" );
	}
	
	return false;
	
}



function slideSwitch() 
{
    var active = jQuery('#slideshow img.active');
	
	active.fadeOut(1000);

    if ( active.length == 0 ) active = jQuery('#slideshow img:last');

    // use this to pull the images in the order they appear in the markup
    var next =  active.next().length ? active.next()
        : jQuery('#slideshow img:first');

    active.addClass('last-active');

		
	next.addClass('active')
        .fadeIn( 1000, function() 
        {
            active.removeClass('active last-active');
        });
}

function startSlideShow()
{
	$( '#slideshow img.active' ).show();
	startInterval();
//	setTimeout("startInterval()", 5000);
}
	
function startInterval()
{
	setInterval( "slideSwitch()", 6000 );
}

