﻿//<!--
/************
This file is part of Genwi JS Lib.
All Rights Reserved.

Desc: Reader. creates and controls list and expanded view.
Test SVN Merge
************/

function Reader()
{
    this.objName = 'Reader';
    this.bAjaxInProgress = false;
    this.aRows = [];
    this.aExpRows = [];
    this.aData = [];
    this.aDataIdx = [];
    this.aTopRowCollHTML = [];
    this.aTopRowExpHTML = [];
    this.aFullItemHTML = {};
    this.aItemsAreaDims = [];
    this.sExpRowId = this.sUserName = null;
    this.sItemId = this.sRateItemId = null;
    this.bExpRowDisplay = false;
    this.sUrl = this.jListUrl = this.sPopoutPlayerUrl = this.sPopoutTitle = null;
    this.iPageNum = 1;
    this.iFeedIdx = -1;
    this.iCurSelIdx = -1;
    this.totalPages = 100;
    this.bOnScrollPaging = true;
    this.bPageLoading = false;
    this.bPageLoaded = false;
    this.bShareItemClick = false;
    this.bFilterView = false;
    this.sFilterItem = null;
    this.sCurTimeLine = null;
    this.oMainLayer = this.oItemsArea = this.oPageTitle = this.oLoader = 
    this.oPageLoader = this.oPageLoadingText = this.oDialog = this.oCatText = 
    this.oDateLayer = this.oDateBox = null;
    this.sDateLyrHTML = null;
    this.sBaseUrl = _GL.getBaseUrl();
    this.sAjaxBaseUrl = null;
    this.aSSUrl = {'save':'addpage.aspx?addtopage=existing&itemId=','share':'share.aspx?itemId='};
    this.sAjaxUtilsUrl = this.sAjaxBaseUrl + 'AjaxServices/utils.asmx/';
    this.sAjaxMyStuffUrl = this.sAjaxBaseUrl + 'AjaxServices/MyStuff.asmx/';
    this.sUnsubscribeFeedUrl = this.sAjaxMyStuffUrl + 'AjaxUserSubscriptionsUnsubscribe?ChannelID=<#1#>';
    this.sPublicWireUrl = null;
    this.sWhiteStar = this.sBaseUrl + 'images/white_star.gif';
    this.sGoldStar = this.sBaseUrl + 'images/gold_star.gif';
    this.sCommentIcon = _GL.getPNGImg(this.sBaseUrl + 'images/icon_comment.png',24,24,null);
    this.sShareIcon = _GL.getPNGImg(this.sBaseUrl + 'images/icon_share.png',24,24,null);
    this.sSaveIcon = _GL.getPNGImg(this.sBaseUrl + 'images/icon_save.png',24,24,null);
    this.sEmailIcon = _GL.getPNGImg(this.sBaseUrl + 'images/icon_email.png',24,24,null);
    this.sCloseIcon = _GL.getPNGImg(this.sBaseUrl + 'images/icon_close.png',24,24,null);
    this.sSpacer = this.sBaseUrl + 'images/s.gif';
    this.sPostAjaxHTML = null;
    this.bFBConnect = false;
    this.bNaviDisabled = false;
    this.aFBAction = {'video':'watched','blog':'read','news':'read','podcast':'heared'};
    
    this.initReaderState = function()
    {
        with(this)
        {
            aRows = [];
            sExpRows = [];
            aData = [];
            aDataIdx = [];
            aTopRowCollHTML = [];
            aTopRowExpHTML = [];
            sExpRowId = null;
            bExpRowDisplay = false;;
            iPageNum = 1;
            iFeedIdx = -1;
            totalPages = 100;
            sFilterItem = null;
            oMainLayer.setValue('');
        }
    }
    
    this.init = function()
    {
        with(this)
        {
            //if (document.domain.has('localhost'))
            //    sABUrl = 'http://' + document.domain + ':' + location.port + '/wire_follow/';
          
            var cL = _GL.oClient;
            sAjaxBaseUrl = typeof sABUrl != 'undefined' ? sABUrl : this.sBaseUrl;
            sPublicWireUrl = sAjaxBaseUrl + 'json/jpublic.aspx?order=<#1#>';
            sAjaxUtilsUrl = sAjaxBaseUrl + 'AjaxServices/utils.asmx/';
            sAjaxMyStuffUrl = sAjaxBaseUrl + 'AjaxServices/MyStuff.asmx/';
            sUnsubscribeFeedUrl = sAjaxMyStuffUrl + 'AjaxUserSubscriptionsUnsubscribe?ChannelID=<#1#>';
            sUserName = typeof(f_username) != 'undefined' ? f_username : username;
            oMainLayer = new HTMLLayer('wire');
            oItemsArea = new HTMLLayer('itemsarea');
            oLoader = new HTMLLayer('displayLoader');
            oPageLoader = new HTMLLayer('pageLoader');
            oPageLoadingText = new HTMLLayer('pageLoadingText');
            oCatText = new HTMLLayer('catText');
            oDateLayer = new HTMLLayer('dateLyr');
            oDateBox = new HTMLLayer('dateBoxSpan');
            sDateLyrHTML = oDateLayer.getValue();
            var sAjaxDir = typeof(sReaderAjaxDir) != 'undefined' ? sReaderAjaxDir : 'jlists';
            jListUrl = sAjaxBaseUrl + 'json/'+sAjaxDir+'.aspx?username=' + sUserName;
            sUrl = jListUrl;
            
            aItemsAreaDims = _GL.getOffsetLT('itemsarea');
            
            if (document.documentElement)
                document.documentElement.style.overflow = 'hidden';
            else
                document.body.style.overflow = 'hidden';
            
            if (cL.bIE && cL.iVer<=6)
            {
                var winWidth = _GL.doc.documentElement.clientWidth;
                if (winWidth < screen.width)
                {
                    window.resizeTo(screen.width,screen.height);
                    window.moveTo(0,0);
                }
            }
           
            setMainLayerDims();
            
            if (bOnScrollPaging)
            {
                oItemsArea.elem.onscroll = function()
                {
                    with(oReader)
                    {
                        if (iPageNum < totalPages && !bPageLoading && 
                            (this.scrollTop + this.clientHeight) >= (this.scrollHeight - 100) )
                        {
                            bPageLoading = true;
                            oPageLoader.show(true);
                            oPageLoadingText.setValue('Loading Page ' + ++iPageNum);
                            /*if (oMyStuff.bPublicView)
                                refreshPublicView(oMyStuff.sPublicOrder,oMyStuff.sPublicFilter, iPageNum);
                            else
                            */
                                requestData(sUrl + '&page=' + iPageNum);
                        }
                    }
                }
            }
            
			window.onresize = setMainLayerDims;
			document.onkeydown = navigateFeed;
			
			var sChannelId = _GL.getQueryValue('channelid');
			if (sChannelId)
				getChannelData(sChannelId, _GL.getQueryValue('channeltitle'), 'Source');
			else
			{
				//requestData(sUrl);
				var oFilterLink = sPageName.is('follow') ? 'allItemsLink' : 'snapShotLink';
				var oSS = _GL.getUIElem(oFilterLink);
				if (oSS)
				    oSS.onclick();
				else if (sPageName.is('items'))
				{
				    var oAll = _GL.getUIElem('panelAll');
				    if (oAll)
				        oAll.onclick();
				}
				else
				    requestData(sUrl);
			}
        }
    }
    
    this.setMainLayerDims = function()
    {
        var obj = typeof this.objName != 'undefined' ? this : oReader;
        
        if (typeof this.objName == 'undefined' && !obj.bPageLoaded)
            return;
            
        with(obj)
        {
            var aDims = _GL.getOffsetLT(oMainLayer.name),
                aConDims = _GL.getOffsetLT(oItemsArea.name),
                winHeight = _GL.win.innerHeight, winWidth = _GL.win.innerWidth,
                cL = _GL.oClient, sH, sW, footer = _GL.getUIElem('footer'),
                footerHeight = footer ? footer.offsetHeight : '21';

            if (cL.bIE && typeof(winHeight) == 'undefined' && cL.iVer>=6)
            {
		        winHeight = _GL.doc.documentElement.clientHeight;
		        winWidth = _GL.doc.documentElement.clientWidth;
		    }
		    
		    sH = winHeight - aConDims[1] - footerHeight;
		    sW = winWidth - aConDims[0];
            
		    oItemsArea.setStyle('width', sW + 'px');
		    oItemsArea.setStyle('height', sH + 'px');
		    oLoader.setStyle('left', (aConDims[0] + 300)+ 'px');
            oLoader.setStyle('top', (aDims[1] + (oItemsArea.elem.offsetHeight/2) - 100)+'px');
            
            oPageLoader.setStyle('top', (winHeight - footerHeight - 90) + 'px');
            oPageLoader.setStyle('width', (winWidth - 28) + 'px');
            
        }
    }
    
    this.addPageTitle = function(pTitle, pHref)
    {
        var oT = this.oPageTitle;
        oT.setText(pTitle);
        oT.elem.href = pHref;
    }
    
    this.addRows = function(pData, pChannelSubType)
    {
        if (!this.bFBConnect)
        {
            try
            {
                this.bFBConnect = typeof FB != 'undefined' && FB.Connect.get_status().result == 1;
            }
            catch(e)
            {
                this.bFBConnect = false;
            }
        }
        
        var oD = pData, len = oD.length,
            bFADisplay = oMyStuff.oFilterAlert.elem.style.visibility.is('visible'),
            bSubFound = false, bIsFollow = typeof sPageName != 'undefined' && sPageName.is('follow');
            
        for (var i=0; i<len; i++)
        {
            if (!oD[i])
                continue;
            
            var aList = oD[i], id = aList.itemid, rowId = 'row_' + id;
            
            if (this.aData[rowId])
                continue;
            
            var oDiv = _GL.createElement('div'),
                tnail = aList.thumbnails && aList.thumbnails.length > 0 && aList.thumbnails[0].length > 0 ? 
                        aList.thumbnails[0] : null,
                cImg = aList.channelImage ? aList.channelImage : this.sSpacer,
                cTitle = aList.channeltitle.unescapeSingleQuote().cleanLineBreaks(),
                shid = aList.shareid, shtitle = aList.sharetitle, 
                shimg = aList.shareimage || this.sSpacer,
                snote = aList.sharenote || "";
                
            var fldTitle = aList.foldertitle, shSubTitle;
            if (shid)
                shSubTitle = aList.sharenote ? "shared this with you" : fldTitle ? 
                            " saved this in <a href=\"#\" onclick=\"oReader.getChannelData('"+aList.folderid+"','"+fldTitle.escapeSingleQuote()+"','collection','"+id+"');return false;\">" + fldTitle + "</a>" : shid ? 
                            "shared this with you" : "";
            
            if (fldTitle)
                snote = "";
            
            var tnImg = tnail ? '<img class="entry-thumbnail" src="'+tnail+'" />' : '', shComments;
            if (shid)
                shComments = sShareComment.replaceTokens(shimg,shtitle,shSubTitle,snote,shid,id);
            var sRemHtml = (sPageName.is('items')) ? sItemRemove.replaceTokens(id,aList.folderid?aList.folderid:"",shid?shid : "") : "";
            var rateHTML = "";
            if (aList.stars)
                rateHTML = ratingHTML.replaceTokens(id,Math.round(aList.stars));
            
            var rowClass = !tnail ? 'item-no-image' : '';
            if (aList.rating.hasAny('1','2'))
                rowClass += ' item-mature';
            if (aList.itemtype.toLowerCase().hasAny('video','tv'))
                rowClass += ' item-video';
            if (aList.itemtype.toLowerCase().hasAny('music','podcast'))
                rowClass += ' item-audio';
            
            var subStr = aList.subscribed || bFADisplay ? "" : 
                         addToWireHtml.replaceTokens(id,aList.channelid,cTitle);
            
            if (!bSubFound && bFADisplay && !aList.subscribed)
                bSubFound = true;
           
            var sFBStr = this.bFBConnect ? sPostFacebookStr.replaceTokens(id) : "";
            
            var sItemHtml = sItemSum.replaceTokens(tnImg,aList.itemlink,aList.itemtitle,id,
                                                    aList.subtitle,aList.channelid,cTitle,cImg,
                                                    shid ? shComments : "", aList.comments,rateHTML,
                                                    aList.date ? ' ' + aList.date : "",
                                                    aList.views,rowClass,aList.gwitemlink,sRemHtml,
                                                    subStr, sFBStr,bIsFollow ? " target=_top" : "");

            oDiv.innerHTML = sItemHtml;
            oDiv.id = 'rowouter_'+id;
            this.oMainLayer.elem.appendChild(oDiv);
            
            var oSumDiv = _GL.getUIElem('rowclick_'+id);
            oSumDiv.parentId = rowId;
            oSumDiv.onclick = this.expandRow;
            
            this.aData[rowId] = aList;
            var iDX = this.aDataIdx.length;
            this.aDataIdx[iDX] = id;
            
            var row = this.aRows[rowId] = new HTMLLayer(rowId);
            //row.setClass('item hentry closed');
            row.bExpanded = false;
            row.sTnail = tnail;
            row.iDataIdx = iDX;
            row.onmouseover = function(){this.setClass(this.getClass().replace('closed','itemsel'));}
            row.onmouseout = function(){this.setClass(this.getClass().replace('itemsel','closed'));}
            this.aExpRows[rowId] = new HTMLLayer('rowfull_' + id);
        }
        
        if (bSubFound)
           this.addToWireonFilter();
    }
    
    this.expandRow = function(pId)
    {
        var oR = oReader, id = this.parentId,
            expId = oR.sExpRowId,
            sFullHTML = oR.getFullItemHTML(id),
            row = oR.aRows[id];
        
        oR.iFeedIdx = row.iDataIdx;
        oR.iCurSelIdx = -1;
        
        if (expId && expId.is(id))
            return;
         
        if (expId && expId != id)
        {
            oR.collapseItem();
            oR.sItemId = id.substr(4);
            oR.expandItem(id);
        }
        else
        {
            oR.sItemId = id.substr(4);
            oR.expandItem(id);
        }
        
        if (oR.aData[id].shareid && oR.aData[id].shareread == '0')
        {
            var oShareCount = _GL.getUIElem('shareCountTxt'),
                oShareSpan = _GL.getUIElem('shareCount'),
                sShareCount = oShareCount ? oShareCount.innerHTML : null, newCount;
            
            if (sShareCount && sShareCount.length > 0)
            {
                newCount = parseInt(sShareCount);
                if (--newCount > 0)
                    oShareCount.innerHTML = newCount;
                else if (newCount <= 0)
                    oShareSpan.innerHTML = "";
            }
            
            oR.aData[id].shareread = '1';
        }
        
        oR.sendItemClick('item');
    }
    
    this.expandItem = function(pId)
    {
        var aListViewDims = this.aItemsAreaDims,
            st = this.oItemsArea.elem.scrollTop, aRowDims = [], iOffset, oTnLyr;
            
        var id = pId, aRowDims = _GL.getOffsetLT(id),
            iOffset = aRowDims[1] - aListViewDims[1],
            sFullHTML = this.getFullItemHTML(id);
        
        this.bExpRowDisplay = true;
        var oNewRow = this.aRows[id], oNewExp = this.aExpRows[id],
            oNewFullDesc = _GL.getUIElem('fulldesc_'+this.sItemId),
            oNewRem = _GL.getUIElem('itemRem_'+this.sItemId),
            oNewSumRow = _GL.getUIElem('rowsum_'+this.sItemId),
            oFavli = _GL.getUIElem('favli_'+this.sItemId),
            oFBli  = this.bFBConnect ? _GL.getUIElem('fbli_'+this.sItemId) : null,
            oShareli = _GL.getUIElem('shareli_'+this.sItemId),
            oEmailli = _GL.getUIElem('emailli_'+this.sItemId),
            oTnLyr = _GL.getUIElem('tnail_'+this.sItemId),
            oSI = _GL.getUIElem('sourceInfo_'+this.sItemId),rowClass;
        
        oNewRow.onmouseout();
        rowClass = oNewRow.getClass();
        
        oNewRow.bExpanded = true;
        oNewSumRow.style.display = 'none';
        oFavli.style.display = 'block';
        oShareli.style.display = 'block';
        oEmailli.style.display = 'block';
        if (oFBli)
            oFBli.style.display = 'block';
        oTnLyr.style.display = 'none';
        oNewExp.show(true);
        if (oNewRow.sTnail)
            oNewRow.setClass(rowClass + ' item-no-image');
        oNewRow.setClass(oNewRow.getClass().replace('closed', 'open'));
        oNewFullDesc.innerHTML = sFullHTML;
        oSI.className = 'source-info-exp';
        this.sExpRowId = id;
        this.oItemsArea.elem.scrollTop = oMyStuff.bFilterAlertShow ? iOffset - 67 : iOffset - 10;
    }
    
    this.collapseItem = function()
    {
        var id = this.sExpRowId,
            oCurRow = this.aRows[id],
            oExp = this.aExpRows[id],
            oRowSum = _GL.getUIElem('rowsum_'+this.sItemId),
            oRowFullDesc = _GL.getUIElem('fulldesc_'+this.sItemId),
            oRemDiv = _GL.getUIElem('itemRem_'+this.sItemId),
            oFavli = _GL.getUIElem('favli_'+this.sItemId),
            oFBli  = this.bFBConnect ? _GL.getUIElem('fbli_'+this.sItemId) : null,
            oShareli = _GL.getUIElem('shareli_'+this.sItemId),
            oEmailli = _GL.getUIElem('emailli_'+this.sItemId),
            oTnLyr = _GL.getUIElem('tnail_'+this.sItemId),
            oSI = _GL.getUIElem('sourceInfo_'+this.sItemId),
            rowClass = oCurRow.getClass();
            
            this.sExpRowId = null;
            oCurRow.bExpanded = false;
            if (oCurRow.sTnail)
                oCurRow.setClass(rowClass.replace('item-no-image', ''));
            oCurRow.setClass(oCurRow.getClass().replace('open', 'closed'));
            
            oExp.show(false);
            oRowSum.style.display = 'block';
            oFavli.style.display = 'none';
            oShareli.style.display = 'none';
            oEmailli.style.display = 'none';
            if (oFBli)
                oFBli.style.display = 'none';
            oTnLyr.style.display = 'block';
            oRowFullDesc.innerHTML = '';
            oSI.className = 'source-info';
            this.bExpRowDisplay = false;
    }
   
    this.navigateFeed = function(pEvent)
    {
        if (oReader.bNaviDisabled)
            return;
            
        var evt = pEvent||event, bOpen = false, bSel = false, bUpSel = false, iCurSI = -1, oR = oReader;
        //j:74, k:75,n:78,o:79,p:80.
        switch(evt.keyCode)
        {
            case 74:
                if (oR.iCurSelIdx == -1)
                    oR.iFeedIdx++;
                else
                {
                    oR.iFeedIdx = oR.iCurSelIdx;
                    oR.iCurSelIdx = -1;
                }
                bOpen = true;
                break;
            case 75:
                if (oR.iCurSelIdx != -1)
                {
                    oR.iFeedIdx = oR.iCurSelIdx;
                    oR.iCurSelIdx = -1;
                    bOpen = true
                }
                else if (oR.iFeedIdx > 0)
                {
                    --oR.iFeedIdx;
                    bOpen = true;
                }
                break;
            case 78:
                if (!oR.sExpRowId)
                {
                    iCurSI = oR.iCurSelIdx;
                    if(oR.iCurSelIdx == -1 && oR.iFeedIdx >= 0)
                        oR.iCurSelIdx = oR.iFeedIdx;
                    else
                        oR.iCurSelIdx++;
                   
                    bSel = true;
                    bUpSel = true;
                }
                break;
            case 80:
                if (!oR.sExpRowId && oR.iCurSelIdx > 0)
                {
                    iCurSI = oR.iCurSelIdx;
                    --oR.iCurSelIdx;
                   
                    bSel = true;
                    bUpSel = false;
                }
                break;
            case 79:
                if (oR.iCurSelIdx != -1)
                    oR.iFeedIdx = oR.iCurSelIdx;
                bOpen = true;
                break;
        }
        if (bOpen)
        {
            var oSumDiv = _GL.getUIElem('rowclick_'+oR.aDataIdx[oR.iFeedIdx]),
                id = oR.aDataIdx[oR.iFeedIdx], oRow = oR.aRows['row_'+id];
            
            if (oRow && oRow.bExpanded)
            {
                var oClose = _GL.getUIElem('close_'+ id);
                if (oClose)
                    oClose.onclick();
            
            }
            else if (oSumDiv)
                oSumDiv.onclick();
        }
        
        if (bSel)
        {
            var oCurSelRow = iCurSI != -1 ? oR.aRows['row_'+oR.aDataIdx[iCurSI]] : null;
            if (oCurSelRow)
                oCurSelRow.onmouseout();
           
            var oRow = oR.aRows['row_'+oR.aDataIdx[oR.iCurSelIdx]],
                aDims = _GL.getOffsetLT(oRow.name), itemElem = oR.oItemsArea.elem,
                iOffset = aDims[1] - oR.aItemsAreaDims[1], 
                ih = parseInt(itemElem.scrollTop)+parseInt(itemElem.offsetHeight);
                
            if (iOffset > ih && bUpSel)
                itemElem.scrollTop = itemElem.scrollTop + itemElem.offsetHeight;

            if (iOffset < itemElem.scrollTop && !bUpSel)
                itemElem.scrollTop = itemElem.scrollTop - itemElem.offsetHeight;    
                
            oRow.onmouseover();
        }
    }
    
    this.getFullItemHTML = function(pId)
    {
        var s = this.aFullItemHTML[pId], id = pId.substr(4);
        if (s)
            return s;
            
        var oD = this.aData[pId];
        s = "";
        if (oD.playerurl)
            s += '<div style="text-align:center;margin-top:10px;">' + oD.playerurl + '<a href="#" onclick="oReader.openPopout(\'' + pId + '\');return false;">popout</a></div>';
        
        s += '<div style="margin-top:10px;">'+oD.description+'</div>';
      
        this.aFullItemHTML[pId] = s;
        return s;
    }
    
    this.getTimeLine = function(pTxt)
    {
        var d = _GL.createElement('div');
        d.className = 'time-range';
        d.innerHTML = pTxt;
        return d;
    }
    
    this.openTitle = function(pId)
    {
        var oRow = this.aRows['row_'+pId];
        return oRow.bExpanded;
    }
    
	this.openPopout = function(pId)
	{
		this.sPopoutPlayerUrl = this.aData[pId].playerurl;
		this.sPopoutTitle = this.aData[pId].itemtitle;
		_GL.openWindow(_GL.getBaseUrl() + 'popout.html', 'popout_' + pId, 425, 350,'no','no','no','no','no','no');
		return false;
	}
	
    this.displayNoFeedMsg = function(pMsg)
    {
        if (pMsg.is('0'))
        {
            var oPub = _GL.getUIElem('publicWire');
            if (oPub)
                oPub.onclick();
        }
        else
        {
            var s = '<div class="noFeedMsg">' + pMsg + '</div>';
            this.oMainLayer.setValue(s);
        }
    }
    
    this.setDateText = function(pTitle)
    {
        var sImg = '&nbsp;&nbsp;<img src="images/drop_down_arrow.gif" style="position:relative;top:4px;"/>';
        this.oDateBox.setValue(pTitle + sImg);
    }
    
    this.getFolderFeeds = function(pId, pTitle)
    {
        oMyStuff.getFilterView(pId, pTitle, 'Folder', oFilterDD.aFilterParamName['Folder']);
    }
    
    this.postToFacebook = function(pId)
    {
        if (!iFBTemplateId || typeof FB == 'undefined')
            return;
        
        var oD = this.aData['row_'+pId],
            tnail = oD.thumbnails.length > 0 ? oD.thumbnails[0] : null,
            url =  oD.itemlink, fltitle = oD.subtitle, shtitle,desc = oD.description,
            title = oD.itemtitle,
            sourcelink = _GL.getBaseUrl() + 'channel/' + oD.channelid,
            sourcetitle= oD.channeltitle,
            action = this.aFBAction[oD.itemtype.toLowerCase()];
        /* 
        sourcelink, sourcetitle
        changed the posturl back to itemlink instead of this.sBaseUrl+oD.gwitemlink
        if (desc.length > 300)
            desc = desc.substr(0,300);
        */
        
        shtitle = fltitle.length > 75 ? fltitle.substr(0,75) : fltitle;
        
        if (typeof action == 'undefined')
            action = 'read';
                
        var oFBData = {"images":[{"src":tnail,"href":url}],"shortcontent":shtitle,"fullcontent":fltitle,"posturl":url,"title":title,"action":action,"username":username,"sourcelink":sourcelink,"sourcetitle":sourcetitle};
        
        FB.Connect.showFeedDialog(iFBTemplateId, oFBData, null, null, null, FB.RequireConnect.promptConnect); 
        /*
        { "images": [ { "src": "ITEM IMAGE URL IF AVAILABLE", "href": "URL link back to genwi item page" } ], "shortcontent": "~2lines", "fullcontent": "~4-5lines", "posturl": "GENWI ITEM URL", "title": "ITEM TITLE", action:"liked, etc" } 
        */
    }
    
    /********* Ajax requests to get data ******/
    this.getChannelData = function(pCid, pCTitle,pSource,pItemId)
    {        
        oMyStuff.updateFilterAlert(pCid, pCTitle,pSource,null,pItemId);
        this.bFilterView = false;
        this.refreshListView([{'name':'channelid','val':pCid}]);
    }
    
    this.getFolderView = function()
    {
    
        this.refreshListView([{'name':'channelid','val':pCid}]);
    }
    
    this.getUsersFeeds = function(pUid,pUtitle)
    {
        with(this)
        {
            /*
            oMyStuff.aLvlBCs.lvl1 = '<a href="' + _GL.getBaseUrl()+'people/'+pUid+'">'+pUtitle+'</a>';
            oMyStuff.aLvlBCs.lvl2 = null;
            oMyStuff.updateBrearCrumb();
            */
            bFilterView = false;
            sUrl = jListUrl + '&order=from&fromid=' + pUid; 
            requestData(sUrl);
        }
    }
    
    this.getFilterItem = function(pFilter)
    {
        oMyStuff.aLvlBCs.lvl1 = this.bFilterView ? '<a href="#" onclick="oMyStuff.switchItemView(\'' + oMyStuff.sCurFilterView + '\',\'' + oMyStuff.sCurFilterOrder + '\');return false;">' + oMyStuff.sBCText + '</a>' : pFilter;
        oMyStuff.aLvlBCs.lvl2 = oReader.bFilterView ? pFilter : null;
        oMyStuff.aLvlBCs.lvl3 = null;
        oMyStuff.updateBrearCrumb();
        this.sFilterItem = pFilter;
        this.refreshListView([{'name':'filter','val':'type/'+pFilter + '/'}]);
    }
    
    this.refreshListView = function(pArray)
    {
        with(this)
        {
            var s = '';
            for (var i=0; i<pArray.length; i++)
            {
                var v = pArray[i].val;
                if (!v.is(oMyStuff.sCurFilterOrder))
                    s += '&' + pArray[i].name + '=' + pArray[i].val;
            }
            if (s.has('popular'))
            {
                if (sUrl.has('&popular'))
                    sUrl = sUrl.substr(0,sUrl.indexOf('&popular')) + s;
                else if (!bFilterView)
                    sUrl += s;
                else
                    sUrl = jListUrl + s;
            }
            else
                sUrl = jListUrl + s;
            
            if (bFilterView)
                sUrl += '&order=' + oMyStuff.sCurFilterOrder;
            
            if (oMyStuff.sCurFilterOrder.is('favs') && typeof channelId != 'undefined')
                sUrl += '&channelid=' + channelId;
            
            requestData(sUrl);
        }
    }
    
    this.refreshPublicView = function(pOrder, pFilter,pPage)
    {
        var sUrl = this.sPublicWireUrl.replaceTokens(pOrder);
        if (pFilter)
            sUrl += '&filter='+pFilter;
        this.sUrl = sUrl;
        this.requestData(sUrl);
    }
       
    this.ssFeed = function(pType, pId)
    {
        this.bNaviDisabled = true;
        if (!_GL.isAthenticated())
        {
            this.showLoginDialog();
            return;
        }
	    
	    pUrl = 'http://' + document.domain + '/' + this.aSSUrl[pType];
            
        if (!oOverlay)
            oOverlay = new OverlayPopup('mb-overlay', '500', '400', true, pUrl + pId, 'mb');
        else
            oOverlay.sUrl = pUrl + pId;
        
        oOverlay.display();
        return;
    }
    
    this.showReply = function(pId)
    {
        var oRepSec = _GL.getUIElem('comment-form'+pId),
            oRepLink = _GL.getUIElem('showRepLink'+pId),
            repMsg = _GL.getUIElem('postReplyMsg'+pId)
            s = oRepLink.innerHTML,
            bShow = s.toLowerCase().has('message');
        
        repMsg.innerHTML = "";
        oRepSec.style.display = bShow ? 'block' : 'none';
        oRepLink.innerHTML = bShow ? 'Hide' : oRepLink.sToggleHtml;
        oRepLink.sToggleHtml = s;
        this.bNaviDisabled = bShow;
    }
    
    this.postReply = function(pUserTo, pId)
    {
        this.sPostReplyId = pId;
        var sUrl = this.sAjaxMyStuffUrl + 'AjaxUserMessagesSendMessage?UserTo='+pUserTo+'&MessageTitle=';
        sUrl += this.aData['row_'+pId].itemtitle;
        sUrl += '&MessageText=' + _GL.getUIElem('comment-message'+pId).value;
        var oAjax = new Ajax('sendEmail', sUrl, true,  ['oReader.processPostReply']);
        
        oAjax.iResponseType = oAjax.RESPONSE_HTML;
        oAjax.bShowProgress = true;
        oAjax.sShowProgressId = 'postButton'+pId;
        this.sPostAjaxHTML = _GL.getUIElem('postButton'+pId).innerHTML;
        oAjax.send();    
    }
    
    this.processPostReply = function(pResp)
    {
        var id = this.sPostReplyId, 
            e = _GL.getUIElem('postButton'+id), repMsg = _GL.getUIElem('postReplyMsg'+id),
            eMsg = _GL.getUIElem('comment-message' + id);
        e.innerHTML = this.sPostAjaxHTML;
        repMsg.innerHTML = 'Reply sent successfully.';
        eMsg.value = "";
    }
    
    this.emailFeed = function(pObj)
    {
        with(this)
        {
            if (!_GL.isAthenticated())
            {
                showLoginDialog();
                return;
            }
            
            oDialog = null;
            oDialog = new Overlay('mb-overlay', 600, 420, true,'mb');
            var s = '';
            s += sMBHeader.replaceTokens('Tell a Friend', 'oReader');
            s += sMBContent.replaceTokens(emailHTML.replaceTokens('oReader'));
            
            oDialog.setContent(s);
            oDialog.display(true);
            oDialog.setPosition();
        }
    }
    
    this.sendEmail = function()
    {
        var oErrMsg = _GL.getUIElem('errorMsg');
        oErrMsg.style.display = 'none';
        var toEmailVal = _GL.getUIElem('toEmails').value,
            fromEmailVal = _GL.getUIElem('fromEmail').value,
            nameVal =  _GL.getUIElem('fromName').value,
            _1 = toEmailVal.isEmpty() ? 'Friend\'s Email Address' : '',
            _2 = fromEmailVal.isEmpty() ? ', Your Email Address' : '', 
            _3 = nameVal.isEmpty() ? ', Your name' : '';
        
        if (_1 || _2 || _3)
        {
            oErrMsg.innerHTML = sEmailErrMsg.replaceTokens(_1, _2, _3);
            oErrMsg.style.display = 'block';
            return;   
        }
           
        var sUrl = this.sAjaxUtilsUrl + 'mailItem2?to=' + toEmailVal;
        sUrl += '&from=' + fromEmailVal;
        sUrl += '&name=' + nameVal;
        sUrl += '&emailmessage=' + _GL.getUIElem('emailMessage').value;
        sUrl += '&itemid=' + this.sItemId,
        oEmailBody = _GL.getUIElem('emailBody'),
        oLoaderDiv = _GL.createElement('div');
        
        var oAjax = new Ajax('emailFeed', sUrl, true,  ['oReader.processSendEmail']);
        oAjax.iResponseType = oAjax.RESPONSE_HTML;
        oEmailBody.innerHTML = "";
        oEmailBody.appendChild(oLoaderDiv);
        oLoaderDiv.innerHTML = sLoaderSmallHtml;
        oAjax.send();
    }
    
    this.processSendEmail = function(pResp)
    {
        var oEmailBody = _GL.getUIElem('emailBody');
        oEmailBody.innerHTML = emailSuccHtml.replaceTokens('oReader');
    }
    
    this.mouseOverStar = function(pObj, pOver)
    {
        var pn = _GL.getParentNode(pObj), cl = pObj.className;
        if (pn)
            pn.className = pOver ? cl + 's' : '';
    
    }
     
    this.rateItem = function(pRating, pItemId)
    {
        if (!_GL.isAthenticated())
        {
            this.showLoginDialog();
            return;
        }
    
        this.sRateItemId = pItemId;
        var sUrl = this.sAjaxUtilsUrl + 'addItemRating?itemId=' + pItemId + '&rating=' + pRating;
        var oAjax = new Ajax('rateItem', sUrl, true, ['oReader.processRatingResponse']);
        oAjax.iResponseType = oAjax.RESPONSE_HTML;
        oAjax.sShowProgressId = 'rate_' + pItemId;
        oAjax.bShowProgress = true;
        var oRating = _GL.getUIElem('rate_' + this.sRateItemId);
        this.sPostRatingHTML = oRating.innerHTML;
        oRating.onmouseover = null;
        oRating.onMouseout = null;
        oAjax.send();
    }
    
    this.processRatingResponse = function(pResp)
    {
        // show the saved rating.
        var oRating = _GL.getUIElem('rate_' + this.sRateItemId),
            v = oXmlHelper.getValue(pResp.oResponseXml,'string');
        if (oRating)
        {
            oRating.innerHTML = this.sPostRatingHTML ? this.sPostRatingHTML : 'Rating Added';
            var oRO = _GL.getUIElem('rateOuter_' + this.sRateItemId), 
                oCR = _GL.getUIElem('curRating_' + this.sRateItemId);
            
            if (oRO)
                oRO.className = 'rating';
            
            if (oCR && v)
                oCR.style.width = v + '%';
        }
    }
    
    this.sendItemClick = function(pReq)
    {
        var cmd = pReq == 'item' ? 'addItemClicks' : 'addSharedItemClicks';
        var sUrl = this.sAjaxUtilsUrl + cmd + '?itemId=' + this.sItemId;
        
        if (pReq == 'item')
        {
            sUrl += '&referUrl=';
            sUrl += (sPageName == 'follow') ? this.sUserName : location.href;
            if (this.aData['row_' + this.sItemId].shareid)
                this.bShareItemClick = true;
        }
        else
        {
            sUrl += '&userName=' + this.sUserName;
            this.bShareItemClick = false;
        }
        var oAjax = new Ajax('itemClick', sUrl, true, ['oReader.processNoResponse']);
        oAjax.iResponseType = oAjax.RESPONSE_HTML;
        oAjax.send();
    }
    
    this.processNoResponse = function()
    {
        if (this.bShareItemClick)
            this.sendItemClick('share');
        else
            return;
    }
    
    this.unsubscribeChannel = function(pCid)
    {
        var oAjax = new Ajax('inlineAjax', this.sUnsubscribeFeedUrl.replaceTokens(pCid), true,  ['oReader.postUnsubChannel']);
        oAjax.iResponseType = oAjax.RESPONSE_HTML;
        oAjax.bShowProgress = true;
        oAjax.sShowProgressId = 'unsubSec';
        //oFilterDD.resetData('From');
        oAjax.send();
    }
    
    this.postUnsubChannel = function()
    {
        oMyStuff.removeFilterAlert();
    }
    
    this.addToWire = function(pChannelId, pChannelName, pDiv, pProcess)
    {
        if (!_GL.isAthenticated())
        {
            _GL.redirect(_GL.getBaseUrl() + 'json/jutils.aspx?action=subscribe&itemid=' + pChannelId);
            return;
        }
        var sUrl = this.sAjaxUtilsUrl+'addFavoriteChannel?channelId=' + pChannelId;
        this.sAWChannel = pChannelId;
        this.aAWChannelName = pChannelName;
        
        var oAjax = new Ajax('addToWire', sUrl, true,  [pProcess ? 'oReader.postAddToWire' : 'oReader.noProcess']);
        oAjax.iResponseType = oAjax.RESPONSE_HTML;
        oAjax.bShowProgress = true;
        oAjax.sShowProgressId = pDiv;
        oAjax.send();
    }
    
    this.postAddToWire = function(pResp)
    {
        if (sPageName == 'follow')
            _GL.redirect(_GL.getBaseUrl() + 'people/mystuff.aspx?username=' + username + '&channelid=' + this.sAWChannel + '&channeltitle=' + this.aAWChannelName, false, true);
        else
            this.getChannelData(this.sAWChannel, this.aAWChannelName, 'source');
    }
    
    this.noProcess = function(pResp)
    {
        return;
    }
    this.addToWireonFilter = function()
    {
        var oUnsubSec = _GL.getUIElem('unsubSec');
        if (oUnsubSec && !sPageName.is('items'))
        {
            var oFC = oMyStuff.oFilterChannel, oFCE = oFC.elem;
            if (oFCE)
            {
                var lnk = oFCE.href, cid = lnk.substr(lnk.indexOf('channel/')+8),
                    cname = oFCE.textContent ? oFCE.textContent : oFCE.innerText;
                oUnsubSec.innerHTML = '[<a id="subChannel" href="#" onclick="oReader.addToWire(\''+cid+'\',\''+cname+'\',\'unsubSec\',false);return false;">Follow &raquo;</a>]';
            }
        }
    }
    
    this.showLoginDialog = function()
    {
        with(this)
        {
            oDialog = null;
            oDialog = new Overlay('mb-overlay', 600, 250, true,'mb');
            var sLoginText = loginPromptHTML.replaceTokens(sPageName.is('follow') ? " target=_top" : "");
            var s = '';
            s += sMBHeader.replaceTokens('Login', 'oReader');
            if (sPageName.is('follow'))
                
            s += sMBContent.replaceTokens(sLoginText);
            
            oDialog.setContent(s);
            oDialog.display(true);
            oDialog.setPosition();
        }
    }
    
    this.requestData = function(pUrl)
    {
        with(this)
        {
            oCatText.setValue(sFilterItem ? sFilterItem : 'All');
            if (!pUrl.hasAny('shared','snap'))
            {
                //if (!pUrl.has('popular'))
                //{
                    oDateLayer.setValue(sDateLyrHTML);
                    oDateBox.elem = _GL.getUIElem(oDateBox.name);
                //}
            }
            
            if (!bPageLoading)
            {
                oLoader.show(true);
                bPageLoading = true;
                initReaderState();
            }
            
            bAjaxInProgress = true;
            
            var oAjax = new Ajax('listViewReq', (pUrl + '&rd='+_GL.getRandomNumber()), true, ['oReader.processResponse']);
            oAjax.iResponseType = oAjax.RESPONSE_JSON;
            oAjax.send();
        }
    }
    
    this.processResponse = function(pResp)
    {
        with(this)
        {
            if (oOverlay)
                oOverlay.close();
            oLoader.show(false);
            oPageLoader.show(false);
            bAjaxInProgress = false;
            var oD = pResp.oJSON;
            if (oD)
            {
                totalPages = oD.totalpages
                if (oD.lists)
                    addRows(oD.lists, oD.channelsubtype);
                else if (oD.message)
                {
                    var oMS = oMyStuff;
                    if (oMS.bPublicView && oMS.sCurFilterOrder.is('popular'))
                        this.refreshPublicView('shared',oMS.sPublicFilter,'1');
                    else
                        displayNoFeedMsg(oD.message);
                }
            }
            bPageLoading = false;
            bPageLoaded = true;
        }
    
    }
    /**** End AJAX ******/
}

var oReader = new Reader();
var oOverlay = null;
//_GL.addOnLoadListener(oReader.init);
