var previewwin;
Ext.onReady(function(){
	var storesearch = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({url: "/content/Ajax/search/"}),
	    reader: new Ext.data.DjangoJsonReader({
	        root: 'ignitables',
	        totalProperty: 'total'
	    }, [
	       {name: 'title'},
           {name: 'rating'},
           {name: 'sharing'},
           {name: 'author'},
           {name: 'authorid'},
           {name: 'total_views'},
           {name: 'igdata'},
           {name: 'date_created'},
           {name: 'dir_size'},
           {name: 'encoded_id'}
	    ])
	});

	// Custom search template
    var resultTpl = new Ext.XTemplate(
        '<tpl for="."><div class="search-item" style="height: 65px;">',
            '<img src="/content/pipe/?igcid={encoded_id}&relPath=&fileName=tn_small.jpg" align="left" />',
            '<h3>{[fm.ellipsis(values.title, 32)]}</h3>',
            '<br/><span>by {author}</span>',
        '</div></tpl>'
    );

	function searchFn(){
        	window.location = String.format('/search/?q={0}', Ext.getCmp('searchbox').getRawValue());
    }

    var kmap = new Ext.KeyMap("searchtext", {
		key: 13,
		fn: searchFn,
		scope: searchbox
	});

	var searchbox = new Ext.form.ComboBox({
        store: storesearch,
        baseCls: 'x-plain',
        style: 'color: #ccc; background-image: url(/static/images/search_bkg.png); background-color: transparent; padding: 10px; font-size: 14px; border: 0px;',
        displayField:'name',
        typeAhead: false,
        loadingText: 'Searching...',
        width: 200,
        emptyText: 'Search',
        id: 'searchbox',
        hideTrigger:false,
        triggerClass: 'x-form-search-trigger2',
        tpl: resultTpl,
        onTriggerClick: searchFn,
        applyTo: 'searchtext',
        itemSelector: 'div.search-item',
		onSelect: function(record){ // override default onSelect to do redirect
            window.location = String.format('/p/{0}', record.data.encoded_id);
        }
    });
	previewwin = new Ext.Window({
        title: 'Preview',
        width: 500,
        height: 480,
        minHeight: 480,
        minWidth: 500,
        aspect: true,
        resizable: false,
        preserveRatio : true,
        id: 'previewwin',
        layout: 'fit',
        plain:true,
        closeAction: 'hide',
        modal: true,
        buttonAlign:'right',
     	contentEl: 'previewwin',
     	buttons:[
			{
				text     : 'Close',
	            handler  : function(){
	               previewwin.hide();
	            }
	        }
     	]
    });
});

function previewClick(encid){
	Ext.get('previewwin').update(String.format('<object width="480" height="415"><param name="movie" value="https://www.ignitecast.com/e/{0}/"></param><param name="bgcolor" value="#000000"></param><param name="allowFullScreen" value="true"></param><embed src="https://www.ignitecast.com/e/{0}/" type="application/x-shockwave-flash" allowFullScreen="true" bgcolor="#000000" width="480" height="415"></embed></object>', encid));
	previewwin.show();
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


if ((typeof Range !== "undefined") && !Range.prototype.createContextualFragment)
{
    Range.prototype.createContextualFragment = function(html)
    {
        var frag = document.createDocumentFragment(),
        div = document.createElement("div");
        frag.appendChild(div);
        div.outerHTML = html;
        return frag;
    };
}

