

function fav(button) {
	var id = button.id;

    var check = id.split('_')[0];

    if (check == 'sign')
    {
    	var sign = id;
    	var uid = id.split('_')[3];
    	var type = id.split('_')[1];
        var id = id.split('_')[2];
        var img = type+'_'+id+'_'+uid;
    }
    else
    {
    	var sign = "sign_"+id;
    	var uid = id.split('_')[2];
    	var type = id.split('_')[0];
    	var id = id.split('_')[1];
    }
    var img = type+'_'+id+'_'+uid;

   var fav_layouts = getCookie('fav_layouts');
   if (!fav_layouts) {
      var expires=new Date();
       expires.setDate(expires.getDate()+5);

       _setCookie('fav_layouts', 'test', expires, '/', cookieDomain, null);

      fav_layouts = '';

   }
   var favs = fav_layouts.split('|');
   if (!favs || '' == favs) {
      favs = [];
   }

   if (check == 'save') {
      favs.push(id);

      fav_store_cookie(favs);

      var el = $(button);
      while (!el.hasClass('fav_holder')) {
         el = $(el[0].parentNode);
      }

      el[0].innerHTML = '<a href="#"><img width="16" height="16" border="0" title="Added to Favorites" alt="Added to Favorites" src="http://images.fbskins.com/images/faves/heart-saved.png" style="vertical-align:middle;"></a>'+
       '<a href="/facebook-layouts/saved.php"> Saved</a> '; /*+
       ' | <a href="view.php?id='+id+'">Preview</a>' +
       ' | <a href="#" onclick="return AjaxApplyLayout('+id+')">Select Layout</a>';*/
   }
   else if (check == 'removesaved') {
      var newFavs = [];
      // Remove clicked layout
      for (var i = 0; i < favs.length; ++i) {
         if (favs[i] != id) newFavs.push(favs[i]);
      }

      fav_store_cookie(newFavs);

      var el = $(button);
      while (!el.hasClass('fav_holder')) {
         el = $(el[0].parentNode);
      }

      el[0].innerHTML = '<a href="#"><img width="16" height="16" border="0" title="Add to Favorites" alt="Add to Favorites" src="http://images.fbskins.com/images/faves/heart-save.png" style="vertical-align:middle;"></a>'+
       '<a href="#" id="save_'+id+'" onclick="return fav(this)"> Save</a> ';/* +
       ' | <a href="view.php?id='+id+'">Preview</a>' +
       ' | <a href="#" onclick="return AjaxApplyLayout('+id+')">Select Layout</a>';*/
   }

   return false;
}

function fav_store_cookie(rgFavs) {
   var new_fav = rgFavs.join('|');

   var expires=new Date();
   expires.setDate(expires.getDate()+5);
   _setCookie('fav_layouts', new_fav, expires, null, cookieDomain, null);
}
