$(function() {
  fitBg();
});

$(window).resize(function(){
  fitBg();
});

function fitBg() {
  // bg stretch
  var imgW = 1458;
  var imgH = 987;
  
  var w = $(document).width();
  var h = $(document).height();
  $('#bg').width(w);
  $('#bg').height(h);
  
  w > imgW ? $('#bg img').width(w) : $('#bg img').width(imgW);
  h > imgH ? $('#bg img').height(h) : $('#bg img').height(imgH);
}

// png hover images (sic)
/*$('#nav li:not(.active) img').hover(function() {
  var src = $(this).attr('src').replace('.png', '_active.png');
  $(this).attr('src', src);
}, function() {
  var src = $(this).attr('src').replace('_active', '');
  $(this).attr('src', src);
});*/

// png hover images (sic)
$(function() {
  $('#nav li:not(.active) img').each(function() {
    var src = $(this).attr('src').replace('.png', '_active.png');
    $(this).after('<img src="' + src + '" alt="" class="hover" />');
  });
  
  $('#categories li:not(.active) img').each(function() {
    var src = $(this).attr('src').replace('.png', '_active.png');
    $(this).after('<img src="' + src + '" alt="" class="hover" />');
  });
  
  $('#overview a').hover(function() {
    $('img', this).hide();
  }, function() {
    $('img', this).show();
  });
});

$('#nav li:not(.active) a').mouseenter(function() {
  $('img:first', this).toggle();
  var num = $(this).hasClass('arrow_next') ? '22px' : '0';
  $('img:last', this).css('left', num);
});
$('#nav li:not(.active) a').mouseleave(function() {
  $('img:first', this).toggle();
  $('img:last', this).css('left', '-300px');
});

$('#categories li:not(.active) a').mouseenter(function() {
  $('img:first', this).toggle();
  var num = $(this).hasClass('arrow_next') ? '22px' : '0';
  $('img:last', this).css('left', num);
});
$('#categories li:not(.active) a').mouseleave(function() {
  $('img:first', this).toggle();
  $('img:last', this).css('left', '-300px');
});

// offer
$('#overview a').mouseenter(function() {
  var num = $(this).parent().prevAll().length;
  $('#products li:eq(' + num + ') a').css('font-weight', 'bold');
}).bind('mouseleave', function() {
  var num = $(this).parent().prevAll().length;
  $('#products li:eq(' + num + ') a').css('font-weight', 'normal');
});

// photogallery
$(function() {
  if(typeof(gallery) !== 'undefined') {
    // replace arrows links
    var num = $('.gallery .active').prevAll().length;
    if (gallery[num - 1]) {$('#prevtImg').attr('href', num - 1);}
    if (gallery[num + 1]) {$('#nextImg').attr('href', num + 1);}
  }
  var num = Math.floor(Math.random() * 11 - 5);
  $('.gallery').css({
    'transform': 'rotate('+ num +'deg)',
    '-webkit-transform': 'rotate('+ num +'deg)',
    '-moz-transform': 'rotate(' + num + 'deg)'
  });
});

$('.gallery a').live('click', function(e) {
  if ($(this).parent().get(0).tagName == 'LI') {
    // numbers links
    var num = $(this).parent().prevAll().length;
  }
  else {
    // arrows links
    var num = parseInt($(this).attr('href'));
  }
  
  $('.gallery li').removeClass('active');
  $('.gallery li').eq(num).addClass('active');
  
  // get image
  var img = gallery[num];
  var alt = img.title ? img.title : img.src;
  
  // set image
  var path = $('.gallery img:first').attr('src').split('/');
  path.pop();
  var image = $('<img />').attr({src: path.join('/') + '/' + img.src, alt: alt}).css('display', 'none');

  $('.gallery img:first').after(image);
  $('.gallery img:first').fadeOut('slow', function(){
    $(this).remove();
    $('.gallery img:last').fadeIn('slow');
  });

  // previous
  if (gallery[num - 1]) {
    if ($('#prevImg').length != 0) {$('#prevImg').attr('href', num - 1);}
    else {$('.gallery').append('<a href="' + (num - 1) + '" id="prevImg">&laquo;</a>');}
  } 
  else {
    $('#prevImg').remove();
  }
  
  // next
  if (gallery[num + 1]) {
    if ($('#nextImg').length != 0) {$('#nextImg').attr('href', num + 1);}
    else {$('.gallery').append('<a href="' + (num + 1) + '" id="nextImg">&raquo;</a>');}
  } 
  else {
    $('#nextImg').remove();
  }

  // no clickthru
  e.preventDefault();
});
