﻿var contentsearch = function() {
    this.keyword = null;
    this.displaycount = 6;
    this.locationId = 1;
    this.jsonservice = new JSONService();
    this.isExactMatch = false;
    this.gup = function(name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.href);
        if (results == null) {
            return ""
        }
        else {
            return results[1];
        }
    };
    this.fetch = function() {
        this.keyword = this.gup("keyword");
        
        if ($.query.get('locationId')) {
            contentsearch.locationId = $.query.get('locationId');

        }
        
        if (this.keyword !== "") {
            if($.query.get('searchClause') == 'exactmatch')
            {
                document.getElementById("topsearchkeyword").value = '"' + this.keyword + '"' ;
                this.isExactMatch = true;
            }
            else
            {
                document.getElementById("topsearchkeyword").value = this.keyword;
            }
            mycontentsearch.showProcessing();
            this.jsonservice.loadScript("contentsearchfetch_0001", "services/content.aspx?action=fetchsearchresults&keyword=" + this.keyword + "&searchClause=or&callback=mycontentsearch.render&expires=5");
        }
    };
    
    this.showProcessing = function()
    {
        var arrHtml = []; 
          
        arrHtml[arrHtml.length] = '<table border="0" cellpadding="4" cellspacing="0" width="100%" height="100">';
        arrHtml[arrHtml.length] = '<tr>';
        arrHtml[arrHtml.length] = '     <td valign="middle" align="center">';
        arrHtml[arrHtml.length] = '         <span class="text">Search is being processed. Please Wait...</span><br/><br/><img src="assets/rotation.gif" border="0" />';
        arrHtml[arrHtml.length] = '     </td>';
        arrHtml[arrHtml.length] = '</tr>';
        arrHtml[arrHtml.length] = '</table>';
    
        document.getElementById('contentsearch1').style.display = 'none';
        document.getElementById('divProcessing').style.display = '';
        document.getElementById('divProcessing').innerHTML = arrHtml.join('');
    };
    
    this.render = function(strJSON) {
        var objJson = eval("(" + strJSON + ")");
        var field = null;
        var strHtml = [];
        var intAllCount = 0;
        var strBGColor = "white";
        var i = 0;
        //sameple row
        //{rows:[{category:"other",field:"content",id:"1",title:"About Us - Staff",link:"page.htm?id=1",hypertext:""}]}
        var divContentSearch = document.getElementById("contentsearch1");
        strHtml[strHtml.length] = '<table cellspacing="0" cellpadding="10" border="0" width="100%">';
        strHtml[strHtml.length] = '<tr><td>';

        //let's create news search results category first
        strHtml[strHtml.length] = '<table id="alltable" style="border: solid 1px silver;" cellspacing="0" cellpadding="0" border="0" width="100%">';
        strHtml[strHtml.length] = '     <tr><td nowrap colspan="2"><div class="search-results">' + objJson.rows.length + ' Item(s) found for <b>' + this.keyword + '</b></div></td></tr>';
        for (i = 0; i < objJson.rows.length; i++) {
            intAllCount++;
            strBGColor = (i % 2 == 0) ? "white" : "#DDDDDD";
            if (intAllCount < this.displaycount) {
                if(contentsearch.locationId == 1)
                {
                    strHtml[strHtml.length] = '    <tr style="background-color:' + strBGColor + ';"><td style="padding:5px" align="right">' + eval(i + 1) + '.</td><td style="padding:5px"><a href="' + objJson.rows[i].link + '&keyWord='+ this.keyword +'&chkExact=flase" alt="" title="' + objJson.rows[i].title + '">' + objJson.rows[i].title + '</a></td></tr>';
                }
                else
                {
                    strHtml[strHtml.length] = '    <tr style="background-color:' + strBGColor + ';"><td style="padding:5px" align="right">' + eval(i + 1) + '.</td><td style="padding:5px"><a href="javascript:" onclick="alert(\'You are moving away from the Pencil Exchange to the Pencil New York website.\');window.open(\'' + objJson.rows[i].link + '&keyWord='+ this.keyword +'&chkExact=flase\',\'contentwindow\');" alt="" title="' + objJson.rows[i].title + '">' + objJson.rows[i].title + '</a></td></tr>';
                }
                if (objJson.rows[i].normaltext.length > 0) {
                    strHtml[strHtml.length] = '    <tr style="background-color:' + strBGColor + ';"><td></td><td style="padding:5px">' + objJson.rows[i].normaltext.substring(0, 230) + '...</td></tr>';
                }
            }
            else {
                if(contentsearch.locationId == 1)
                {
                    strHtml[strHtml.length] = '    <tr style="background-color:' + strBGColor + ';" style="display:none;"><td style="padding:5px" align="right">' + eval(i + 1) + '.</td><td style="padding:5px"><a href="' + objJson.rows[i].link + '&keyWord='+ this.keyword +'&chkExact=false" alt="" title="' + objJson.rows[i].title + '">' + objJson.rows[i].title + '</a></td></tr>';
                }
                else
                {
                    strHtml[strHtml.length] = '    <tr style="background-color:' + strBGColor + ';" style="display:none;"><td style="padding:5px" align="right">' + eval(i + 1) + '.</td><td style="padding:5px"><a href="javascript:" onclick="alert(\'You are moving away from the Pencil Exchange to the Pencil New York website.\');window.open(\'' + objJson.rows[i].link + '&keyWord='+ this.keyword +'&chkExact=flase\',\'contentwindow\');" alt="" title="' + objJson.rows[i].title + '">' + objJson.rows[i].title + '</a></td></tr>';
                }
                
                if (objJson.rows[i].normaltext.length > 0) {
                    strHtml[strHtml.length] = '    <tr style="background-color:' + strBGColor + ';" style="display:none;"><td></td><td style="padding:5px">' + objJson.rows[i].normaltext.substring(0, 230) + '...</td></tr>';
                }
            }
        }
        strHtml[strHtml.length] = '    <tr><td style="padding:5px" colspan="2"><a id="allcountshowall" style="display:none;font-weight:normal" href="javascript:mycontentsearch.allshowall();">Show All</a></td>';
        strHtml[strHtml.length] = '</table>';
        strHtml[strHtml.length] = '<br />';

        document.getElementById('contentsearch1').style.display = '';
        divContentSearch.innerHTML = strHtml.join('');
        
        if (intAllCount > this.displaycount) {
            document.getElementById("allcountshowall").style.display = "block";
        }
        
        if(this.keyword != "")
        {
            highLightSearch.highlightSearchTerms(this.keyword,"contentsearch1",this.isExactMatch);
        }
        document.getElementById('divProcessing').style.display = 'none';
    };
    this.allshowall = function() {
        var objTable = document.getElementById("alltable");
        var objTrs = objTable.rows;
        if(document.getElementById("allcountshowall").innerText=="Show All")
        {
            for (var i = 0; i < objTrs.length; i++) {
                if (objTrs[i].style.display == "none") {
                    objTrs[i].style.display = "block";
                }
            }
            document.getElementById("allcountshowall").innerText = "Show Top 5";
        }
        else
        {
            for (var i = 0; i < objTrs.length; i++) {
                if (i<(this.displaycount*2)-1) 
                {
                    objTrs[i].style.display = "";
                }
                else
                {
                   objTrs[i].style.display = "none";
                }
            }
            document.getElementById("allcountshowall").innerText = "Show All";
        }
        //document.getElementById("allcountshowall").style.display = "none";
    };
};
var mycontentsearch = new contentsearch();
mycontentsearch.fetch();
