﻿
var overlayEnabled = false;
var lastItemSelected;
var isIE6;

$(document).ready(function()
{
    $(".productFinderLifeStages").hover(function() { productFinderToggle($(this), 'on'); }, function() { productFinderToggle($(this), 'off'); });

    isIE6 = typeof document.body.style.maxHeight === "undefined";

    if (isIE6)     $("#container").css("height", "100%");
    
    $('#addThis').mouseover(function(){return addthis_open(this, '', '[URL]', '[TITLE]')}).mouseout(function(){addthis_close()}).click(function(){return addthis_sendto()});

    var sizeIE6Iframe = function()
    {
        var overlayViewportHeight = document.documentElement.offsetHeight + document.documentElement.scrollTop - 4;
        var overlayViewportWidth = document.documentElement.offsetWidth - 21;
        $('#DOMWindowIE6FixIframe').css({ 'height': overlayViewportHeight + 'px', 'width': overlayViewportWidth + 'px' });
    };

    var sizeOverlay = function()
    {
        var $DOMWindowOverlay = $('#DOMWindowOverlay');
        if (isIE6)
        {//if IE 6
            var overlayViewportHeight = document.documentElement.offsetHeight + document.documentElement.scrollTop - 4;
            var overlayViewportWidth = document.documentElement.offsetWidth - 21;
            $DOMWindowOverlay.css({ 'height': overlayViewportHeight + 'px', 'width': overlayViewportWidth + 'px' });
        } else
        {//else Firefox, safari, opera, IE 7+
            $DOMWindowOverlay.css({ 'height': '100%', 'width': '100%', 'position': 'fixed' });
        }
    };

    var productFinderMainBox = $('#productFinderMainBox');
    var anchoredPositions = productFinderMainBox.position();
    var anchoredPositionX = anchoredPositions.left + productFinderMainBox.width() - 3;
    var anchoredPositionY = anchoredPositions.top + 5;

    $('#productFinderContent').css('top', anchoredPositionY + 'px');
    $('#productFinderContent').css('left', anchoredPositionX + 'px');

    $('#productFinderSubmitButton').hover(function() { if (isIE6 != true) $('#productFinderFooter').css('background-color', '#006600'); },
        function() { if (isIE6 != true) $('#productFinderFooter').css('background-color', '#339900'); });

    $('.productFinderLifeStages').click(function()
    {
            // clear the last item selected
            if (lastItemSelected != null) disableFlyOut(lastItemSelected);

            lastItemSelected = $(this);
            overlayEnabled = true;

            var exists = $('body #DOMWindowOverlay');
            if (exists.length == 0)
            {
                $('body').append('<div id="DOMWindowOverlay" style="z-index:10;display:none;position:absolute;top:0;left:0;background-color:white;filter:alpha(opacity=80);-moz-opacity: 0.80;opacity: 0.80;"></div>');
                if (isIE6)
                {//if IE 6
                    $('body').append('<iframe id="DOMWindowIE6FixIframe"  src="blank.htm"  style="width:100%;height:100%;z-index:90;position:absolute;top:0;left:0;filter:alpha(opacity=0);"></iframe>');
                    sizeIE6Iframe();
                    $('#productFinderHeaderContent').css('margin-bottom', '0px');
                    $('#productFinderClose').css('width', '125px');
                    $('#productFinderSubmitButton').css('margin-right', '15px');
                }
            }
            
            sizeOverlay();
            var $DOMWindowOverlay = $('#DOMWindowOverlay');
            $DOMWindowOverlay.fadeIn('fast', enableFlyOut(lastItemSelected));

            $('.productFinderLifeStagesButton').removeClass('productFinderLifeStagesButtonEnabled');

            $('#productFinderMainBox').css('position', 'absolute').css('z-index', '1000');
            $('#productFinderContent').fadeIn('slow', function() { $(this).css('z-index', '1000') });
            $('#productFinderClose').click(function()
            {
                disableFlyOut(lastItemSelected);
                closePopup()
            });
            
        return false;
    });

    $('#productFinderSubmitButton').click(function(e)
    {
        e.preventDefault();

        var names = [];
        var conditions = $('#productFinderConditionTable input:visible:checked').each(function()
        {
            names.push(this.value);
        });

        var payload = JSON2.stringify({ list: names });

        $.ajax({
            type: "POST",
            url: "../../../../../Product-Results-Page/Your-Product-Results.aspx/SaveSearchResults",
            data: payload,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg)
            {
                window.location = "../../../../../Product-Results-Page/Your-Product-Results.aspx";
            },
            error: function(msg)
            {
                alert('There was an error while trying to submit your search criteria');
            }

        });
    });

});

function productFinderToggle(e, toggle)
{
    if (e.length == 1)
    {
        var item = e[0];
        switchBackground(item.id);
    }
}

function enableFlyOut(e)
{
    if (e.length == 1)
    {
        var item = e[0].id;
        var selectedText = "Infant"
        if (item == 'productFinderInfant')
        {
            selectedText = "Infant";
        }
        if (item == 'productFinderChild')
        {
            selectedText = "Child";
        }
        if (item == 'productFinderAdultOral')
        {
            selectedText = "Adult (Oral Use)";
        }
        if (item == 'productFinderAdultTube')
        {
            selectedText = "Adult (Tube Feeding)";
        }
        var backgroundImage = $("#" + item).css('background-image');
        if (backgroundImage.indexOf('UP.gif') > -1)
        {
            backgroundImage = backgroundImage.replace("UP.gif", "CLICK.gif");
        }
        else
        {
            backgroundImage = backgroundImage.replace("OVER.gif", "CLICK.gif");
        }
        
        $("#" + item).css('background-image', backgroundImage);
        
        $("#productFinderSelectionText").text(selectedText);
        GetProductFinderConditions(selectedText);
    }
}

function GetProductFinderConditions(lifeStage)
{
    $('#productFinderConditionTable').html('&nbsp;&nbsp;&nbsp;&nbsp;Please wait while the available conditions are retrieved...<br /><br /><br />');
    var hostname = "http://" + window.location.host + "/SearchUtilities.aspx";
    PageMethod(hostname, 'GetConditionListForLifeStage', ['"lifeStage":"' + lifeStage + '"'], onGetConditionSuccess, onGetConditionSuccess);
}

function onGetConditionSuccess(msg)
{
    var exists;
    exists = $('body #conditionValues');
    if (exists.length == 0) {
        $('body').append('<div id="conditionValues" style="display: none;">' + msg.d + '</div>');
    }
    else
    {
        $('body #conditionValues').html = msg.d;
    }

    // clean up the html
    var conditions = $('body #conditionValues');

    var html = $('body #conditionList').html();

    $('body #conditionValues').remove();
    $('#productFinderConditionTable').html(html);
    
}

function disableFlyOut(e)
{
    if (e.length == 1)
    {
        var item = e[0].id;
        var backgroundImage = $("#" + item).css('background-image');
        if (backgroundImage.indexOf('CLICK.gif') > -1)
        {
            backgroundImage = backgroundImage.replace("CLICK.gif", "UP.gif");
        }
        else
        {
            backgroundImage = backgroundImage.replace("OVER.gif", "UP.gif");
        }
        
        $("#" + item).css('background-image', backgroundImage);
    }
}


function switchBackground(e, toggle)
{
    var backgroundImage = $("#" + e).css('background-image');

    if (toggle != 'on' && backgroundImage.indexOf('UP.gif') > -1)
    {
        backgroundImage = backgroundImage.replace("UP.gif", "OVER.gif");
    }
    else backgroundImage = backgroundImage.replace("OVER.gif", "UP.gif");
    
    $("#" + e).css('background-image', backgroundImage);
}

function closePopup()
{
    if (overlayEnabled)
    {
        $('#DOMWindowOverlay').fadeOut('fast');
        $('#DOMWindowOverlay').remove();
        $('#productFinderContent').fadeOut('slow');
        overlayEnabled = false;

        if (isIE6)
        {

            $("#DOMWindowIE6FixIframe").remove();
	    $("#container").css("height", "100%");
            $("body", "html").css({ height: "auto", width: "auto" });
            $("html").css("overflow", "");	    
            location.reload(true);
        }
    }

    return false;
}

function PageMethod(pagePath, fn, paramArray, successFn, errorFn)
{
 //Create list of parameters in the form:
//{"paramName1":"paramValue1","paramName2":"paramValue2"}
 var paramList = '';
 if (paramArray.length > 0)
 {
 for (var i=0; i<paramArray.length; i++)
 {
   if (paramList.length > 0) paramList += ',';
   paramList += paramArray[i];
   }
 }
 paramList = '{' + paramList + '}';
 //Call the page method
 $.ajax({
   type: "POST",
   url: pagePath + "/" + fn,
   contentType: "application/json; charset=utf-8",
   data: paramList,
   dataType: "json",
   success: successFn,
   error: errorFn
 })
;
}

// Check whether links are external:
// (Only works with elements that have href):
$.extend($.expr[':'], {
    external: function(a, i, m) {
        if (!a.href) { return false; }
        if (a.hostname && a.hostname!= window.location.hostname) {
            var fixedHostname = a.hostname.replace(/^\s+|\s+$/g, '').toLowerCase().replace('qa.', '').replace('stage.','').replace('www.', '');
            //fixedHostname = fixedHostname.left(fixedHostname.indexOf(':'), fixedHostname.length - fixedHostname.indexOf(':'));
            if (jQuery.inArray(fixedHostname, safeList) >= 0) {
                return false;
            }
            if (fixedHostname.indexOf('javascript') > 0 || fixedHostname.indexOf('(') > 0 )
            {
                return false;
            }
            
            if (a.hostname.substr(0, a.hostname.indexOf(':')) == window.location.hostname)
            {
                //alert(a.hostname.substr(0, a.hostname.indexOf(':')));
                return false;
            }
            else 
            {
                return true;
            }
        }
        else return false;
    }
});

 var msg = "\nLinks which take you out of Abbott Laboratories worldwide \n"+
 "web site are not under the control of Abbott Laboratories, \n"+
 "and Abbott Laboratories is not responsible for the contents \n"+
 "of any such site or any further links from such site. Abbott \n"+
 "Laboratories is providing these links to you only as a \n"+
 "convenience, and the inclusion of any link does not imply \n"+
 "endorsement of the linked site by Abbott Laboratories.\n\n"+
 "Do you wish to leave this site?"; 

    $(document).ready(function() { 
        $('a:external').attr('target', '_blank').click(function(e) 
         { 
            if (confirm(msg) !=true ) 
            {
                e.preventDefault();    
            }
          }
        )
    });
     


