$(document).ready(function() {

  $('div.pager_needed').pager('ul');
  $('a.fancybox').fancybox();

  showRandomReference();

  // make links open in new window
  $('a.external').click(function() { 
    window.open($(this).attr('href'));
    return false; 
  });
  $('#image_wrapper a').mouseover(slide);
});

function slide() {
  var className = $(this).attr('class');
  $('#image_wrapper img').not('.'+className).fadeOut(400);
  $('#image_wrapper img.'+className).fadeIn(400);
}

function toggleSharePopup() {
  if($('#share_popup:visible').length === 0) {
    $('#share_popup').fadeIn(200);
  } 
  else {
    $('#share_popup').fadeOut(200);
  }
}
function vinkkaa() {
  var email = $('#vinkkaa_email').val();
  var name = $('#vinkkaa_nimi').val();
  var url = '/tell_a_friend.php';
  var location = window.location.href;
  if(email.length == 0) { 
    $('#vinkkaa_email').css('border-color','#D71635');
    return; 
  } else {
    $('#vinkkaa_email').css('border-color','#C9C7C6');
  }
  $.post(url,{email:email,name:name,url:location},function(data) {
    $('#vinkkaa_email').val('');
    if(data == 'ERR') {

    } 
    else {
      $('#vinkkaa_email').val('');
      $('#vinkkaa_nimi').val('');

      $('#vinkkaa_success').fadeIn(200,function() {
        setTimeout(function() {
          $('#share_popup').fadeOut(200,function() {
            $('#vinkkaa_success').hide();
          });
        },4000);
      });
    }
  });
}
function switchReference() {
  $('div#description span.title').html(references[1].title).fadeIn(200);
  $('img#reference').attr('src',references[1].img).fadeIn(200);
}
function randomToN(maxVal,floatVal) {
   var randVal = Math.random()*maxVal;
   return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}
function showRandomReference() {
  var count = $('div#reference_titles a').length;
  var rand = randomToN(count-1);

  var duration = 600;
  $('div#reference_titles a:eq('+rand+')').fadeIn(duration,function() { $(this).addClass('act'); });
  $('div#reference_images img:eq('+rand+')').fadeIn(duration,function() { $(this).addClass('act'); });
  
  setInterval(function() { showReference('fwd'); },20000);
}
function showReference(direction) {
  var duration = 600;
  
  // link
  var link = $('div#reference_titles a.act');
  link.fadeOut(duration, function() { $(this).removeClass('act'); });
  
  // image
  var img = $('div#reference_images img.act');
  img.fadeOut(duration, function() { $(this).removeClass('act'); });

  if(direction == 'fwd') {
    var nextLink = (link.next().length > 0) ? link.next() : $('div#reference_titles a:first');
    var nextImg = (img.next().length > 0) ? img.next() : $('div#reference_images img:first')
  }
  else {
    var nextLink = (link.prev().length > 0) ? link.prev() : $('div#reference_titles a:last');
    var nextImg = (img.prev().length > 0) ? img.prev() : $('div#reference_images img:last')
  }

  nextLink.fadeIn(duration,function() { $(this).addClass('act'); });
  nextImg.fadeIn(duration,function() { $(this).addClass('act'); });
  
}
/* */
