var closebox = function(e)
{
  if (confirm("Voulez-vous supprimer ce module ?"))
  {
    var targetContent = $('.bloc_content', this.parentNode.parentNode.parentNode);
    targetContent.parent().remove();
    save_prefs();
  }
  return false;
}

$(document).ready(function(){

  // init modules
  $('a.buttonclose').bind('click', closebox);

  // sortables boxes
  $('#sort-box').sortable({
   items : '>li',
   scroll:true,
   revert:true,
   handle : $('.left_action'),
   cursor : 'move',
   //distance : 3,
   tolerance : 'guess',
   containment : $('#maintd'),
   //delay: 150,
   //zIndex : 2000,
   start :  function(e,ui) { /*$('#myGallery').attr('id','myGallery2');  $('#myGallery2').hide(); */ $(ui.helper).html('<div style="width:'+$(ui.helper).width()+'px;height:'+$(ui.helper).height()+'px" class="duringmove"><br/><br/><br/>D&eacute;posez le module<br/>&agrave l\'endroit souhait&eacute;.</div>');},
   stop : function(e,ui) {  /*$('#myGallery2').attr('id','myGallery');  $('#myGallery').show(); window.onDomReady(startGallery); */ },
   update: save_prefs
  });

  // resizable
  i = 0;
  $('#sort-box').children('li').each(function(){

    t = tab_modules[i];

    if(!t) return;

    if(! $('#module-'+t[0]).is('*')) return;

    // modules normaux
    if(t[0] < 1000)
    {
      if(t[1] < 302) t[1] = 302; $('#module-'+t[0]).width(t[1]);
      $(this).resizable({
        minWidth : 302,
        maxWidth : 934,
        height : $(this).height(),
        handles : 'e',
        containment : $('#sort-box'),
        stop : save_prefs
      });
    }
    // modules perso
    else
    {
      if(t[1] < 296) t[1] = 296; $('#module-'+t[0]).width(t[1]);
      $(this).resizable({
        minWidth : 296,
        maxWidth : 934,
        height : $(this).height(),
        handles : 'e',
        containment : $('#sort-box'),
        stop : save_prefs
      });
    }

    // bug fix on MSIE6
    if(jQuery.browser.msie && jQuery.browser.version < 7)
      $(this).height($('#module-'+t[0]).height());
    
    i++;

  });	

  $('#sort-box').css('visibility','visible');

});

function save_prefs()
{
  var ids = [] ;
  var str_tab = [] ;

  ids = $('ul#sort-box').sortable("toArray");

  if(ids.length == 0 && is_loggued == 0)
  {
    $('#none-not-loggued').show();
    set_cookie('pperso_not_loggued',' ',10000);
    return;
  }

  if(ids.length == 0 && is_loggued == 1)
  {
    $('#def-mod').hide(); $('#none-mod').show();
  }
  else
  {
    for(var i= 0 ; i < ids.length ; i++)
    {
      if(ids[i] == null ) continue; // avoid bug

      c = i+1 ; h = 0;
      w = $('#'+ids[i]).width() ;

      // height change while resizing on msie...
      if(jQuery.browser.msie) $('#'+ids[i]).height($('#module-'+t[0]).height());

      id=ids[i].split('-');

      //if($('#module-'+id[1]+' div.bloc_content').css('display') == 'none')  h = 1;
      str_tab.push('m'+c+'='+id[1]+'&w'+c+'='+w+'&h'+c+'='+h);
    }
  }
  str = str_tab.join('&');

  if(is_loggued == 0) set_cookie('pperso_not_loggued',str,10000);
  else
  {
    $.ajax({
      type: 'POST',
      url: '/prefs_ajax',
      data: str,
      success: function(msg){ if(msg) alert(msg);  },
      error: function() { alert('Erreur d\'enregistrement de vos préférences !'); }
    });
  }
}

function set_cookie(name, value, days)
{
  var expire = new Date (); expire.setTime (expire.getTime() + (24 * 60 * 60 * 1000) * days);
  document.cookie = name + "=" + escape(value) + "; expires=" +expire.toGMTString();
}

function get_cookie(name)
{
  var startIndex = document.cookie.indexOf(name);
  if (startIndex != -1)
  {
    var endIndex = document.cookie.indexOf(";", startIndex);
    if (endIndex == -1) endIndex = document.cookie.length;
    return unescape(document.cookie.substring(startIndex+name.length+1, endIndex));
  }
  else return null;
}

function delete_cookie(name)
{
  var expire = new Date (); expire.setTime (expire.getTime() - (24 * 60 * 60 * 1000));  document.cookie = name + "=; expires=" + expire.toGMTString();
}

