// Handle global actions.
$(document).ready(function() {
    // Show tooltips
    if (is_ie6()) {
        // Don't use the 'slide' effect for IE6. It causes some blinking issues.
        $(".show-tooltip").tooltip({
            offset: [15, 0], 
            predelay: 500
        });
        $(".show-coupon-disclaimer-tooltip").tooltip({
            position: 'center left', 
            offset: [75, 0], 
            predelay: 500,
            tipClass: 'disclaimer-tooltip'
        });
    } else {
        $(".show-tooltip").tooltip({
            offset: [15, 0], 
            effect: 'slide',
            predelay: 500
        });
        $(".show-coupon-disclaimer-tooltip").tooltip({
            position: 'center left', 
            offset: [75, 0], 
           	effect: 'slide',
            predelay: 500,
            tipClass: 'disclaimer-tooltip'
        });
    }

	// Configure the modal dialog that displays when a user goes from the patient site to the HCP site.
	handleHCPSiteAccess();

	// Configure external hyperlinks to popup a dialog box that tells the user they are leaving the site.
 	handleExternalLinks();
});

function is_iPad(){
    return (navigator.platform.indexOf("iPad") != -1);
}

function is_iPhone(){
    return (
        //Detect iPhone
        (navigator.platform.indexOf("iPhone") != -1) ||
        //Detect iPod
        (navigator.platform.indexOf("iPod") != -1)
    );
}

function is_ie6(){
    return (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
}

// Configure the modal dialog that displays when a user goes from the patient site to the HCP site.
function handleHCPSiteAccess()
{
	$(".hcpsite").overlay({
        top: 260,
        mask: {
            color: '#000000',
            loadSpeed: 200,
            opacity: 0.6
        },
        closeOnClick: false
    });
    $('.hcpsite').click(function() {
        var link = $(this).attr('href');
        $('#hcp-site-dialog a.external-url').attr('href', link);
        return false;
    });
    $('#hcp-site-dialog #hcp-btnDialogContinue').click(function() {
        var link = $('#hcp-site-dialog a.external-url').attr('href');
        window.location = link;
    });
}

// Configure external hyperlinks to popup a dialog box that tells the user they are leaving the site.
function handleExternalLinks()
{
	// Creating custom :external selector
    $.expr[':'].external = function(obj){
        return !obj.href.match(/^mailto\:/) && (obj.href != "") && (obj.hostname != location.hostname);
    };

    // Add 'external' CSS class to all external links
    $("a:external[rel='']").addClass('external-link');

    // Handle all the events for the external link modal dialog.
    $("a:external[rel!='']").addClass('external');
    $(".external").overlay({
        top: 260,
        mask: {
            color: '#000000',
            loadSpeed: 200,
            opacity: 0.6
        },
        closeOnClick: false
    });
    $('.external').click(function() {
        var link = $(this).attr('href');
        $('#leaving-site-dialog a.external-url').attr('href', link);
        return false;
    });
    $("#leaving-site-dialog #btnDialogContinue").click(function() {
        var link = $('#leaving-site-dialog a.external-url').attr('href');
        window.open(link);
    });
}

// Hack to stop videos from playing in the background in IE.
function DeleteContentsOfFancybox () {
    $("#fancy_wrap #fancy_frame").remove();
}

// Closes lightbox window.
function closeLightbox()
{
	$.fn.fancybox.close();
}

// Closes lightbox window and navigates to specified URL.
function closeLightboxRedirect(redirectUrl)
{
	$.fn.fancybox.close();
	window.location = redirectUrl;
}
// Closes lightbox window and opens a new browser with the specified URL.
function closeLightboxRedirectNewWindow(redirectUrl)
{
	$.fn.fancybox.close();
	window.open(redirectUrl);
}

