/*
Javascript general para todas las páginas de PCS Equipos
Todas las páginas incluyen Prototype.js (http://prototype.conio.net) con utilidades generales para mejorar la OP de Javascript.
Documentación de la librería en http://www.sergiopereira.com/articles/prototype.js.html
@2006 Aardvark / i2b
--------------------------------------------------*/
/*
Namespace
Otras clases y funciones debieran definirse aquí
ej: EQUIPOS.UnaClase = function(){}
--------------------------------------------------*/
var PCS = PCS || {};
/*
Utils
--------------------------------------------------*/
PCS.Utils = {
	hideSelects:function(){
		if(window.attachEvent){
			$$("select").each(
				function(elem){
					Element.hide(elem);
				}
			);
		}
	},
	showSelects:function(){
		if(window.attachEvent){
			$$("select").each(
				function(elem){
					Element.show(elem);
				}
			);
		}
	},
	togglePanel:function(div,link){
		var link = $(link);
		var div = $(div);
		div.toggle();
		if(div.style.display == 'none'){
			link.addClassName("closed");
			link.title = "Ver";
		}
		else {
			link.removeClassName("closed");
			link.title = "Esconder";
		}
		link.blur();
		return false;
	}
};
/*
Buscador
--------------------------------------------------*/
PCS.SearchBox = {
	message:"Buscar Equipos",
	field_id:"search-field",
	form_id:"search-form",
	button_id:"search-submit",
	fieldset_id:"search-box",
	loader:"search-loader",
	search_ready:false,
	speed:0,
	open:false,
	scount:0,
	focus:function(e) {	
		var elem = Event.element(e);
		if($F(PCS.SearchBox.field_id) == PCS.SearchBox.message)
			elem.value = "";
		//alert(this.id);
		//Event.stopObserving(PCS.SearchBox.field_id,"focus",PCS.SearchBox.focus);
	},
	blur:function(){
		if($F(PCS.SearchBox.field_id) == "")
			$(PCS.SearchBox.field_id).value = PCS.SearchBox.message;
	},
	submit:function(){
		var valor = $F(this.field_id);
		if(valor==this.message || valor =="")return false;
		var url = $(this.form_id).action;
		var pars = "q="+valor;
		$(this.loader).show();
		$(this.button_id).hide();
		PCS.SearchBox.close();
		var ajax = new Ajax.Request(
			url,
			{
				method:"get",
				parameters:pars,
				onSuccess:this.searchSuccess,
				onFailure:this.searchFailure,
				onError:this.searchFailure
			}
		);
	},
	searchSuccess:function(req){
		$(PCS.SearchBox.loader).hide();
		$(PCS.SearchBox.button_id).show();
		PCS.SearchBox.open = true;
		if(!PCS.SearchBox.search_ready){
			var div = document.createElement("div");
			div.id = "rc";
			div.style.display = "none";
			if(!window.attachEvent){
				Element.makePositioned(PCS.SearchBox.fieldset_id);
				$(PCS.SearchBox.fieldset_id).appendChild(div);
			}
			else {
				document.body.appendChild(div);
				// Lo siguiente es de los docs de Prototype, para que funcione Event.stopObserving()
				Event.observe(window,"resize",PCS.SearchBox.closeFromResize);
			}
			PCS.SearchBox.search_ready = true;
		}
		if(window.attachEvent){
			var pos = Position.cumulativeOffset($(PCS.SearchBox.fieldset_id));
			var subs = PCS.SearchBox.scount<1?0:18;
			$("rc").style.left = (pos[0])+"px";
			$("rc").style.top = (pos[1]+25)+"px";
			PCS.Utils.hideSelects();
			PCS.SearchBox.scount++;
		}
		$("rc").innerHTML = req.responseText;
		Element.show(PCS.SearchBox.button_id);
		Element.hide("search-loader");
		Element.show($("rc"));
		//new Effect.Appear("rc",{duration:PCS.SearchBox.speed});
	},
	searchFailure:function(req){
		alert("error "+req.responseText+"sss");
	},
	closeFromResize:function(){//arreglo asqueroso para el asqueroso IE
		//alert(PCS.SearchBox.scount);
		if(window.attachEvent && PCS.SearchBox.scount <= 1){
			PCS.SearchBox.scount++;
			return false;
		}
		PCS.SearchBox.scount = 0;
		PCS.SearchBox.close();
	},
	close:function(){
		if(!$("rc"))return false;
		if(!$("rc").visible())return false;
		if(window.attachEvent){
			Event.observe(window,"resize",PCS.SearchBox.closeFromResize);
			Event.stopObserving(window, 'resize', PCS.SearchBox.closeFromResize);
		}
		PCS.Utils.showSelects();
		//new Effect.Fade("rc",{duration:PCS.SearchBox.speed});
		Element.hide($("rc"));
		PCS.SearchBox.open = false;
	},
	init:function(){
		$(PCS.SearchBox.field_id).value = PCS.SearchBox.message;
		Event.observe(PCS.SearchBox.field_id,"focus",PCS.SearchBox.focus);
		Event.observe(PCS.SearchBox.field_id,"blur",PCS.SearchBox.blur);
	}
};
Event.observe(window,"load",PCS.SearchBox.init);
/*
Ajax universal
--------------------------------*/
/* Se define un objeto con callbacks globales */
PCS.globalCallbacks = {
	/*onCreate: function(){
		$$(".show-onload").each(function(e){e.show()});
		$$(".hide-onload").each(function(e){e.hide()});
	},
	onComplete: function() {
		if(Ajax.activeRequestCount == 0){
			$$(".show-onload").each(function(e){e.hide()});
			$$(".hide-onload").each(function(e){e.show()});//;new Effect.Highlight(e)});
		}
	}*/
};
/* Se registran los callbacks en Ajax.Responders */
//Ajax.Responders.register( PCS.globalCallbacks );

/*
Agregada de www.vivabit.com/code/domready/domready.js
--------------------------------------------------------*/
Object.extend(Event, {
  _domReady : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;

    if (this._timer)  clearInterval(this._timer);
    
    this._readyCallbacks.each(function(f) { f() });
    this._readyCallbacks = null;
},
  onDOMReady : function(f) {
    if (!this._readyCallbacks) {
      var domReady = this._domReady.bind(this);
      
      if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", domReady, false);
      
        /*@cc_on @*/
        /*@if (@_win32)
            document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
            document.getElementById("__ie_onload").onreadystatechange = function() {
                if (this.readyState == "complete") domReady(); 
            };
        /*@end @*/

        
        if (/WebKit/i.test(navigator.userAgent)) { 
          this._timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState)) domReady(); 
          }, 10);
        }
        
        Event.observe(window, 'load', domReady);
        Event._readyCallbacks =  [];
    }
    Event._readyCallbacks.push(f);
  }
});
