top.landingpage =
{
   LPPARAMNAME:"demo", // the param used to control - if found in domain name it will trigger

   translateToStr: // translate pointer to a dict of values (k:v).
           function (options, pointer)
           {
              var res = {}
              for(var i in options)
              {
                 var loc = pointer[i]
                 res[i] = options[i][loc]
              }
              return res
           },

   buildName: // build a  "url" part of the html to dissplay (later doing prefix+name+".html")
           function (prefix, options, pointer)
           {
              var res = this.translateToStr(options, pointer)
              var url=prefix
              for (var i in options)
              {
                 var cur=res[i]
                 if (!cur) cur=options[i][0]
                  if (cur)
                  {
                     url=url+"_"+cur
                  }
              }
              return url
              /*var state = res["state"]
              if(state == null)
              {
                 state = ''
              }
              var prepost = res["prepost"]
              var booktype = res["booktype"]
              var landingPageVer = res["landingPageVer"]
              var flash = res["flash"]
              if(landingPageVer == null)
              {
                 landingPageVer = options["landingPageVer"]
              }
              var chosen = prefix + state + "_" + prepost + "_" + booktype
              if(landingPageVer != null)
              {
                 chosen = chosen + landingPageVer
              }
              if(flash != null)
              {
                 chosen = chosen + flash
              }
              return chosen*/

           },
   buildLPControlParam: // We use a top.location url as LP controller. build the paream array concatinated with the ususal &
           function (options, pointer)
           {
              var params = this.translateToStr(options, pointer)
              var param = this.LPPARAMNAME + "=%s"
              var item = param
              var res = ""
              for(var i in params)
              {
                 item = item.replace("%s", params[i])
                 if(params[i])
                 {
                    res = res + item
                 }
                 item = "&" + param
              }
              return res
           },
   urlChosenParamsArr : // transform location url to control param by extracting the the "control param" values in array
           function (theArgName, loc)
           {
              if(loc == null)
              {
                 loc = top.location
              }
              sArgs = loc.search.slice(1).split('&');
              var res = [];
              for(var i = 0; i < sArgs.length; i++)
              {
                 if((sArgs[i] == null) || (sArgs[i] == ""))
                 {
                    continue
                 }
                 var kv = sArgs[i].split('=')
                 if(kv.length < 2)
                 {
                    continue
                 }
                 if(kv[0] == theArgName)
                 {
                    res[res.length] = kv[1]
                 }
              }
              return res
           },

   createOptions: // land page (LP) domain options. construct the option we want
           function (landingPagesNum, withflashOption)
           {
              var options =
           {
              state:["open","pend","closed"],
              prepost:["pre","post"],
                 booktype:["g", "p"]
                 //landingPageVer:[]
              }
              if(landingPagesNum > 1)
              {
                 options.landingPageVer = []
                 for(i = 0; i < landingPagesNum; i++)
                 {
                    options.landingPageVer[i] = "v" + (i + 1)
                 }
              }
              // detect
              // add option
              if(withflashOption)
              {
                 options["flash"] = ["", "flash"]
              }

              return options
           },


   createSearchDictForOptions: // return the options as dict (index values) for simple search
           function (options)
           {
              // find attr in current
              var searchDictForOptions = {}
              for(var propName in options)
              {
                 var valuearr = options[propName]
                 var valueDict = {}
                 for(var i = 0; i < valuearr.length; i++)
                 {
                    var value = valuearr[i]
                    valueDict[value] = i
                 }
                 searchDictForOptions[propName] = valueDict
              }
              return searchDictForOptions
           },

   makeRefDictFromArr:// recieve value array of props return dict with index values
           function (options, arrayOfchosen)
           {
              var searchDictForOptions = this.createSearchDictForOptions(options);
              var result = {}

              for(var i = 0; i < arrayOfchosen.length; i++)
              {
                 var chosenValue = arrayOfchosen[i];

                 for(var propName in searchDictForOptions)
                 {
                    var searchDictForOption = searchDictForOptions[propName];
                    var indexOfChosenValue = searchDictForOption[chosenValue]
                    if(indexOfChosenValue != null)
                    {
                       result[propName] = indexOfChosenValue
                    }
                 }
              }
              return result
           },

        /*translate jsp variables to the starting point - that get the info from the server*/
   makeRefDictFromTopInfo: // construct default (top.info) pointer for chossing LP
           function (options)
           {
              var landingPagesNum = top.info.numOfLandingPages;
              options = this.createOptions(landingPagesNum);

              var landingPageNamePath;
              var state = "closed";
              var prepost = "pre"
              var pg = "g";
              var landingPageNum = "";
              var LANDING_STRING = "landing_";
              if(top.info.isPostSignIn == "true")
              {
                 prepost = "post";
              }
              else
              {
                 prepost = "pre";
              }

              if(top.info.isOpen == "true")
              {
                 state = "open";
              }
              else if(top.info.isClosing == "true")
              {
                 state = "pend";
              }
              else if(top.info.isClose == "true")
              {
                 state = "closed";
              }

              if(top.info.bookType == "General")
              {
                 pg = "g";
              }
              else if((top.info.bookType == "Community"))
              {
                 pg = "p";
              }
              var flash = null
              var landingPagesNum = top.info.numOfLandingPages;
              if(landingPagesNum != undefined && landingPagesNum != "1")
              {
                 // Generate a random number from 1 to landingPagesNum.
                 // (Math.random() generates a random number from 0 to 1)
                 landingPageNum = "v" + Math.ceil(Math.random() * landingPagesNum)
              }
              var arr = [state,prepost,pg,landingPageNum]

              var res = this.makeRefDictFromArr(options, arr)
              // flash option is not in top info,
              // if detected then default is with flash
              if(options["flash"] != null)
              {// 1 is _flash
                 res[options] = 1;
              }

              return res
           },

   getCombinedInfoAndUrl:// get default, override with url parameter
           function (options, pointerModification)
           {
              if(pointerModification == null)
              {
                 pointerModification = this.makeRefDictFromArr(options, this.urlChosenParamsArr(this.LPPARAMNAME))
              }
              var pointer = this.makeRefDictFromTopInfo(options)
              for(var i in pointerModification)
              {
                 pointer[i] = pointerModification[i]
              }
              return pointer;
           },
   getAllVariations:// create arr with various options change and corresponding url
           function (options, pointer)
           {
              res = {}
              for(var i in options)
              {
                 var cur = pointer[i]
                 if (!cur) cur=0
                 var curName = options[i][cur]
                 var prev = (cur - 1 + options[i].length) % options[i].length
                 var prevName = options[i][prev]
                 pointer[i] = prev
                 var prevParam = this.buildLPControlParam(options, pointer)
                 pointer[i] = cur
                 var next = (cur + 1) % options[i].length
                 var nextName = options[i][next]
                 pointer[i] = next
                 var nextParam = this.buildLPControlParam(options, pointer)
                 pointer[i] = cur
                 res[i] = [curName,prevName,prevParam, nextName, nextParam]
              }
              return res
           },

   getChangeOptionPointer:// unused
           function (attr, options, by)
           {
              if(options == null)
              {
                 options = this.options
              }
              var pointer = this.getCombinedInfoAndUrl(options)
              if(pointer[attr] == null)
              {
                 // no next its null- like no ver, next do nothing
                 return pointer;
              }
              // cycle through +by
              pointer[attr] = (pointer[attr] + by) % options[attr].length
              return pointer
           },

   getChangeOptionUrl://unused
           function (attr, prefix, options, by)
           {
              if(options == null)
              {
                 options = this.options
              }
              if(prefix == null)
              {
                 prefix = top.getBaseAppUrl() + "framesetHome.do"
              }
              var pointer = this.getChangeOptionPointer(attr, options, by)
              var param = this.buildLPControlParam(options, pointer)

              return prefix + "?" + param
           },

   getLandingPageAndInitWithOptions: // start LP by options and prefix
           function(options, prefix)
           {
              if(prefix == null)
              {
                 prefix = "landing"
                 //prefix = "landing_"
              }
              this.options = options

              var pointer = this.getCombinedInfoAndUrl(options)
              var name = this.buildName(prefix, options, pointer)
              return name;
           },
   getLandingPageAndInitOptionsFactory: // start LP by create options and build url
           function(landingPagesNum, prefix, withflashOption)
           {
              if(landingPagesNum == null)
              {
                 landingPagesNum = 0
              }
              var options = this.createOptions(landingPagesNum, withflashOption)
              return this.getLandingPageAndInitWithOptions(options, prefix)
           },
   getLandingPageAndInitOptions:
           function(landingPagesNum, withflashOption)
           {
              return this.getLandingPageAndInitOptionsFactory(
                      landingPagesNum,
                      null, // default prefix
                      withflashOption)
           },


   addLinksToNavigateThroughLandingPages:// create navigation pane
           function ()
           {
              var url = "" + top.location
              //if(url.indexOf(this.LPPARAMNAME) == -1)
              url=url.split('?')[1]
              if((!url)||(url.indexOf(this.LPPARAMNAME) == -1))
              //if(this.urlChosenParamsArr(this.LPPARAMNAME).length == 0)
              {
                 return ''
              }
              var innerhtml = ""

              // add URL to SB
              var sburl = 'top.navigateToArticle(0);'
              var item = "<li><a href='javascript:void(0);' onClick='%sburl'>Enter SharedBook</a></li>"
              item = item.replace('%sburl', sburl)
              innerhtml += item

              // add URL to MB
              var mburl = top.getBaseAppUrl() + 'memorybookMarketingPageTop.do'
              item = "<li><a href='%mburl' target='_top'>Enter MemoryBook</a></li>"
              item = item.replace('%mburl', mburl)
              innerhtml += item

              var options = this.options
              var pointer = this.getCombinedInfoAndUrl(options)
              var res = this.getAllVariations(options, pointer)
              var prefix = top.getBaseAppUrl() + "framesetHome.do?"
              for(var i in res)
              {
                 // display state: close<== open ==>pend
                 var item = "<li>%scat:</li>"
                 item = item.replace('%scat', i)
                 innerhtml += item
                 item = "<li><a href='%spurl' target='_top'>%sptext&lt;=</a> <b>[%scur]</b> <a href='%snurl' target='_top'>=&gt;%sntext</a></li>"
                 item = item.replace('%scur', res[i][0])
                 item = item.replace('%spurl', prefix + res[i][2])
                 item = item.replace('%sptext', res[i][1])
                 item = item.replace('%snurl', prefix + res[i][4])
                 item = item.replace('%sntext', res[i][3])
                 innerhtml += item
              }

              innerhtml = "<ul>" + innerhtml + "</ul>"
              return innerhtml
           },
   setInternalPage:
           function (doc, landingPageNamePath)
           {
              var internalLPElement = doc.getElementById("internalLP");
              if(internalLPElement)
              {
                 internalLPElement.src = landingPageNamePath;
              }
           },

     setContributionsList:
           function (doc)
           {
            var url = "/pilot/ajaxGetContributionList.do";
            dojo.io.bind({
            url: url,
            load: function(type, data, evt)
            {
               processContributionsList(doc, data);
            },
            error: function(type, errObj)
            {
               alert("Ajax returned with error. type = " + type + " ; errorObj = " + errObj);
            },
            mimetype: "text/json"
         });
           }

}

function processContributionsList(doc, data)
{
	var contributionsDiv = doc.getElementById("recentContributionsDiv");

   if(data.contributionsList != null && (data.contributionsList.length > 0))
   {
      var contributionsTable = doc.createElement("table");
      var contributionsTableBody = doc.createElement("tbody");

      var contributionsContainerTable = doc.createElement("table");
      var contributionsContainerTableBody = doc.createElement("tbody");
      var contributionsContainerRow = doc.createElement("tr");
      var contributionsContainerCell = doc.createElement("td");

      var topTitleRow = doc.createElement("tr");
      topTitleRow.setAttribute("align", "left");
      topTitleRow.setAttribute("className", "contribution-top-title-font");
      topTitleRow.setAttribute("class", "contribution-top-title-font");
      var topTitleCell = doc.createElement("td");
      //var titleText = doc.createTextNode("Recent 10 Contributions");
      var titleImg = doc.createElement("img");
      titleImg.setAttribute("src", "/pilot/images/recent_posts.gif");
      titleImg.setAttribute("alt", "Recent Contributions");
      topTitleCell.appendChild(titleImg);
      topTitleRow.appendChild(topTitleCell);
      contributionsContainerTableBody.appendChild(topTitleRow);
      contributionsContainerTable.appendChild(contributionsContainerTableBody);

      var contributionIconSrc = "/pilot/images/myAccount/icn_popup_comment.gif";

      var headRef = doc.getElementsByTagName("head")[0];
      var cssRef = doc.createElement("link");
      cssRef.setAttribute("rel", "stylesheet");
      cssRef.setAttribute("type", "text/css");
      cssRef.setAttribute("href", "/pilot/css/contributions.css");
      headRef.appendChild(cssRef);

      contributionsTable.setAttribute("cellPadding", "0");
      contributionsTable.setAttribute("cellSpacing", "0");
      contributionsTable.setAttribute("width", "800");
      contributionsTable.setAttribute("className", "contribution-table-style");
      contributionsTable.setAttribute("class", "contribution-table-style");

      var titleRow = doc.createElement("tr");
      titleRow.setAttribute("className", "contribution-list-title");
      titleRow.setAttribute("class", "contribution-list-title");

      var titleCell1 = doc.createElement("td");
      var titleTextNode1 = doc.createTextNode(" ");
      titleCell1.setAttribute("width", "30");
      titleCell1.setAttribute("className", "contribution-text-font");
      titleCell1.setAttribute("class", "contribution-text-font");
      titleCell1.appendChild(titleTextNode1);
      titleRow.appendChild(titleCell1);

      var titleCell2 = doc.createElement("td");
      var titleTextNode2 = doc.createTextNode("Title");
      titleCell2.setAttribute("width", "340");
      titleCell2.setAttribute("className", "contribution-text-font");  //IE
      titleCell2.setAttribute("class", "contribution-text-font");      //FF
      titleCell2.setAttribute("align", "left");
      titleCell2.appendChild(titleTextNode2);
      titleRow.appendChild(titleCell2);

      var titleCell3 = doc.createElement("td");
      var titleTextNode3 = doc.createTextNode("Author");
      titleCell3.setAttribute("width", "150");
      titleCell3.setAttribute("className", "contribution-text-font");
      titleCell3.setAttribute("class", "contribution-text-font");
      titleCell3.setAttribute("align", "left");
      titleCell3.appendChild(titleTextNode3);
      titleRow.appendChild(titleCell3);

      var titleCell4 = doc.createElement("td");
      var titleTextNode4 = doc.createTextNode("Post Location");
      titleCell4.setAttribute("width", "280");
      titleCell4.setAttribute("className", "contribution-text-font");
      titleCell4.setAttribute("class", "contribution-text-font");
      titleCell4.setAttribute("align", "left");
      titleCell4.appendChild(titleTextNode4);
      titleRow.appendChild(titleCell4);

      contributionsTableBody.appendChild(titleRow);
      if(data.contributionsList != null)
      {
         var len = data.contributionsList.length;
         for(i=0; i<len; i++)
         {
            var contributionObj = data.contributionsList[i];

            //<TR>
            var row = doc.createElement("tr");
            row.setAttribute("className", "contribution-text-font");
            row.setAttribute("class", "contribution-text-font");

            //<TD-1>: comment icon image
            var cell1 = doc.createElement("td");
            cell1.setAttribute("align", "center");
            var icon = doc.createElement("img");
            icon.setAttribute("src", contributionIconSrc);
            icon.setAttribute("width", "14");
            icon.setAttribute("height", "12");
            cell1.appendChild(icon);

            //<TD-2>: contribution title
            var cell2 = doc.createElement("td");
            cell2.setAttribute("align", "left");

            var link1 = doc.createElement("a");
            link1.setAttribute("target", "workArea");
            link1.setAttribute("onclick", "top.setCurrentSection('');top.handleBookAreaTabChange(" + contributionObj.rootType + ");");
            link1.setAttribute("href", "/pilot/navigateBook.do?operation=gotoBookmarkOperation&pageId=" +
                                      contributionObj.page + "&bookmarkId=" + contributionObj.bookLocationId +
                                      "&elementType=" + contributionObj.type + "&elementId=" + contributionObj.id);
            link1.setAttribute("id", "titleLink2");

            var textNode2 = doc.createTextNode(top.trimLastWord(contributionObj.title, 40));
            link1.appendChild(textNode2);
            cell2.appendChild(link1);

            //<TD-3>: contribution owner
            var cell3 = doc.createElement("td");
            cell3.setAttribute("align", "left");

            var link2 = doc.createElement("a");
            link2.setAttribute("target", "workArea");
            link2.setAttribute("onclick", "top.setCurrentSection('viewProfile')");
            link2.setAttribute("href", "/pilot/previewProfile.do?operation=previewOperation&profileUserId=" + contributionObj.ownerId);
            var textNode3 = doc.createTextNode(top.trimLastWord(contributionObj.ownerId, 30));
            link2.appendChild(textNode3);
            cell3.appendChild(link2);

            //<TD-4>: contribution book location
            var cell4 = doc.createElement("td");
            cell4.setAttribute("align", "left");

            var link3 = doc.createElement("a");
            link3.setAttribute("target", "workArea");
            link3.setAttribute("onclick", "top.setCurrentSection('');");
            link3.setAttribute("href", "/pilot/navigateBook.do?operation=gotoChapterOperation&chapterId=" + contributionObj.chapter);
            var textNode4 = doc.createTextNode(top.trimLastWord(contributionObj.chapterDisplayName, 45));
            link3.appendChild(textNode4);
            cell4.appendChild(link3);

            var textNode5 = doc.createTextNode(", ");
            cell4.appendChild(textNode5);

            var link4 = doc.createElement("a");
            link4.setAttribute("target", "workArea");
            link4.setAttribute("onclick", "top.setCurrentSection('');");
            link4.setAttribute("href", "/pilot/navigateBook.do?operation=gotoPageOperation&selectedPageId=" + contributionObj.page);
            var textNode6 = doc.createTextNode("Pg. " + contributionObj.page);
            link4.appendChild(textNode6);
            cell4.appendChild(link4);

            row.appendChild(cell1);
            row.appendChild(cell2);
            row.appendChild(cell3);
            row.appendChild(cell4);

            contributionsTableBody.appendChild(row);
         }
      }
      contributionsTable.appendChild(contributionsTableBody);

      contributionsContainerCell.appendChild(contributionsTable);
      contributionsContainerRow.appendChild(contributionsContainerCell)
      contributionsContainerTableBody.appendChild(contributionsContainerRow)
      contributionsContainerTable.appendChild(contributionsContainerTableBody)

      doc.getElementById("recentContributionsDiv").innerHTML = "";
      contributionsDiv.appendChild(contributionsContainerTable);
   }
   else
   {
      contributionsDiv.innerHTML = "";   
      contributionsDiv.innerHTML = "No contributions have been posted yet.";
   }


   //top.debug(contributionsDiv.innerHTML);
}