function setSource( elid, image ) {
    var source = "";
    if (isDarkColor( $('#content .editBox').css('background-color') )) {
        source = '/../../images/icons/White/' + image;
    } else {
        source = '/../../images/icons/Black/' + image;
    }
    
    document.getElementById(elid).src = source;
}

$(document).ready(function() {

  $('input').placeholder();

  $('a#showChangeBgForm').click(function(){
    $('#changeBgForm').fadeIn(150);
    $(this).parent().hide();
    return false;
  });

  $('#changeBgFormCancel').click(function(){
    $('#changeBgForm').hide();
    $('#showChangeBgForm').parent().fadeIn(150);
    
    return false;
  });

  $('#content .journalEntry').hoverIntent(
    function(){
      $(this).find('.journalEntryOwnerSection').fadeIn();
    },
    function(){
      $(this).find('.journalEntryOwnerSection').fadeOut();
    }
    );
  //flyOuts
  $('.flyout').hoverIntent(
    function(){
      $(this).find('.flyoutContainer').fadeIn();
      $(this).find('.flyoutTrigger').addClass('active');
      
    },
    function(){
      $(this).find('.flyoutContainer').fadeOut();
      $(this).find('.flyoutTrigger').removeClass('active');
    }
    );
    
  //loginFlyOut
  /*$('#login').hoverIntent(
    function(){
      $(this).find('.flyoutContainer').fadeIn();
      $(this).find('.flyoutTrigger').addClass('active');

    },
    function(){}
    );*/
  
  
  
  
  

    
  $(".highlightImageLinks a img").css({
    opacity:1
  }).hover(function(){ 
    $(this).fadeTo("fast", 0.8);
  },function(){ 
    $(this).fadeTo("slow", 1);
  });

  $('#cancelProfileImport').click(function(){
    window.location.replace("/profile/edit");
  });
    
  $(".journalEntry .entryDelete a").click(function () {
    if (confirm('Are you sure?')) {
  }
  });
    
   $('#hoverlogin').click(function(event){
    //event.stopPropagation();
    if ( ! clickedLogin ) {
      
      stayon = true;
      $('.flyoutContainer').fadeIn();
      $('.flyoutTrigger').addClass('active');
     
      clickedLogin = true;
  } else {
     if( $('.flyoutTrigger').hasClass('active') ){
      $('.flyoutContainer').fadeOut();
      $('.flyoutTrigger').removeClass('active');

      clickedLogin = false;
    }}
 });
 
 $('.fr').mouseout( function() {
     stayon = false;
     window.setTimeout('LoginOut();', 900);
  });
  
  $('.fr').mouseover( function() {
     stayon = true;
  });
  
  $('#hoverlogin').mouseout( function() {
     stayon = false;
     window.setTimeout('LoginOut();', 900);
  });
  
  $('#hoverlogin').mouseover( function() {
     stayon = true;
  });
   
});

function getX(el) {
  x = el.offsetLeft;
  if (!el.offsetParent) return x;
  else return (x+getX(el.offsetParent));
}

function getY (el) {
  y = el.offsetTop;
  if (!el.offsetParent) return y;
  else return (y+getY(el.offsetParent));
}

/**
 * This method is parsing and rbg string and return true if the color is dark
 * or false if it is a bright color
 * 
 * Example: rgb(87, 0, 0)
 */
function isDarkColor(rgbStr) {
  var colorArray = rgbStr.substring(4, rgbStr-5).split(',');
  var r = Number($.trim(colorArray[0]));
  var g = Number($.trim(colorArray[1]));  
  var b = Number($.trim(colorArray[2]));  
  
  var sum = r + b + g;
  if (sum > 128*3) {
    return false;
  }
  return true;
}


  
  
    
  var clickedLogin = false;
  var stayon = false;
  
  function LoginOut() {
      if ( ! stayon ) {
        $('.flyoutContainer').fadeOut();
        $('.flyoutTrigger').removeClass('active');
        clickedLogin = false; 
      }
  }
  
    
  
   
