/**
 * @author camilla.starke
 */


$(document).ready(function () {

    $('.default-value').each(function () {
        var default_value = this.value;
        $(this).css('color', '#666'); // this could be in the style sheet instead
        $(this).focus(function () {
            if (this.value == default_value) {
                this.value = '';
                $(this).css('color', '#333');
            }
        });
        $(this).blur(function () {
            if (this.value == '') {
                $(this).css('color', '#666');
                this.value = default_value;
            }
        });
    });

    //hide shopping cart table before loading page
    $('.shopping_cart div').hide();

    //hide all tabs in the search wrapper and show the first tab
    //$('div.search_wrapper').children().hide();
    //$('div.search_wrapper div:first-child').show();


    //shows the shopping cart table and changing image on clicking the image show
    $('div.shopping_cart').click(function () {

        if ($(this).parent().parent().attr('class') == 'shopping_cart_is_not_empty') {

            $(this).toggleClass('shopping_cart_open').children('.shopping_cart div').toggle();

            $(this).mouseleave(function () {
                $('.shopping_cart div').hide();
            });
        };
    });

    //shows the contries in the footer and
    $('div.country').click(function () {

        $('.countries').show();

        $('.country_selector').mouseleave(function () {
            $('.countries').hide();
        });
    });
    //Function for changing the content of div Tire selector
    $(function () {
        var tabContainers = $('div.tire_selector > div');
        $('div.tire_selector ul.tire_selector_menu a').click(function () {
            tabContainers.hide().filter(this.hash).show();
            $('div.tire_selector ul a').parent().removeClass('active');
            $(this).parent().addClass('active');
            return false;
        }).filter(':first').click();
    });

    //Function for expanding and collapsing Service offerings in the right column
    $(function () {

        $('a.show_more_offerings').click(function () {
            $('.campaign_view_hidden').show();
            $(this).hide();
            $('#service_offerings').addClass('service_offerings_height');
            //$('#service_offerings').css('display','block');
            return false;
        });

        $('a.show_less_offerings').click(function () {
            $('.campaign_view_hidden').hide();
            $('a.show_more_offerings').show();
            $('#service_offerings').removeClass('service_offerings_height');
            return false;
        });
    })

    //Function for showing and hidding search box on productlistpage
    /*
    $(function () {
    $('.hide_tire_selector').parent().hide();
    $('.show_tire_selector').parent().click(function () {
    $('.tire_selector_hidden').show();
    $(this).hide();
    $('.hide_tire_selector').parent().show();
    return false;
		   
    }),
    $('.hide_tire_selector').parent().click(function () {
    $('.tire_selector_hidden').hide();
    $(this).hide();
    $('.show_tire_selector').parent().show();
    return false;
    });
    }); */

    //Function for making even rows of Product list table grey
    $(function () {
        $(".product_list tbody tr:nth-child(even)").addClass("even");
    });


    $(function () {
        $('.green_button').hover(function () {
            $(this).toggleClass('green_button_hover');
        });
    });

    //Function for reg search
    $(function () {

        $('.registrationnr_Letters').focus(function () {
            if ($(this).val() == 'ABC') {
                this.value = '';
            }
        });

        $('.registrationnr_Numbers').focus(function () {
            if ($(this).val() == '123') {
                this.value = '';
            }
        });

        $('.registrationnr_Letters').keyup(function () {
            this.value = this.value.replace(/[^a-zA-Z]/g, '');
            if ($(this).val().length > 2) {
                $(".registrationnr_Numbers").focus();
            }
        });
        $('.registrationnr_Numbers').keyup(function () {
            this.value = this.value.replace(/[^0-9\.]/g, '');
            if ($(this).val().length > 2) {
                $("#regnr .green_button_large a").focus();
            }
        });

    });
    $(function () {
        $('.order_show').click(function () {
            $(this).parent().parent().next().toggle();
            $(this).toggleClass('order_hide');
        })

    });

    $(function () {
        if ($('.startpage_campaign_container .box_shape_shadow').length >= 4) {
            $('.startpage_campaign_container .box_shape_shadow:last-child').addClass('last');
        }
    });

    $(function () {

        var valgrey = $('#dimension .valgrey').hide(),
            mess = $('#dimension .mess').hide(),
            DDLsw = $('#dimension .m_DDLSectionWidth'),
            DDLar = $('#dimension .m_DDLAspectRatio'),
            DDLd = $('#dimension .m_DDLDiameter');

        var validateThese = [DDLsw, DDLar, DDLd];

        $('.tiresearch_button').click(function () {
            var allValid = true;
            for (var i = 0; i < validateThese.length; i++) {
                var ddl = validateThese[i];
                if (ddl.val() == -1) {
                    ddl.siblings('.valgrey').show().css('color', 'red');
                    mess.css('color', 'red');
                    allValid = false;
                }
                else {
                    ddl.siblings('.valgrey').css('color', '#6D6D6D');
                }
            }
            if (allValid) {
                valgrey.hide();
                mess.hide();
            } else {
                mess.show();
                return false;
            }
        });
    });

    /* shows the testbox on the product card*/
    $(function () {
        if ($('#tester .test_item').length != 0) {
            $('#testContainer').show();
        }
    });

    //Shows and hides the netprice on the productcard for resellers
    $('#price_box #left input').click(function () {
        var checked = $('#price_box #left input:checked').val();
        if (checked == undefined) {
            $('#price_box #right .price').show();
            $('#price_box #right .productConsumerPrice').removeClass('consumer_price');
            $('#price_box #right .productConsumerPriceLabel').removeClass('consumer_price_label');


        }
        else {
            $('#price_box #right .price').hide();
            $('#price_box #right .productConsumerPrice').addClass('consumer_price');
            $('#price_box #right .productConsumerPriceLabel').addClass('consumer_price_label');
        }
    });

    $(function () {
        var defaultValue = $("#txtSearchWorkshop").val();
        $('#btnSearchWorkshop').click(function () {
            var searchValue = $("#txtSearchWorkshop").val();
            var link = $(".tire_change_button").attr("href");
            var url = link + "?address=" + (searchValue != defaultValue ? searchValue : '');
            window.location = url;
        });
    });

    $(function () {
        $('#txtSearchWorkshop').keypress(function (e) {
            code = e.keyCode ? e.keyCode : e.which;
            if (code.toString() == 13) {
                var location = $("#txtSearchWorkshop").val();
                var link = $(".tire_change_button").attr("href");
                var url = link + "?address=" + location;
                window.location = url;
                return false;
            }
        });
    });

});
