﻿    
    // function for resizing various stuff.
    function make_fit() {
      var currentPosition = 0;
      var win_height = ($(window).height() - 108);
      var win_width = $(window).width();
      var slideWidth = $('#container img').width();
      var img_number = $('#container .slide').size();
      var ratio = 9/6;
      $('#container img, #container, .control, #slideshow').height(win_height);
      $('#container img,#container .slide').width(win_height/ratio);
      $('#container').width(win_width);
      $('#slideinner').width(20000);
    }
    
    // on load
    $(window).bind("load", function(){
      make_fit();
    });
    
    // on browser resize
    $(window).bind("resize", function(){
  
      make_fit();
        
    });
    
    // Insert controls in the DOM
      $('#slideshow')
        .prepend('<div class="control" id="go_back"></div>')
        .append('<div class="control" id="go_fwd"></div>');
      
      

        
    $(function(){
      
      make_fit();
      
      var currentPosition = 0;
      var slideWidth = $('#container img').width();
      var slidesWidth = $('#container').width();
      var slides = $('.slide');
      var numberOfSlides = slides.size();
      var win_width = $(window).width();
      var slideNum = win_width/slideWidth;
      
            
//      console.log((numberOfSlides*slideWidth));      

      // wrap slides with div
      slides
        .wrapAll('<div id="slideinner"></div>');

      // Create event listeners for .control clicks
      $('.control')
        .bind('click', function(){
          // determine new position
          currentPosition = ($(this).attr('id')=='go_fwd') ? currentPosition+1 : currentPosition-1;
            // hide/show controls
            manageControls(currentPosition);
          //  console.info(currentPosition);
              
            // Move slideinner using margin-left.
            // Need to figure a way to dynamically adjust slideWidth.
            $('#slideinner').animate({
               'marginLeft' : (slideWidth+2)*(-currentPosition)
            });
            
        });
      
      // manageControls: Hides and Shows controls depending on currentPosition
      function manageControls(position){
      
        // Hide left arrow if position is first slide
        if(position==0){ $('#go_back').hide() } else{ $('#go_back').show() }
        // Hide right arrow if position is last slide
        if(position==numberOfSlides-1){ $('#go_fwd').hide() } else{ $('#go_fwd').show() }

        
      }
      
      
      // Hover stuff for stupid browsers      
      $("#stores tr").hover(
        function () {
          $(this).addClass("hover");
        },
        function () {
          $(this).removeClass("hover");
        }
      );


      
    // function for filtering the store locator table
      $.fn.sorted = function(customOptions) {
        var options = {
          reversed: false,
          by: function(a) { return a.text(); }
        };
        $.extend(options, customOptions);
        $data = $(this);
        arr = $data.get();
        arr.sort(function(a, b) {
          var valA = options.by($(a));
          var valB = options.by($(b));
          if (options.reversed) {
            return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;        
          } else {    
            return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;  
          }
        });
        return $(arr);
      };


    $('#filter_form').submit(function () {
      
        return false;
      });
    
    // forcus the filter search field
    $("input.filter_input").focus();  
   
     // listen for keyup from filter input
     $(".filter_input").keyup(function () {
    
        var filter = $(this).val(), count = 0;
    
        $(".filtered:first tr").each(function () {
              if ($(this).text().search(new RegExp(filter, "i")) < 0) {
                  $(this).addClass("hidden");
              } else {
                  $(this).removeClass("hidden");
                  count++;
              }
          });
      });
      
      
      $('label.required_field').append(' <span>*</span>');
      
  
      
      $('.validate_form').validate();
        $.validator.messages.required = "This field is required";    
    if (lang == 'da-DK') {
        $.validator.messages.required = "Husk at udfylde feltet";    
    }      

    // Test med lazyload. Virker ikke pt. En mulig forbedring.
      //$("img:right-of-fold").lazyload({ placeholder: "http://carla2010.net.dynamicweb.dk/files/system/carla2010/graphics/grey.gif", effect: "fadeIn"});

    
    // Gallery
    
    
    // Tilfoj alt-nummer til onAfter()
    $('#gallery img').each(function(index) {
      //console.log(index);
      $(this).attr('alt',index);
    });
    
    $('#gallery').cycle({
      fx: 'fade',
      startingSlide: 0,
      prev:   '#prev',
      next:   '#next',
      speed:  0,
      after: onAfter,
      timeout: 0
    });




    $(".do_fancy").fancybox({
      cyclic: false,
      overlayOpacity:   '1',
      overlayColor:  '#ffffff',
      showNavArrows:  true,
      transitionIn:  'none',
      transitionOut:  'none',
      speedIn:  0,
      speedOut:  0,
      changeSpeed:  0,
      changeFade:   0
    });



});
  
  

    function onAfter(curr, next, opts) {
      var current = $('#gallery div:visible img').attr('alt');
      //console.log(current);
        $('#prev')[current == 0 ? 'hide' : 'show']();
      $('#next')[current == opts.slideCount - 1 ? 'hide' : 'show']();
    }

