﻿var searchPageSize = 16;
var searchCurPage = 1;

var PostingTickerDelay = 600;
var PostingTickerSpeed=700;
var PostingTickerFirst = 0;
var SearchTickerTimer;

function setupzSearch()
{
   $(".zSearchButon").unbind('click',handlesearchPopup);
   $(".zSearchButton").click(handlesearchPopup);
}

function setupzSearchTicker()
{
    getPostingTickerList();

}

function doTickerAnimate()
{
    PostingTickerFirst = $("ul.postingTickerList li:first").clone(true);
    
    SearchTickerTimer = setInterval(animateTicker,PostingTickerDelay);
}

function animateTicker()
{

    
    //$("ul.postingTickerList li:first").animate({opacity: 0}, PostingTickerSpeed).fadeOut('slow', function () {$(this).remove();});
    animateRemove($("ul.postingTickerList li:first"));
    //PostingTickerBump(PostingTickerFirst);
}

function animateRemove(listItem)
{

    if ($(listItem).find(".postingTickerTitle").html() == "")
    {
        $(listItem).remove();
        PostingTickerBump(PostingTickerFirst);
        animateTicker();
    }
    else
    {
        if ($(listItem).find(".postingTickerTitle").html() != null) {
            $(listItem).find(".postingTickerTitle").html($(listItem).find(".postingTickerTitle").html().substring(1).trim());
        }
    }
}

function PostingTickerBump(titem)
{        
	$('ul.postingTickerList').append(PostingTickerFirst);
	$('ul.postingTickerList li:last').animate({opacity:1}, PostingTickerSpeed).fadeIn('slow');
	PostingTickerFirst = $("ul.postingTickerList li:first").clone(true);
}



function getPostingTickerList()
{

    var modToGet  = postingCtlPath+"GetPosting.aspx?cmd=GetList&ModuleId="+postingModuleId+"&basepath="+DNNBasePath
    
    $.ajax({
            type: "GET",
            url: modToGet,
            dataType: "xml",
            success: showPostingTickerFromFeed,                       
            error: function(req,stat,er){},
            cache: false            
       });   
}


function showPostingTickerFromFeed(xml)
{
    var xmlModuleId;
    var tickerStr="<ul class=\"postingTickerList\">";
      feed = xml;
      $(xml).children("rss").children("channel").each(function() {                  
          var channelTitle = $(this).children("title").text();            
          if (channelTitle == "Postings")
            {                                   
                xmlModuleId = $(this).children("moduleid").text();                                                                
                $(this).children("item").each(function() {                
                    var xmlItemId = $(this).children("itemid").text();
                    var xmlTitle = $(this).children("title").text();                                                                              
                    var xmlItemModuleId = $(this).children("[nodeName=InceptionPoint.PostingFeed:ItemModuleId]").text();                                        
                    var cellid = $(curPostCellId).attr("id")                   
                    var linkstub = "<li><a ";                                     
                    linkstub += "profilerModuleId=\""+xmlItemModuleId+"\" profilerItemId=\""+xmlItemId+"\" postingitemid=\""+xmlItemId+"\" class=\"profileCollect postingPopupLink\">"
                                       
                    tickerStr += linkstub;                                        
                    tickerStr += "<span class=\"postingTickerTitle\">"+xmlTitle+"</span>";                                                          
                    tickerStr +="</a></li>";                    
                    
                });
                tickerStr += "</ul>";
                $("#zPostTickerDiv").html(tickerStr);
                setupPostingPopupLinks();
            }
         });
         doTickerAnimate();
}




function setSearchPagerHandles()
{
  $(".morePagerButton").unbind('click',handleSearchPage);
  $(".morePagerButton").click(handleSearchPage);
  
  $(".morePagerPreviousButton").unbind('click',handleSearchPrevious);
  $(".morePagerPreviousButton").click(handleSearchPrevious);
  
  $(".morePagerNextButton").unbind('click',handleSearchNext);
  $(".morePagerNextButton").click(handleSearchNext);
}

function handleSearchPrevious() {

    $("#mainMoreContent").prepend("<img src=\"images/ajax-loader.gif\" />");
    var ttop = $("#mainMorePopup").offset().top;
    $('html, body').animate({scrollTop:ttop}, 500);
    getPopupSearch(searchCurPage-1);    
}

function handleSearchNext() {

    $("#mainMoreContent").prepend("<img src=\"images/ajax-loader.gif\" />");
    var ttop = $("#mainMorePopup").offset().top;
    $('html, body').animate({scrollTop:ttop}, 500);
    getPopupSearch(searchCurPage+1);    
}

function handleSearchPage() {
    $("#mainMoreContent").prepend("<img src=\"images/ajax-loader.gif\" />");
    var ttop = $("#mainMorePopup").offset().top;
    $('html, body').animate({scrollTop:ttop}, 500);
    getPopupSearch(parseInt($(this).text()));        
}


function handlesearchPopup()
{

    searchCurPage = 1;            
    
    
    
    $("#mainMorePopup").css({top:($(window).scrollTop()+50)+"px"}).show(); 
    $("#mainMorePopup").show();
    $("#mainMoreContent").html("<img src=\"images/ajax-loader.gif\" />");
   
    getPopupSearch(searchCurPage);
    
}



function getPopupSearch(page)
{
    
    $("#mainMorePager").html("&nbsp;");

   searchCurPage= page;
   var modToGet ;
   var query = $(".zSearchQuery").val();
   
   modToGet = zSearchCtlPath +"getSearch.aspx?tabid="+zSearchTabId+"&portalid="+zSearchPortalId+"&profileid="+zSearchProfileId+"&basepath="+searchDNNBasePath+"&pageSize="+searchPageSize+"&pageNum="+searchCurPage+"&query="+query;

   
    $.ajax({
            type: "GET",
            async: true,
            url: modToGet,
            dataType: "xml",
            success: ajshowsearchPopup, 
                                  
            error:tgerror,  
            cache: false
            
       });   
}

function tgerror(xreq, tstat, err) {
    //alert(tstat);
}

function ajshowsearchPopup(xml)
{
    
    parseZSearchStubThumbGridXml(xml, (searchCurPage * searchPageSize) - (searchPageSize-1));        
    showmorePopup();   
}

function parseZSearchStubThumbGridXml(xml,startitem) {
    totalResults = 0;
    feed = xml;
    var postingdivhtm = "<div id=\"mainPostingSearchContent\" class=\"PostingPopupContent\">";
    var postingli = "<ul class=\"PopupPostingList postingWrapper\">";
          var gridhtm="";

          $(xml).children("rss").children("channel").each(function () {

              var channelTitle = $(this).children("title").text();

              var cellTitle = $(this).children("[nodeName=InceptionPoint.SearchFeed:SearchTitle]").text();

              if (cellTitle != "") $("#mainMoreTitle").html(cellTitle);
              var xmlModuleId;


              if (channelTitle == "Posting Feed") {
                  if ($(this).children("item").size() > 0) {
                      xmlModuleId = $(this).children("moduleid").text();

                      var headerli = "<li><div class=\"PostingListTitle\"></div><div class=\"PostingListTerms PostingHeader\">Terms</div><div class=\"PostingListWages PostingHeader\">Wages</div><div class=\"PostingListDate PostingHeader\">Posted</div>";
                      headerli += "<img src=\"images/grid/greydot.png\" style=\"width:100%;height:1px;\"></li>";
                      postingli += headerli;
                      $(this).children("item").each(function () {
                          var xmlItemId = $(this).children("itemid").text();
                          var xmlTitle = $(this).children("title").text();
                          if (xmlTitle != "") {
                              var xmlPostingDate = $(this).children("[nodeName=InceptionPoint.SearchFeed:PostingDateStr]").text();
                              var xmlPostingLocation = $(this).children("[nodeName=InceptionPoint.SearchFeed:Location]").text();
                              var xmlItemModuleId = $(this).children("[nodeName=InceptionPoint.SearchFeed:ItemModuleId]").text();
                              var xmlTerms = $(this).children("[nodeName=InceptionPoint.SearchFeed:Terms]").text();
                              var xmlWages = $(this).children("[nodeName=InceptionPoint.SearchFeed:Wages]").text();
                              var cellid = $(curPostCellId).attr("id")
                              var linkstub = "<a ";

                              linkstub += "profilerModuleId=\"" + xmlItemModuleId + "\" profilerItemId=\"" + xmlItemId + "\" postingitemid=\"" + xmlItemId + "\" class=\"profileCollect postingPopupLink\">"

                              var newli = "<li>";
                              newli += linkstub;

                              newli += "<div class=\"PostingListTitle\">" + xmlTitle + "";
                              newli += "<span class=\"PostingListLocation\">" + xmlPostingLocation + "</span></div>";
                              newli += "<div class=\"PostingListTerms\">" + xmlTerms + "</div>";
                              newli += "<div class=\"PostingListWages\">" + xmlWages + "</div>";
                              newli += "<div class=\"PostingListDate\">" + xmlPostingDate + "</div>";
                              


                              newli += "</a>";
                              newli += "<img src=\"images/grid/greydot.png\" style=\"width:100%;height:1px;\">";
                              newli += "</li>";
                              postingli += newli;
                          }
                      });
                      postingli += "</u>";
                      postingdivhtm += postingli;
                  }

              }





              if (channelTitle == "Search Feed") {
                  var itemiter = 1;
                  var curcol = 1;
                  var tcells = 4
                  gridhtm += "<table class=\"StubThumbGrid\"><tr>";
                  $(this).children("item").each(function () {
                      if ($(this).children("title").text() != "") {
                          //if (itemiter < startitem + searchPageSize && itemiter >= startitem) {
                          totalResults = $(this).children("[nodeName=InceptionPoint.SearchFeed:TotalResults]").text();
                          if (curcol > tcells) {
                              gridhtm += "</tr><tr>";
                              curcol = 1;
                          }
                          gridhtm += "<td class=\"StubThumbSpacer\"></td><td class=\"StubThumbGridContentCell\" id=\"StubThumbGridMoreCell" + itemiter.toString() + "\">";
                          gridhtm += "<div class=\"StubThumbOuterdiv\">";
                          var xmlItemId = $(this).children("itemid").text();
                          var xmlTitle = $(this).children("title").text();
                          var xmlDescription = $(this).children("description").text();
                          //xmlDescription = xmlDescription.substring(0,100);

                          var xmlImag = $(this).children("[nodeName=InceptionPoint.SearchFeed:Image]").text();
                          xmlImag = xmlImag.replace("//", "/").replace("http:/", "http://");
                          var xmlItemType = $(this).children("[nodeName=InceptionPoint.SearchFeed:ItemType]").text();
                          var xmlItemModuleId = $(this).children("[nodeName=InceptionPoint.SearchFeed:ItemModuleId]").text();
                          var linkstub = "<a ";
                          if (xmlItemType == 'zLinks') {
                              linkstub += "profilerModuleId=\"" + xmlItemModuleId + "\" profilerItemId=\"" + xmlItemId + "\" linkitemid=\"" + xmlItemId + "\" class=\"profileCollect zLinkStub\">"
                          }
                          else if (xmlItemType == 'Popup Articles') {
                              linkstub += "profilerModuleId=\"" + xmlItemModuleId + "\" profilerItemId=\"" + xmlItemId + "\" articleitemid=\"" + xmlItemId + "\" class=\"profileCollect articlePopupLink\">"
                          }

                          gridhtm += linkstub + xmlImag + "<div class=\"stubThumbGridTitleText\">" + xmlTitle + "</div><div class=\"stubThumbGridText\">" + xmlDescription + "</a></div>";
                          gridhtm += "</div>";
                          gridhtm += "</td>";
                          curcol++;
                          //}

                          itemiter++;
                      }
                  });
                  gridhtm += "</tr></table>";

              }


              postingdivhtm += "</div>";
              $("#mainMoreContent").html(postingdivhtm);
              $("#mainMoreContent").append("<br/><br/>" + gridhtm);
              //$("#mainMoreContent").html(postingdivhtm + "<br/><br/>" + gridhtm);
              setupMorePopupLinks();
              setupPostingPopupLinks();

              

              var numpages = Math.round(totalResults / morePageSize);

              var pagerHtm = "";

              var curClass = "";
              if (searchCurPage != 1) {
                  pagerHtm += "<a class=\"morePagerPreviousButton\" >Previous</a> ";
              }

              for (i = 1; i <= numpages; i++) {
                  if (i == searchCurPage)
                      pagerHtm += "<span class=\"moreCurPage\" >" + i.toString() + "</span>";
                  else
                      pagerHtm += "<a class=\"morePagerButton\" >" + i.toString() + "</a> ";

              }

              if (searchCurPage < numpages) {
                  pagerHtm += "<a class=\"morePagerNextButton\" >Next</a> ";
              }

              $("#mainMorePager").html(pagerHtm);
              setSearchPagerHandles();

          }); 
		  if (window.adjustTitleSizes()) adjusttitleSizes();
}
