﻿/**
 *
 * Tactik3 main javscript file
 * see bottom of file for global variables
 *
 */

/**
 *
 * --- GameFlash object
 *
 */

toutpetits = {};
toutpetits.ui = {};
toutpetits.api = {};


//Front-end API
heap.api.toutpetits = {};
heap.api.setup({
  object: heap.api.toutpetits, 
  'api_class':'tp_users',
  url: heap.urlmap.api_json,
  methods: ['login','logout','signup','editparent','editchild','erasechild','addchild','pwreminder','pwreset']
});

/**
 *
 * --- UI Page object
 *
 */

toutpetits.ui = function (config)
{
  this.base_uri = location.pathname;  
  this.hash = location.hash.substr(1);
  this.querystring = location.search.substr(1);
  this.config = config;
  
  this.get_params = {};
  var split1 = this.querystring.split('&');
  for (var i=0; i < split1.length; i++)
  {
    var split2 = split1[i].split('=');
    this.get_params[split2[0]] = split2[1];
  }
    
  this.initialize();
  
};

toutpetits.ui.prototype.initialize = function()
{
  var that = this; 
  
  $("#ilogin").bind('click', function(){
    $(".panel").hide();
    $("#login_submit").fadeIn('fast');
    return false;
  });
  
  $("#icancel").bind('click', function(){
    $(".panel").hide();
    $("#login_intro").fadeIn('fast');
    return false;
  });
  
  $("#ilogin_submit").bind('click', function(){
    toutpetits.api.loginSubmit();
    return false;
  });
  
  $("#ilogout").bind('click', function(){
    toutpetits.api.logoutSubmit();
    return false;
  });
  
  $("#iadd_kid").bind('click', function(){
   
    var kid_bloc = $("#bloc_kid_container .js-bloc_kid").eq(0).clone(true);
    kid_bloc.find("a.idelete_kid").addClass('js-unsaved').show();
    $('#bloc_kid_container').append(kid_bloc);
    
    if($("#bloc_kid_container .js-bloc_kid").length >= 15)
    {
      $(this).closest('.isubmit').hide();
      $('#ikids_limit').show();
    } 
    
    return false;
  });
  
  $("#iadd_kid_mod").bind('click', function(){
    
    var kid_bloc = $("#bloc_kid_container .js-bloc_kid").eq(0).clone(true);
    var new_id = new Date().getTime();
    
    kid_bloc.find("a.isave_kid").addClass('js-unsaved');    
    kid_bloc.find("a.icancel_kid").addClass('js-unsaved');    
    kid_bloc.find("a.idelete_kid").addClass('js-unsaved');
    
    var kid_block_aff = kid_bloc.find('.affichage_kid').hide();
    kid_block_aff.attr('id',kid_block_aff.attr('id')+new_id);
        
    var kid_block_mod = kid_bloc.find('.modification_kid').show();
    kid_block_mod.attr('id',kid_block_mod.attr('id')+new_id);
    
    kid_block_mod.find('input[name="k_slug"]').val('');
    kid_block_mod.find('input[name="k_email"]').val('');
    kid_block_mod.find('input[name="k_username"]').val('');
    
    $('#bloc_kid_container').append(kid_bloc);
    $('#bloc_kid_container .js-bloc_kid:last').find('.modification_kid').show();
    $('#bloc_kid_container .js-bloc_kid:last').find('.modification_kid').find('.bloc_kid .avatar a.next').trigger('click');
    
    Cufon.refresh('.isubmit .font_d');
    
    if($("#bloc_kid_container .js-bloc_kid").length >= 15)
    {
      $(this).closest('.isubmit').hide();
      $('#ikids_limit').show();
    } 
    
    return false;
  });
  
  $("#isubmit_inscription").bind('click', function(){
    toutpetits.api.inscriptionSubmit();
    return false;
  });
  
  $("#isubmit_password").bind('click', function(){
    toutpetits.api.sendPasswordSubmit();
    return false;
  });
  
  $("#ireset_password").bind('click', function(){
    toutpetits.api.resetPasswordSubmit();
    return false;
  });
  
  $("a.switch").bind('click', function(){
    var target = $(this).attr('href');
    $(".gpanel").hide();
    $(target).fadeIn('fast');    
    return false;
  });
  
  $("#isubmit_modification").bind('click', function(){
    toutpetits.api.modificationParentSubmit(this);
    return false;
  });
  
  $("a.iedit_kid").bind('click', function(){
    var target = $(this).closest('.js-bloc_kid').find('.modification_kid').eq(0);//attr('href');
    $(this).closest(".masquable").hide();
    $(target).fadeIn('fast');    
    return false;
  });
  
  $("a.icancel_kid").live('click', function(){
    if($(this).is('.js-unsaved'))
    {
      $(this).closest('.js-bloc_kid').remove();
      $('.isubmit.add_kid').show();
      $('#ikids_limit').hide();
    }
    else
    {
      var target = $(this).closest('.js-bloc_kid').find('.affichage_kid').eq(0);//$(this).attr('href');
      $(this).closest(".masquable").hide();
      $(target).fadeIn('fast');    
    }
    return false;
  });
  
  $("a.isave_kid").live('click', function(){
    var target = '#modification_parent';
    $(".gpanel").hide();
    $(target).fadeIn('fast');
    if($(this).is('.js-unsaved'))
    {
      toutpetits.api.ajoutEnfantSubmit(this);   
    }
    else
    {
      toutpetits.api.modificationEnfantSubmit(this); 
    }
    return false;
  });
  
  $("a.idelete_kid").live('click', function(){
    if($(this).is('.js-unsaved'))
    {
      $(this).closest('.js-bloc_kid').remove();
    }
    else
    {
      var target = '#modification_parent';
      $(".gpanel").hide();
      $(target).fadeIn('fast');
      if (confirm(heap.translate('inscription_kid_confirm_suppression')))
      {
        toutpetits.api.suppressionSubmit(this);
        $('.isubmit.add_kid').show();
        $('#ikids_limit').hide();
      }
    }
    
    return false;
  });
  
  $("#header .main .nouser a").fancybox({
    href: '#fb_login',
    autoDimensions: true,
    centerOnScroll: true,
    onStart : function ()
    {
      
    },
    onComplete : function ()
    {
     
    }
  });
   
  $("input.focusable").bind('focus', function(){        
    if($(this).is('.focusable'))
    {
      $(this).removeClass('focusable');
      $(this).val('');
    }
    return false;
  });
  
  $("#itabs li").bind('mouseenter mouseleave', function(){
    Cufon.refresh();
    return false;
  });
  
  $('a.expander').bind('click', function(){
    var target = $(this).attr('rel');
    var params_hide = { height: "0px", opacity: 0 };
    var params_show = { height: "122px", opacity: 1 };
        
    if($(this).hasClass('selected'))
    {
      $(this).removeClass('selected');
      $(target).animate(params_hide, 300, function(){
        $(this).css({display: 'none'});
      });      
    }
    else
    {
      $(this).addClass('selected');
      $(target).css({display: 'block'});      
      $(target).animate(params_show, 700, function(){
        
      });
    }
		Cufon.refresh();		
    return false;
  });
  
  $('.bloc_kid .avatar a.next').bind('click', function(){ 
    if($(this).is('.active')) return false;
    $(this).addClass('active');
    
    var kcontainer = $(this).closest('.avatar');
    var kavatars = kcontainer.find('.picto span');
    var curr_index = kcontainer.find('input[name="document_index"]').val();
    curr_index++;
    
    if(kavatars.length > curr_index)
    {
      
    }
    else
    {
      curr_index = 0;
    }
      kavatars.hide();
      kavatars.eq(curr_index).fadeIn();
      kcontainer.find('input[name="document_index"]').val(curr_index);
      kcontainer.find('input[name="document"]').val(kavatars.eq(curr_index).find('img').attr('alt'));
      
    $(this).removeClass('active');
    return false;
  });
  
  $('.bloc_kid .avatar a.prev').bind('click', function(){   
    if($(this).is('.active')) return false;
    $(this).addClass('active');
    
    var kcontainer = $(this).closest('.avatar');
    var kavatars = kcontainer.find('.picto span');
    var curr_index = kcontainer.find('input[name="document_index"]').val();
    curr_index--;

    if(curr_index >= 0)
    {
      
    }
    else
    {
      curr_index = kavatars.length - 1;
    }
      kavatars.hide();
      kavatars.eq(curr_index).fadeIn();
      kcontainer.find('input[name="document_index"]').val(curr_index);
      kcontainer.find('input[name="document"]').val(kavatars.eq(curr_index).find('img').attr('alt'));
    $(this).removeClass('active');
    return false;
  })

  $('a.tooltipable').bind('mouseenter', function(){
    $('.tooltip').hide();
    $(this).find('.tooltip').fadeIn('fast');  
  
  });
  $('a.tooltipable').bind('click', function(e){
    e.preventDefault();
  });
  $('a.tooltipable').bind('mouseleave', function(){
    $(this).find('.tooltip').hide();  
  });
  
  $('#homepage a.logo').bind('click', function(){
    Toutpetits_killZephir();
    return false;
  });  
  
  $('#homepage a#zephir_trigger').bind('click', function(){
    Toutpetits_JouerClicked();
    return false;
  }); 
  
  Cufon.replace('.font_d', { fontFamily: 'Ducky', hover: false });
  
  // pub check : remove container if no pub returned by adsserver
  var pub_exists_header = false;
  $('#header .pub').not('.pub-disabled').children().each(function(index, dom_elem) {
    if(dom_elem.tagName && (dom_elem.tagName.toLowerCase() != 'script') && (dom_elem.tagName.toLowerCase() != 'noscript'))
    {
      pub_exists_header = true;
    }
  });
  if(!pub_exists_header)
  {
    //animate({ backgroundColor: '#DDD4C9' }, 2000, function(){});
    //$('#header .pub').remove();
    $('#header .top').animate({ marginTop: '-105px' }, 800, function(){$('#header .pub').remove();});
  }
  
  var pub_exists_rcol = false;
  $('#content .rcol .pub').not('.pub-disabled').children().each(function(index, dom_elem) {
    if(dom_elem.tagName && (dom_elem.tagName.toLowerCase() != 'script') && (dom_elem.tagName.toLowerCase() != 'noscript'))
    {
      pub_exists_rcol = true;
    }
  });
  if(!pub_exists_rcol)
  {
    $('#content .rcol .pub').remove();
    $('#content .rcol').css({ 'background-image': 'none' });
  }
  
}; // end ui.initialize

toutpetits.ui.prototype.openZephir = function(url)
{
  var that = this; //,width=1100,height=600
  that.popzephir = window.open(url,"popzephir","resizable=no,directories=no,location=no,status=no,scrollbars=yes,menubar=no,fullscreen=yes");
  //window.location.href = url;
}; 

toutpetits.closeZephir = function()
{
  var that = this;
  if (tpUI.popzephir.document) { tpUI.popzephir.close(); }

}; 

/**
 *
 * --- API object
 *
 */

toutpetits.api.loginSubmit = function()
{
  var params = new Object();
  params['email'] = $('input#ilemail').val();
  params['password'] = $('input#ilpassword').val();    
  $('#iform_login .errors').html('');
  
  heap.api.toutpetits.login(toutpetits.api.loginCallback, params);

  return false;
};

toutpetits.api.loginCallback = function(response)
{
  if(response.type == heap.SUCCESS)
  {
    window.location.href = heap.urlmap.index;
  }
  else 
  {    
  	// validate login response errors
    switch (response.code) 
    {
      case "AUTH_WRONG_PASSWORD":
        string = heap.translate('ilogin_pw_validator');
        break;
      default:
        string = heap.translate('ilogin_email_validator');
        break;
    }
   $('#iform_login .errors').html('<span>'+string+'</span>');
  }
  return false;
};

/**
 * 
 */
 
toutpetits.api.logoutSubmit = function()
{
  heap.api.toutpetits.logout(toutpetits.api.logoutCallback, {});

  return false;
};

toutpetits.api.logoutCallback = function(response)
{
  if(response.type == heap.SUCCESS) {
	  window.location.href = heap.urlmap.index;
	} else {
		$('#ilogout').removeClass('disabled');
	}
	  
	return false;
};

/**
 * 
 */
 
toutpetits.api.sendPasswordSubmit = function()
{
  $('.errors').hide().find('div').hide();
  var params = { 
    email: $('input#iemail').val()
  };
  heap.api.toutpetits.pwreminder(toutpetits.api.sendPasswordSubmitCallback, params);

  return false;
};

toutpetits.api.sendPasswordSubmitCallback = function(response)
{
  if(response.type == heap.SUCCESS) {
    $('#panel_mdp').hide();
	  $('#panel_response').fadeIn();
	} else {		
	  $('.errors').show().find('div').fadeIn();
	}
	return false;
};

/**
 * 
 */
 
toutpetits.api.resetPasswordSubmit = function()
{
  $('.errors').hide().find('div').hide();
  var params = { 
    "validation_code": $('input#ivalidation_code').val(),
    "password": $('input#ipassword').val(),
    "password-confirm": $('input#ipassword-confirm').val()
  };
  heap.api.toutpetits.pwreset(toutpetits.api.resetPasswordSubmitCallback, params);

  return false;
};

toutpetits.api.resetPasswordSubmitCallback = function(response)
{
  if(response.type == heap.SUCCESS) {
    $('#panel_mdp').hide();
	  $('#panel_response').fadeIn();
	} else {		
	  $('.errors').show().find('div.'+response.code).fadeIn();
	}	  
	return false;
};

/**
 * 
 */
 
toutpetits.api.inscriptionSubmit = function()
{
  $('.error_msg').fadeOut();
  $('.errors').slideUp();
  //alert('inscriptionSubmit1');
  var form_object = new Object();
  form_object['username'] = $('#iusername:not(.focusable)').val();
  form_object['email'] = $('#iemail:not(.focusable)').val();
  form_object['email-confirm'] = $('#iemail-confirm:not(.focusable)').val();
  form_object['region'] = $('#iregion').val();
  form_object['password'] = $('#ipassword').val();
  form_object['password-confirm'] = $('#ipassword-confirm').val();
  form_object['allow_newsletter'] = $('#inewsletter').is(':checked') ? 1 : 0;
  form_object['meta_rapport'] = $('#irapport').is(':checked') ? 1 : 0;
  
  var children = [];
  $('#bloc_kid_container .bloc_kid').each(function(i, domElem){
    
    var child = {};
    var item = $(domElem);
    
    child.username = item.find('input[name=k_username]:not(.focusable)').val();
    child.meta_dob = item.find('select[name=k_dbo_y]').val() + '-' + item.find('select[name=k_dbo_m]').val() + '-' + item.find('select[name=k_dbo_d]').val();
    child.meta_avatar = item.find('input[name=document]').val();
    children.push(child);
  });
  
  form_object['children'] = children;
  
  heap.api.toutpetits.signup(toutpetits.api.inscriptionSubmitCallback, form_object);

  return false;
};

toutpetits.api.inscriptionSubmitCallback = function(response)
{
  //alert('inscriptionSubmitCallback');
  if(response.type == heap.SUCCESS) {
	  window.location.href = heap.urlmap.parent_comptes_confirmation;
	} else {
    var errors = response.data;
			  
    $.each(errors, function(err_key, err_value){ 
      if(err_key == 'children')
      {
        $.each(err_value, function(i, child){
          $.each(child, function(child_err_key, child_err_value){
            //console.log('child_err_key='+child_err_key+' - child_err_value = '+child_err_value);
            $('#bloc_kid_container .js-bloc_kid').eq(i).find('.errors').show().find('.err_'+child_err_value).fadeIn();
          });
        });
      }
      else
      {
        $('#ierr_'+err_value).fadeIn();
      }      
    });
	}
	return false;
};

/**
 * 
 */
 
toutpetits.api.modificationParentSubmit = function(elem)
{
  $('.error_msg').fadeOut();
  $('.errors').slideUp();
    
  var box = $(elem).parents(".js-boite-parent");
  var params = {
    "slug" : box.find("input[name=slug]").val(), 
    "username" : box.find("input[name=username]").val(), 
    "email" : box.find("input[name=email]").val(), 
    "region" : box.find("select[name=region]").val(), 
    "password" : box.find("input[name=password]").val(), 
    "password-confirm" : box.find("input[name=password-confirm]").val(), 
    "allow_newsletter" : (box.find('input[name=newsletter]').is(':checked') ? 1 : 0),
    "meta_rapport" : (box.find('input[name=rapport]').is(':checked') ? 1 : 0)
  };
  
  heap.api.toutpetits.editparent(toutpetits.api.modificationParentSubmitCallback, params);
  return false;
};

toutpetits.api.modificationParentSubmitCallback = function(response)
{
  if(response.type == heap.SUCCESS) {
	  $('#modification_parent a.switch').eq(0).trigger('click');
	} else {
    var errors = response.data;
		  
    $.each(errors, function(err_key, err_value){ 
      if(err_key == 'children')
      {
        $.each(err_value, function(i, child){
          $.each(child, function(child_err_key, child_err_value){
            //console.log('child_err_value = '+child_err_value);
            $('#bloc_kid_container .bloc_kid').eq(i).find('.errors').show().find('.err_'+child_err_value).fadeIn();
          });
        });
      }
      else
      {
        $('#ierr_'+err_value).fadeIn();
      }      
    });
	}
  
  return false;
};

/**
 * 
 */
 
toutpetits.api.modificationEnfantSubmit = function(elem)
{
  $('.error_msg').fadeOut();
  $('.errors').slideUp();
   
  var box = $(elem).parents(".js-boite-enfant");
  $("input[name=target]").val(box.closest('.js-bloc_kid').index() - 1);
  
  if(! box.find("input[name=k_username]").val())
    return false;
    
  var params = {
    "slug" : box.find("input[name=k_slug]").val(), 
    "username" : box.find("input[name=k_username]").val(), 
    "email" : box.find("input[name=k_email]").val(), 
    "meta_dob" : box.find('select[name=k_dbo_y]').val() + '-' + box.find('select[name=k_dbo_m]').val() + '-' + box.find('select[name=k_dbo_d]').val(), 
    "meta_avatar" : box.find("input[name=document]").val(),
    "password" : $('input[name="password"]').val(),
    "password-confirm" : $('input[name="password-confirm"]').val()
  };
  heap.api.toutpetits.editchild(toutpetits.api.modificationEnfantSubmitCallback, params);
  return false;
};

toutpetits.api.modificationEnfantSubmitCallback = function(response)
{
  var target_index = $("input[name=target]").val();
  var target = $('.js-bloc_kid').eq(target_index);
    
  if(response.type == heap.SUCCESS) {	  
    target.find('.modification_kid').hide();
    target.find('.affichage_kid').find('span.username').text(target.find('.modification_kid').find("input[name=k_username]").val());
    var selected_avatar_index = target.find('.modification_kid').find("input[name=document_index]").val();
    //console.log('selected_avatar_index='+selected_avatar_index);
    target.find('.affichage_kid').find('.avatar .picto img').attr('src', target.find('.modification_kid').find(".avatar .picto span").eq(selected_avatar_index).find('img').attr('src'));
    target.find('.affichage_kid').find('.grow span').not('.username').hide();
    target.find('.affichage_kid').css({ backgroundColor: '#F8C900'}).show();
    target.find('.affichage_kid').animate({ backgroundColor: '#DDD4C9' }, 2000, function(){});
	} else {
    var errors = response.data;
			  
    $.each(errors, function(err_key, err_value){      
      target.find('.modification_kid .errors').show().find('.err_'+err_value).fadeIn();
    });
	}
  return false;
};

/**
 * 
 */
 
toutpetits.api.ajoutEnfantSubmit = function(elem)
{
  $('.error_msg').fadeOut();
  $('.errors').slideUp();
  
  var box = $(elem).parents(".js-boite-enfant");
  $("input[name=target]").val(box.closest('.js-bloc_kid').index() - 1);
  
  var params = {
    "slug" : box.find("input[name=k_slug]").val(), 
    "username" : box.find("input[name=k_username]").val(), 
    "email" : box.find("input[name=k_email]").val(), 
    "meta_dob" : box.find('select[name=k_dbo_y]').val() + '-' + box.find('select[name=k_dbo_m]').val() + '-' + box.find('select[name=k_dbo_d]').val(), 
    "meta_avatar" : box.find("input[name=document]").val()
  };
  heap.api.toutpetits.addchild(toutpetits.api.ajoutEnfantSubmitCallback, params);
  return false;
};

toutpetits.api.ajoutEnfantSubmitCallback = function(response)
{
  var target_index = $("input[name=target]").val();
  var target = $('.js-bloc_kid').eq(target_index);
  
  if(response.type == heap.SUCCESS) {    
    target.find('.modification_kid').hide();
    target.find('.affichage_kid').find('span.username').text(target.find('.modification_kid').find("input[name=k_username]").val());
    var selected_avatar_index = target.find('.modification_kid').find("input[name=document_index]").val();
    //console.log('selected_avatar_index='+selected_avatar_index);
    target.find('.affichage_kid').find('.avatar .picto img').attr('src', target.find('.modification_kid').find(".avatar .picto span").eq(selected_avatar_index).find('img').attr('src'));
    target.find('.affichage_kid').find('.grow span').not('.username').hide();
    target.find('.affichage_kid').css({ backgroundColor: '#F8C900'}).show();
    target.find('.affichage_kid').animate({ backgroundColor: '#DDD4C9' }, 2000, function(){}); 
    
    target.find(".modification_kid a.isave_kid").removeClass('js-unsaved');    
    target.find(".modification_kid a.icancel_kid").removeClass('js-unsaved');    
    target.find(".modification_kid a.idelete_kid").removeClass('js-unsaved');
    
	} else {
    var errors = response.data;
			  
    $.each(errors, function(err_key, err_value){ 
      target.find('.modification_kid .errors').show().find('.err_'+err_value).fadeIn();
    });
	}
  return false;
};

/**
 * 
 */
 
toutpetits.api.suppressionSubmit = function(elem)
{
  $('.error_msg').fadeOut();
  $('.errors').slideUp();
  
  var box = $(elem).parents(".js-boite-enfant");
  $("input[name=target]").val(box.closest('.js-bloc_kid').index() - 1);
  
  var params = {
    "slug" : box.find("input[name=k_slug]").val()
  };
  
  heap.api.toutpetits.erasechild(toutpetits.api.suppressionSubmitCallback, params);  
  return false;
};

toutpetits.api.suppressionSubmitCallback = function(response)
{
  var target_index = $("input[name=target]").val();
  var target = $('.js-bloc_kid').eq(target_index);
  
  if($(this).is('.js-unsaved'))
  {
    target.remove();
  }
  else
  {       
    if(response.type == heap.SUCCESS) {    
      target.find('.modification_kid').hide();
      target.find('.affichage_kid').css({ backgroundColor: '#F8C900'}).show();
      target.find('.affichage_kid').slideUp();
      target.remove();
    } else {
      var errors = response.data;
          
      $.each(errors, function(err_key, err_value){ 
        target.find('.modification_kid .errors').show().find('.err_'+err_value).fadeIn();
      });
    }
    
  }
  
	return false;
};

/**
 *
 * --- GameFlash object
 *
 */

toutpetits.GameFlash = function(options)
{
  this.swfbridge = (options) ? options : {}; 
  this.swfinstance = null;
  this.swfobj = null;
  this.swfready = false;
};

toutpetits.GameFlash.prototype.initFlash  = function()
{ 
  if (!this.swfbridge.initialized) {
    try{	
        this.swfobj.write(this.swfbridge.flash_container_id);
        this.swfbridge.initialized = true;		
    }
    catch(e){
        //alert("exception caught : "+e);
        return false;
    }
  }  
  if (!this.swfobj || !this.swfobj.tagName) {    
    try{
      var name = this.swfbridge.flash_id;
      var isIE = jQuery.browser.msie; //window[name] = document.forms[1].mosaic;console.log('name='+name);
      if(this.swfinstance = (isIE) ? window[name] : document[name]){
        return true;
      } else return false;
    }
    catch(e){
      alert("exception caught : "+e);
      return false;
    }
  } else return true;

};

toutpetits.GameFlash.prototype.initKidsList = function(children){
  
  if(!this.initFlash()){
    alert("game_flash.initKidsList() not defined"); return;
  }
  else
  {  
    if(!children || !children.length)
      return false;;
    
    var params = [];
    
    $.each(children, function(index, value){
      var child_bkg = heap.urlmap['static']+'/images/kids_avatars/garcon_carte.png';
      var child_avatar = heap.urlmap['static']+'/images/kids_avatars/garcon_tete.png';
      
      if(value._meta && value._meta.avatar)
      {
        child_bkg = heap.urlmap['static']+'/images/kids_avatars/'+value._meta.avatar+'_carte.png';
        child_avatar = heap.urlmap['static']+'/images/kids_avatars/'+value._meta.avatar+'_tete.png';
      }
      
      var child = {
        name: value._meta.pseudo,
        id: value.id,
        bg_path: child_bkg,
        avatar_path: child_avatar        
      };
      
      if(value.element_id) child.element_id = value.element_id;
      
      jQuery('<input type="hidden" />')
        .attr('name', 'childavatar_'+child.id) 
        .attr('value', ''+child.avatar_path) 
        .appendTo("body");
        
      params.push(child);        
    });
    
    this.swfinstance.flashapp_buildKidsMenu(params);
  }
  return false;

};

/**
 *
 * External Itf
 *
 */

Toutpetits_onKidClicked = function(user_id)
{  
  var user_avatar = $('input[name="childavatar_'+user_id+'"]').val();
  var element_id = $('input[name="kid_selector_game"]').val();
  var url = heap.urlmap.zephir+'?user_id='+user_id+'&user_avatar='+encodeURIComponent(user_avatar); 
  
  if(element_id)
    url += '&element_id='+element_id;
  
  tpUI.openZephir(url);
  //window.location.href = url;
};

Toutpetits_JouerClicked = function()
{
  var url = heap.urlmap.zephir;
  tpUI.openZephir(url);  
  //window.location.href = url;
};

/*
Toutpetits_killZephir = function()
{
  alert('Toutpetits_killZephir')  ;
  var url = heap.urlmap.zephir;
  tpUI.closeZephir();  
  //window.location.href = url;
};
*/

/**
 *
 * utils
 *
 */


function SerializeObject(obj, indentValue)
{
  var hexDigits = "0123456789ABCDEF";
  function ToHex(d)
  {
    return hexDigits[d >> 8] + hexDigits[d & 0x0F];
  }
  function Escape(string)
  {
    return string.replace(/[\x00-\x1F'\\]/g,
        function (x)
        {
          if (x == "'" || x == "\\") return "\\" + x;
          return "\\x" + ToHex(String.charCodeAt(x, 0));
        })
  }

  var indent;
  if (indentValue == null)
  {
    indentValue = "";
    indent = ""; // or " "
  }
  else
  {
    indent = "\n";
  }
  return GetObject(obj, indent).replace(/,$/, "");

  function GetObject(obj, indent)
  {
    if (typeof obj == 'string')
    {
      return "'" + Escape(obj) + "',";
    }
    if (obj instanceof Array)
    {
      result = indent + "[";
      for (var i = 0; i < obj.length; i++)
      {
        result += indent + indentValue +
            GetObject(obj[i], indent + indentValue);
      }
      result += indent + "],";
      return result;
    }
    var result = "";
    if (typeof obj == 'object')
    {
      result += indent + "{";
      for (var property in obj)
      {
        result += indent + indentValue + "'" +
            Escape(property) + "' : " +
            GetObject(obj[property], indent + indentValue);
      }
      result += indent + "},";
    }
    else
    {
      result += obj + ",";
    }
    return result.replace(/,(\n?\s*)([\]}])/g, "$1$2");
  }
}








