var haltLoop = false;
var animActive = false;
var baseFolder = '/';

$(document).ready(function() {
  $('div#topMenu ul li').append('<div class="left" /><div class="right" />');

  // add arrows, shadows, glare
  $('div.box > h2, div.box > h3, div.box > h4, div#footer h4').addClass('title');
  $('h2.title, h3.title, h4.title, h5.title').append('<span class="glare">&nbsp;</span><span class="shadow">&nbsp;</span><span class="arrowLeft">&nbsp;</span><span class="arrowRight">&nbsp;</span>');
  
  // var slideHeight = $('div.box.slide').height() - 1;
  // $('div.box.slide').css('height',slideHeight+'px').css('overflow','hidden');
  $('div.box.slide ul.slideList li div.handle > a').click(openSlide);
  $('div.box.slide div.control a.pause').click(toggleHalt);
  $('div.box.slide div.control a.next').click(openNext);
  $('div.box.slide div.control a.prev').click(openPrev);
  
  setTimeout(autoSlide,4000);
  
  DD_roundies.addRule('.rb4px', '4px', true);
  DD_roundies.addRule('.button', '4px', true);
  DD_roundies.addRule('input[type="button"]', '4px', true);
  DD_roundies.addRule('input[type="submit"]', '4px', true);
  DD_roundies.addRule('input[type="reset"]', '4px', true);
  DD_roundies.addRule('.button div.contents', '0 0 4px 4px', true);
  DD_roundies.addRule('.rt4px', '4px 4px 0 0', true);
  DD_roundies.addRule('h2.title', '4px 4px 0 0', true);
  DD_roundies.addRule('.facebook h3', '4px 4px 0 0', true);
  DD_roundies.addRule('#topMenu ul li:first-child', '4px 0 0 4px', true);
  DD_roundies.addRule('div.box h2.title.tabs a.tab', '4px 4px 0 0', true);  
  DD_roundies.addRule('div.tabs ul li', '4px 4px 0 0', true);
  DD_roundies.addRule('div.linkList', '4px', true);
  DD_roundies.addRule('div.linkList ul li:first-child', '4px 0 0 4px', true);

  $('a.external').click(function() { 
    window.open($(this).attr('href'));
    return false; 
  });

  window.fbAsyncInit = function() {
    FB.init({appId: '126981917311890', status: true, cookie: true, xfbml: true});
  };
  (function() {
    var e = document.createElement('script'); e.async = false;
    e.src = document.location.protocol +
      '//connect.facebook.net/fi_FI/all.js';
    document.getElementById('fb-root').appendChild(e);
  }());

});

function toggleHalt() {
  haltLoop = (haltLoop) ? false : true;
  return false;
}
function autoSlide() {
  if(!haltLoop) { 
    openNext(); 
    haltLoop = false;
  }
  var asInterval = setTimeout(autoSlide,4000);
}
function openPrev() {
  var obj = $('ul.slideList li.act').prev();
  if(obj.length === 0) { obj = $('ul.slideList li:last'); }
  obj.find('div.handle a').click();
  haltLoop = true;
  return false;
}
function openNext() {
  var obj = $('ul.slideList li.act').next();
  if(obj.length === 0) { obj = $('ul.slideList li:first'); }
  obj.find('div.handle a').click();  
  haltLoop = true;
  return false;
}
function openSlide() {
  var obj = $(this);
  var duration = 1000;

  if(obj.parent().parent().hasClass('act')) {
  window.location = obj.attr('href');
  }
  
  var prev = obj.parent().parent().siblings().find('div.slide').stop(false,true);
  var next = obj.parent().parent().find('div.slide');
  
  prev.slideUp(duration);
  next.slideDown(duration);
  
  obj.parent().parent().addClass('act').siblings().removeClass('act');
  
  /*
  var handle = $(this);
  var li = $(this).parent();
  var slide = li.find('div.contents');
  var siblings = li.siblings('.open');
  var forward = true;
  if(animActive) { return false; }

  if(li.siblings('.open').length === 0) { // first init
    li.find('div.contents').css('left','auto').css('right',0);
  }
  else if(li.nextAll('.open').length > 0) { // back
    forward = false;
    siblings.find('div.contents').css('right','auto').css('left',0);

    if(li.prevAll().length === 0) { // first
      li.find('div.contents').css('left','auto').css('right',0);
    }
  }
  else if(li.prevAll('.open').length > 0) { // forward  
    siblings.find('div.contents').css('right',0).css('left','auto');
    li.find('div.contents').css('left',0).css('right','auto');
  }
  
  animActive = true;
  siblings.find('a.handle').animate({opacity:1},duration);
  li.find('a.handle').animate({'opacity':0},duration);

  siblings.removeClass('open').animate({'width':'44px'},duration);
  li.addClass('open').animate({'width':'696px'},duration,function() {
    animActive = false;
    if(!forward) {
      siblings.find('div.contents').css('right',0).css('left','auto');
    }
  });
*/

  return false;
}
function fbs_click() {
  u=location.href;
  t=document.title;
  window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
  return false;
}
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 openTab(obj) {
  obj.addClass('act').siblings().removeClass('act');
  
  var tab = obj.attr('href').substr(1);
  $('div.box.playerOTY div.tab').removeClass('act');
  $('#'+tab).addClass('act');
}
function doSearch(e) {
  var keycode;
  if (window.event) { 
    keycode = window.event.keyCode;
  } else if (e) { 
    keycode = e.which;
  }

  if(keycode == 13) {
    $('#searchForm').submit();
    return false;
  }
  
  return true;
}
function addVote(page_id,vote,module) {
  var total = parseFloat($('#votesTotal').val());
  var positive = parseFloat($('#votesPositive').val());
  var negative = parseFloat($('#votesNegative').val());

  total++;
  if(vote > 0) {
    positive++;
    $('#votesPositive').val(positive);
  }
  else {
    negative++;
    $('#votesNegative').val(negative);
  }

  $('#votesTotal').val(total);

  var totalPlus = Math.round(positive/total*100);
  var totalMinus = Math.round(negative/total*100);

  $('#votePlus').html(totalPlus+'%').removeAttr('onclick').click(function() { return false; });
  $('#voteMinus').html(totalMinus+'%').removeAttr('onclick').click(function() { return false; });

  if(module == 'gallery') {
    $.post(baseFolder+'module.php?id=26&act=addVote',{item_id:page_id,vote:vote});
  }
  else {
    $.post(baseFolder+'module.php?id=2&act=addVote',{page_id:page_id,vote:vote});
  }
}

/* */