TT_Pages = Class.create();
TT_Pages.prototype = {
  	initialize: function(name, page_type) {
  	    this.name = name;
  	    this.page_type = page_type;
  	    this.url = ANTIKGUIDE_PATH + 'antik_pages';
        this.tt_bindFunctions();
  	},

    tt_bindFunctions: function () {
        this.tt_callback = this.tt_callback.bind(this);       
        this.tt_editText = this.tt_editText.bind(this);
        this.tt_addPage = this.tt_addPage.bind(this);
        this.tt_addChildPage = this.tt_addChildPage.bind(this);
        this.tt_addTemplate = this.tt_addTemplate.bind(this);
        this.tt_addPortlet = this.tt_addPortlet.bind(this);
        this.tt_editTextCancel = this.tt_editTextCancel.bind(this);
        this.tt_handleAddPage = this.tt_handleAddPage.bindAsEventListener(this);
        this.tt_handleAddChildPage = this.tt_handleAddChildPage.bindAsEventListener(this);
        this.tt_handleAddTemplate = this.tt_handleAddTemplate.bindAsEventListener(this);
        this.tt_handleAddPortlet = this.tt_handleAddPortlet.bindAsEventListener(this);        
        this.tt_handleEditInplace = this.tt_handleEditInplace.bindAsEventListener(this);
        this.tt_saveTemplateParagraph = this.tt_saveTemplateParagraph.bind(this);
        this.tt_editUebersicht=this.tt_editUebersicht.bind(this); 
        this.tt_editUebersichtcallback = this.tt_editUebersichtcallback.bind(this);
        this.tt_handleEditUebersicht=this.tt_handleEditUebersicht.bindAsEventListener(this);
        
        this.tt_fotoers = this.tt_fotoers.bind(this);
        this.tt_EditUebersichtBrowserTitle=this.tt_EditUebersichtBrowserTitle.bind(this); 
        this.tt_tt_EditUebersichtBrowserTitlecallback = this.tt_EditUebersichtBrowserTitlecallback.bind(this);
        this.tt_handleEditBrowserTitle=this.tt_handleEditBrowserTitle.bindAsEventListener(this);
    },

    tt_handleAddPage: function (type, current) {
    	this.current = current;
        var AddForm = new TT_PagesForm(type);
        TT_Event.observe(AddForm, 'ok', this.tt_addPage);
    },

    tt_addPage: function (page) {
        Waitting.tt_show();
        this.name = page['name'];
        page['current'] = this.current;
        new Ajax.Request( this.url + '/addPage', {
            parameters: {data: $H(page).toJSON()},
            onComplete: this.tt_callback
        });
    },

    tt_editPage: function (divContent) {
        if (divContent.childNodes[0]) {
            var divTitle1 = divContent.childNodes[0];
            var textContent = divTitle1.innerHTML;
        } if (divContent.childNodes[1]) {
            var divTitle2 = divContent.childNodes[1];
            textContent += TT_SYMBOL + divTitle2.innerHTML;
        } if (textContent==undefined) 
            var textContent = divContent.innerHTML;
        var curName = this.name;
        if (divContent.newURL) {
            var newName = divContent.newURL;
            newName = newName.gsub(' ', '_');
            this.name = newName;
            var msg = 'Your new page\'s url: ';
            msg += this.url + '/' + newName;
            new TT_Message( msg );
        } else newName = this.name;
        var page = {
            'name'  : curName,
            'title' : textContent,
            'new_name': newName
        };
        Waitting.tt_show();
        new Ajax.Request( this.url + '/editPage', {
            parameters: {data: $H(page).toJSON()},
            onComplete: this.tt_callback
        });
    },

    tt_handleAddChildPage: function (type, current) {
    	this.current = current;
        var AddForm = new TT_PagesForm(type);
        TT_Event.observe(AddForm, 'ok', this.tt_addChildPage);
    },
    
    tt_addChildPage: function (page) {
        Waitting.tt_show();
        page['parent_name'] = this.name;
        page['current'] = this.current;
        new Ajax.Request( this.url + '/addChildPage', {
            parameters: {data: $H(page).toJSON()},
            onComplete: this.tt_callback
        });
    },

    tt_handleAddTemplate: function () {
        var AddForm = new TT_TemplatesForm();
        TT_Event.observe(AddForm, 'ok', this.tt_addTemplate);
    },

	tt_handleEditUebersicht: function (pUeber) {    	
        var EditForm = new TT_UebersichtForm(pUeber);
        TT_Event.observe(EditForm, 'ok', this.tt_editUebersicht);
    },
    
    tt_editUebersicht: function(uebersichts){    	
    	Waitting.tt_show();        
        new Ajax.Request( this.url + '/editChildpage', {
            parameters: {data: $H(uebersichts).toJSON()},
            onComplete: this.tt_editUebersichtcallback//this.tt_callback
        });
    },
    
    tt_editUebersichtcallback: function (transport) {        
        try { $('content').update(transport.responseText);
        } catch (e) { alert('add haendler callback error' + e.toString()); }        
        
        var str='The description, image, and browser title have been saved!';
        new TT_Announce(str,
        	function(result){if(result){window.location = window.location.href; Waitting.tt_close();}});
    },
    
    
    tt_handleEditBrowserTitle: function (uebersich){  
        var EditForm = new TT_MainPageForm(uebersich);
// Chinh lai css cho form edit browser
    	$("dlgBody").removeClassName('thientan13_dlgBody');
		$("lbl_browsertitle").removeClassName('tt_dlgLeft');
		$("browsertitle").ancestors()[0].removeClassName('tt_dlgRight');
     	$("lbl_browsertitle").addClassName('tt_dlgLeft_tt13');
     	$("browsertitle").ancestors()[0].addClassName('tt_dlgRight_tt13');
		$("browsertitle").setStyle({'width':'90%'});
 //
        TT_Event.observe(EditForm, 'ok', this.tt_EditUebersichtBrowserTitle);
    },
     
    tt_EditUebersichtBrowserTitle: function(uebersichts){    	
    	Waitting.tt_show();
    	new Ajax.Request( this.url + '/savePage', {
            parameters: {data: $H(uebersichts).toJSON()},
            onComplete: this.tt_EditUebersichtBrowserTitlecallback
        });
    },
    
    tt_EditUebersichtBrowserTitlecallback: 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();*/}});
    },
    
    tt_addTemplate: function (template) {
        Waitting.tt_show();
        template['name'] = this.name;
        new Ajax.Request( this.url + '/addTemplate', {
            parameters: {data: $H(template).toJSON()},
            onComplete: this.tt_callback
        });
    },

    tt_handleDeleteTemplate: function (id) {
        new TT_Question('Are you sure to delete this template?', 
            function(result) {
                if(!result) return;
                var act = '/deleteTemplates/';
                this.tt_deleteTemplate(id);
            }.bind(this) )
    },

    tt_deleteTemplate: function (id) {
        var url = this.url+'/deleteTemplates/'+this.name;
        Waitting.tt_show();
        new Ajax.Request( url, {
            parameters: {data: $H({0:id}).toJSON()},
            onComplete: this.tt_callback
        });
    },

    tt_regEditInplace: function (element) {
        if (element.id=='' || element.id==undefined) return;
        Event.observe(element, 'click', this.tt_handleEditInplace);
    },
    
    tt_editElement: function (element) {
    	if (element.id=='' || element.id==undefined) return;
		//this.tt_handleEditE(element);		
		element.onclick = function() {
			alert("1234");
		};
    },
    
    tt_handleEditE: function (element) {
		var divContent = this.tt_takeID(element);
        var EditText = new TT_EditInplaceForm(divContent, this.page_type);
       TT_Event.observe(EditText, 'ok', this.tt_editText);
        TT_Event.observe(EditText, 'cancel', this.tt_editTextCancel);    	
    }, 
    
    tt_takeID: function (element) {
    	if(element.id!=''){
    		return element;
    	}
    	return this.tt_takeID(element.parentNode);
    },
    
    tt_mkDragdropable: function (divContent) {
        var DragDrop = new TT_GridDragDrop();
        DragDrop.tt_makeListContainer(divContent);
        var divPar = $('template_' + id).childNodes;
        for (var i=0; i<divPar.length; i++) {
            var parItem = divPar[i];
            parItem.style['width'] = parItem.getWidth() + 'px';
            DragDrop.tt_makeDraggable(parItem, parItem);
        }    
        DragDrop.tt_endDropCallBack = this.tt_saveTemplateParagraph;
    },

    tt_handleEditInplace: function (e) {     
        var divContent = e.currentTarget;
        if (divContent.isFinish) {divContent.isFinish=false; return; }
        if (divContent==undefined || divContent.id == '') 
            divContent = divContent.parentNode;
        Event.stopObserving(divContent, 'click', this.tt_handleEditInplace);
    	var index=divContent.innerHTML.indexOf(" [Click here to edit]");
		if(index>=0) divContent.innerHTML=divContent.innerHTML.substr(0,index);
        var EditText = new TT_EditInplaceForm(divContent, this.page_type);
        TT_Event.observe(EditText, 'ok', this.tt_editText);
        TT_Event.observe(EditText, 'cancel', this.tt_editTextCancel);
    },

    tt_editText: function (divContent) {
        var _id = divContent.id.split('_');
        var id = _id[1];
        var img_des = _id[2];
        var type = _id[0];
        if (_id[2] && _id[2]=='img') {
            this.tt_saveTemplateImgTitle(id, divContent);
        } else {
            switch (type) {
                case 'page'     : this.tt_editPage(divContent); break;
                case 'text'     : if(id!='ImageListItem')this.tt_saveTemplateImgTitle(id, divContent); 
                				  else this.tt_saveChildpageTextTitle(divContent);
                				  break;
    			case 'textarea' : if(id!='ImageListItem') this.tt_saveTemplateTextarea(id, divContent); 
                				  else this.tt_saveChildpageTextarea(divContent);
                				  break;			
                case 'paragraph': this.tt_saveTemplateParagraph(divContent); break;
                case 'img'      : if(id!='ImageListItem') this.tt_saveTemplateImg(id, divContent); 
                				  else this.tt_saveChildpageImg(divContent);
                				  break;
                case 'gallery'  : this.tt_saveTemplateGallery(id, divContent); break;
                default         : break;
            }
        }
        this.tt_editTextCancel(divContent);
    },

    tt_saveChildpageTextTitle: function (divContent){
		var page_id=divContent.ancestors()[0].id;
		var page = {}
        page['title'] = divContent.innerHTML;
		page['id']=page_id;
        Waitting.tt_show();

        new Ajax.Request( this.url + '/editChildpage', {
            parameters: {data: $H(page).toJSON()},
            onComplete: this.tt_saveTemplateCallback
        });
    },
    
	tt_saveChildpageTextarea: function (divContent) {
        var textContent = divContent.innerHTML;
        var page_id=divContent.ancestors()[0].id;
        this.tt_saveChildpageText(page_id,textContent);
    },

    tt_saveChildpageText: function (page_id, textContent) {
		var page = {}
        page['description'] = textContent;
		page['id']=page_id;
        Waitting.tt_show();
        new Ajax.Request( this.url + '/editChildpage', {
            parameters: {data: $H(page).toJSON()},
            onComplete: this.tt_saveTemplateCallback
        });
    },

	tt_saveChildpageImg: function (divContent){
        var img = divContent.childNodes[0];
        var imgSrc = img.src;
		var imgName = img.name;
        var arr = imgSrc.split('/uploads');
        var imgURL = 'uploads' + arr[1];      
        this.tt_saveChildpage(imgName,imgURL);
    },

   	tt_saveChildpage: function (imgName,value) {
        var page = {}
        page['image'] = value;
		page['id']=imgName;
        Waitting.tt_show();
        new Ajax.Request( this.url + '/editChildpage', {
            parameters: {data: $H(page).toJSON()},
            onComplete: this.tt_saveTemplateCallback
        });
    },
	
    tt_editTextCancel: function (divContent) {
        divContent.isFinish = true;
        Event.observe(divContent, 'click', this.tt_handleEditInplace);
    },

    tt_saveTemplateImg: function (id, divContent) {
        var img = divContent.childNodes[0];
        var imgName = img.name;
        var imgSrc = img.src;
        var arr = imgSrc.split('/uploads');
        var imgURL = 'uploads' + arr[1];
        this.tt_saveTemplate(id, imgName, imgURL);
    },
    
    tt_saveTemplateImgTitle: function (id, divContent) {
        var textContent = divContent.innerHTML;
        var att = divContent.getAttribute('name');
        this.tt_saveTemplate(id, att, textContent);
    },

    tt_saveTemplateGallery: function (id, divContent) {
        this.tt_saveTemplate(id, 'gallery', divContent.galleryString);
    },

    tt_saveTemplateParagraph: function (divContent) {
        var id = divContent.id;
        id = id.split('_')[1];
        var textContent = '';
        var divPar = divContent.parentNode.childNodes;
        for (var i=0; i<divPar.length-1; i++)
            textContent += divPar[i].innerHTML + TT_SYMBOL;
        textContent += divPar[divPar.length-1].innerHTML;
        this.tt_saveTemplateText(id, textContent);
    },

    tt_saveTemplateTextarea: function (id, divContent) {
        var textContent = divContent.innerHTML;
        textContent = textContent.gsub('<br>', TT_SYMBOL);
        this.tt_saveTemplateText(id, textContent);
    },

    tt_saveTemplateText: function (id, textContent) {
        this.tt_saveTemplate(id, 'content', textContent);
    },

    tt_saveTemplate: function (id, att, value) {
        var template = {}
        template['id'] = id;
        template[att] = value;
        template['name'] = this.name;
        Waitting.tt_show();
        new Ajax.Request( this.url + '/editTemplate', {
            parameters: {data: $H(template).toJSON()},
            onComplete: this.tt_saveTemplateCallback
        });
    },

    tt_handleAddPortlet: function (AllPortlets) {
        var AddForm = new TT_PortletForm(AllPortlets);
        TT_Event.observe(AddForm, 'ok', this.tt_addPortlet);
    },

    tt_addPortlet: function (portletId) {
        var portlet = {
            'name': this.name,
            'portlet_id': portletId
        };
        Waitting.tt_show();
        new Ajax.Request( this.url + '/addPortlet', {
            parameters: {data: $H(portlet).toJSON()},
            onComplete: this.tt_callback
        });
    },

    tt_handleDeletePortlet: function (pId) {
        new TT_Question('Are you sure to delete this portlets?', 
            function(result) {
                if(!result) return;
                var act = '/deletePortlets/';
                this.tt_deletePortlet(pId);
            }.bind(this) )
    },

    tt_deletePortlet: function (pId) {
        var url = this.url+'/deletePortlets/'+this.name;
        Waitting.tt_show();
        new Ajax.Request( url, {
            parameters: {data: $H({0:pId}).toJSON()},
            onComplete: this.tt_callback
        });
    },

    tt_handleZoomImg: function (imgSrc) {
        new TT_ImageViewer(imgSrc);
    },

    tt_callback: function (transport) {
        try { $('content').update(transport.responseText);
        } catch (e) { alert('add haendler callback error' + e.toString()); }
        Waitting.tt_close();
     },
    
    tt_saveTemplateCallback: function () {
        Waitting.tt_close();
    },
    
    tt_handleArchive: function (id) {
        Waitting.tt_show();
        var page = {'name': this.name};
        new Ajax.Request( this.url + '/archive', {
            parameters: {data: $H(page).toJSON()},
            onComplete: function (transport) {
                Waitting.tt_close();
            }.bind(this)
        });
    },
    
	tt_delete:function (page_name,subfix,org_chks){
		var temp=0;var chks = {};
    	var url = this.url + subfix + page_name;		

    	if(!(org_chks.length>1))
    		chks[0] = org_chks['id'];

		for(i=0;i<org_chks.length;i++){
			if(org_chks[i].checked==true)
				chks[temp++] = org_chks[i]['id'];
		}

    	Waitting.tt_show();
        new Ajax.Request( url, {
            parameters: {data: $H(chks).toJSON()},
            onComplete: this.tt_callback
        });
    },
    
    tt_deletePage:function (current){	    
	    var k=$('parent_menu').value;
		var temp=0; var chks = {'pId':k};
		
		var org_chks=document['PagesAdminForm']['page'];
    	var url = this.url + '/deletePages/'+current;		
    	if(!(org_chks.length>1))
    		chks[0] = org_chks['id'];

		for(i=0;i<org_chks.length;i++){
			if(org_chks[i].checked==true)
				chks[temp++] = org_chks[i]['id'];
		}
		
    	Waitting.tt_show();
        new Ajax.Request( url, {
            parameters: {data: $H(chks).toJSON()},
            onComplete: this.tt_callback
        });
    },
    
    tt_NotdeletePage:function (current){
    	var org_chks=document['PagesAdminForm']['page'];
    		for(i=0;i<org_chks.length;i++)
				org_chks[i].checked=false;
    },
 
    tt_fotoers: function (page) {
        Waitting.tt_show();       
		console.log($H(page).toJSON());
        new Ajax.Request( this.url + '/fotoers', {
            parameters: {data: $H(page).toJSON()},
            onComplete: this.tt_callback
        });
    },

    
    tt_fotoersform:function(){		
//		var temp=new TT_FotoForm();
//		TT_Event.observe(temp, 'ok', this.tt_fotoers);
		Waitting.tt_show();
		var temp=new TT_Finder({
            showField: false,
            foto:true,                        
        });        
    }
}