var __ajax_loader = '<span id="ajax_loader"><img src="images/ajax-loader.gif" />cargando...</span>';

function reportError (e)
{
    alert ('Error');
}

function error_tranferencia (win, error)
{
  alert (error);
  win.history.back();
}


// Popup
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}



//Activacion de los submenus en IE

startList = function() {
if (document.all&&document.getElementById) {
navRoot = document.getElementById("nav");
for (i=0; i<navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" over";
}
node.onmouseout=function() {
this.className=this.className.replace(" over", "");
}
}
}
}
}

// Envia un formulario a través de Ajax
// Se supone que la ventana de Lightbox está abierta

function sendFormAjax (container, form, lbox, complete)
{
	var params = Form.serialize (form);
	var myAjax = new Ajax.Updater(
			container,
      form.action,
      {
				method: 'post', 
				postBody: params, 
				onLoading: function (){$('ajax_msg').style.display = 'block'}, 
				onComplete: function ()
											{ 
												lbox.deactivate ();
												if (complete)
													eval (complete);
											} 
			}
	);
}

// Envia un formulario a través de Ajax
// Se supone que la ventana de Lightbox está abierta

function sendFormAjaxDict (form, lbox, complete)
{
	var params = Form.serialize (form);
	var myAjax = new Ajax.Request(
      form.action,
      {
				method: 'post', 
				postBody: params, 
				onLoading: function (){$('ajax_msg').style.display = 'block'}, 
				onComplete: function ()
											{ 
												window.location.reload();
												lbox.deactivate ();
												if (complete)
													eval (complete);
												
											} 
			}
	);
}

function saveForm (form, redirect_url)
{
	form = $(form);
	var params = Form.serialize (form);
	var myAjax = new Ajax.Request(
      form.action,
      {
				method: 'post', 
				postBody: params,
				onLoading: function (){ lbSaving.activate () }, 
				onComplete: saveFormComplete.bindAsEventListener (this)
			}
	);
}

function saveFormComplete (e)
{
	eval (e.responseText);
	Event.observe ('load', window, saveFormDeactivateLightbox, false);
}

function saveFormDeactivateLightbox ()
{
	lbSaving.deactivate ();
}

function showWebText (e)
{
	var key = String.fromCharCode (e.which);
	if (key == 'T')
	{
		els = document.getElementsByClassName('bull_dict');
		for (i = 0 ; i < els.length ; i++)
			els [i].style.display = 'inline';
	}
}
function hideWebText (e)
{
	var key = String.fromCharCode (e.which);
	if (key == 'T')
	{
		els = document.getElementsByClassName('bull_dict');
		for (i = 0 ; i < els.length ; i++)
			els [i].style.display = 'none';
	}
}
function saveContent  (el, id_a)
{
	$(id_a).innerHTML = 'Espere, por favor';
	$(el).form.submit();
	return true;
}



// ----------------------------------------------------------------
// tablas
// __________________________

function mouseover_tabla (e)
{
  Element.addClassName (e, 'tabla_par_over');
}

function mouseout_tabla (e)
{
  
  Element.removeClassName (e, 'tabla_par_over');
}


// ----------------------------------------------------------------
// clase Tabs
// __________________________
// argumentos:		ids  -  array asociativo multiple con el id de la pestana y el id del div a ocultar o mostrar
// 							class_active  -  nombre de la clase de la pestana cuando esta activa
//							class_inactive  -  nombre de la clase de la pestana cuando esta inactiva
//							nombre_objeto  -   nombre de la variable que contiene el objeto que se va a crear
// ----------------------------------------------------------------

var Tabs = Class.create ();

Tabs.prototype = {
  
  initialize:  function (ids, class_active, class_inactive, start_on) {
            			this.ids = ids;
			            this.class_active = class_active;
			            this.class_inactive = class_inactive;
									this.start_on = !start_on ? 0 : start_on;
			            this.eventos();
									this.actua (false, this.start_on);
          		},
          
  eventos: function () {
            for (var i = 0 ; i < this.ids.length ; i++)
              Event.observe (this.ids [i][0], 'click', this.actua.bindAsEventListener (this, i), false);
          },
          
  actua:    function (e, current) {
            tab = $(this.ids [current][0]);
						box = $(this.ids [current][1]);
            tab.className = this.class_active; 
            Element.show (box); 
            for (var i = 0 ; i < this.ids.length ; i++)
              if (i != current)
							{
								$(this.ids[i][0]).className =  this.class_inactive;
              	Element.hide (this.ids[i][1]);
							}
          }
};



// ----------------------------------------------------------------
// clase ToggleImg
// __________________________
// argumentos:    ids  -  array asociativo con el id de la imagen que actua como boton y el id del div a ocultar o mostrar
//               img_on  -  direccion de la imagen que se muestra cuando esta el div abierto
//              img_off  -  direccion de la imagen que se muestra cuando esta el div cerrado
// ----------------------------------------------------------------


var ToggleImg = Class.create();

ToggleImg.prototype = {
  
  initialize:    function (ids, img_on, img_off) {
              this.ids = ids;
              var el_img = $(ids[0]);
              var el_div = $(ids[1]);
              el_img.div = ids[1];
              el_img.img_on = img_on;
              el_img.img_off = img_off;
              Event.observe (el_img, 'click', this.actua, true);
            },
            
  
  actua:      function (e) {
              var el_img = Event.element (e);
              var el_div = $(el_img.div);
              el_div.style.display = el_div.style.display == 'none' ? 'block' : 'none';
            
              el_img.src = el_div.style.display == 'block' ? el_img.img_on : el_img.img_off;
            }
};



// ----------------------------------------------------------------
// clase Borrar
// __________________________
// argumentos:    ids  -  array asociativo con el id de la etiqueta o div que actua como boton y el id del div a ocultar o mostrar
//               id_marcador  -  id del elemento input oculto donde se anotan los elementos que estan siendo borrados
//              valor  -  valor que se anota en el input oculto
// ----------------------------------------------------------------


var Borrar = Class.create();

Borrar.prototype = {
  
    initialize:    function (ids, id_marcador, valor) {
                var el_a = $(ids[0]);
                var el_div = $(ids[1]);
                el_a.div = ids[1];
                el_a.marcador = id_marcador;
                el_a.valor = valor;
                Event.observe (el_a, 'click', this.actua, true);
              },
              
    actua:      function (e) {
                var a = confirm ('¿Está seguro de quiere borrar?');
                var el_a = Event.element (e);
                if (a) 
                {
                  if (el_a.marcador != false)
                    $(el_a.marcador).value += el_a.valor + ',';  
                  Element.remove (el_a.div);
                }
              }
};


// ----------------------------------------------------------------
// clase EditaGuarda
// __________________________
// argumentos:    click_edita  -  el id del boton que edita el contenido
//               click_guarda  -  el id del boton que guarda los cambios
//              contenedor  -  el editor que se encuentra oculto
//              ids_comunicados  -  array asociativo multiple con el siguiente par (id_campo_input, id_campo_div), donde div es el campo donde se guardan los cambios
// ----------------------------------------------------------------

var EditaGuarda = Class.create();

EditaGuarda.prototype = {
  
  initialize:    function (click_edita, click_guarda, contenedor, ids_comunicados) {
              var el_click_edita = $(click_edita);
              var el_click_guarda = $(click_guarda);
              
              el_click_edita.contenedor = contenedor;
              el_click_edita.click_guarda = click_guarda;
              el_click_guarda.click_edita = click_edita;
              el_click_guarda.contenedor = contenedor;
              el_click_guarda.ids_comunicados = ids_comunicados;
              
              
              Event.observe (el_click_edita, 'click', this.abre, true);
              Event.observe (el_click_guarda, 'click', this.guarda, true);
                
              for (var a = 0 ; a < ids_comunicados.length ; a++)
              {
                var el_input = $(ids_comunicados[a][0]);
                el_input.click_edita = click_edita;
                el_input.contenedor = contenedor;
                el_input.ids_comunicados = ids_comunicados;
                Event.observe (el_input, 'keydown', this.guarda, true);
              }
                 
            },
  
  abre:        function (e) {
              var el_click_edita = Event.element (e);
              var el  = $(el_click_edita.contenedor);
              Element.show ($(el_click_edita.click_guarda));
              Element.show (el_click_edita.contenedor);
              Element.hide (el_click_edita);
            },
            

  guarda:      function (e) {
              if (e.type == 'keydown' && e.keyCode != 13) return;
              var el_click_guarda = Event.element (e);
              for (var a = 0 ; a < el_click_guarda.ids_comunicados.length ; a++)
                if (el_click_guarda.ids_comunicados[1] != false)
                  $(el_click_guarda.ids_comunicados[a][1]).innerHTML = $F(el_click_guarda.ids_comunicados[a][0]);
              
                
              Element.hide (el_click_guarda.contenedor);
              Element.hide (el_click_guarda);
              Element.show (el_click_guarda.click_edita);
            }
};

// ----------------------------------------------------------------
// clase ToggleName
// __________________________
// argumentos:    click_edita  -  el id del boton que edita el contenido
//               click_guarda  -  el id del boton que guarda los cambios
//              contenedor  -  el editor que se encuentra oculto
// ----------------------------------------------------------------

var ToggleName = Class.create();

ToggleName.prototype = {
  
  initialize:    function (click_edita, click_guarda, contenedor) {
              var el_click_edita = $(click_edita);
              var el_click_guarda = $(click_guarda);
              
              el_click_edita.contenedor = contenedor;
              el_click_guarda.click_edita = click_edita;
              el_click_guarda.contenedor = contenedor;
              el_click_edita.click_guarda = click_guarda;
              
              Event.observe (el_click_edita, 'click', this.abre, true);
              Event.observe (el_click_guarda, 'click', this.guarda, true);
            },
  
  abre:        function (e) {
              var el_click_edita = Event.element (e);
              var el  = $(el_click_edita.contenedor);
              Element.show (el_click_edita.contenedor);
              Element.show (el_click_edita.click_guarda);
              Element.hide (el_click_edita);
            },
            

  guarda:      function (e) {
              if (e.type == 'keydown' && e.keyCode != 13) return;
              var el_click_guarda = Event.element (e);
              Element.hide (el_click_guarda.contenedor);
              Element.hide (el_click_guarda);
              Element.show (el_click_guarda.click_edita);
            }
};


// ----------------------------------------------------------------
// clase ToggleRadio
// __________________________
// argumentos:    ids  -  array asociativo multiple con el siguiente par (id_input, id_div)
// ----------------------------------------------------------------

var ToggleRadio = Class.create();

ToggleRadio.prototype = {
  
  initialize:    function (ids) {
              for (var a = 0 ; a < ids.length ; a++)
              {
                var el_input = $(ids[a][0]);
                el_input.div = ids[a][1];
                el_input.ids = ids;
                Event.observe (el_input, 'change', this.actua, true);
              }
            },
            
  actua:      function (e) {
              var el_input = Event.element (e);
              var el_div = $(el_input.div);
              var ids = el_input.ids;
              for (var a = 0 ; a < ids.length ; a++)
              {
                $(ids[a][1]).style.display = 'none';
              }
              el_div.style.display = 'block';
            }
};  


// ----------------------------------------------------------------
// clase ToggleCheckBox
//      en un grupo de botones radio se adjudica un div para cada boton.
//      si esta activado se muestra el div, sino pues no.
// __________________________
// argumentos:    ids  -  array asociativo multiple con el siguiente par (id_input, id_div)
// ----------------------------------------------------------------

var ToggleCheckBox = Class.create();

ToggleCheckBox.prototype = {
  
  initialize:    function (ids) {
              var el_input = $(ids[0]);
              el_input.div = ids[1];
              Event.observe (el_input, 'click', this.actua, true);
            },
            
  actua:      function (e) {
              var el_input = Event.element (e);
              var el_div = $(el_input.div);
              if (el_input.checked == true)
                el_div.style.display = 'block';
              else
                el_div.style.display = 'none';
            }
};  
// ----------------------------------------------------------------
// clase ToggleCheckBoxNo
//      en un grupo de botones radio se adjudica un div para cada boton.
//      si esta activado se no muestra el div, sino se muestra.
// __________________________
// argumentos:    ids  -  array asociativo multiple con el siguiente par (id_input, id_div)
// ----------------------------------------------------------------

var ToggleCheckBoxNo = Class.create();

ToggleCheckBoxNo.prototype = {
  
  initialize:    function (ids) {
              var el_input = $(ids[0]);
              el_input.div = ids[1];
              Event.observe (el_input, 'click', this.actua, true);
            },
            
  actua:      function (e) {
              var el_input = Event.element (e);
              var el_div = $(el_input.div);
              if (el_input.checked == true)
                el_div.style.display = 'none';
              else
                el_div.style.display = 'block';
            }
};  



// ----------------------------------------------------------------
// clase DirectoDiv
// __________________________
// argumentos:    ids  -  array asociativo multiple con el siguiente par (id_input, id_div)
// ----------------------------------------------------------------

var DirectoDiv = Class.create();

DirectoDiv.prototype = {
  
  initialize:    function (input, div) {
              $(input).div = div;
              Event.observe ($(input), 'keyup', this.actua, true);
            },
            
  actua:      function (e) {
              var input = Event.element (e);
              var div = $(input.div);
              div.innerHTML = input.value;
            }
};  



// ----------------------------------------------------------------
// clase ToggleSelect
// __________________________
// argumentos:    id_select  -  id del elemento select
//              opciones  -  array asociativo con el siguiente par [varlor_option, id_div]
// ----------------------------------------------------------------

var ToggleSelect = Class.create();

ToggleSelect.prototype = {
  
  initialize:    function (id_select, opciones) {
              $(id_select).opciones = opciones;
              Event.observe ($(id_select), 'change', this.actua, true);
            },
            
  actua:      function (e) {
              var el_select = Event.element (e);
              var opciones = el_select.opciones;
              var seleccionada = $F(el_select);
              for (var a = 0 ; a < opciones.length ; a++)
              {
                if ($(opciones[a][1]) != false)
                {
                  if (opciones[a][0] == seleccionada)
                    $(opciones[a][1]).style.display = 'block';
                  else $(opciones[a][1]).style.display = 'none';
                }
              }
            }
};  



// ----------------------------------------------------------------
// clase EditarDiv
// __________________________
// argumentos:    
//              opciones  -  array asociativo con el siguiente par [id_option, id_div]
// ----------------------------------------------------------------


var EditarDiv = Class.create();

EditarDiv.prototype = {
  
    initialize:    function (id_div, id_input, clase_over, texto_defecto) {
                var div = $(id_div);
                var input = $(id_input);
                div.id_input = id_input;
                div.clase_over = clase_over;
                input.id_div = id_div;
                input.texto_defecto = texto_defecto;
                input.style.display = 'none';
                Event.observe (div, 'click', this.muestra_input, true);
                Event.observe (div, 'mouseover', this.anade_clase, true);
                Event.observe (div, 'mouseout', this.quita_clase, true);
                Event.observe (input, 'blur', this.muestra_div, true);
              },
            
  muestra_input:  function (e) {
              var div = Event.element (e);
              $ (div.id_input).style.display = 'inline';
              div.style.display = 'none';
            },
            
  muestra_div:    function (e) {
              var input = Event.element (e);
              $ (input.id_div).style.display = 'block';
              input.style.display = 'none';
              if ($F (input) != '')
                $ (input.id_div).innerHTML = $F (input);
              else
                $ (input.id_div).innerHTML = input.texto_defecto;
            },
            
  anade_clase:    function (e) {
              var div = Event.element (e);
              Element.addClassName (div, div.clase_over);
            },
            
  quita_clase:    function (e) {
              var div = Event.element (e);
              Element.removeClassName (div, div.clase_over);
            }
            
};  


var BorraCampos = Class.create ();

BorraCampos.prototype = {
  
  initialize:  function (ids, boton) {
            
            $(boton).ids = ids;
            Event.observe (boton, 'click', this.actua, true);
          },
          
  actua:    function (e) {
            var a = confirm (LANG_DELETE_BLOCK);
            if (a)
            {
              var boton = Event.element(e);
              var ids = boton.ids;
              for (a = 0 ; a < ids.length ; a++)
              {
                var nodo = $(ids[a])
                if (nodo.nodeName == 'IMG')
                  nodo.src = 'images/nofoto.gif';
                  
                if (nodo.nodeName == 'TEXTAREA' || nodo.nodeName == 'INPUT')
                  nodo.value = '';
              }
            }
          }
          
};




function delete_data (mensaje, url)
{
  var a = confirm (mensaje);
  if (a) window.location.href = url;
}

// ----------------------------------------------------------------
// Anadir a homepage project
// __________________________

function add_proyect_to_home (id_project, update_id)
{
	if (update_id == "projects_index") action = 'addtohome';
	else action = 'addtohome';
	new Ajax.Updater (update_id, '/admin/cards/' + action + '/' + id_project, 
								{	onLoading:function(request){ $('add_to_home_content').style.display = 'none'; $('add_to_home_content_ajax').style.display = 'block' }, 
									onComplete:function(request){ $('add_to_home_content_ajax').style.display = 'none' ; $('add_to_home_content_ajax_complete').style.display = 'block'}, 
									asynchronous:true, 
									evalScripts:true, 
									requestHeaders:['X-Update', update_id]
								}
							) 
}




// ----------------------------------------------------------------
// Muestra la lista de modelos de la marca
// __________________________

function selectModel (make_id, node_id)
{
	url = BASE_URL + '/makes_models/ajx_get_by_model/' + make_id;
	
	var myAjax = new Ajax.Updater(
				node_id,
				url, 
				{
					method: 'get', 
					onFailure: reportError
				});
}
function reportError (e)
{
	alert (e);
}


function add_action_to_section (data, div_id, variable)
{
	$(div_id).value = data;
	eval (variable + '.deactivate ()');
}
function add_action_to_block (data, div_id, variable)
{
	$(div_id).value = data;
	eval (variable + '.deactivate ()');
}
