TT_MainPage = Class.create();
TT_MainPage.prototype = {
  	initialize: function(controler, page_id, template_id,browsertitle) {
  	    this.controler = controler;
  	    this.page_id = page_id;
  	    this.template_id = template_id;
  	    this.browsertitle=browsertitle;
  	    this.Items = new Array();
  	    this.url = ANTIKGUIDE_PATH + controler;
  	    this.tt_bindFunctions();
  	    this.tt_handleEditInplace = this.tt_handleEditInplace.bindAsEventListener(this);
  	},
    
    tt_bindFunctions: function () {      
        this.tt_editUebersicht=this.tt_editUebersicht.bind(this); 
        this.tt_editUebersichtcallback = this.tt_editUebersichtcallback.bind(this);
        this.tt_handleEditUebersicht=this.tt_handleEditUebersicht.bindAsEventListener(this);
    },
    
    tt_regEditInplace: function (element) {
        this.Items.push(element);
        Event.observe(element, 'click', this.tt_handleEditInplace);
    },

    tt_startAllObserve: function () {
        for (var i=0; i<this.Items.length; i++) {
            Event.observe(this.Items[i], 'click', this.tt_handleEditInplace);
        }
    },
    
    tt_stopAllObserve: function () {
        for (var i=0; i<this.Items.length; i++) {
            Event.stopObserving(this.Items[i], 'click', this.tt_handleEditInplace);
        }
    },
    
    tt_handleEditInplace: function (e) {
        var divContent = e.currentTarget;
        if (divContent.isFinish) {divContent.isFinish=false; return; }
        this.tt_stopAllObserve();
        Event.stopObserving(divContent, 'click', this.tt_handleEditInplace);
        var EditText = new TT_EditInplaceForm(divContent, this.page_type);
        TT_Event.observe(EditText, 'ok', this.tt_handleOk.bind(this));
        TT_Event.observe(EditText, 'cancel', this.tt_editTextCancel.bind(this));
    },
    
    tt_handleOk: function (divContent) {
            var _id = divContent.id.split('_');
            var id = _id[1], type = _id[0];
            if (type=='page') this.tt_savePageTitle(divContent);
            else this.tt_saveTemplate(divContent);
    },
    
    tt_savePageTitle: function (divContent) {
        Waitting.tt_show();
        var page = {
            'id': this.page_id,
            'title': divContent.innerHTML
        };
        new Ajax.Request( this.url+'/savePage', {
            parameters: {data: $H(page).toJSON()},
            onComplete: function () {Waitting.tt_close();}
        });
        this.tt_editTextCancel(divContent);    
    },
    
    tt_saveTemplate: function (divContent) {
        Waitting.tt_show();
        var template = {
            'id': this.template_id,
            'page_id': this.page_id,
            'content': $(this.controler+'_container').innerHTML
        };
        new Ajax.Request(this.url+'/save', {
            parameters: {data: $H(template).toJSON()},
            onComplete: function () {Waitting.tt_close();}
        });
        this.tt_editTextCancel(divContent);
    },
    
    tt_editTextCancel: function (divContent) {
        divContent.isFinish = true;
        this.tt_startAllObserve();
        Event.observe(divContent, 'click', this.tt_handleEditInplace);
    },
    
    tt_handleEditUebersicht: function (ttitle) {
    	var uebersich={
    		'title'			:ttitle,
    		'id'			:this.page_id,    		
    		'browsertitle'	:this.browsertitle
    	};
 
        var EditForm = new TT_MainPageForm(uebersich);
        TT_Event.observe(EditForm, 'ok', this.tt_editUebersicht);
    },
    
    tt_editUebersicht: function(uebersichts){    	
    	Waitting.tt_show();
        new Ajax.Request( this.url + '/savePage', {
            parameters: {data: $H(uebersichts).toJSON()},
            onComplete: this.tt_editUebersichtcallback
        });
    },
    
    tt_editUebersichtcallback: function (transport) {
        var str='The browser title have been saved!';
        new TT_Announce(str,
        	function(result){if(result){window.location = window.location.href; Waitting.tt_close();}});
     }
}
