(function($) {

    $.extend({
        equalHeight: function() {
            $('.EqualHeight').children().height('auto');
            $('.EqualHeight').each(function() {
                var maxHeight = 0;
                $(this).children().each(function() {
                    if ($(this).height() > maxHeight) {
                        maxHeight = $(this).height();
                    }
                });
                $(this).children().height(maxHeight);
            });
        },

        init: function() {

            $(window).resize(function() {
            });

            $(window).load(function() {

                // Suckerfish menu
                var menuItems = $(".cedMenu > li");
                for (var i = 0; i < menuItems.length; i++) {
                    menuItems[i].onmouseover = function() {
                        $(this).addClass("cedHover");
                        var selectIE6Fix = $(".ie6 .cedUnderMenuSelect");
                        if (selectIE6Fix.length) {
                            $(selectIE6Fix).hide();
                        }
                    };
                    menuItems[i].onmouseout = function() {
                        $(this).removeClass("cedHover");
                        var selectIE6Fix = $(".ie6 .cedUnderMenuSelect");
                        if (selectIE6Fix.length) {
                            $(selectIE6Fix).show();
                        }
                    };
                    if ($(menuItems[i]).children("ul").length) {
                        $(menuItems[i]).addClass("cedGotChildren");
                    }

                    if (($(menuItems[i]).children("ul").length) && ($(menuItems[i]).hasClass('cedCurrent'))) {
                        $(menuItems[i]).addClass("cedCurrentGotChildren");
                    }
                }

                // Popup
                $(".cedServicesMenu li.cedMailLink a").click(function() {
                    $(".cedPopupContainer .cedPopup1").show();
                });
                $(".cedPopupContainer .cedPopup a.cedClose").click(function() {
                    $(".cedPopupContainer .cedPopup").hide();
                });



                if ($(".cedFaq").length > 0) {

                    var id;
                    var loc = document.location.toString();
                    if (loc.match('#')) {
                        id = document.location.toString().split('#')[1];
                    }
                    else {
                        id = $(".cedFaq a").first().attr('id');
                    }

                    $(".cedFaq li").each(function() {

                        if ($('a', this).attr('id') != id) {
                            $(this).children('.cedFaqContent').hide();
                        }
                        else {
                            $(this).addClass('cedItemOpened');
                        }

                        $(this).children('a').click(function() {
                            if ($(this).parent().hasClass('cedItemOpened')) {
                                $(this).parent().removeClass('cedItemOpened');
                                $(this).next().slideUp();
                                $(this).parent().siblings().removeClass("cedItemOpened");
                                $(this).parent().siblings().children('.cedFaqContent').slideUp();
                            } else {
                                $(this).parent().addClass('cedItemOpened');
                                $(this).next().slideDown();
                                $(this).parent().siblings().removeClass("cedItemOpened");                                
                                $(this).parent().siblings().children('.cedFaqContent').slideUp();
                                
                            }
                            return false;
                        });
                    });
                }

                if ($('select').length) {
                    $('select').selectBox();
                }

                /*var params = {
                changedEl: "select",
                visRows: 5,
                scrollArrows: true
                };
                $(params.changedEl).parent().css('zoom',1);
                cuSel(params);*/

                /*if($('.form_cont').length){
                $('.tab_items').children().hide();
                $('.form_cont').children(':first').addClass('activeItem');
                $('.tab_items').children(':first').show();
                $('.form_cont li a').click(function(){
                $('.form_cont li').removeClass('activeItem');
                $(this).parent().addClass('activeItem');
                $('.tab_items').children().hide();
                $('.tab_items').children(':eq('+ $(this).parent().index()+')').show();
                return false;
                });
                }*/

                if ($('.section_switch').length) {
                    $('.section_switch').each(function() {
                        if (!$(this).attr('checked')) {
                            $(this).parent().nextAll().hide();
                        } else {
                            $(this).parent().css('padding', '0 0 17px 151px');
                        }
                        $(this).change(function() {
                            if (!$(this).attr('checked')) {
                                $(this).parent().nextAll().hide();
                                $(this).parent().removeAttr('style');
                            } else {
                                $(this).parent().nextAll().show();
                                $(this).parent().css('padding', '0 0 17px 151px');
                            }
                        });
                    });
                }

                $.equalHeight();


            });
        }
    });

})(jQuery);

jQuery(function($) {
    $.init(); // Inits content fixes
});

CheckSubProductsSelection = function() {
    var hasSmthToOrder = $(".subproduct-check-choise:checked");
    $(".error_message").hide();


    if (hasSmthToOrder.length > 0) {
        return true;
    }

    $(".error_message").show();
    return false;
};


RecalculateCartSummary = function(sender) {
    var tr = $(sender).parents("tr.order-item");

    // check if tr exists
    if (tr.length == 0) {
        return false;
    }
    var count = $(sender).val();
    var id = $("input.order-item-id", tr).val();
    var cost = $("td.order-item-cost", tr);

    $.ajax(
        {
            url: "/handlers/webwinkel/carthandler.ashx",
            data: { action: "changeNumber", id: id, count: count },
            type: "POST",
            cache: false,
            success: function(data) {
                $(".summary-total-count").html(data.TotalCount);
                $(".summary-total-cost").html(data.TotalCost);

                //find current
                for (var i = 0; i < data.OrderList.length; i++) {
                    if (data.OrderList[i].ItemID != id)
                        continue;
                    cost.html(data.OrderList[i].CostStr);
                    break;
                }

                if (data.ErrorMesage != null) {
                    alert(data.ErrorMesage);
                }
            }
        }
    );

    return false;
};

RemoveItemFromCart = function(sender) {
    var tr = $(sender).parents("tr.order-item");

    // check if tr exists
    if (tr.length == 0) {
        return false;
    }

    var id = $("input.order-item-id", tr).val();

    $.ajax(
        {
            url: "/handlers/webwinkel/carthandler.ashx",
            data: { action: "removeItem", id: id },
            type: "POST",
            cache: false,
            success: function(data) {
                $(".summary-total-count").html(data.TotalCount);
                $(".summary-total-cost").html(data.TotalCost);
                //find current
                var notInCart = true;

                for (var i = 0; i < data.OrderList.length; i++) {
                    if (data.OrderList[i].ItemID == id) {
                        notInCart = false;
                        break;
                    }
                }

                if (data.OrderList.length < 1)
                    $(".orderButton").remove();
                
                if (notInCart)
                    $(tr).remove();

                if (data.ErrorMesage != null) {
                    alert(data.ErrorMesage);
                }
            }
        }
    );

    return false;

};

ChangeFormPartVisibility = function(sender) {
    var checked = $(sender).attr("checked");
    var fieldset = $(sender).parents("fieldset");

    if (checked) {
        $("dl", fieldset).show();
    }
    else {
        $("dl", fieldset).hide();
    }

};

var ValidatedAndSubmited = false;

ValidateAndSubmitOrderForm = function(sender) {

    var form = $(document.forms[0]);
    var action = $(sender).attr('href');
    $(form).attr('action', action);
    $(form).validate({
        errorClass: "error",
        highlight: function(element, errorClass) {
            $(element).parents("dl").addClass(errorClass);
        },
        unhighlight: function(element, errorClass) {
            $(element).parents("dl").removeClass(errorClass);
        },
        showErrors: function() {
            this.defaultShowErrors();
        },
        errorPlacement: function() { }
    });

    if ($(form).valid() && !ValidatedAndSubmited) {
        ValidatedAndSubmited = true;
        $(form).submit();

    }

};

function initOverLabels() {
    if (!document.getElementById) return;

    var labels, id, field;

    // Set focus and blur handlers to hide and show 
    // LABELs with 'overlabel' class names.
    labels = document.getElementsByTagName('label');
    for (var i = 0; i < labels.length; i++) {

        if (labels[i].className == 'overlabel') {

            // Skip labels that do not have a named association
            // with another field.
            id = labels[i].htmlFor || labels[i].getAttribute('for');
            if (!id || !(field = document.getElementById(id))) {
                continue;
            }

            // Change the applied class to hover the label 
            // over the form field.
            labels[i].className = 'overlabel-apply';

            // Hide any fields having an initial value.
            if (field.value !== '') {
                hideLabel(field.getAttribute('id'), true);
            }

            // Set handlers to show and hide labels.
            field.onfocus = function() {
                hideLabel(this.getAttribute('id'), true);
            };
            field.onblur = function() {
                if (this.value === '') {
                    hideLabel(this.getAttribute('id'), false);
                }
            };

            // Handle clicks to LABEL elements (for Safari).
            labels[i].onclick = function() {
                var id1, field1;
                id1 = this.getAttribute('for');
                if (id1 && (field1 = document.getElementById(id))) {
                    field1.focus();
                }
            };

        }
    }
};


function hideLabel(fieldId, hide) {
    var fieldFor;
    var labels = document.getElementsByTagName('label');
    for (var i = 0; i < labels.length; i++) {
        fieldFor = labels[i].htmlFor || labels[i].getAttribute('for');
        if (fieldFor == fieldId) {
            labels[i].style.textIndent = (hide) ? '-9999px' : '0px';
            return true;
        }
    }
    return false;
};

window.onload = function() {
    setTimeout(initOverLabels, 50);
};

CheckMinLength = function(id, minLength) {
    var val = $("#" + id).val();
    
    if (val && val.length >= minLength)
        return true;
    
    return false;


}

