﻿window.log = function () {
    log.history = log.history || [];
    log.history.push(arguments);
    arguments.callee = arguments.callee.caller;
    if (this.console) console.log(Array.prototype.slice.call(arguments));
};


$(function () {


    //* *** TNB LAYER ***
    $("#openTNBLayer").live("click", function () {
        showOverlay();
        $("#TNBLayer").show();
        $('.scroll-pane').jScrollPane();
        return false;
    });

    if ($('#policyList').length) {
        Policy.init();
    }

    //* *** POLICY ACCORDION *** 
    var Policy = {
        init: function () {
            var policyAccordion = $("#policyList");
            var allH2 = policyAccordion.find("h2");
            var allLi = policyAccordion.find("li");
            allH2.click(openPolicySection);

            function openPolicySection() {
                allLi.removeClass("open");
                $(this).parent("li").addClass("open");
            }
        }
    };


    //* *** LAYER ***

    //*** CAPTCHA Layer
    $(".captchaHelpLayerTrigger").live('click', function (e) {
        e.preventDefault();
        $("#codeHelp").show();
    });

    $(".captchaHelpLayerTrigger").live('click', function (e) {
        e.preventDefault();
        $("#codeHelpContact").show();
    });

    $(".close").live("click", hideLayer);

    function hideLayer(e) {
        e.preventDefault();
        $(this).parents('.layer').hide();
    }


    $(".unileverGroup").live('click', function (e) {
        e.preventDefault();
        $("#unileverBrands").show();
    });

    $("#legalcontactlayerlink").live('click', function (e) {
        e.preventDefault();
        $("#EMailAnDenDatenschutz").show();
    });


    //* *** Email an den DatenSchutz ***
    var AjaxForm = {
        selector: 'form.ajaxRequest',
        //userId: $('body').attr('data-userhash'),
        init: function () {
            var _this = this;
            $(this.selector).each(function (index) {
                $(this).attr('id', 'ajaxRequest_' + index);
                _this.formObserve($(this).attr('id'));
            });
        },
        formObserve: function (id) {
            var _this = this;
            var form = $('#' + id);
            form.submit(function () {
                if (form.serialize() != "") {
                    $.ajax({
                        //url: form.attr('action')+"?userId="+_this.userId,
                        url: form.attr('action'),
                        data: form.serialize(),
                        type: form.attr('method'),
                        cache: false,
                        success: function (data) {
                            form.replaceWith(data);
                            _this.init();
                        }
                    });
                }
                return false;
            });
        }
    };
    if ($('form.ajaxRequest').length) {
        AjaxForm.init();
    }

    var ExclusiveData = {
        all: 'exclusive-all',
        only: 'exclusive-only',
        init: function () {
            var _this = this;
            $('input.exclusive').live('change', function () {
                _this.checkFormFields($(this));
            });
        },
        checkFormFields: function (e) {
            var _this = this;

            if (e.attr('checked') === true) {
                if (e.hasClass(_this.all)) {
                    $('input.' + _this.only).attr('checked', false);
                } else {
                    $('input.' + _this.all).attr('checked', false);
                }

            }
        }
    };

    /* FORM */
    if ($('input.exclusive').length) {
        ExclusiveData.init();
    }

    /* Siteutilities */

    $('a.layerLink').live('click', function (e) {
        e.preventDefault();
        var link = $(this).attr('href');
        $('#su').load(link + ' .layer', function () {
            Policy.init();
            AjaxForm.init();
        });
    });

    //* *** *** Maxlength textarea
    var $charsRemaining = $('#charsRemaining');
    $('textarea[maxlength]').keydown(function () {

        var max = parseInt($(this).attr('maxlength'));
        var charsCount = $(this).val().length;
        var charsRemaining = max - charsCount;

        if (charsCount >= max) {
            $(this).val($(this).val().substr(0, max));
            $charsRemaining.css("color", "#fb1d1a");
            $charsRemaining.text("noch 0 Zeichen");
        } else {
            $charsRemaining.removeAttr("style");
            $charsRemaining.text("noch " + charsRemaining + " Zeichen");
        }
    });



});
 
