/*!
 * 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;
    //console.info(portOptions.moduleValues);
    var store = new Ext.data.Store({
              url: '/ajax/editor/json/getSimpleData.cfm',    
              baseParams:{
                start:0, 
                limit: maxItemsPerPage, 
                schoolID: currentSchool.id,
                gcdID: currentPage.gcdID,
                schoolAlias: currentSchool.Alias,
                currentModule: portOptions.storeParams.keyword,
                truncate_text: portOptions.moduleValues.truncate_text,
                strip_tags: portOptions.moduleValues.strip_tags
              },
              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', dateFormat:'Y-m-d'}
                ]
              })
            });
    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">',
                '<tpl if="this.opt.moduleValues.truncate_text">',
                  '<div class="postHolder_content"><span class="x-editable">{[Ext.util.Format.ellipsis(values.dataContent,this.opt.moduleValues.truncate_text)]}</span></div>',
                '</tpl>',
                '<tpl if="!this.opt.moduleValues.truncate_text">',
                  '<div class="postHolder_content"><span class="x-editable">{dataContent}</span></div>',
                '</tpl>',
              '</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")}',
                '</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">',
                '<tpl if="this.opt.moduleValues.truncate_text">',
                  '<div class="postHolder_content"><span class="x-editable">{[Ext.util.Format.ellipsis(values.dataContent,this.opt.moduleValues.truncate_text)]}</span></div>',
                '</tpl>',
                '<tpl if="!this.opt.moduleValues.truncate_text">',
                  '<div class="postHolder_content"><span class="x-editable">{dataContent}</span></div>',
                '</tpl>',              '</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")}',
                '</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">',
                '<tpl if="this.opt.moduleValues.truncate_text">',
                  '<div class="postHolder_content"><span class="x-editable">{[Ext.util.Format.ellipsis(values.dataContent,this.opt.moduleValues.truncate_text)]}</span></div>',
                '</tpl>',
                '<tpl if="!this.opt.moduleValues.truncate_text">',
                  '<div class="postHolder_content"><span class="x-editable">{dataContent}</span></div>',
                '</tpl>',              '</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")}',
                '</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>',
                  '<tpl if="this.opt.moduleValues.truncate_text">',
                    '<div class="postHolder_content"><span class="x-editable">{[Ext.util.Format.ellipsis(values.dataContent,this.opt.moduleValues.truncate_text)]}</span></div>',
                  '</tpl>',
                  '<tpl if="!this.opt.moduleValues.truncate_text">',
                    '<div class="postHolder_content"><span class="x-editable">{dataContent}</span></div>',
                  '</tpl>',                
                '</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")}',
                '</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">',
                '<tpl if="this.opt.moduleValues.truncate_text">',
                  '<div class="postHolder_content"><span class="x-editable">{[Ext.util.Format.ellipsis(values.dataContent,this.opt.moduleValues.truncate_text)]}</span></div>',
                '</tpl>',
                '<tpl if="!this.opt.moduleValues.truncate_text">',
                  '<div class="postHolder_content"><span class="x-editable">{dataContent}</span></div>',
                '</tpl>',              '</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")}',
                '</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">',
                '<tpl if="this.opt.moduleValues.truncate_text">',
                  '<div class="postHolder_content"><span class="x-editable">{[Ext.util.Format.ellipsis(values.dataContent,this.opt.moduleValues.truncate_text)]}</span></div>',
                '</tpl>',
                '<tpl if="!this.opt.moduleValues.truncate_text">',
                  '<div class="postHolder_content"><span class="x-editable">{dataContent}</span></div>',
                '</tpl>',              '</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")}',
                '</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">',
                '<tpl if="this.opt.moduleValues.truncate_text">',
                  '<div class="postHolder_content"><span class="x-editable">{[Ext.util.Format.ellipsis(values.dataContent,this.opt.moduleValues.truncate_text)]}</span></div>',
                '</tpl>',
                '<tpl if="!this.opt.moduleValues.truncate_text">',
                  '<div class="postHolder_content"><span class="x-editable">{dataContent}</span></div>',
                '</tpl>',              '</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")}',
                '</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());
    //console.info(portOptions);
    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: portOptions.moduleValues.strip_tags != undefined? false: true ,
				border: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: '',
                    emptyMsg: "No posts to display",
                    items:['-',toggleSummary]
                }),
        items: {
                  xtype: 'dataview',
                  ref: '../dataView',
                  store: store,
									border:false,
                  showPreview: true,
                  tpl: tpl2Use,
                  autoHeight:false,
                  autoWidth: true,
                  //multiSelect: true,
                  overClass:'x-view-over',
                  itemSelector:'div.postHolder_wrap',
                  emptyText: '<div class="paddedPortalDiv">No posts to display</div>',
                  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">{[Ext.util.Format.ellipsis(values.dataContent,this.opt.moduleValues.truncate_text)]}</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")}',
                '</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,
				border:false,
        tbar: new Ext.PagingToolbar({
                    pageSize: maxItemsPerPage,
                    hidden: !portOptions.moduleValues.widgetpagertop,
                    store: store,
                    displayInfo: true,
                    displayMsg: '',
                    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}',
                    displayMsg: '',
                    emptyMsg: "No posts to display",
                    items:['-',toggleSummary]
                }),
        items: {
                  xtype: 'dataview',
                  ref: '../dataView',
                  store: store,
									border:false,
                  showPreview: true,
                  tpl: tpl2Use,
                  autoHeight:false,
                  autoWidth: true,
                  //multiSelect: true,
                  overClass:'x-view-over',
                  itemSelector:'div.postHolder_wrap',
                  emptyText: 'No posts to display',
                  listeners: {
                    contextmenu: function(dv,i,n,e){
                      
                    }
                  }
                }
    });


}

Ext.extend(rotatorPortlet, Ext.Panel);


scopePortlet = function(portOptions){
    var xd = Ext.data;    
    var maxItemsPerPage = portOptions.moduleValues.max_items_per_page || 5;
    //console.info(portOptions.moduleValues);
    /*var store = new Ext.data.Store({
              url: '/ajax/editor/json/getSimpleData.cfm',        
              baseParams:{
                start:0, 
                limit: maxItemsPerPage, 
                schoolID: currentSchool.id,
                gcdID: currentPage.gcdID,
                schoolAlias: currentSchool.Alias,
                currentModule: portOptions.storeParams.keyword,
                truncate_text: portOptions.moduleValues.truncate_text,
                strip_tags: portOptions.moduleValues.strip_tags
              },
              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();*/

    //alert(portOptions.toSource());
		//tpl2Use = tplPlain;
	var todaysDate = new Date();
	var NavItemIndex = todaysDate.format('n') - 1;
	var monthArray = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'Novemeber', 'December'];
	var navHandler = function(direction){

		var NumItem = NavItemIndex + direction;
		NavItemIndex = NumItem;
		dataPanel.load({
				url: "/ajax/scope/json/getScopeData.cfm",
				params: {month:NavItemIndex+1, gcdid:currentPageGCDID }
			});

		if (NumItem == 0){
			this.backBtn.setDisabled(true);
			this.backBtn.setText('');
		} else{
			this.backBtn.setDisabled(false);
			this.backBtn.setText( '< ' + monthArray[NumItem - 1]);
		}
		if (NumItem == 11){
			this.nextBtn.setDisabled(true);
			this.nextBtn.setText('');
		} else{
			this.nextBtn.setDisabled(false);
			this.nextBtn.setText(monthArray[NumItem + 1] + ' >');
		}
	};
	
	var dataPanel = new Ext.Panel({
			autoScroll:true,
			autoHeight:true,
			baseCls:'scope_Innerpnl'
		});
  scopePortlet.superclass.constructor.call(this, {
			border:false,
			autoHeight:true,
			activeItem: NavItemIndex, // make sure the active item is set on the container config!
			defaults: {
					// applied to each contained panel
					border:false,
					autoScroll:true,
					padding:5,
					baseCls:'scope_Innerpnl'
			},
			// just an example of one possible navigation scheme, using buttons
			bbar: [
					{
							id: 'move-prev',
							text: (NavItemIndex - 1) == 0 ? '' : '< ' + monthArray[NavItemIndex - 1],
							ref:'../backBtn',
							handler: navHandler.createDelegate(this, [-1]),
							disabled: (NavItemIndex - 1) == 0 ? true : false
					},
					'->', // greedy spacer so that the buttons are aligned to each side
					{
							id: 'move-next',
							ref:'../nextBtn',
							text: (NavItemIndex + 1) == 11 ? '' :  monthArray[NavItemIndex + 1] + ' >',
							handler: navHandler.createDelegate(this, [1]),
							disabled: (NavItemIndex + 1) == 11 ? true : false
					}
			],
			// the panels (or "cards") within the layout
			items: [
			dataPanel
			],
			listeners: {  
				afterrender: function(cont){ 
					var startDate = new Date();
					dataPanel.load ({
						url: "/ajax/scope/json/getScopeData.cfm",
						params: {month: startDate.format('m'),  gcdid:currentPageGCDID }
					});
				}
			}
			
	});

	

}

Ext.extend(scopePortlet, Ext.Panel);

hmkPortlet = function(portOptions){
    var xd = Ext.data;    
    var maxItemsPerPage = portOptions.moduleValues.max_items_per_page || 5;
   
	var todaysDate = new Date();
	var NavItemIndex = todaysDate.format('n') - 1;
	var monthArray = new Ext.data.SimpleStore({  
			value:1,
			fields: ['id', 'month'],  
			data : [[1,'January'], [2,'February'], [3,'March'], [4,'April'], [5,'May'], [6,'June'], [7,'July'], [8,'August'], [9,'September'], [10,'October'], [11,'November'], [12,'December']]
		});
	var weekStore = new Ext.data.JsonStore({
    // store configs
    autoDestroy: true,
    url: '/ajax/hmk/getWeeks.cfm',
		baseParams:{monthID:1},
    storeId: 'myStore',
    // reader configs
    root: 'weeks',
    idProperty: 'id',  
    fields: ['id', 'week'],
		listeners: {  
			load:function(s,dr,o){
				weekList.setValue(1);
			}
		}
});
	var dataPanel = new Ext.Panel({
				autoScroll:true,
				autoHeight:true,
				baseCls:'scope_Innerpnl'
			});
	var weekList = new Ext.form.ComboBox({
			xtype     : 'combo',
			width     :150,
			store     : weekStore,
			name: 'WeekID',  
			displayField: 'week',
			valueField: 'id',
			forceSelection: true,
			mode: 'remote',
			triggerAction: 'all',
			editable:false,
			ref:'../weekbar',
			listeners: {  
				select: function(f,r,i){  
					dataPanel.load ({
						url: "/ajax/hmk/json/getHmkData.cfm",
						params: {month:monthList.getValue() , week: r.data.id, gcdid:currentPageGCDID }
					});
					
				}
			}
	});
	var monthList = new Ext.form.ComboBox({
				xtype     : 'combo',
				width     : 100,
				store     : monthArray,
				name: 'monthID',  
				fieldLabel: 'Month', 
				displayField: 'month',
				valueField: 'id',
				forceSelection: true,
				mode: 'local',
				triggerAction: 'all',
				editable:false,
				ref:'../monthbar',
				listeners: {  
					select: function(f,r,i){  
						weekStore.setBaseParam('monthID',r.data.id);
						weekStore.load();
						dataPanel.load ({
							url: "/ajax/hmk/json/getHmkData.cfm",
							params: {month: r.data.id, week: 1, gcdid:currentPageGCDID }
						});
						
					}
				}
		});
  hmkPortlet.superclass.constructor.call(this, {
			border:false,
			autoHeight:true,
			defaults: {
					// applied to each contained panel
					border:false,
					autoScroll:true,
					padding:5,
					baseCls:'scope_Innerpnl'
			},
			// just an example of one possible navigation scheme, using buttons
			tbar: [
					'->', // greedy spacer so that the buttons are aligned to each side
					'Month: ',
					monthList,
					weekList
			],
			items:[dataPanel],
			listeners: {  
				afterrender: function(cont){ 
					var startDate = new Date();
					dataPanel.load ({
						url: "/ajax/hmk/json/getHmkData.cfm",
						params: {month: startDate.format('n'), week: 1, gcdid:currentPageGCDID }
					});
					cont.monthbar.setValue(startDate.format('n'));
					weekStore.setBaseParam('monthID',startDate.format('n'));
					weekStore.load();
					cont.weekbar.setValue(1);
				}
			}

	});

	

}

Ext.extend(hmkPortlet, Ext.Panel);

