var timer;

var Sending;

var HoverButt;
var HoverBand;

var ActPage = '';
var ActButt = '';

$(document).ready(function(){
  $('div.subscribe_offer').css('display', 'block');
  $('div.sticky_menu').css('display', 'block');

  $('#email').val(readCookie('email'));
  $('#lastname').val(readCookie('lastname'));
  $('#firstname').val(readCookie('firstname'));

  $('a.btn_left').mouseover(function() {
    StopTimer();
    HoverButt = true;

    if (ActButt == $(this).attr('id')) { return; }

    if (ActButt != '') {
      DeactivateButton(ActButt);
    } 

    ActButt = $(this).attr('id');

    $(this)
      .stop()
      .animate({backgroundColor: '#cecece', borderRightColor: '#cecece'}, 300);

    if (ActPage != ActButt) {
      $('#band_' + ActPage).css('display', 'none');
    }
   
    ActPage = ActButt;

    $('div.sticky_menu').css('pointer-events', 'auto');
    $('#band_' + ActPage).stop().css('opacity', 1).fadeIn(300);
    $('div.band').stop().css('opacity', 1).fadeIn(300);
  }).mouseout(function() {
    HoverButt = false;
    StartTimer();
  });   

  $('div.band').mouseover(function() {
    StopTimer();

    if (!HoverBand) {
      StopTimer;
      HoverBand = true;
      $(this).stop().css('opacity', 1).fadeIn(300);
      $('#band_' + ActPage).stop().css('opacity', 1).fadeIn(300);
    }
  }).mouseout(function() {
    HoverBand = false;
    StartTimer();
  });
});

function StartTimer() {
  timer = setTimeout("OnTimer()", 300);
}

function StopTimer() {
  clearTimeout(timer);
}

function OnTimer() {
  $('div.sticky_menu').css('pointer-events', 'none');
  $('#errmess').css('display', 'none');

  $('#band_' + ActPage).stop().fadeOut(300);
  
  $('div.band').stop().fadeOut(300, function() {
    $('#band_' + ActPage).css('display', 'none');
  });

  DeactivateButton(ActButt);
  ActButt = '';
}

$(document).click(function() {
  HideBaloon();
});

$('.baloon').click(function(e) {
  e.stopPropagation();
});


function SaveDelicious(url, title) {
  window.open('http://www.delicious.com/save?v=5&noui&jump=close' + 
    '&url=' + encodeURIComponent(url) + 
    '&title=' + encodeURIComponent(title), 
    'delicious', 'toolbar=no, width=550, height=550'); 
  return false;
}

function getY(oElement) {
  var iReturnValue = 0;

  while( oElement != null ) {
    iReturnValue += oElement.offsetTop;
    oElement = oElement.offsetParent;
  }

  return iReturnValue;
}

function getX(oElement) {
  var iReturnValue = 0;

  while( oElement != null ) {
    iReturnValue += oElement.offsetLeft;
    oElement = oElement.offsetParent;
  }

  return iReturnValue;
}

function ShowBaloon(element, text) {
  var el = document.getElementById(element);
  var bn = $('.baloon');

  bn.stop().fadeOut(300, function() {
    var bn = $('.baloon');

    $('#baloon').html(text);

    bn.css('left', getX(el) + el.offsetWidth / 2 - bn.width() / 2);
    bn.css('top', getY(el) + el.offsetHeight + 16);
    bn.css('opacity', 1);

    bn.fadeIn(500);
    el.focus();
  });
}

function HideBaloon() {
  $('.baloon').fadeOut(300);
}

function DeactivateButton(button) {
  $('#' + button)
   .stop()   
   .animate({backgroundColor: '#bcbcbc', borderRightColor: '#7f7f7f'}, 300);
}

function FeedbackMessage(text, iserror) {
  if (iserror) color = '#ab0000'; 
  else color = '004eab';

  $('#errmess')
    .stop()
    .css('opacity', 1)
    .fadeOut(300)
    .html(text)
    .css('color', color)
    .fadeIn(300);
}

function DoSearch() {
  if($('#search').val() == '') {
    ShowBaloon('search', 'Please specify a search string');
    return false;    
  }

  return true;
}

function DoSubscribe() {
  if (Sending) return;

  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  var address = $('#semail').val();

  if(reg.test(address) == false) {
    ShowBaloon('semail', 'Please specify a valid e-mail address');
    return;
  } 

  Sending = true;

  $.ajax(
    {url: 'subscribe?email=' + address,
    success: function(data, status, jqXHR) {
      Sending = false;
      ShowBaloon('semail', data);

      if (jqXHR.status == 200) {
        $('#semail').val('');
      }
    },
    error: function() {
      ShowBaloon('semail', 'Cannot connect to the server');
      Sending = false;
    }
  });
}

function DoFeedback(url) {
  if (Sending) return;

  $('#errmess').stop().hide();

  if ($('#comment').val() == '') {
    FeedbackMessage('Please enter your comment', true);
    $('#comment').focus();
    return;
  }

  var postData = 
    '&firstname=' + escape($('#firstname').val()) + 
    '&lastname=' + escape($('#lastname').val()) + 
    '&comment=' + escape($('#comment').val()) + 
    '&email=' + escape($('#email').val()) + 
    '&link=' + escape(url);

  createCookie('firstname', $('#firstname').val(), 1000);
  createCookie('lastname', $('#lastname').val(), 1000);
  createCookie('email', $('#email').val(), 1000);

  Sending = true;

  $.ajax({
    url: 'comment',
    type: 'POST',
    data: postData,
    success: function(data, status, jqXHR) {
      Sending = false;
      if (jqXHR.status == 200) {
        FeedbackMessage(data, false);
        $('#comment').val('');
      } else FeedbackMessage(data, true);
    },
    error: function() {
      FeedbackMessage('Cannot connect to the server', true);
      Sending = false;
    }
  });
}

function createCookie(name, value, days) {
  if (days) {
    var date = new Date();

    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    var expires = "; expires=" + date.toGMTString();
  } else var expires = "";

  document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');

  for(var i=0; i < ca.length; i++) {
    var c = ca[i];

    while (c.charAt(0) == ' ') c = c.substring(1, c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name, "", -1);
}
