/*******************************************************************************
 * Fonctions de l'application FO de Esitpa Conseil
 ******************************************************************************/

function addTenderFileField(button)
{
  var row = button.parentNode  ;
  var new_row = row.cloneNode(true) ;
  if (document.all) {
    new_row.getElementsByTagName('input')[0].value = "";
    row.parentNode.appendChild(new_row);
  }
  else {
    new_row.down().value = ""
    row.insert({after: new_row});
  }
}

function removeTenderFileField(button)
{
    if (button.parentNode.parentNode.getElementsByTagName('div').length > 1)
      button.parentNode.parentNode.removeChild(button.parentNode);
    else {
      if (document.all) {
        //under ie, you can not reset the input type file value
        //so radical method, add a new row and delete the old one
        addTenderFileField(button);
        removeTenderFileField(button);
      }
      else {
        button.parentNode.getElementsByTagName('input')[0].value = "";
      }
    }
}


/**
 * Gadget pour le loader
 */
function addFileSubmit(){
  var table_box = $('addFile_content').down('div') ;
  var loading_box = $('addFile_loading') ;

  //slide up du formulaire
  new Effect.SlideUp(table_box,{afterFinish:function(){
    Effect.SlideDown(loading_box) ;
    }}) ;
}




function initRoomsSelector() {

  if($$('.search_property_rooms').length>0)
  {
    $$('.search_property_rooms li').each(function(e){
      e.onclick = function(){switchRoomNumber(this) ;}
    })
  }
}

function initPropertyBoxes()
{
  var boxes = ($$('.property_box')) ;
  if(boxes.length>0)
  {
    boxes.each(function(e){
      e.onmouseover = function(){e.addClassName('hover');}
      e.onmouseout = function(){e.removeClassName('hover');}
    }) ;
  }
}

function switchRoomNumber(li)
{
  id_rooms_nb = /.*_(\d+)$/ ;
  id_rooms_nb.exec(li.id) ;
  var id_input_rooms = "input_rooms_" + RegExp.$1 ;
  if ($(id_input_rooms).checked)
  {
    $(id_input_rooms).checked = false ;
    $('rooms_nb_'+RegExp.$1).removeClassName('selected') ;
  }else
  {
    $(id_input_rooms).checked = true ;
    $('rooms_nb_'+RegExp.$1).addClassName('selected') ;
  }
}

/**
 * Autocomple Postal Code, Ville, et Commune
 */
function setSelectedPostalCode(input,li)
{
  var id_city = /.*_(\d+)/
  id_city.exec(li.id) ;
  $('id_city').value = RegExp.$1 ;
}

/**
 * Switch Main property picture (interface with flash scroller)
 */
function switchPropertyPicture(url_size_3,url_size_4,name)
{
  //updatable elements
  hs_link = $$('#property_picture a')[0] ;
  hs_img = $$('#property_picture img')[0] ;

  //link update
  hs_link.href = url_size_4 ;
  hs_link.onclick = function()
  {
    return hs.expand(this, {
      captionText: name
    }) ;
  }

  //image update
  hs_img.src = url_size_3 ;
  hs_img.alt = name ;
}

/******************************************************************************/
/* GMap                                                                       */
/******************************************************************************/
  function showMap(link,name,address,thumbnail)
  {
    var gmapsrc = '/inc/gmap.php?name=' + name + '&address=' + address + '&thumburi=' + thumbnail ;
    // var gmapsrc = 'http://spiritualmind.free.fr' ;
    return hs.htmlExpand(link, { objectType: 'iframe', src: gmapsrc, contentId: 'gmap_box'} );
  }


window.onload = function()
{
  initRoomsSelector() ;
  initPropertyBoxes() ;
}

/******************************************************************************/
/* Flash Pub                                                                  */
/******************************************************************************/
function removeFlash()
{
	$('flashCornerPeel').remove();
}

function displayPropertBoxes()
{
  $$('#prime_property_box .property_box').each(function(e){e.setStyle({'display':'block'})}) ;
}
