$(document).ready(function(){
  // if Cpt. Clepto's saved position is out of the window, position him
  //  on the right or bottom edge
  if($.cookie('cpt_x')) {
    ww = $(window).width();
    if($.cookie('cpt_x') < ww - 100) {
      $('#cpt_clepto').css('left', $.cookie('cpt_x') + "px");
    }
    else {
      $('#cpt_clepto').css('left', ww - $('#cpt_clepto_large').outerWidth() + "px");
    }
  }

  if($.cookie('cpt_y')) {
    wh = $(window).height();
    if($.cookie('cpt_y') < wh - 100) {
      $('#cpt_clepto').css('top', $.cookie('cpt_y') + "px");
    }
    else {
      $('#cpt_clepto').css('top', wh - $('#cpt_clepto_large').outerHeight() + "px");
    }
  }

  $("#cpt_clepto").draggable();

  /* iPhone Drag */
  $('#cpt_clepto').touch({
      animate: false,
      sticky: false,
      dragx: true,
      dragy: true,
      rotate: false,
      resort: true,
      scale: false
  });

  /* Save Cpt. Clepto's position on Unload */
  $(window).bind("unload", function() {
    var date = new Date();
    date.setTime(date.getTime() + (24 * 60 * 60 * 1000)); // Cookie Validity in microseconds
    $.cookie('cpt_x', $('#cpt_clepto').position().left, { path: '/', expires: date });
    $.cookie('cpt_y', $('#cpt_clepto').position().top,  { path: '/', expires: date });
  }); 

});

function show_cpt() {
  $('#cpt_clepto_large').show();
  $('#cpt_clepto_small').hide();
}

function hide_cpt() {
  $('#cpt_clepto_large').hide();
  $('#cpt_clepto_small').show();
}

