﻿/*********************
Secret Products JS V1
*********************/

$(document).ready(function() {
    productsHover();
    discontinuedProducts();
});

var showCount = 0;
var hideCount = 0;
function productsHover()
{
    if (!$('#hoverContainer').length)
    {
        $('body').append('<div id="hoverContainer"><div class="mid"></div></div>');
    }
    var displayTimeout;
    $('.productCategories > div').bind('mouseover',function(){
    
        //Calculate tooltip position
        var topPos = $(this).offset().top;
        var leftPos = $(this).offset().left;
        
        leftPos = leftPos;
        topPos = topPos - 30;
       
        
        //Get Tooltip content

        var tooltipContent = $(this).attr('id') + '_content';
        tooltipContent = $('#' + tooltipContent).html();

       $('#hoverContainer .mid').html(tooltipContent);
        
        var hoverContainerHeight = $('#hoverContainer').height();
        if($(this).attr('id') == 'landingScentExpressions')
        {
            $('#hoverContainer').css({'background':'url(/Images/Products/bkgdHoverContentScentExpressions.png) no-repeat', 'width':'304px', 'height':'374px'});
        }
        else
        {
            $('#hoverContainer').css({'background':'url(/Images/Products/bkgdHoverContent.png) no-repeat', 'width':'304px', 'height':'294px'});
        }
        
        if($(this).parent().attr('id') != "bkgdClinicalProtection")
        {
            leftPos = leftPos - 5;
            $('#hoverContainer .mid .hoverContent_inner .productContent').css('margin-left', '0');
        }
        else
        {
            leftPos = leftPos - 10;
        }
        
        
        $('#hoverContainer').css({
            'top': topPos,
            'left': leftPos
        });
        /*
        if(!$('#hoverOffTop, #hoverOffBtm, #hoverOffLeft, #hoverOffRight').length)
        {
            $('#hoverContainer .mid').append('<div id="hoverOffTop"></div><div id="hoverOffBtm"></div><div id="hoverOffLeft"></div><div id="hoverOffRight"></div>');
        }
        
        $('#hoverOffLeft, #hoverOffRight, .mid').css('height', hoverContainerHeight);
        */
        $('#hoverContainer').show();
        $('.productCategories').css('opacity', '.6'); 
    });

    
    $('#hoverContainer').bind('mouseout',function(){
            $('#hoverContainer').hide();
            $('.productCategories').css('opacity', '1');
    });

}

function discontinuedProducts()
{
    $('#platinumDiscontinued .left select').change(function(){
        CallPageMethod("GetDiscontinuedProductAlternativeProductByUpcCode", platinumDiscontinuedProductSubstitue, fail, "upcCode", $('#platinumDiscontinued .left select').val());
    });
    
    $('#flawlessTouch .left select').change(function(){
        CallPageMethod("GetDiscontinuedProductAlternativeProductByUpcCode", flawlessDiscontinuedProductSubstitue, fail, "upcCode", $('#flawlessTouch .left select').val());
    });
    
    $('#scentExpressions .left span').click(function(){
        $('#scentExpressions .left span').each(function(){
            $(this).removeClass('active');
        });
        
        $(this).addClass('active');
        CallPageMethod("GetDiscontinuedProductAlternativeProductByUpcCode", scentExpressionsDiscontinuedProductSubstitue, fail, "upcCode", $(this).attr("name"));
    });
}

function CallPageMethod(methodName, onSuccess, onFail) {
    var args = '';
    var l = arguments.length;
    if (l > 3) {
        for (var i = 3; i < l - 1; i += 2) {
            if (args.length != 0) args += ',';
            args += '"' + arguments[i] + '":"' + arguments[i + 1] + '"';
        }
    }
    var loc = "/WebUtils/AjaxPageMethods.aspx";
    $.ajax({
        type: "POST",
        url: loc + "/" + methodName,
        data: "{" + args + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: onSuccess,
        fail: onFail
    });
}

function platinumDiscontinuedProductSubstitue(response) {
    var discontinuedProduct = eval('(' + response.d + ')');
    $("#platinumDiscontinued .right").html(discontinuedProduct.htmlText);
}

function flawlessDiscontinuedProductSubstitue(response) {
    var discontunedProduct = eval('(' + response.d + ')');
    $("#flawlessTouch .right").html(discontunedProduct.htmlText);
}

function scentExpressionsDiscontinuedProductSubstitue(response) {
    var discontunedProduct = eval('(' + response.d + ')');
    $("#scentExpressions .right").html(discontunedProduct.htmlText);
}

function fail(response) {
    alert("an error occurred.");
}