$.fn.linkSelect = function() {
	this.each(function() {
		obj = $(this);

		obj.after('<div id="select_' + obj.attr('id') + '" class="select_container">'
        + '<div class="select_arrow_down"></div>'
				+ '  <div class="select_options"> </div>'
				+ '</div>');
		obj.find('option').each(function(i){
			 $('#select_' + obj.attr('id') + ' .select_options').append('<a href="'
					 + $(this).attr('value')
					 + '" class="select_option">'
					 + $(this).html()
					 + '</a>').hide();
		});

    $('#select_' + obj.attr('id') + '.select_container').click(function(){
      $(this).find('.select_options').slideToggle("fast");
    });

    if (obj.attr('id') == 'brand_id')
    {
      $("#select_brand_id .select_arrow_down").html('Kies merk');
    }
    else if (obj.attr('id') == 'productgroup_id')
    {
      $("#select_productgroup_id .select_arrow_down").html('Kies productgroep');
    }

    obj.remove();
  });
}

$.fn.customScroll = function() {
	this.each(function() {
		var obj = $(this);
    var paneHeight = obj.find('.scroll-pane').height();
    var paneOffset = obj.find('.scroll-pane ul').offset().top;
    var paneFlow = obj.find('.flow').height();
    var itemRowHeight = obj.find('.scroll-pane li.first').height();
    var startMargin = parseFloat(obj.find('.scroll-pane ul').css('margin-top'), 10);
    var ready = true;

    $('#'+ obj.attr('id') + ' .scroll_up').bind('click', function() {
      var currentOffset = obj.find('.scroll-pane ul').offset().top;

      if (paneOffset > currentOffset && ready)
      {
        ready = false;
        obj.find('.scroll-pane ul').animate({
          'marginTop' : '+=' + (2 * itemRowHeight) + 'px'
        }, 500, function () {
          ready = true;

          if (paneOffset <= (currentOffset + (2 * itemRowHeight)))
          {
            obj.find('.scroll_up').hide();
          }
        })
        obj.find('.scroll_down').show();
      }

      return false;
    });

    $('#'+ obj.attr('id') + ' .scroll_down').bind('click', function() {
      var currentOffset = obj.find('.scroll-pane ul').offset().top;

      if (paneOffset <= (currentOffset + paneHeight - paneFlow - startMargin) && ready)
      {
        ready = false;
        obj.find('.scroll-pane ul').animate({
          'marginTop' : '-=' + (2 * itemRowHeight) + 'px'
        }, 500, function () {
          ready = true;

          if (paneOffset > (currentOffset + paneHeight - paneFlow - startMargin - (2 * itemRowHeight)))
          {
            obj.find('.scroll_down').hide();
          }
        })
        obj.find('.scroll_up').show();
      }

      return false;
    });
  });
}

// Cycle / slider
$(document).ready(function () {
  var currentSlide = 1;
  var startMargin = parseInt($("#slideshow_overlay").css('margin-top'));
  var numberSlides = $("#slideshow .slideshow").children().length;
  var heightMenuItem = 23;

  var slideshow = $('#slideshow .slideshow').cycle({
    fx: 'fade',
    speed: 1500,
    timeout: 5000,
    before:  onBeforeSlideshow
//    pagerAnchorBuilder: function(idx, slide) {
//        return '#slideshow .slideshow_menu li:eq(' + idx + ') a';
//    },
//    pager: '#slideshow .slideshow_menu ul',
//    pagerEvent: 'mouseover',
//    pauseOnPagerHover: true
  });

  function onBeforeSlideshow() {
    if (currentSlide % numberSlides == 1) {
      $("#slideshow_overlay").stop(true).animate({
        'marginTop' : startMargin + 'px'
      })
      currentSlide = 1;
    } else {
      $("#slideshow_overlay").animate({
        'marginTop' : '+='+ heightMenuItem + 'px'
      })
    }

    $('.slideshow_menu ul li').each(function(){
      $(this).removeClass('selected');
    });

    $('.slideshow_menu ul li').eq(currentSlide-1).addClass('selected');

    currentSlide++;
  }

  $('.slideshow_menu ul li').hover(function(){
      var index = $(this).index();

      $('#slideshow .slideshow').cycle('pause');
      $('#slideshow .slideshow').children().eq(index).css('z-index', '49').css('opacity', '1');
      $('#slideshow .slideshow').children().eq(index).show();

      $("#slideshow_overlay").stop(true).animate({
        'marginTop' : startMargin + heightMenuItem*index + 'px'
      });
    }, function(){
      var index = $(this).index();
      
      $('#slideshow .slideshow').cycle('resume');
      
      $('#slideshow .slideshow').children().eq(index).css('z-index', '9').css('opacity', '0');
      $('#slideshow .slideshow').children().eq(index).hide();

      var slideSelected = 0;
      $('.slideshow_menu ul li').each(function(){
        if ($(this).attr('class') == 'selected')
        {
          slideSelected = $(this).index();
        }
      });
      
      $("#slideshow_overlay").animate({
        'marginTop' : startMargin + heightMenuItem*slideSelected + 'px'
      });
    }
  );


  $('#header .header').cycle({
    fx: 'fade',
    speed: 1500,
    timeout: 5000,
    before:  onBeforeHeader
  });

  function onBeforeHeader() {
  }

  $('#references .slides').cycle({
    fx: 'fade',
    speed: 1500,
    timeout: 3000,
    before:  onBeforeReference
  });

  function onBeforeReference() {
    var index = $(this).index();

    $("#references .links .color").each(function() {
      $(this).hide();
    });

    $("#references .links .grey").each(function() {
      $(this).show();
    });

    $("#references .links li").eq(index).find(".grey").hide();
    $("#references .links li").eq(index).find(".color").show();
  }

 $('#references .links li').hover(function(){
      var index = $(this).index();

      $('#references .slides').cycle('pause');
      $('#references .slides a').eq(index).css('z-index', '49').css('opacity', '1');
      $('#references .slides a').eq(index).show();
    }, function(){
      var index = $(this).index();

      $('#references .slides').cycle('resume');

      $('#references .slides a').eq(index).css('z-index', '9').css('opacity', '0');
      $('#references .slides a').eq(index).hide();

      var slideSelected = 0;
      $('.slideshow_menu ul li').each(function(){
        if ($(this).attr('class') == 'selected')
        {
          slideSelected = $(this).index();
        }
      });
    }
  );
  $('#actions .slideshow').cycle({
    fx: 'fade',
    speed: 1500,
    timeout: 5000,
    before:  onBeforeAction
  });

  function onBeforeAction() {
    $("#actions a").toggleClass('selected');
  }

  $('#actions li').hover(function(){
      $('#actions .slideshow').cycle('pause');
      $('#actions .slideshow').cycle($(this).index());
  }, function() {
      $('#actions .slideshow').cycle('resume');
  });
  
// custom select
  $("#productgroup_id").linkSelect();
  $("#brand_id").linkSelect();


// direct links
  $("#direct_links .category .title").bind("click", function(){
    var item = $(this);

    $("#direct_links .category .title").each(function() {
      if ($(this).html() != item.html())
      {
        $(this).next().slideUp(250);
        $(this).parent().removeClass('opened');
      }
    });

    $(this).parent().toggleClass('opened');
    $(this).next().slideToggle(250);
  });

// left menu orange blocks
  $("#column_left .block strong, #column_left .block h3").bind("click", function(){
    var item = $(this);

    $("#column_left .block strong, #column_left .block h3").each(function() {
      if ($(this).html() != item.html())
      {
        $(this).next().slideUp(250);
      }
    });

    item.next().slideToggle(250);
  });

  // start configuration = first opened
  $("#column_left .block:first").find('.content').slideDown(250);

  // custom scroll
  $('#branches').customScroll();
});
