SM = [];
Ext.onReady(function() {
	var SearchCont = Ext.query('.SearchCont');
	var SiteMapCont = Ext.query('.SiteMapCont');
	var PrintCont = Ext.query('.PrintCont');
	if (SearchCont.length>0) SM.Search(SearchCont);
	if (SiteMapCont.length>0) SM.SiteMap(SiteMapCont);
	if (PrintCont.length>0) SM.Print(PrintCont);
/*	
	if (SearchCont) alert("SearchCont")
	if (SiteMapCont) alert("SiteMapCont")
	if (PrintCont) alert("PrintCont")
*/
});

SM.OnPrint = function(Obj){
	if (!Obj.innerHTML) {
		alert("Siden kan ikke printes");
		return
	}
	var v = Obj.innerHTML;
	var t = site_navn +"_"+ SITE_ID
	ifrm = Ext.get("_printer_container");
	if (!ifrm){
		var ifrm = document.createElement("IFRAME");
		ifrm.width = 0
		ifrm.height = 0
		ifrm.id = "_printer_container"
		ifrm.frameborder = 0;
		Obj = Ext.getBody();
		
		Obj.appendChild(ifrm);
		ifrm = Ext.get(ifrm);
		ifrm.setStyle("border","none")
		var css = '<link	rel="stylesheet" href="' + share_css + 'print.css" type="text/css" media="print">';
	
	    ifrm.dom.contentWindow.document.write('<html><head><title>Side_'  + SITE_ID+"_" + site_navn + '</title>');
	    ifrm.dom.contentWindow.document.write(css);
	    ifrm.dom.contentWindow.document.write('</head><'+'body'+'>');
	    ifrm.dom.contentWindow.document.write(v);
	    ifrm.dom.contentWindow.document.write('<'+'/'+'body'+'><'+'/'+'html'+'>');
	    ifrm.dom.contentWindow.document.close();
	}
    ifrm.dom.contentWindow.print();
 //	Obj.dom.removeChild(Obj.dom.lastChild)

};
SM.onShowSearch = function(id){

	alert(id)	
};

SM.OnSearch = function(o){
	var v = o.getValue();
	if (Ext.isEmpty(v)) return false;
//	showObject(o)	
	s = Ext.get("_search_container");
	if (s)  win.close();
	var reader = new Ext.data.JsonReader({
			root : 'topics',
			totalProperty : 'totalCount',
			id : 'id'
		}, [{
				name : 'id'
			},{
				name : 'headline'
			}, {
				name : 'author'
			}, {
				name : 'date_created'
		}]
	);

	    var ds = new Ext.data.Store({
	        proxy: new Ext.data.HttpProxy({
	            url: server_addr + 'SHARE/script/search.php'
	        })
	        ,reader: reader
	        ,baseParams: {limit:20}
	    });
		ds.setDefaultSort('date_created', 'DESC');
	
	    // Custom rendering Template for the View
	    var resultTpl = new Ext.XTemplate(
	        '<tpl for=".">',
	        '<div onclick="SM.onShowSearch({id});" class="search-item">',
	            '<h3><span>{date_created}<br />af {author}</span>',
	            '{headline}',
	            '<p></p>',
	        '</div></tpl>'
	    );
	
	    var panel = new Ext.Panel({
	        title:''
	        ,autoHeight: true
	        ,autoScroll:true
	        ,border : false
	        ,items: new Ext.DataView({
	            tpl: resultTpl
	            ,store: ds
	            ,itemSelector: 'div.search-item'
	        })
	    });

        win = new Ext.Window({
            id: "_search_container"
	        ,title    : 'Søge vindue'
            ,closable : true
            ,width    : 600
            ,height   : 350
            ,constrain: true
            ,autoScroll: true
            ,collapsible: true
            ,border : false
            ,plain    : true
            ,items: [panel]
            ,tbar: [
	            'Søg: ', ' ',
	            new Ext.app.SearchField({
	                store: ds,
	                width:320,
	                value: v
	            })
	        ],bbar: new Ext.PagingToolbar({
	            store: ds,
	            pageSize: 20,
	            displayInfo: true,
	            displayMsg: 'Topics {0} - {1} of {2}',
	            emptyMsg: "No topics to display"
	        })
        });
        win.show();
    ds.load({params:{
    	start:0
    	,limit:20
    	,sort: 'date_created'
    	,dir: 'DESC'
    	,query: v
    }});
};

SM.Search = function(o){
	for (var n=0; n<o.length; n++){
//		var obj = Ext.get(o[n]);
		var searchTrigger = new Ext.form.TriggerField({
		    xtype: 'textfield'
	    	,name: 'searchTrigger'
	        ,hideLabel: true
	    	,triggerClass: "x-form-search-trigger"
	    	,width: 170
	    	,fieldLabel:''
	    	,enableKeyEvents: true
	    	,value:  ""
		});

		searchTrigger.onTriggerClick = function(){
			SM.OnSearch(searchTrigger);
		}
			
	
		searchTrigger.on("specialkey",SM.OnSearch);
		
//		searchTrigger.on("keyUp",SM.OnSearch);
		
	    var form = new Ext.form.FormPanel({
	        baseCls: 'x-plain'
	        ,autoWidth: true
	        ,defaultType: 'textfield'
	        ,items: [searchTrigger]
	        ,renderTo: o[n]
	    });
	}
};
	

SM.Search1 = function(o){
	for (var n=0; n<o.length; n++){
		var win;
		var obj = Ext.get(o[n]);
		obj.on(
			"mouseover"
			,function(){
		        if(!win){
		            win = new Ext.Window({
			            title    : 'Layout Window',
			            closable : true,
			            width    : 600,
			            height   : 350,
			            //border : false,
			            plain    : true
		            });
		        }
		        win.show(obj);
		        win.on("close",function(){
		        	win = null;
		        })
			}
			,this)
//		showObject(obj)
	}
};

SM.SiteMap = function(o){
	
	for (var n=0; n<o.length; n++){
		var win;
		var obj = Ext.get(o[n]);
		obj.on(
			"click"
			,function(){
        if(!win){
            win = new Ext.Window({
                applyTo     : 'bodyContainer',
                layout      : 'fit',
                width       : 500,
                height      : 300,
                closeAction :'hide',
                plain       : true,
                buttons: [{
                    text     : 'Submit',
                    disabled : true
                },{
                    text     : 'Close',
                    handler  : function(){
                        win.hide();
                    }
                }]
            });
        }
        win.show(obj);
			}
			,this)
//		showObject(obj)
	}
};

SM.Print = function(o){
	for (var n=0; n<o.length; n++){
		var obj = Ext.get(o[n]);
		obj.on(
			"click"
			,function(){
				var p = Ext.get("bodyContainer");
//				showObject(p);
				this.OnPrint(p.dom)
			}
			,this)
	}
};