var ModelForm = Class.create({
    initialize: function(form,config) {
        
        this.form = form;
        this.config = config.evalJSON();

        // disables all the read_only fields and registers a callback to re-enable them when the submit button is clicked
        $A(this.config.read_only).each(function(s) {
            $('id_' + s).setAttribute('disabled','disabled');
        });
        
        Event.observe($(this.form), 'submit', this.remove_disabled.bindAsEventListener(this));
        
        this.config.config_fields.each(function(s) {
            Event.observe($('id_'+s), 'change', this.reload_form.bindAsEventListener(this));
        }.bind(this));

        // custom stuff for publication_form
        if(this.form == 'publication_form') {
            this.refresh_slug_row();
            var but = new Element("input", {type:'button',value:'Suggest'});
            $('id_slug').insert({after:but});
            Event.observe(but,'click',this.suggest_slug.bindAsEventListener(this));
            Event.observe('id_pubtype','change',this.refresh_slug_row.bindAsEventListener(this));
        }
        // end custom stuff for publication_form
    },

    suggest_slug:function() {
        // first gather all relevant fields required to generate slug
        params = {};
        var slugfields = $A(['id_pubtype','id_author1','id_conference','id_year','id_journal','id_slug','id_inpub']);
        $(slugfields).each(function(s) {
            if( $(s) ) {
                params[s] = $(s).getValue();
            } else {
                params[s] = null;
            }
        }.bind(this));
        params['id_obj'] = this.config.id;

        new Ajax.Request('/cms/get_publication_slug/', {
            method:'get',
            parameters: params,
			onComplete: function(transport) {
			},
            onSuccess: function(transport){
                var response = transport.responseText || "no response text";
                $('id_slug').value = response;
            },
            onFailure: function(){ 
                $('id_slug').value = "";
            }
          });            
    },

    refresh_slug_row: function() {
        // if id_slug is already filled in and the form-row has the class 'errors"
        if ($('id_pubtype').value != "manual") {
            $('id_slug').readOnly = true;
        } else {
            $('id_slug').readOnly = false;
        }
    },

    reload_form: function() {
         var query = ''
         this.config.config_fields.each(function(s) {
             query = query + '?' + s + '=' + $('id_'+s).value;
         });
         // see http://developer.mozilla.org/en/docs/DOM:window.location
         var href = window.location.protocol + '//' + window.location.host + window.location.pathname + query;
         window.location = href; // now reload the page
    },
    remove_disabled: function() {
         this.config.read_only.each(function(s) {
             $('id_' + s).removeAttribute('disabled');
         });
         return true;
    }
    
});

var cms = {
    init: function() {
        Event.observe(window,'load',function(){
		});
    },

	handle_admin: function() {
		 Event.observe(window,'load',function(){
		 });
	},

    add_flashvideo: function(id,width,height,path,thumb,link,shortdesc,longdesc) {
         var s = new SWFObject('/common/jwplayer/player.swf','player',width,height,'9');
         s.addParam('allowfullscreen','true');
         s.addParam('allowscriptaccess','always');
         flashvars= new Hash();
         flashvars.set('file',path);
         if(thumb) {
             flashvars.set('image',thumb);
         }
         s.addParam('flashvars',flashvars.toQueryString());
         s.write(id);
         return s;
    }
};


