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

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

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

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

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

});

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

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


