jQuery(document).ready(function ($) {




    /** some functions to set equal height **/



    $.fn.equalHeights = function (px) {
        $(this).each(function () {
            var currentTallest = 0;
            $(this).children().each(function (i) {
                if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
            });
            if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
            // for ie6, set height since min-height isn't supported
            if ($.browser.msie && $.browser.version == 6.0) {
                $(this).children().css({ 'height': currentTallest });
            }
            $(this).children().css({ 'min-height': currentTallest });
        });
        return this;
    };

    Number.prototype.pxToEm = String.prototype.pxToEm = function (settings) {
        //set defaults
        settings = jQuery.extend({
            scope: 'body',
            reverse: false
        }, settings);

        var pxVal = (this == '') ? 0 : parseFloat(this);
        var scopeVal;
        var getWindowWidth = function () {
            var de = document.documentElement;
            return self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
        };

        /* When a percentage-based font-size is set on the body, IE returns that percent of the window width as the font-size. 
        For example, if the body font-size is 62.5% and the window width is 1000px, IE will return 625px as the font-size. 	
        When this happens, we calculate the correct body font-size (%) and multiply it by 16 (the standard browser font size) 
        to get an accurate em value. */

        if (settings.scope == 'body' && $.browser.msie && (parseFloat($('body').css('font-size')) / getWindowWidth()).toFixed(1) > 0.0) {
            var calcFontSize = function () {
                return (parseFloat($('body').css('font-size')) / getWindowWidth()).toFixed(3) * 16;
            };
            scopeVal = calcFontSize();
        }
        else { scopeVal = parseFloat(jQuery(settings.scope).css("font-size")); };

        var result = (settings.reverse == true) ? (pxVal * scopeVal).toFixed(2) + 'px' : (pxVal / scopeVal).toFixed(2) + 'em';
        return result;
    };


    $('.jcarousel').jcarousel({
        scroll: 1
    });

    if ($('.tabs').length > 0) {

    }
    else {
        $('#main_content').equalHeights();
    }



    function mycarousel_initCallback(carousel) {
        jQuery('.jcarousel-control').show();

        jQuery('.jcarousel-control a').bind('click', function () {
            carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
            return false;
        });

        jQuery('.jcarousel-scroll select').bind('change', function () {
            carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
            return false;
        });

        jQuery('#mycarousel-next').bind('click', function () {
            carousel.next();
            return false;
        });

        jQuery('#mycarousel-prev').bind('click', function () {
            carousel.prev();
            return false;
        });
    };


    $('.wallpapers').jcarousel({
        scroll: 1,
        initCallback: mycarousel_initCallback
    });

    $('.wallpapers li.wallpapers-item:last').addClass('jcarousel-item-last');




    // handles hidden elemts

    /* deactivated :-(
    $('ul.hide5').each(function () {
    $(this).find('li:gt(4)').parent().parent().addClass('elements_hidden');
    $(this).find('li:gt(4)').addClass('hidden_element');
    $(this).find('.elements_hidden a.hidden_link').show();
    });

    $('ul.hide2').each(function () {
    $(this).find('li.items:gt(1)').parent().parent().addClass('elements_hidden');
    $(this).find('li.items:gt(1)').addClass('hidden_element');
    $(this).find('.elements_hidden a.hidden_link').show();
    });
 

    $('.hidden_link').click(
    function ()
    {
    //$(this).parent().find('ul li').removeClass('hidden_element');
    $(this).parent().find('ul li').fadeIn();
    $(this).parent().removeClass('elements_hidden');
    $(this).hide();
    return false;
    }
        
    );
    */







    // create custom animation algorithm for jQuery called "bouncy" 

    // add taget="_blank" to '.external' links
    $(".external").attr("target", "_blank");

    // set alt attribut empty -> so browsers will not show it at mouseover
    $("img.tooltip[alt]").attr('alt', '');
    $("img.tooltip[title]").tooltip({
        /* tooltip configuration goes here */
        tip: '#tooltip',

        // one configuration property 
        position: "top center",

        // another property 
        opacity: 1




        // ... the rest of the configuration properties 
    });

    var $j = jQuery;
    $j.fn.extend({
        toggleValue: function (defaultText) {
            return this.each(function () {

                if ($j.trim($j(this).val()) == '') {
                    $j(this).val(defaultText);
                }

                $j(this).focus(function () {
                    if ($j(this).val() == defaultText) {
                        $j(this).val('');
                    }
                    $j(this).blur(function () {
                        if ($j.trim($j(this).val()) == '') {
                            $j(this).val(defaultText);
                        }
                    });
                });
            });
        }
    });

    // left navigation:
    // remove background of the first li
    $('#left_navigation ul li:not(:first)').addClass('divider');

    // add border-line to the left navigation


    $('#left_navigation ul:first').addClass('bg');


    // add divider class to display a bolder at the right side,
    // do not add a border to the last element 

    $('#topnavigation ul li:not(:last)').addClass('divider');
    $('#topnavigation ul li:last').addClass('last');


    $('#bottomnavigation_left li:not(:last)').addClass('divider');
    $('#bottomnavigation_left li:last').addClass('last');

    $('.smallteaser:last').css('margin-bottom', '0px');


    // set background if first main_navigation li is active
    if ($('#navigation ul li:first').hasClass('active')) {
        $('#navigation').addClass('active');
    }


    // add slideDown when dealer_search at the topnavigation is clicked
    $('#top_dealer_search a').click(
      function () {
          if ($('#top_dealer_search div').css('display') == 'none') {
              $('#top_dealer_search div').slideDown();
              return false;
          }
          else {
              $('#top_dealer_search div').slideUp();
              return false;
          }
      }
   );

    // Edit this via the CMS to set the default values for several input fields:
    $('#dealer_search_input, #teaser_dealer_search_input').toggleValue('PLZ, Ort oder Händlername');
    $('#search_input').toggleValue('Suche');



    $('#mainnavigation li:first').hover(
      function () {
          $('#navigation').addClass('active_hover_div');
      },
      function () {
          $('#navigation').removeClass('active_hover_div');
      }
   );


    $("a.image_link").fancybox({
        'titlePosition': 'inside',
        'transitionIn': 'elastic',
        'transitionOut': 'elastic'
    });


    $("a.flash_link").fancybox({
        'titlePosition': 'inside',
        'transitionIn': 'none',
        'transitionOut': 'none',
        'padding': 10,
        'autoScale': false,
        'width': 820,
        'height': 525,
        'type': 'swf'

    });

    $("a.lightbox_iframe_820").fancybox({
        'titlePosition': 'inside',
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'type': 'iframe',
        'width': 910,
        'height': '90%',
        'autoScale': false

    });

    $("a.lightbox_iframe_890").fancybox({
        'titlePosition': 'inside',
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'type': 'iframe',
        'width': 910,
        'height': '90%',
        'autoScale': false
    });

    $("a.lightbox_iframe_710").fancybox({
        'titlePosition': 'inside',
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'type': 'iframe',
        'width': 730,
        'height': 220,
        'autoScale': false
    });


    $.easing.bouncy = function (x, t, b, c, d) {
        var s = 1.70158;
        if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
        return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
    }

    // create custom tooltip effect for jQuery Tooltip 
    $.tools.tooltip.addEffect("bouncy",

    // opening animation 
    function (done) {
        this.getTip().animate({ top: '+=8' }, 500, 'bouncy', done).show();
    },

    // closing animation 
    function (done) {
        this.getTip().animate({ top: '-=15' }, 500, 'bouncy', function () {
            $(this).hide();
            done.call();
        });
    }
    );

    // ie6 fix
    if ($.browser.msie && $.browser.version == 6.0) {
        $('#dealer_image').hide().show();

        $('#mainnavigation li').hover(
        function () {
            $(this).addClass('active_hover');
        },
        function () {
            $(this).removeClass('active_hover');
        }
      );


        $('#mainnavigation li.active').hover(
        function () {
            return false;
        },
        function () {

        }
      );
    }





    $('.tabs').accessibleTabs({
        // Der Name der Klasse die dem Div zugewiesen
        // welche um das Markup herumgeschrieben wird
        wrapperClass: 'wrapper_content',
        // Der Name der Klasse die das aktuelle Tab markiert
        currentClass: 'current',
        // Tag oder valider Query Selector der Elemente 
        // aus denen die Tabs Navigation erzeugt wird
        // (die Originale werden entfernt)
        tabhead: 'h3.tabhead',
        // Tag oder valider Query Selector der Elemente die
        // als Inhalte der Tabs genutzt werden sollen
        tabbody: '.tabbody',
        // Anzeigeeffekte:  'fadeIn', 'slideDown' oder 'show'
        fx: 'fadeIn',
        // Geschwindigkeit (String|Number): 'slow', 'normal', oder 'fast')
        // oder die Milisekunden die die Anzeigeeffekte dauern sollen
        fxspeed: 'normal',
        // Text um Screenreadern anzuzeigen welches der ausgewählte Tab ist
        currentInfoText: '',
        // Definition wo der Text eingefügt wird
        // Entwender 'prepend' oder 'append'
        currentInfoPosition: 'prepend',
        // Klasse des span mit dem Infotext
        currentInfoClass: 'current-info'
    });

    $('.tab_ul li:first').addClass('first');


    /* notepad */


    $('#notepad .notepad_button').attr('disabled', 'disabled');
    $('#notepad .notepad_button').addClass('disabled');

    $('#notepad .notepad_checkbox').click(

      function () {
          var checked = 0;
          $('#notepad .notepad_button').attr('disabled', 'disabled');
          $('#notepad .notepad_button').addClass('disabled');

          $("#notepad .notepad_checkbox").each(
              function () {
                  if ($(this).attr('checked')) {
                      checked++;
                  }
              }
            );

          if (checked > 1) {
              $('#notepad .notepad_button').attr('disabled', '');
              $('#notepad .notepad_button').removeClass('disabled');
              $('#notepad .buttons img.tooltip').hide();
          }
          else {
              $('#notepad .buttons img.tooltip').show();
          }
      }

   );


    $(".fade_info .fadeout").fadeIn(3000);
    $(".fade_info .fadeout").fadeOut(2000);




    $('.radio_click img').click(
      function () {
          //$(this).parent().find('ul li').removeClass('hidden_element');

          is_disabled = $(this).parent().find('input').attr('disabled');
          if (!is_disabled) {
              if ($(this).parent().find('input').attr('checked') == true) {
                  $(this).parent().find('input').attr('checked', false);
              }
              else {
                  $(this).parent().find('input').attr('checked', 'checked');
              }
          }

      }

   );

    $('ul.product_teaser li').mouseover(
      function () {
          $(this).addClass('hover');
      }
   );

    $('ul.product_teaser li').mouseout(
      function () {
          $(this).removeClass('hover');
      }
   );

    $('ul.product_teaser li').click(
      function () {
          var href = $(this).find('a').attr('href');
          window.location.href = href;
      }
   );


    $('ul.product_teaser li p').click(
      function () {
          return false;
      }
   );







    $('a.tab_link').click(
      function () {
          $('#main_content').equalHeights();
          return true;
      }
   );



    /* only for xhtml dummy */

    $('.show_product_compare_button').click(
      function () {
          window.location.href = 'produktberater-step-6.html';
      }
   );

    $('.product_guide_step .orange_button').click(
      function () {
          window.location.href = 'produktberater-step-5.html';
      }
   );


    $('.chain_selection .show_product_compare_button').click(
      function () {
          window.location.href = 'ketten-schienen-berater-step-2.html';
      }
   );

    // set min-height. Necessary , if left_navigation is higher than content_middle
    var left_navigation_min_height = $('#left_navigation').css('min-height');
    $('.content_middle').css('min-height', left_navigation_min_height);


    $('#left_navigation ul li a').parent().find('ul li a:first').addClass('afterfirst');
    $('#left_navigation ul li a').parent().find('ul ul li a:first').addClass('afterfirst');
    $('#left_navigation ul li a').parent().find('ul ul ul li a:first').addClass('afterfirst');
    $('#left_navigation ul li a').parent().find('ul ul ul ul li a:first').addClass('afterfirst');


    $('#main_content').equalHeights();

});