jQuery(document).ready(function() {
    // only show initial fade on homepage
    /*
    if (window.location.pathname != "/")
    jQuery('.fadeOffer').removeClass('fadeOffer')

    // OFFER FADE
    jQuery('.fadeOffer .boxOffer').animate({ opacity: 1 }, 4000).fadeOut('slow', removeOffer);
    function removeOffer() {
    jQuery('.fadeOffer').removeClass('fadeOffer')
    }
    */

    jQuery('.prodScience').hide();
    jQuery('.prodIngredients').hide();
    jQuery('.prodPress').hide();
    jQuery('.prodShare').hide();

    jQuery('a.tabProdDesc').click(function() {
        jQuery('.tabNav a').removeClass('selected');
        jQuery(this).addClass('selected');
        jQuery('.boxInfo').hide();
        jQuery('.prodDesc').show();
    });

    jQuery('a.tabProdScience').click(function() {
        jQuery('.tabNav a').removeClass('selected');
        jQuery(this).addClass('selected');
        jQuery('.boxInfo').hide();
        jQuery('.prodScience').show();
    });

    jQuery('a.tabProdIngredients').click(function() {
        jQuery('.tabNav a').removeClass('selected');
        jQuery(this).addClass('selected');
        jQuery('.boxInfo').hide();
        jQuery('.prodIngredients').show();
    });

    jQuery('a.tabProdPress').click(function() {
        jQuery('.tabNav a').removeClass('selected');
        jQuery(this).addClass('selected');
        jQuery('.boxInfo').hide();
        jQuery('.prodPress').show();
    });

    jQuery('a.tabProdShare').click(function() {
        jQuery('.tabNav a').removeClass('selected');
        jQuery(this).addClass('selected');
        jQuery('.boxInfo').hide();
        jQuery('.prodShare').show();
    });

    // Sub nav
    jQuery('a.navLink').hover(function() {
        jQuery(this).parent().children(".subNav").show();
    }, function() {
        jQuery(this).parent().children(".subNav").hide();
    });

    jQuery('.subNav').hover(function() {
        jQuery(this).parent().addClass('selected');
        jQuery(this).show();
    }, function() {
        jQuery(this).parent().removeClass('selected');
        jQuery(this).hide();
    });

    // Sub nav - offers
    /*
    jQuery('a.btnOffers').hover(function() {
    jQuery(this).parent().children(".boxOffer").show();
    }, function() {
    jQuery(this).parent().children(".boxOffer").hide();
    });

    jQuery('.boxOffer').hover(function() {
    jQuery(this).parent().addClass('selected');
    jQuery(this).show();
    }, function() {
    jQuery(this).parent().removeClass('selected');
    jQuery(this).hide();
    });
    */

    // Basket
    jQuery('a.linkBasket').hover(function() {
        jQuery(this).parent().children(".boxBasket").show();
    }, function() {
        jQuery(this).parent().children(".boxBasket").hide();
    });

    jQuery('.boxBasket').hover(function() {
        jQuery(this).show();
    }, function() {
        jQuery(this).hide();
    });

    if (jQuery('.customerMessage').length > 0) { BlockCustomerMessage(); }

    if (jQuery('.samples').length > 0) { BlockSamples(); }

    jQuery('#hpicksamples').click(function() { BlockSamples(); return false; });

    jQuery("#btnclosewin").click(function() { jQuery.unblockUI(); return false; });

    jQuery('[id$="btnNoSamples"],[id$="btnUpdateBasketSamples"]').click(function(evt) {
        evt.preventDefault();
        ids = '';

        jQuery(".boxSamples :checkbox").each(function() {
            if (jQuery(this).is(':checked')) { ids += jQuery(this).parents(".boxCheck").find('.nodisplay').text() + ','; }
        });

        if (jQuery(this).hasClass('button')) {
            window.setTimeout("__doPostBack('ctl00$Content$ucBasketPageSamples$btnUpdateBasketSamples',ids);", 100);
        } else {
            window.setTimeout("__doPostBack('ctl00$Content$ucBasketPageSamples$btnNoSamples',ids);", 100);
        }
        return false;
    });
    //TODO: CONFIGURE NUMBER OF SAMPLEs    
    jQuery(".boxSamples :checkbox").click(function() {
        var n = 0;
        var samples = jQuery(".hdnSampleCount").val();
        jQuery(".boxSamples :checkbox").each(function() { if (jQuery(this).is(':checked')) { n++; } });
        if (n > samples)
            return false;
    });

    // Gift message menu
    jQuery('a.linkAddGiftMsg').click(function() {
        if (jQuery('.MBBoxGiftMsg').is(":visible") == true)
            jQuery('.MBBoxGiftMsg').slideUp();
        else
            jQuery('.MBBoxGiftMsg').slideDown();
    });

    // sign in
    jQuery('a.linkSignIn').toggle(function() {
        jQuery('.boxSignInMessages').hide();
        jQuery('.boxSignIn').show();
    }, function() {
        jQuery('.boxSignIn').hide();
        jQuery('.boxSignInMessages').show();
    });

    //Reviews (comments) sign in
    jQuery('a.commentsLinkSignIn').toggle(function() {
        jQuery('.boxSignInMessages').hide();
        jQuery('.boxSignIn').show();
        jQuery.scrollTo(0, 500);
    }, function() {
        jQuery('.boxSignIn').hide();
        jQuery('.boxSignInMessages').show();
    });

    //carousel
    jQuery('.itemCDesc').hide();

    jQuery('a.linkCInfo').toggle(function() {
        jQuery(this).parent().parent().parent().children(".itemCDesc").show();
    }, function() {
        jQuery(this).parent().parent().parent().children(".itemCDesc").hide();
    });

    jQuery('.panel img').hover(function() {
        jQuery(this).parent().parent().parent().children(".itemCDesc").show();
    }, function() {
        jQuery(this).parent().parent().parent().children(".itemCDesc").hide();
    });

    jQuery('.PerfectPat img').hover(function() {
        jQuery(this).parent().parent().parent().children(".itemCDesc").show();
    }, function() {
        jQuery(this).parent().parent().parent().children(".itemCDesc").hide();
    });

    jQuery('.itemCDesc').hover(function() {
        jQuery(this).show();
    }, function() {
        jQuery(this).hide();
    });

});

/* Newsletter */

(function(jQuery)
{
    jQuery.extend({
        getGo: function(url, params)
        {
            document.location = url + '?' + jQuery.param(params);
        },
        postGo: function(url, params)
        {
            var $form = jQuery("<form>").attr("method", "post").attr("action", url);
            jQuery.each(params, function(name, value)
            {
                jQuery("<input type='hidden'>").attr("name", name).attr("value", value).appendTo($form);
            });

            $form.appendTo("body");
            $form.submit();
        }
    });
})(jQuery);

function NewsLetterSend(textBoxId)
{
    var params =
    {
        'email': jQuery('#' + textBoxId).val(),
        'key': 'sub-1322-1.2;1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0;;subscribe-2.htm', // REN Newsletter
        'double_optin': '0',
        'pass_email': '0',
        'thankyou_url': 'http://www.renskincare.com/Account/RegisterPage.aspx?newsletter=1'
    };

    jQuery.postGo('http://ren.emailmsg.net/cgi-bin/optin.pl', params);
}

function NewsLetterSendUSA(textBoxId)
{
    var params =
    {
        'email' : jQuery('#' + textBoxId).val(),
        'key': 'sub-1322-1.3;1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0;;subscribe-3.htm', // REN USA Newsletter
        'double_optin' : '0',
        'pass_email' : '0',
        'thankyou_url': 'http://www.renskincare.com/Account/RegisterPage.aspx?newsletter=1'
    };

    jQuery.postGo('http://ren.emailmsg.net/cgi-bin/optin.pl', params);
}

function ShowPopup(id)
{
    jQuery('.popup').hide();
    jQuery('#' + id).fadeIn();
    jQuery('#overlay').fadeIn();
    jQuery.scrollTo(0, 500);
    return false;
}

function HidePopup(element)
{
    jQuery('.popup').fadeOut();
    jQuery('#overlay').fadeOut();
    return false;
}

function BlockSamples()
{
    width = 0;
    if(jQuery.browser.msie && jQuery.browser.version=="6.0"){width = -200;}
    else{width = (jQuery(window).width()/2) - 480;}
      jQuery.blockUI({ 
            message: jQuery("[id$='pnlSamples']"), 
            fadeIn: 700, 
            fadeOut: false,  
            showOverlay: true,
            title: "Ren Skincare Free Samples",
            css: { 
                width: '930px', 
                top: '100px',
                left: width +'px',
                right: '10px',
                border: 'none', 
                padding: '0', 
                backgroundColor: '#f6f6f6',
                color: '#000', 
                cursor:'pointer'
            }
        });
}

function BlockCustomerMessage()
{
    jQuery.blockUI({
        message: jQuery("[id$='pnlCustomerMessage']"),
        fadeIn: 700,
        fadeOut: 700,
        showOverlay: true,
        css: {
            width: '930px',
            top: '100px',
            left: (jQuery(window).width() - 950) / 2 + 'px',
            right: '10px',
            border: 'none',
            padding: '0',
            backgroundColor: '#f6f6f6',
            color: '#000',
            cursor: 'pointer'
        }
    });
}

function CleanPostalCode(firstPostCodeControlId, otherPostalCodeControlId)
{
    var postalCode = jQuery('#' + firstPostCodeControlId).val();
    var postalCodeReversed = "";
    var spaceAdded = false;

    if ((postalCode != null) && (postalCode != '') && (postalCode.indexOf(' ') == -1) && isNaN(postalCode[postalCode.length - 1])) {

        for (var i = postalCode.length - 1; i >= 0; i--) {
            var character = postalCode.charAt(i);
            if (!isNaN(character) && !spaceAdded) {
                postalCodeReversed += character + " ";
                spaceAdded = true;
            }
            else {
                postalCodeReversed += character;
            }
        }

        postalCode = "";
        for (var i = postalCodeReversed.length - 1; i >= 0; i--) {
            postalCode += postalCodeReversed.charAt(i);
        }
    }

    jQuery('#' + firstPostCodeControlId).val(postalCode);

    if (otherPostalCodeControlId != null) {
        jQuery('#' + otherPostalCodeControlId).val(postalCode);
    }
}
