var Rbm = {
}
Rbm.Form={
    remote:function (form,container){
        var optionsForRequest = {
                onSuccess:function(obj){
                    try{
                        
                        //var scripts = obj.responseText.extractScripts();
                        try{
                          /*scripts = "window._tmp= function(){"+scripts+"}";
                          eval(scripts);
                          var fn = window._tmp;
                          window._tmp = null;
                          fn();*/
                        }catch(e){
                            alert(e+" - Erro ao executar o script do retorno");
                        }
                        var cnt = container;
                        var html = obj.responseText;
                        if(!$(container)){
                            eval(' container = '+container);
                        }
                        $(container).update(html);
                        try{
                            var last = $(container).down(':last-child');
                            $(last).scrollTo();
                        }catch(e){}



                    }catch(e){
                        alert(e);
                    }
                },
                onFailure:function(obj){
                    alert('falhou');
                    //alert('Ocorreu um erro por favor informe o erro apresentado a seguir ao desenvolvedor');
                    document.write('<h1>Por favor informe este erro</h1> '.obj.responseText);
                },
                onException:function(obj,e){
                    alert('exception')
                    //alert('Ocorreu um erro por favor informe o erro apresentado a seguir ao desenvolvedor');
                    alert(e);
                    document.write('<h1>Por favor informe este erro</h1> '.obj.responseText);
                }

            }



        //checa se tem campos do tipo file no form
        var files = $(form).select('input[type=file]');
        if(files.length > 0){
            try{
            $(form).fileRequest(
                optionsForRequest
            );
            }catch(e){
                alert(e);
            }
        }else{
            $(form).request(
                optionsForRequest
            );
        }


              
    }
}
Rbm.Popup = {
    PopupObserver : function(url,width,height,callBack){
        this._popup = Rbm.Popup.open(url,width,height);
        this.callBack = callBack;
        
        this.onTimer = function(){
            if(this._popup.closed){
                clearInterval(this.timer);
                this.callBack();
            }
        }
        this.timer = setInterval(this.onTimer.bind(this),500);//escuta para ver se o popup foi fechado
    },
    open: function(url,width,height){
        var top = (screen.height - height) / 2;
	var left = (screen.width - width) / 2;
        var parameters =  "scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=" + width + ",height=" + height + ",top=" + top + ",left=" +  left;
        return window.open(url, '_blank', parameters);
    }
}

Rbm.Youtube = Class.create(
       {
        initialize:function(width,height){
            this.width= typeof width !='undefined'?width:300;
            this.height= typeof height !='undefined'?height:210;
        },
        showPlayer:function(url,id){
          url = this.parseUrl(url);
          if(!url)return;
          var container = document.getElementById(id);
          try{
            var urlatual = container.getElementsByTagName('embed').item(0);
            urlatual = urlatual.src;
            if(urlatual == url)return;
          }catch(e){

          }
          container.innerHTML = this._showPlayer(url);
        },
        _showPlayer:function(url){
          var ret= '<object width="'+this.width+'" height="'+this.height+'">'+
                    '<param name="movie" value='+url+'>'+
                    '</param><param name="allowFullScreen" value="true"></param>'+
                    '<param name="wmode" value="transparent" />'+
                    '<param name="allowscriptaccess" value="always"></param>'+
                    '<embed wmode="transparent" src="'+url+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+this.width+'" height="'+this.height+'">'+
                '</embed>'+
             '</object>  ';
         return ret;
        }
        ,
        parseUrl:function(url){
           var reg = /http:\/\/[^\"\']+/;
           var url = reg.exec(url);
           url = url?url[0]:'';
           url = url.replace('watch?v=', 'v/');
           url = url.split('&');
           url = url[0];
           url = url?url + "&hl=pt-BR&fs=1":null;

           return url;
        }

    }
);
Rbm.Flash = Class.create({
    height:'100%',
    width :'100%',
    transparent:true,
    url:'',
    initialize:function(url,width,height,transparent){
        this.url = url;
        this.height = height?height:this.height;
        this.width = width?width:this.width;
        this.transparent = transparent?transparent:this.transparent;

    },
    display:function(container){
        
        var transparent = this.transparent?'transparent':'normal';
        $(container).update(
            '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+this.width+'" height="'+this.height+'" id="teste" align="middle">'+
                '<param name="allowScriptAccess" value="sameDomain" />'+
                 '<param name="menu" value="false" />'+
                '<param name="movie" value="'+this.url+'" />'+
                '<param name="quality" value="best" />'+
                '<param name="bgcolor" value="#ffffff" />'+
                '<embed src="'+this.url+'" wmode="'+transparent+'" menu="false" '+
                        'quality="best" width="'+this.width+'" height="'+this.height+'" '+
                        'name="teste" align="middle" allowScriptAccess="sameDomain" '+
                        'type="application/x-shockwave-flash" '+
                        'pluginspage="http://www.macromedia.com/go/getflashplayer" />'+
                '<param name="wmode" value="'+transparent+'" />'+
            '</object>'
        );
    }


})

/**
 *  Para manipulações de select
 */
Rbm.Select = {
    /**
     * Utilizado para carregamento de select com ajax
     * @param  origem  id|objeto Select de origem
     * @param  destino  id|objeto Select de origem
     * @param  selected  string valor selecionado
     * @param  url 
     */
    requestOnchange:function(origem,destino,selected,url){        
        origem = $(origem);
        var params = {};
        params[origem.name] = $F(origem);
        if(!params[origem.name]){
             $(destino).update('<option value="">--Selecione--</option>');
             return;
        }
        var callback = function(destino,selected,ajax){                
                $(destino).update(ajax.responseText)
                $(destino).setValue(selected);
        }.bind(this,destino,selected);
        
        new Ajax.Request(url,
                {
                  parameters:params,
                  onSuccess: callback,
                  onException:function(obj,e){
                      alert(e);
                      var str = obj.responseText;
                      str = "<h1>Erro na requisição ajax</h1>"+str;
                      document.write(str);
                  },
                  onFailure:function(obj){
                      var str = obj.responseText;
                      str = "<h1>Erro na requisição ajax</h1>"+str;
                      document.write(str);
                  }
                }
        );
            
    }
}


var methods = {
         triggerEvent: function(element,eventName){
                    if (document.createEvent)
                    {
                        var evt = document.createEvent('HTMLEvents');
                        evt.initEvent(eventName, true, true);

                        return element.dispatchEvent(evt);
                    }

                    if (element.fireEvent){
                        return element.fireEvent('on' + eventName);
                    }
            }
        }
Element.addMethods(methods);

