/*!
 * Ext JS Library 3.0+
 * Copyright(c) 2006-2009 Ext JS, LLC
 * licensing@extjs.com
 * http://www.extjs.com/license
 */



Ext.ux.Portal = Ext.extend(Ext.Panel, {
    layout : 'column',
    autoScroll : true,
    cls : 'x-portal',
    defaultType : 'portalcolumn',
    
    initComponent : function(){
        Ext.ux.Portal.superclass.initComponent.call(this);
        this.addEvents({
            validatedrop:true,
            beforedragover:true,
            dragover:true,
            beforedrop:true,
            drop:true
        });
    },


    initEvents : function(){
        Ext.ux.Portal.superclass.initEvents.call(this);
        this.dd = new Ext.ux.Portal.DropZone(this, this.dropConfig);
    },
    
    beforeDestroy : function() {
        if(this.dd){
            this.dd.unreg();
        }
        Ext.ux.Portal.superclass.beforeDestroy.call(this);
    }
});

Ext.reg('portal', Ext.ux.Portal);

Ext.ux.Portal.DropZone = function(portal, cfg){
    this.portal = portal;
    Ext.dd.ScrollManager.register(portal.body);
    Ext.ux.Portal.DropZone.superclass.constructor.call(this, portal.bwrap.dom, cfg);
    portal.body.ddScrollConfig = this.ddScrollConfig;
};

Ext.extend(Ext.ux.Portal.DropZone, Ext.dd.DropTarget, {
    ddScrollConfig : {
        vthresh: 50,
        hthresh: -1,
        animate: true,
        increment: 200
    },

    createEvent : function(dd, e, data, col, c, pos){
        return {
            portal: this.portal,
            panel: data.panel,
            columnIndex: col,
            column: c,
            position: pos,
            data: data,
            source: dd,
            rawEvent: e,
            status: this.dropAllowed
        };
    },

    notifyOver : function(dd, e, data){
        var xy = e.getXY(), portal = this.portal, px = dd.proxy;

        // case column widths
        if(!this.grid){
            this.grid = this.getGrid();
        }

        // handle case scroll where scrollbars appear during drag
        var cw = portal.body.dom.clientWidth;
        if(!this.lastCW){
            this.lastCW = cw;
        }else if(this.lastCW != cw){
            this.lastCW = cw;
            portal.doLayout();
            this.grid = this.getGrid();
        }

        // determine column
        var col = 0, xs = this.grid.columnX, cmatch = false;
        for(var len = xs.length; col < len; col++){
            if(xy[0] < (xs[col].x + xs[col].w)){
                cmatch = true;
                break;
            }
        }
        // no match, fix last index
        if(!cmatch){
            col--;
        }

        // find insert position
        var p, match = false, pos = 0,
            c = portal.items.itemAt(col),
            items = c.items.items, overSelf = false;

        for(var len = items.length; pos < len; pos++){
            p = items[pos];
            var h = p.el.getHeight();
            if(h === 0){
                overSelf = true;
            }
            else if((p.el.getY()+(h/2)) > xy[1]){
                match = true;
                break;
            }
        }

        pos = (match && p ? pos : c.items.getCount()) + (overSelf ? -1 : 0);
        var overEvent = this.createEvent(dd, e, data, col, c, pos);

        if(portal.fireEvent('validatedrop', overEvent) !== false &&
           portal.fireEvent('beforedragover', overEvent) !== false){

            // make sure proxy width is fluid
            px.getProxy().setWidth('auto');

            if(p){
                px.moveProxy(p.el.dom.parentNode, match ? p.el.dom : null);
            }else{
                px.moveProxy(c.el.dom, null);
            }

            this.lastPos = {c: c, col: col, p: overSelf || (match && p) ? pos : false};
            this.scrollPos = portal.body.getScroll();

            portal.fireEvent('dragover', overEvent);

            return overEvent.status;
        }else{
            return overEvent.status;
        }

    },

    notifyOut : function(){
        delete this.grid;
    },

    notifyDrop : function(dd, e, data){
        delete this.grid;
        if(!this.lastPos){
            return;
        }
        var c = this.lastPos.c, col = this.lastPos.col, pos = this.lastPos.p;

        var dropEvent = this.createEvent(dd, e, data, col, c,
            pos !== false ? pos : c.items.getCount());

        if(this.portal.fireEvent('validatedrop', dropEvent) !== false &&
           this.portal.fireEvent('beforedrop', dropEvent) !== false){

            dd.proxy.getProxy().remove();
            dd.panel.el.dom.parentNode.removeChild(dd.panel.el.dom);
            
            if(pos !== false){
                if(c == dd.panel.ownerCt && (c.items.items.indexOf(dd.panel) <= pos)){
                    pos++;
                }
                c.insert(pos, dd.panel);
            }else{
                c.add(dd.panel);
            }
            
            c.doLayout();

            this.portal.fireEvent('drop', dropEvent);

            // scroll position is lost on drop, fix it
            var st = this.scrollPos.top;
            if(st){
                var d = this.portal.body.dom;
                setTimeout(function(){
                    d.scrollTop = st;
                }, 10);
            }

        }
        delete this.lastPos;
    },

    // internal cache of body and column coords
    getGrid : function(){
        var box = this.portal.bwrap.getBox();
        box.columnX = [];
        this.portal.items.each(function(c){
             box.columnX.push({x: c.el.getX(), w: c.el.getWidth()});
        });
        return box;
    },

    // unregister the dropzone from ScrollManager
    unreg: function() {
        //Ext.dd.ScrollManager.unregister(this.portal.body);
        Ext.ux.Portal.DropZone.superclass.unreg.call(this);
    }
    
});

blogPortlet = function(portOptions){
    var xd = Ext.data;    
    var maxItemsPerPage = portOptions.moduleValues.max_items_per_page || 5;
    var store = new Ext.data.Store({
              url: '/ajax/editor/json/getSimpleData.cfm',        
              baseParams:{
                start:0, 
                limit: maxItemsPerPage, 
                schoolID: currentSchool.id,
                schoolAlias: currentSchool.Alias,
                currentModule: portOptions.storeParams.keyword
              },
              reader: new Ext.data.JsonReader({
                totalProperty: 'totalCount',
                root: 'data',
                idProperty: 'dataID',
                fields: [
                         {name: 'dataID', type: 'int'},
                         {name: 'thumbID', type: 'int'},
                         {name: 'dataTitle'},
                         {name: 'dataContent'},
                         {name: 'authorID'},
                         {name: 'authorName'},
                         {name: 'authorLink'},
                         {name: 'commentCount'},
                         {name: 'permaLink'},
                         {name: 'permaLinkSchool'},
                         {name: 'schoolName'},
                         {name: 'date_posted', type: 'date', dateFormat: 'Y-m-d H:i:s.u'}
                ]
              })
            });
    store.load();

    
    var tplNoThumb = new Ext.XTemplate(
  		'<tpl for=".">',
  		  '<div class="postHolder">',
          '<div class="postHolder_wrap">',
            '<div class="postHolder_container">',
	            '<div class="postHolder_title"><a href="{permaLink}" class="postTitle postTitleLink"><span class="x-editable" style="font-weight: bold">{dataTitle}</span></a></div>',
	            '<tpl if="!this.opt.moduleValues.hide_author">',
	              '<div class="postHolder_author"><span class="x-editable postAuthor">',
	                'By: <a href="{authorLink}" onclick="return true" ',
	                ' ext:qtitle="{authorName}" ext:qwidth="112" ',
	                ' ext:qtip="&lt;img src=&quot;/images/getProfilePicture.cfm?id={authorID}&useLogo=true&quot; width=&quot;100&quot; /&gt;">{authorName}</a>',
                '</span></div>',
              '</tpl>',
	            '<tpl if="this.myView.showPreview">',
	              '<div class="postHolder_content"><span class="x-editable">{dataContent}</span></div>',
              '</tpl>',
	            '<tpl if="!this.myView.showPreview">',
	              '<div class="postHolder_content"><span class="x-editable">{[Ext.util.Format.ellipsis(Ext.util.Format.stripTags(values.dataContent), 200)]}</span></div>',
              '</tpl>',
	            '<tpl if="!this.opt.moduleValues.hide_posted_date">',
	              '<div class="postHolder_date">',
	                'Posted {date_posted:date("l, M jS, Y @ g:i:s A")}',
                '</div>',
              '</tpl>',
              '<tpl if="!this.opt.moduleValues.hide_footer_links">',
                '<div class="postHolder_options">',
                  '<span><a href="{permaLink}">Permalink</a></span>',
                  '<tpl if="this.opt.moduleValues.show_comments">',
                    ' &bull; ',
                    '<span>{commentCount} Comments</span>',
                  '</tpl>',
                  '<tpl if="permaLinkSchool != permaLink">',
                    ' &bull; ',
                    '<span><a href="{permaLinkSchool}" target="_blank">Syndicated from {schoolName}</a></span>',
                  '</tpl>',
                '</div>',
              '</tpl>',
	            '<div class="x-clear"></div>',
	          '</div>',
	          '<div class="x-clear"></div>',
	        '</div>',
	      '</div>',
      '</tpl>',
      '<div class="x-clear"></div>',{
        myView: this,
        opt: portOptions
      }
    );
    
    var tplPlain = new Ext.XTemplate(
  		'<tpl for=".">',
  		  '<div class="postHolder">',
          '<div class="postHolder_wrap">',
            '<tpl if="!this.opt.moduleValues.hide_list_thumbnails">',
              '<div class="postHolder_thumber">',
                '<img src="/images/thumber.cfm?id={thumbID}&w=90&h=80" />',
                '<div class="x-clear"></div>',
              '</div>',
            '</tpl>',
            '<div class="postHolder_container <tpl if="!this.opt.moduleValues.hide_list_thumbnails">postHolder_container_thumb</tpl>">',
	            '<div class="postHolder_title"><a href="{permaLink}" class="postTitle postTitleLink"><span class="x-editable" style="font-weight: bold">{dataTitle}</span></a></div>',
	            '<tpl if="!this.opt.moduleValues.hide_author">',
	              '<div class="postHolder_author"><span class="x-editable postAuthor">',
	                'By: <a href="{authorLink}" onclick="return true" ',
	                ' ext:qtitle="{authorName}" ext:qwidth="112" ',
	                ' ext:qtip="&lt;img src=&quot;/images/getProfilePicture.cfm?id={authorID}&useLogo=true&quot; width=&quot;100&quot; /&gt;">{authorName}</a>',
                '</span></div>',
              '</tpl>',
	            '<tpl if="this.myView.showPreview">',
	              '<div class="postHolder_content"><span class="x-editable">{dataContent}</span></div>',
              '</tpl>',
	            '<tpl if="!this.myView.showPreview">',
	              '<div class="postHolder_content"><span class="x-editable">{[Ext.util.Format.ellipsis(Ext.util.Format.stripTags(values.dataContent), 200)]}</span></div>',
              '</tpl>',
	            '<tpl if="!this.opt.moduleValues.hide_posted_date">',
	              '<div class="postHolder_date">',
	                'Posted {date_posted:date("l, M jS, Y @ g:i:s A")}',
                '</div>',
              '</tpl>',
              '<tpl if="!this.opt.moduleValues.hide_footer_links">',
                '<div class="postHolder_options">',
                  '<span><a href="{permaLink}">Permalink</a></span>',
                  '<tpl if="this.opt.moduleValues.show_comments">',
                    ' &bull; ',
                    '<span>{commentCount} Comments</span>',
                  '</tpl>',
                  '<tpl if="permaLinkSchool != permaLink">',
                    ' &bull; ',
                    '<span><a href="{permaLinkSchool}" target="_blank">Syndicated from {schoolName}</a></span>',
                  '</tpl>',
                '</div>',
              '</tpl>',
	            '<div class="x-clear"></div>',
	          '</div>',
	          '<div class="x-clear"></div>',
	        '</div>',
	      '</div>',
      '</tpl>',
      '<div class="x-clear"></div>',{
        myView: this,
        opt: portOptions
      }
    );
    
    var tplUnderTitle = new Ext.XTemplate(
  		'<tpl for=".">',
  		  '<div class="postHolder">',
          '<div class="postHolder_wrap">',
            '<div class="postHolder_title"><a href="{permaLink}" class="postTitle postTitleLink"><span class="x-editable" style="font-weight: bold">{dataTitle}</span></a></div>',
            '<div class="x-clear"></div>',
            '<tpl if="!this.opt.moduleValues.hide_list_thumbnails">',
              '<div class="postHolder_thumber">',
                '<img src="/images/thumber.cfm?id={thumbID}&w=90&h=80" />',
                '<div class="x-clear"></div>',
              '</div>',
            '</tpl>',
            '<div class="postHolder_container <tpl if="!this.opt.moduleValues.hide_list_thumbnails">postHolder_container_thumb</tpl>">',
	            '<tpl if="!this.opt.moduleValues.hide_author">',
	              '<div class="postHolder_author"><span class="x-editable postAuthor">',
	                'By: <a href="{authorLink}" onclick="return true" ',
	                ' ext:qtitle="{authorName}" ext:qwidth="112" ',
	                ' ext:qtip="&lt;img src=&quot;/images/getProfilePicture.cfm?id={authorID}&useLogo=true&quot; width=&quot;100&quot; /&gt;">{authorName}</a>',
                '</span></div>',
              '</tpl>',
	            '<tpl if="this.myView.showPreview">',
	              '<div class="postHolder_content"><span class="x-editable">{dataContent}</span></div>',
              '</tpl>',
	            '<tpl if="!this.myView.showPreview">',
	              '<div class="postHolder_content"><span class="x-editable">{[Ext.util.Format.ellipsis(Ext.util.Format.stripTags(values.dataContent), 200)]}</span></div>',
              '</tpl>',
	            '<tpl if="!this.opt.moduleValues.hide_posted_date">',
	              '<div class="postHolder_date">',
	                'Posted {date_posted:date("l, M jS, Y @ g:i:s A")}',
                '</div>',
              '</tpl>',
              '<tpl if="!this.opt.moduleValues.hide_footer_links">',
                '<div class="postHolder_options">',
                  '<span><a href="{permaLink}">Permalink</a></span>',
                  '<tpl if="this.opt.moduleValues.show_comments">',
                    ' &bull; ',
                    '<span>{commentCount} Comments</span>',
                  '</tpl>',
                  '<tpl if="permaLinkSchool != permaLink">',
                    ' &bull; ',
                    '<span><a href="{permaLinkSchool}" target="_blank">Syndicated from {schoolName}</a></span>',
                  '</tpl>',
                '</div>',
              '</tpl>',
	            '<div class="x-clear"></div>',
	          '</div>',
	          '<div class="x-clear"></div>',
	        '</div>',
	      '</div>',
      '</tpl>',
      '<div class="x-clear"></div>',{
        myView: this,
        opt: portOptions
      }
    );
    
    var tplWrapText = new Ext.XTemplate(
  		'<tpl for=".">',
  		  '<div class="postHolder">',
          '<div class="postHolder_wrap">',
            '<div class="postHolder_container">',
              '<div class="postHolder_title"><a href="{permaLink}" class="postTitle postTitleLink"><span class="x-editable" style="font-weight: bold">{dataTitle}</span></a></div>',
	            '<tpl if="!this.opt.moduleValues.hide_author">',
	              '<div class="postHolder_author"><span class="x-editable postAuthor">',
	                'By: <a href="{authorLink}" onclick="return true" ',
	                ' ext:qtitle="{authorName}" ext:qwidth="112" ',
	                ' ext:qtip="&lt;img src=&quot;/images/getProfilePicture.cfm?id={authorID}&useLogo=true&quot; width=&quot;100&quot; /&gt;">{authorName}</a>',
                '</span></div>',
              '</tpl>',
	            '<tpl if="this.myView.showPreview">',
	              '<div class="postHolder_content">',
                  '<tpl if="!this.opt.moduleValues.hide_list_thumbnails">',
                    '<img src="/images/thumber.cfm?id={thumbID}&w=90&h=80" class="contentFloater" />',
                  '</tpl>',
                  '<span class="x-editable">{dataContent}</span>',
                '</div>',
              '</tpl>',
	            '<tpl if="!this.myView.showPreview">',

	              '<div class="postHolder_content">',
                  '<tpl if="!this.opt.moduleValues.hide_list_thumbnails">',
                    '<img src="/images/thumber.cfm?id={thumbID}&w=90&h=80" class="contentFloater"/>',
                  '</tpl>',
                  '<span class="x-editable">{[Ext.util.Format.ellipsis(Ext.util.Format.stripTags(values.dataContent), 200)]}</span>',
                '</div>',
              '</tpl>',
	            '<tpl if="!this.opt.moduleValues.hide_posted_date">',
	              '<div class="postHolder_date">',
	                'Posted {date_posted:date("l, M jS, Y @ g:i:s A")}',
                '</div>',
              '</tpl>',
              '<tpl if="!this.opt.moduleValues.hide_footer_links">',
                '<div class="postHolder_options">',
                  '<span><a href="{permaLink}">Permalink</a></span>',
                  '<tpl if="this.opt.moduleValues.show_comments">',
                    ' &bull; ',
                    '<span>{commentCount} Comments</span>',
                  '</tpl>',
                  '<tpl if="permaLinkSchool != permaLink">',
                    ' &bull; ',
                    '<span><a href="{permaLinkSchool}" target="_blank">Syndicated from {schoolName}</a></span>',
                  '</tpl>',
                '</div>',
              '</tpl>',
	            '<div class="x-clear"></div>',
	          '</div>',
	          '<div class="x-clear"></div>',
	        '</div>',
	      '</div>',
      '</tpl>',
      '<div class="x-clear"></div>',{
        myView: this,
        opt: portOptions
      }
    );
    
    var tplHeader = new Ext.XTemplate(
  		'<tpl for=".">',
  		  '<div class="postHolder">',
          '<div class="postHolder_wrap">',
            '<div class="postHolder_header">',
              '<cfif canUserAdminister() eq true>',
                '<a href="##" class="dimensionLink" title="Get Current Image Dimensions" onclick="showSizes(\'{this.imageID}\')"><span>Dimensions</span></a>',
              '</cfif>',
              '<img id="{this.imageID}" src="/images/getPhoto.cfm?id={dataID}" alt="{dataTitle}" width="{this.opt.moduleValues.header_width}" height="{this.opt.moduleValues.header_height}"/>',
            '</div>',
            '<div class="postHolder_title"><a href="{permaLink}" class="postTitle postTitleLink"><span class="x-editable" style="font-weight: bold">{dataTitle}</span></a></div>',
            '<div class="x-clear"></div>',
            '<div class="postHolder_container">',
	            '<tpl if="!this.opt.moduleValues.hide_author">',
	              '<div class="postHolder_author"><span class="x-editable postAuthor">',
	                'By: <a href="{authorLink}" onclick="return true" ',
	                ' ext:qtitle="{authorName}" ext:qwidth="112" ',
	                ' ext:qtip="&lt;img src=&quot;/images/getProfilePicture.cfm?id={authorID}&useLogo=true&quot; width=&quot;100&quot; /&gt;">{authorName}</a>',
                '</span></div>',
              '</tpl>',
	            '<tpl if="this.myView.showPreview">',
	              '<div class="postHolder_content"><span class="x-editable">{dataContent}</span></div>',
              '</tpl>',
	            '<tpl if="!this.myView.showPreview">',
	              '<div class="postHolder_content"><span class="x-editable">{[Ext.util.Format.ellipsis(Ext.util.Format.stripTags(values.dataContent), 200)]}</span></div>',
              '</tpl>',
	            '<tpl if="!this.opt.moduleValues.hide_posted_date">',
	              '<div class="postHolder_date">',
	                'Posted {date_posted:date("l, M jS, Y @ g:i:s A")}',
                '</div>',
              '</tpl>',
              '<tpl if="!this.opt.moduleValues.hide_footer_links">',
                '<div class="postHolder_options">',
                  '<span><a href="{permaLink}">Permalink</a></span>',
                  '<tpl if="this.opt.moduleValues.show_comments">',
                    ' &bull; ',
                    '<span>{commentCount} Comments</span>',
                  '</tpl>',
                  '<tpl if="permaLinkSchool != permaLink">',
                    ' &bull; ',
                    '<span><a href="{permaLinkSchool}" target="_blank">Syndicated from {schoolName}</a></span>',
                  '</tpl>',
                '</div>',
              '</tpl>',
	            '<div class="x-clear"></div>',
	          '</div>',
	          '<div class="x-clear"></div>',
	        '</div>',
	      '</div>',
      '</tpl>',
      '<div class="x-clear"></div>',{
        myView: this,
        opt: portOptions,
        imageID: Ext.id()
      }
    );
    
    var tplHeaderBelow = new Ext.XTemplate(
  		'<tpl for=".">',
  		  '<div class="postHolder">',
          '<div class="postHolder_wrap">',
            '<div class="postHolder_title"><a href="{permaLink}" class="postTitle postTitleLink"><span class="x-editable" style="font-weight: bold">{dataTitle}</span></a></div>',
            '<div class="postHolder_header">',
              '<cfif canUserAdminister() eq true>',
                '<a href="##" class="dimensionLink" title="Get Current Image Dimensions" onclick="showSizes(\'{this.imageID}\')"><span>Dimensions</span></a>',
              '</cfif>',
              '<img id="{this.imageID}" src="/images/getPhoto.cfm?id={dataID}" alt="{dataTitle}" width="{this.opt.moduleValues.header_width}" height="{this.opt.moduleValues.header_height}"/>',
            '</div>',
            '<div class="x-clear"></div>',
            '<div class="postHolder_container">',
	            '<tpl if="!this.opt.moduleValues.hide_author">',
	              '<div class="postHolder_author"><span class="x-editable postAuthor">',
	                'By: <a href="{authorLink}" onclick="return true" ',
	                ' ext:qtitle="{authorName}" ext:qwidth="112" ',
	                ' ext:qtip="&lt;img src=&quot;/images/getProfilePicture.cfm?id={authorID}&useLogo=true&quot; width=&quot;100&quot; /&gt;">{authorName}</a>',
                '</span></div>',
              '</tpl>',
	            '<tpl if="this.myView.showPreview">',
	              '<div class="postHolder_content"><span class="x-editable">{dataContent}</span></div>',
              '</tpl>',
	            '<tpl if="!this.myView.showPreview">',
	              '<div class="postHolder_content"><span class="x-editable">{[Ext.util.Format.ellipsis(Ext.util.Format.stripTags(values.dataContent), 200)]}</span></div>',
              '</tpl>',
	            '<tpl if="!this.opt.moduleValues.hide_posted_date">',
	              '<div class="postHolder_date">',
	                'Posted {date_posted:date("l, M jS, Y @ g:i:s A")}',
                '</div>',
              '</tpl>',
              '<tpl if="!this.opt.moduleValues.hide_footer_links">',
                '<div class="postHolder_options">',
                  '<span><a href="{permaLink}">Permalink</a></span>',
                  '<tpl if="this.opt.moduleValues.show_comments">',
                    ' &bull; ',
                    '<span>{commentCount} Comments</span>',
                  '</tpl>',
                  '<tpl if="permaLinkSchool != permaLink">',
                    ' &bull; ',
                    '<span><a href="{permaLinkSchool}" target="_blank">Syndicated from {schoolName}</a></span>',
                  '</tpl>',
                '</div>',
              '</tpl>',
	            '<div class="x-clear"></div>',
	          '</div>',
	          '<div class="x-clear"></div>',
	        '</div>',
	      '</div>',
      '</tpl>',
      '<div class="x-clear"></div>',{
        myView: this,
        opt: portOptions,
        imageID: Ext.id()
      }
    );

    Ext.ux.ToggleAction = Ext.extend(Ext.Action, {
        toggle: function(state) {
            this.initialConfig.pressed = state;
            this.callEach('toggle', [state]);
        }
    });

    var toggleSummary = new Ext.ux.ToggleAction({
        text: 'Summary',
        pressed: false,
        enableToggle:true,
        scope:this,
        toggleHandler: function(btn, pressed){
            toggleSummary.toggle(pressed);
            this.showPreview = pressed;
            this.get(0).refresh();
        }
    });

    var tplNoHeader = new Ext.XTemplate(
  		'<tpl for=".">',
  		  '<div class="postHolder">',
          '<div class="postHolder_wrap">',
            '<div class="postHolder_header">',
              '<cfif canUserAdminister() eq true>',
                '<a href="##" class="dimensionLink" title="Get Current Image Dimensions" onclick="showSizes(\'{this.imageID}\')"><span>Dimensions</span></a>',
              '</cfif>',
              '<img id="{this.imageID}" src="/images/getPhoto.cfm?id={dataID}" alt="{dataTitle}" width="{this.opt.moduleValues.header_width}" height="{this.opt.moduleValues.header_height}"/>',
            '</div>',
            '<div class="x-clear"></div>',
            '<div class="postHolder_container">',
	            '<tpl if="!this.opt.moduleValues.hide_author">',
	              '<div class="postHolder_author"><span class="x-editable postAuthor">',
	                'By: <a href="{authorLink}" onclick="return true" ',
	                ' ext:qtitle="{authorName}" ext:qwidth="112" ',
	                ' ext:qtip="&lt;img src=&quot;/images/getProfilePicture.cfm?id={authorID}&useLogo=true&quot; width=&quot;100&quot; /&gt;">{authorName}</a>',
                '</span></div>',
              '</tpl>',
	            '<tpl if="this.myView.showPreview">',
	              '<div class="postHolder_content"><span class="x-editable">{dataContent}</span></div>',
              '</tpl>',
	            '<tpl if="!this.myView.showPreview">',
	              '<div class="postHolder_content"><span class="x-editable">{[Ext.util.Format.ellipsis(Ext.util.Format.stripTags(values.dataContent), 200)]}</span></div>',
              '</tpl>',
	            '<tpl if="!this.opt.moduleValues.hide_posted_date">',
	              '<div class="postHolder_date">',
	                'Posted {date_posted:date("l, M jS, Y @ g:i:s A")}',
                '</div>',
              '</tpl>',
              '<tpl if="!this.opt.moduleValues.hide_footer_links">',
                '<div class="postHolder_options">',
                  '<span><a href="{permaLink}">Permalink</a></span>',
                  '<tpl if="this.opt.moduleValues.show_comments">',
                    ' &bull; ',
                    '<span>{commentCount} Comments</span>',
                  '</tpl>',
                  '<tpl if="permaLinkSchool != permaLink">',
                    ' &bull; ',
                    '<span><a href="{permaLinkSchool}" target="_blank">Syndicated from {schoolName}</a></span>',
                  '</tpl>',
                '</div>',
              '</tpl>',
	            '<div class="x-clear"></div>',
	          '</div>',
	          '<div class="x-clear"></div>',
	        '</div>',
	      '</div>',
      '</tpl>',
      '<div class="x-clear"></div>',{
        myView: this,
        opt: portOptions,
        imageID: Ext.id()
      }
    );

    //alert(portOptions.toSource());
    
    switch(portOptions.moduleValues.layout){

      case "header":
        tpl2Use = tplHeader;
        break;
      case "subheader":
        tpl2Use = tplHeaderBelow;
        break;
      case "noheader":
        tpl2Use = tplNoHeader;
        break;
        
      case "nothumb":
        tpl2Use = tplNoThumb;
        break;

      case "wrap":
        tpl2Use = tplWrapText;
        break;
        
      case "undertitle":
        tpl2Use = tplUnderTitle;
        break;
        
      case "default":
      default:
        tpl2Use = tplPlain;
        break;
    }

    blogPortlet.superclass.constructor.call(this, {
        showPreview: false,
        tbar: new Ext.PagingToolbar({
                    pageSize: maxItemsPerPage,
                    hidden: !portOptions.moduleValues.widgetpagertop,
                    store: store,
                    displayInfo: true,
                    displayMsg: 'Displaying posts {0} - {1} of {2}',
                    emptyMsg: "No posts to display",
                    items:['-',toggleSummary]
                }),
        bbar: new Ext.PagingToolbar({
                    pageSize: maxItemsPerPage,
                    hidden: !portOptions.moduleValues.widgetpagerbottom,
                    store: store,
                    displayInfo: true,
                    displayMsg: 'Displaying posts {0} - {1} of {2}',
                    emptyMsg: "No posts to display",
                    items:['-',toggleSummary]
                }),
        items: {
                  xtype: 'dataview',
                  ref: '../dataView',
                  store: store,
                  showPreview: true,
                  tpl: tpl2Use,
                  autoHeight:false,
                  autoWidth: true,
                  //multiSelect: true,
                  overClass:'x-view-over',
                  itemSelector:'div.postHolder_wrap',
                  emptyText: 'No images to display',
                  listeners: {
                    contextmenu: function(dv,i,n,e){
                      
                    }
                  }
                }
    });


}

Ext.extend(blogPortlet, Ext.Panel);


rotatorPortlet = function(portOptions){
    var xd = Ext.data;    
    var maxItemsPerPage = portOptions.moduleValues.max_items_per_page || 5;
    var store = new Ext.data.Store({
              url: '/ajax/editor/json/getSimpleData.cfm',        
              baseParams:{
                start:0, 
                limit: maxItemsPerPage, 
                schoolID: currentSchool.id,
                schoolAlias: currentSchool.Alias,
                currentModule: portOptions.storeParams.keyword
              },
              reader: new Ext.data.JsonReader({
                totalProperty: 'totalCount',
                root: 'data',
                idProperty: 'dataID',
                fields: [
                         {name: 'dataID', type: 'int'},
                         {name: 'thumbID', type: 'int'},
                         {name: 'dataTitle'},
                         {name: 'dataContent'},
                         {name: 'authorID'},
                         {name: 'authorName'},
                         {name: 'authorLink'},
                         {name: 'commentCount'},
                         {name: 'permaLink'},
                         {name: 'permaLinkSchool'},
                         {name: 'schoolName'},
                         {name: 'date_posted', type: 'date', dateFormat: 'Y-m-d H:i:s.u'}
                ]
              })
            });
    store.load();
    
    var tplPlain = new Ext.XTemplate(
  		'<tpl for=".">',
  		  '<div class="postHolder">',
          '<div class="postHolder_wrap">',
            '<tpl if="!this.opt.moduleValues.hide_list_thumbnails">',
              '<div class="postHolder_thumber">',
                '<img src="/images/thumber.cfm?id={thumbID}&w=90&h=80" />',
                '<div class="x-clear"></div>',
              '</div>',
            '</tpl>',
            '<div class="postHolder_container <tpl if="!this.opt.moduleValues.hide_list_thumbnails">postHolder_container_thumb</tpl>">',
	            '<div class="postHolder_title"><a href="{permaLink}" class="postTitle postTitleLink"><span class="x-editable" style="font-weight: bold">{dataTitle}</span></a></div>',
	            '<tpl if="!this.opt.moduleValues.hide_author">',
	              '<div class="postHolder_author"><span class="x-editable postAuthor">',
	                'By: <a href="{authorLink}" onclick="return true" ',
	                ' ext:qtitle="{authorName}" ext:qwidth="112" ',
	                ' ext:qtip="&lt;img src=&quot;/images/getProfilePicture.cfm?id={authorID}&useLogo=true&quot; width=&quot;100&quot; /&gt;">{authorName}</a>',
                '</span></div>',
              '</tpl>',
	            '<tpl if="this.myView.showPreview">',
	              '<div class="postHolder_content"><span class="x-editable">{dataContent}</span></div>',
              '</tpl>',
	            '<tpl if="!this.myView.showPreview">',
	              '<div class="postHolder_content"><span class="x-editable">{[Ext.util.Format.ellipsis(Ext.util.Format.stripTags(values.dataContent), 200)]}</span></div>',
              '</tpl>',
	            '<tpl if="!this.opt.moduleValues.hide_posted_date">',
	              '<div class="postHolder_date">',
	                'Posted {date_posted:date("l, M jS, Y @ g:i:s A")}',
                '</div>',
              '</tpl>',
              '<tpl if="!this.opt.moduleValues.hide_footer_links">',
                '<div class="postHolder_options">',
                  '<span><a href="{permaLink}">Permalink</a></span>',
                  '<tpl if="this.opt.moduleValues.show_comments">',
                    ' &bull; ',
                    '<span>{commentCount} Comments</span>',
                  '</tpl>',
                  '<tpl if="permaLinkSchool != permaLink">',
                    ' &bull; ',
                    '<span><a href="{permaLinkSchool}" target="_blank">Syndicated from {schoolName}</a></span>',
                  '</tpl>',
                '</div>',
              '</tpl>',
	            '<div class="x-clear"></div>',
	          '</div>',
	          '<div class="x-clear"></div>',
	        '</div>',
	      '</div>',
      '</tpl>',
      '<div class="x-clear"></div>',{
        myView: this,
        opt: portOptions
      }
    );

    Ext.ux.ToggleAction = Ext.extend(Ext.Action, {
        toggle: function(state) {
            this.initialConfig.pressed = state;
            this.callEach('toggle', [state]);
        }
    });

    var toggleSummary = new Ext.ux.ToggleAction({
        text: 'Summary',
        pressed: false,
        enableToggle:true,
        scope:this,
        toggleHandler: function(btn, pressed){
            toggleSummary.toggle(pressed);
            this.showPreview = pressed;
            this.get(0).refresh();
        }
    });
    
    switch(portOptions.moduleValues.layout){
      case "default":
      default:
        tpl2Use = tplPlain;
        break;
    }

    rotatorPortlet.superclass.constructor.call(this, {
        showPreview: false,
        tbar: new Ext.PagingToolbar({
                    pageSize: maxItemsPerPage,
                    hidden: !portOptions.moduleValues.widgetpagertop,
                    store: store,
                    displayInfo: true,
                    displayMsg: 'Displaying posts {0} - {1} of {2}',
                    emptyMsg: "No posts to display",
                    items:['-',toggleSummary]
                }),
        bbar: new Ext.PagingToolbar({
                    pageSize: maxItemsPerPage,
                    hidden: !portOptions.moduleValues.widgetpagerbottom,
                    store: store,
                    displayInfo: true,
                    displayMsg: 'Displaying posts {0} - {1} of {2}',
                    emptyMsg: "No posts to display",
                    items:['-',toggleSummary]
                }),
        items: {
                  xtype: 'dataview',
                  ref: '../dataView',
                  store: store,
                  showPreview: true,
                  tpl: tpl2Use,
                  autoHeight:false,
                  autoWidth: true,
                  //multiSelect: true,
                  overClass:'x-view-over',
                  itemSelector:'div.postHolder_wrap',
                  emptyText: 'No images to display',
                  listeners: {
                    contextmenu: function(dv,i,n,e){
                      
                    }
                  }
                }
    });


}

Ext.extend(rotatorPortlet, Ext.Panel);
