function Search() {

    this.searchUrl = "/WebSearch";
    this.searchResultsPerPage = 10;
    this.groupSimilarResults = false;
    this.currResultsPage = 1;
    this.queryString = '';
    this.htqString = '';
    this.filter = '';
    this.sortBy = 'time';
    this.results = null;
    this.maxPage = 1;
    this.lastPage = -1;
    this.tags = new Array();
    this.editTagId = -1;
    this.getNewResultsCountInterval = 5000;
    this.newResultsCount = 0;
    this.lastNewResultsCount = -1;
    this.hotTopicsGetSent = false;
    this.source = 'All';
    this.category = 'All';

    this.showPageNumbers = function() {
        var pg = 1;
        var l = this.currResultsPage - 10;
        if (l < 1)
            l = 1;
        var r = l + 20;
        if (r > this.maxPage)
            r = this.maxPage;
        l = r - 20;
        if (l < 1)
            l = 1;
        var pages = $("#search_pages_links td.inner");
        for (var i = 0; i < pages.length; i++)
            pages.eq(i).showHide((i+1) >= l && (i+1) <= r);
        $("#search_prev span").addRemoveClass(this.currResultsPage <= 1, 'gray');
        $("#search_next span").addRemoveClass(this.currResultsPage >= this.maxPage, 'gray');
    }

    this.encodeTagsInUrl = function() {
        var s = '';
        for (var i in this.tags) {
            var tag = this.tags[i];
            if (s != '')
                s += ',';
            s += escape(tag.show + "|" + tag.code);
        }
        return s;
    }

    this.makeSearchUrl = function(pg, sortBy, newQuery) {
        if (!sortBy)
            sortBy = this.sortBy;
        if (newQuery)
            return '?search' +
                '&query=' + escape(this.queryString) +
                '&sortby=' + sortBy +
                '&source=' + this.source +
                '&category=' + this.category;
        else
            return '?search' +
                '&page=' + pg +
                '&q=' + escape(this.queryString) +
                '&sortby=' + sortBy +
                '&source=' + this.source +
                '&category=' + this.category +
                '&tags=' + this.encodeTagsInUrl() +
                '&ctx=' + this.searchId;
    }

    this.createNewPages = function() {
        var res = $('#search_results');
        var toPage = this.currResultsPage + 9;
        if (toPage > this.maxPage)
            toPage = this.maxPage;
        for (var pg = 1; pg <= toPage; pg++) {
            if ($('#search_pages_links_' + pg).length > 0)
                continue;
            $('#search_pages_links td.next').before(
                '<td id="search_pages_links_' + pg + '" class="inner' + (pg == this.currResultsPage ? ' on' : '') + '">' +
                    '<a href="javascript:;" onclick="ShowModal(\'#modal_results_win\', \'#modal_results\')"><span>' + pg + '</span></a>' +
                '</td>');
            res.append('<div id="search_results_page' + pg + '"></div>');
        }
        this.showPageNumbers();
    }

    this.renderRowContent = function(row) {
        var title = row.showTitle;
        var tags = '';

        var allTags = this.tags.concat(row.tags);
        for (var i in allTags) {
            var tagTitle = '';
            if(allTags[i].code.endsWith('11')) {
                tagTitle = 'Show only items at site \'' + allTags[i].show + '\'';
                tags += '<a href="javascript:;" onclick="SearchAddTag(\'' + escape(allTags[i].show) + '\', \'' + escape(allTags[i].code) + '\')" ' +
                    'title="' + tagTitle + '">' + allTags[i].show + '</a>&nbsp;';
            } else if(allTags[i].code.endsWith('22')) {
                var name = MapTagToCategory(allTags[i].show);
                tagTitle = 'Show only items in category \'' + allTags[i].show + '\'';
                if (name != "")
                    tags += '<a href="javascript:;" onclick="wowd.goToCategory(\'' + MapTagToCategory(allTags[i].show) + '\')" ' +
                        'title="'  + tagTitle + '">' + allTags[i].show + '</a>&nbsp;';
                else
                    tags += '<a href="javascript:;" onclick="SearchAddTag(\'' + escape(allTags[i].show) + '\', \'' + escape(allTags[i].code) + '\')" ' +
                        'title="' + tagTitle + '">' + allTags[i].show + '</a>&nbsp;';
            } else {
                tagTitle = 'Show only items with tag \'' + allTags[i].show + '\'';
                tags += '<a href="javascript:;" onclick="SearchAddTag(\'' + escape(allTags[i].show) + '\', \'' + escape(allTags[i].code) + '\')" ' +
                    'title="' + tagTitle + '">' + allTags[i].show + '</a>&nbsp;';
            }
        }

        if (!row.showUrl)
            row.showUrl = row.url;

        var subject = 'Re: ' + row.title;
        var body = escape('Here is something I found via Wowd (www.wowd.com):\n\n' + row.url);
        var content =
            '<table width="100%" cellpadding="0" cellspacing="0"><tr>' +
            '<td align="left">' +
                '<div class="title"><a href="' + row.url + '" target="_blank" title="' + row.url + '" onClick="visitPage(\'' + row.url + '\',\'search\');">' + title + '</a></div>' +
                '<div class="snippet">' + row.abs + '</div>' +
                '<div>' +
                    '<span class="timestamp" title="Last viewed: ' + row.lastViewed + ', Last indexed: ' + row.timestamp + '">' + row.lastViewed + '</span>' +
                    '&nbsp;&nbsp;&nbsp;' +
                    '<span class="actions">' +
                        '<a href="mailto:?subject=' + subject + '&body=' + body + '"><img src="gfx/email.gif" title="Send to a friend" alt="Send to a friend" height="16" width="16"></a>' +
                        '<a href="javascript:;" onclick="ShowModal(\'#modal_sharing_win\', \'#modal_sharing\')"><img src="gfx/twitter-action.gif" title="Share this via Twitter" alt="Share this via Twitter" width="16" height="16" /></a>' +
                        '<a href="http://www.facebook.com/sharer.php?u=' + escape(row.url) + '&t=' + escape(row.title) + '" target="_blank"><img src="gfx/facebook-action.gif" title="Share this via Facebook" alt="Share this via Facebook" width="16" height="16" /></a>' +
                        '<a href="javascript:;" onclick="ShowModal(\'#modal_spam_win\', \'#modal_spam\')"><img src="gfx/thumb_down.gif" title="Report as Inappropriate" alt="Report as Inappropriate" height="16" width="16"></a>' +
                    '</span>' +
                    '&nbsp;&nbsp;&nbsp;' +
                    '<span class="tags">' + tags + '</span>' +
                '</div>' +
            '</td>' +
            '</tr></table>';

        return content;
    }

    this.updateRow = function(row) {
        var name = "#sr" + row.ord + (row.subOrd > 0 ? "_sub" + row.subOrd : "");
        $(name).html(this.renderRowContent(row));
        $(name + ' .actions img')
            .css('opacity', 0.5)
            .mouseover(function() {
               $(this).css('opacity', 1.0);
            })
            .mouseout(function() {
               $(this).css('opacity', 0.5);
            });
    }

    this.renderRow = function(container, row) {
        var name = "sr" + row.ord + (row.subOrd > 0 ? "_sub" + row.subOrd : "");
        container.append(
            '<div id="' + name + '" class="result_row">' +
                this.renderRowContent(row) +
            '</div>'
        );
        $('#' + name + ' .actions img')
            .css('opacity', 0.5)
            .mouseover(function() {
               $(this).css('opacity', 1.0);
            })
            .mouseout(function() {
               $(this).css('opacity', 0.5);
            });
    }

    this.renderRows = function(pageId, rows) {
        var id = (pageId - 1) * this.searchResultsPerPage;
        $('#search_cnt_to').html(id + rows.length);
        var page = $('#search_results_page' + pageId);

        for (var i = 0; i < rows.length; i++) {
            rows[i].ord = ++id;

            if (this.results[id-1] != null) {
                if (this.results[id-1].hasSimilarResults != rows[i].hasSimilarResults) {
                    this.results[id-1].hasSimilarResults = rows[i].hasSimilarResults;
                    this.updateRow(this.results[id-1]);
                }
                continue;
            }

            if (i == 0)
                page.html('');

            this.renderRow(page, rows[i]);

            if (i+1 == this.searchResultsPerPage)
                page.append('<div id="finished_page' + pageId + '" style="display:none;"></div>');

            while (this.results.length < id)
                this.results.push(null);
            this.results[id-1] = rows[i];

            if (this.gfxEffects)
                $('#sr' + id).show("fast");
            else
                $('#sr' + id).show();
        }
    }

    this.updateNewResultsCount = function(data) {
        if (data.infoNewResults)
            $("#search_cnt2").text(data.infoNewResults).show();
        if (data.count < 0)
            return;
        if (data.count == this.lastNewResultsCount)
            return;
        this.lastNewResultsCount = data.count;
        var s = data.count <= 100 ? ('<strong>' + data.count + ' new</strong> since your last search') : 'More than 100 new since your last search';
        $("#search_info_new_results span").fadeOut(function() {
            if (data.count > 0)
                $(this).html('<a href="javascript:;" onclick="ShowModal(\'#modal_search_stream_win\', \'#modal_search_stream\')">' + s + '</a>').fadeIn()
            else
                $(this).html(s).fadeIn()
        });
    }

    this.getNewResultsCount = function() {
        var n = Math.random();
        if (n > 0.7) this.newResultsCount++;
        if (n > 0.8) this.newResultsCount++;
        if (n > 0.9) this.newResultsCount++;
        if (n > 0.7) this.updateNewResultsCount({ count: this.newResultsCount });
    }

    this.renderInfoMessage = function(id, msg) {
        msg += '&nbsp;<strong></strong>';
        if (this.queryString != '' && this.tags.length > 0)
            msg += '&nbsp;and';
        if (this.tags.length == 1)
            msg += '&nbsp;tag <strong></strong>';
        else if (this.tags.length > 1)
            msg += '&nbsp;tags <strong></strong>';
        if (this.category != 'All')
            msg += '&nbsp;in category <strong>' + this.category + '</strong>';
        $(id).html(msg + $(id).html());
        $(id + ' strong').eq(0).text(decodeURIComponent(this.queryString));
        if (this.tags.length > 0) {
            msg = '';
            for (var i in this.tags) {
                if (msg != '')
                    msg += ', ';
                msg += this.tags[i].show;
            }
            $(id + ' strong').eq(1).text(msg);
        }
    }

    this.showResults = function(data) {
        var first = this.results.length == 0;
        this.renderRows(data.page, data.results);
        if (first) {
            $('#search_cnt').show();
            $('#search_status').show();
            $('#search_results').show();
            $('#search_pages_links').show();
            $('.sortbar').show();
            this.renderInfoMessage('#search_status_msg', 'Your results for')

            if (this.sortBy == 'time')
                this.tidGetNewResultsCount = setInterval( "search.getNewResultsCount()", this.getNewResultsCountInterval);
        }
    }

    this.showSimilarResults = function(idx) {
        var row = this.results[idx-1];
        if (row.subResults && row.subResults.length > 0) {
            for (var i = 0; i < row.subResults.length; i++)
                $('#sr' + idx + '_sub' + (i+1)).show();
            $("#sr" + idx + " cite.similar").html('<a href="javascript:search.hideSimilarResults(' + idx + ')">Hide similar results »</a>');
            return;
        }

        $.getJSONExt(this.searchURL, {ctx : this.searchId, idx: idx},
            function(data) {
                if (!data.idx || !data.results)
                    return;
                search.results[idx-1].subResults = data.results;
                var container = $('#sr' + idx);
                for (var i = 0; i < data.results.length; i++) {
                    var row = data.results[i];
                    var subOrd = i+1;
                    row.ord = idx;
                    row.subOrd = subOrd;
                    container.after(
                        '<div id="sr' + idx + '_sub' + subOrd + '" class="result_row" style="padding-left:20px">' +
                            search.renderRowContent(row) +
                        '</div>'
                    );
                    $('#sr' + idx + '_sub' + subOrd + ' .actions img')
                        .css('opacity', 0.5)
                        .mouseover(function() {
                           $(this).css('opacity', 1.0);
                        })
                        .mouseout(function() {
                           $(this).css('opacity', 0.5);
                        });
                }
                $("#sr" + idx + " cite.similar").html('<a href="javascript:search.hideSimilarResults(' + idx + ')">Hide similar results »</a>');
            },
            function() {
                search.backendError();
            }
        );
    }

    this.hideSimilarResults = function(idx) {
        var row = this.results[idx-1];
        for (var i = 0; i < row.subResults.length; i++)
            $('#sr' + idx + '_sub' + (i+1)).hide();
        $("#sr" + idx + " cite.similar").html('<a href="javascript:search.showSimilarResults(' + idx + ')">Show similar results »</a>');
    }

    this.dataReady = function(data) {
        this.searchId = data.ctx;

        $('#search_running').hide();
        $('#search_cnt_total').text(data.trackerTotal);

        this.maxPage = Math.ceil(data.trackerTotal / this.searchResultsPerPage);
        this.createNewPages();

        if (data.page && data.results.length > 0)
            this.showResults(data);

        if ((data.networkProblems != undefined) && data.networkProblems) {
            ShowModal('#modal_visits_win', '#modal_visits');
            return;
            $('#search_results').hide();
            $('#search_cnt').hide();
            $('#search_pages_links').hide();
            $('#search_running').hide();
        }

        if (!data.finished) {
            if (data.results.length < this.searchResultsPerPage)
                this.sendQuery();
        } else {
            if (data.lastPage) {
                this.lastPage = data.lastPage;
                if (this.maxPage > this.lastPage)
                    this.maxPage = this.lastPage;
                if (this.maxPage < this.currResultsPage)
                    this.showResultsPage(this.maxPage);
                this.showPageNumbers();
            }
            if (data.page == 1 && this.results.length == 0) {
                this.renderInfoMessage('#search_info', 'No results found for your search');
                $("#search_info").show();
                $('#search_results').show();
                $('#search_cnt').hide();
                $('#search_pages_links').hide();
                $(".sortbar").hide();
            }
        }
        if (!this.hotTopicsGetSent && this.results.length > 0) {
            this.getHotTopics();
            this.hotTopicsGetSent = true;
        }
    }

    this.sendQuery = function() {
        var args;
        if (this.searchId)
            args = {
                ctx : this.searchId,
                page: this.currResultsPage,
                query : this.queryString,
                sortby : this.sortBy,
                source : this.source,
                category : this.category
            };
        else
            args = {
                q: this.queryString,
                sortby: this.sortBy,
                source : this.source,
                category : this.category
            };
        if (this.filter != '')
            args.filter = this.filter;

        $('#search_running').show();
        $.getJSONExt(this.searchUrl, args, function(data) {search.dataReady(data);}, function() {search.finishQuery();});
    }

    this.backendError = function() {
        this.finishQuery();
        $('#search_running').hide();
    }

    this.closeQuery = function() {
        if (!this.searchId)
            return;
        this.searchId = false;
    }

    this.finishQuery = function() {
        if (this.lastPage < 0)
            this.lastPage = this.maxPage;
    }

    this.query = function() {
        this.closeQuery();
        this.queryString = $('#search_box').val();
        
        var s = '';
        for (var i in this.tags) {
            if (s != '')
                s += ';';
            s += this.tags[i].code;
        }
        this.filter = s;

        this.searchId = null;
        this.currResultsPage = 1;
        this.results = new Array();

        $('#search_results').html('');
        $('#search_pages_links td.inner').remove();
        $('#search_pages_links').hide();
        $('#search_info').hide();
        $('#search_cnt').hide();
        $('#search_cnt_from').text('1');
        $('#search_cnt_to').text('1');
        $('#search_info_new_results').showHide(this.sortBy == 'time');
        $('#search_status').hide();
        $('#search_running').show();
        $('#search_results').hide();
        $('.sortbar').hide();

        this.maxPage = 1;
        this.lastPage = -1;
        this.createNewPages();
        this.sendQuery();

        return false;
    }

    this.searchString = function(s) {
        $('#search_box').val(s);
        this.query();
    }

    this.showTags = function() {
        var s = '';
        for (var i in this.tags) {
            s +=
                '&nbsp;<span>' + this.tags[i].show + '</span>' +
                '<a href="javascript:search.removeTag(\'' + escape(this.tags[i].code) + '\')" ' +
                    'id="hide-tagsample" class="remove" title="Remove this tag">x</a>';
        }
        if (s != '')
            $("#search_tags").show().html(s);
        else
            $("#search_tags").hide();
    }

    this.addTag = function(tagShow, tagCode) {
        for (var i in this.tags)
            if (this.tags[i].code == tagCode)
                return;
        this.tags.push({show: tagShow, code: tagCode});
        this.showTags();
        location.href = 'index.jsp' + this.makeSearchUrl(this.currResultsPage, this.sortBy, true) + '&tags=' + this.encodeTagsInUrl();
    }

    this.removeTag = function(code) {
        var l = new Array();
        for (var i in this.tags)
            if (this.tags[i].code != code)
                l.push(this.tags[i]);
        this.tags = l;
        this.showTags();
        if (this.tags.length == 0 && this.queryString == '')
            location.href = 'index.jsp?source=' + this.source + '&category=' + this.category;
        else 
            location.href = 'index.jsp' + this.makeSearchUrl(this.currResultsPage, this.sortBy, true) + '&tags=' + this.encodeTagsInUrl();
    }

    this.showResultsPage = function(page) {
        $('#search_results_page' + this.currResultsPage).hide();
        $('#search_results_page' + page).show();
        $('#search_pages_links_' + this.currResultsPage).removeClass('on');
        $('#search_pages_links_' + page).addClass('on');
        this.currResultsPage = page;
        var fromCnt = (page - 1) * this.searchResultsPerPage + 1;
        var toCnt = page * this.searchResultsPerPage;
        if (toCnt > this.results.length)
            toCnt = this.results.length < fromCnt ? fromCnt : this.results.length;
        $('#search_cnt_from').text(fromCnt);
        $('#search_cnt_to').text(toCnt);
        this.showPageNumbers();
        if (!$('#finished_page' + page).length)
            setTimeout(this.sendQuery(),400);
    }

    this.runSearch = function() {
        var query = $('#search_box').val().trim();
        if (query == '' && this.tags.length == 0)
            return;
        this.closeQuery();
        var tags = this.encodeTagsInUrl();
        if (tags != '')
            tags = '&tags=' + tags;
        var url = 'index.jsp?search&query=' + escape(query) + tags + "&sortby=" + this.sortBy + "&category=" + this.category;
        setTimeout("location.href='" + url + "'", 100);
        return false;
    }

    this.runSortBy = function(sortBy) {
        if (this.sortBy == sortBy)
            return;
        this.sortBy = sortBy;
        this.runSearch();
    }

    this.runEmptySearch = function() {
        hot.category = this.category;
        hot.getHotTopics();
        hot.tidHotTopics = setInterval("hot.getHotTopics();", 180000);

        $(".topics_cloud_search").hide();
        $("#search_sortbar").hide();
        $("#search_running").hide();
        $(".search_empty").show();
        $("#search_box").focus();
    }

    this.clear = function() {
        wowd.goToHotlist(this.category);
    }

    this.goToCategory = function(category) {
        if (this.category == category)
            return;
        
        this.closeQuery();
        this.category = category;
        this.queryString = $('#search_box').val();
        location.href = 'index.jsp' + this.makeSearchUrl(this.currResultsPage, this.sortBy, true) + '&tags=' + this.encodeTagsInUrl();
    }

    this.goToSource = function(source) {
        if (source == this.source)
            return;

        this.closeQuery();
        this.source = source;
        this.queryString = $('#search_box').val();
        location.href = 'index.jsp' + this.makeSearchUrl(this.currResultsPage, this.sortBy, true) + '&tags=' + this.encodeTagsInUrl();
    }

    this.onShow = function() {
    }

    this.onHide = function() {
    }

    this.init = function() {
    }

    this.close = function() {
        this.closeQuery();
    }

    this.parseTags = function(s) {
        this.tags = new Array();
        var tags = s.split(",");
        for (var i in tags) {
            var tag = tags[i];
            var l = tag.split("|");
            if (l.length == 2)
                this.tags.push({show: l[0], code: l[1]});
        }
        this.showTags();
    }

    this.renderAd = function(ad) {
        var s =
            '<div class="ad">' +
                '<div class="title">' +
                    '<a href="' + ad.url + '" onClick="visitPage(\'' + ad.url + '\',\'ads\')" target="_blank">' + ad.title + '</a>' +
                '</div>' +
                '<div class="snippet">' +
                    ad.desc +
                '</div>' +
                '<div class="url">' +
                    '<a href="' + ad.url + '" onClick="visitPage(\'' + ad.url + '\',\'ads\')" target="_blank">' + ad.showURL + '</a>' +
                '</div>' +
            '</div>';
        return s;
    }
    
    this.showAds = function(data) {
        if (data.ads.length == 0)
            return;
        var s = '';
    	for (i in data.ads)
            s += this.renderAd(data.ads[i]);
        $("#ads_cont").html(s);
        $(".segment_ads").fadeIn();    	
    }
    
    this.getHotTopics = function() {
        var sort = (this.sortBy == "rank" ? 0 : 1);
        $('#topics_box h2').text('Hot Topics For ' + this.queryString)
            .attr('title', 'This is a tag cloud of the hot topics contained in the pages that matched your search');
        $.getJSONExt('/HotTopics', {t : sort, q: this.queryString, category: this.category, type: 's'}, function(data) {
            search.showHotTopics(data);
        });
    }

    this.showHotTopics = function(data) {
        var container = $('#topics_cloud');
        var topics = data.topics;
        var content = '';
        if (topics.length == 0) {
            content = '<span>No hot topics in your search.</span>';
        } else {
            var terms = [], v, s, values = [];
            for (t in topics) {
                v = parseFloat(topics[t].weight);
                s = topics[t].topic;
                terms.push( {
                    topic : s,
                    value : v
                });
                values.push(v);
            }
            terms = terms.sort(function(f, s) {
                if (f.topic < s.topic)
                    return -1;
                if (f.topic == s.topic)
                    return 0;
                return 1;
            });

            values = values.sort();
            var min = values[0], max = values[values.length - 1];

            var add = (min < 0 ? Math.abs(min) : 0);
            max = (max + add + 1.1);

            min = 999999999999, max = -1;
            for (t in terms) {
                terms[t].value = (add + terms[t].value + 1.1) + 1;
                if(terms[t].value < min)
                    min = terms[t].value;
                if(terms[t].value > max)
                    max = terms[t].value;
            }

            var nmax = 0, nmin = 1;
            for (t in terms) {
                terms[t].value = terms[t].value / (max+min);
                if(terms[t].value < nmin)
                    nmin = terms[t].value;
                if(terms[t].value > nmax)
                    nmax = terms[t].value;
            }

            min = nmin;
            max = nmax;
            var id = 0;
            for (t in terms) {
                var topic = terms[t].topic, weight = terms[t].value, fontSize = this
                .getFontSize(min, max, weight), color = this.getColor(min, max,
                    weight);
                content += 
                    '<span id="ht' + id + '" class="cloud_topic_item" style="font-size:' + fontSize + 'px; color:' + color + ';" onmouseover="hot.showHidePlus(' + id + ', true)" onmouseout="hot.showHidePlus(' + id + ', false)" onClick="visitPage(\'' + topic + '\',\'tag\')">' +
                        this.generateSearchLink(topic, id) +
                        this.generateSearchLink2(topic, id) +
                    '</span>';
                id++;
            }
        }
        $("#topics_info").fadeOut('slow', function(){container.hide().html(content).fadeIn('slow')});
    }

    this.getFontSize = function(min, max, val) {
        var minFontSize = 10, maxFontSize = 18;
        var unit = 1 / (max - min);
        var diff;
        if((max - min) <= 1)
            diff = min * unit;
        else
            diff = min / unit;
        if (min == max)
            return minFontSize + (maxFontSize - minFontSize) / 2;
        if(val == min)
            return minFontSize;
        if(val == max)
            return maxFontSize;
        return minFontSize + (((val*unit) - diff) * (maxFontSize - minFontSize));
    }

    this.getColor = function(min, max, val) {
        return 'black';
    }
    this.showHidePlus = function(id, showHide) {
        //$("#ht" + id + " a").eq(1).css('color', showHide ? "#fc8a00" : "#fff");
    }

    this.generateSearchLink = function(topic, id) {
        return '<a href="index.jsp?search&query=' + escape(topic) + '&sortby=' + this.sortBy + '&category=' + this.category + '">' + topic + '</a>';
    }

    this.generateSearchLink2 = function(topic, id) {
        return '<a href="index.jsp?search&query=' + escape(topic) + '&sortby=' + this.sortBy + '&category=' + this.category + '" style="color:#fff;padding-left:2px;" title="Add this term to current search query">+</a>';
    }

    this.run = function() {
        if (HasArgument('sortby'))
            this.sortBy = GetArgumentSortBy();

        if (HasArgument('category'))
            this.category = GetArgumentCategory();
        else
            this.category = 'All';

        var e = $('.bar #' + this.category);
        if (e.length == 0) {
            this.category = 'All'
            e = $('.bar #' + this.category);
        }
        e.addClass('sel');
        e.parent().prev().hide();
        e.parent().next().hide();

        if (HasArgument('source'))
            this.source = GetArgumentSource() == 'News' ? 'News' : 'All';
        else
            this.source = 'All';

        if (this.source == 'All') {
            $('#search_show_feed_all').addClass('sel');
            $('#search_show_feed_all td').eq(0).addClass('selimg');
        } else {
            $('#search_show_feed_news').addClass('sel');
            $('#search_show_feed_news td').eq(0).addClass('selimg');
        }

        if (this.sortBy == 'time') {
            $('#search_show_scope_time').addClass('sel');
            $('#search_show_scope_time td').eq(0).addClass('selimg');
        } else {
            $('#search_show_scope_rank').addClass('sel');
            $('#search_show_scope_rank td').eq(0).addClass('selimg');
        }
        $("#search_show_scope").show();

        if (HasArgument('tags'))
            this.parseTags(GetArgumentTags());
        if (HasArgument('htq'))
            this.htqString = GetArgumentQuery('htq').trim();
        if (HasArgument('query') && (GetArgumentQuery('query').trim() != '' || this.tags.length > 0))
            this.searchString(GetArgumentQuery('query'));
        else
            this.runEmptySearch();

        if (HasArgument('query')) {
            $.getJSONExt('/a', { q : this.queryString, cat: this.category },
                function(data) {
                    search.showAds(data);
                },
                function() {}
            );
        }
        
        if (this.queryString != '' || this.tags.length > 0)
            $("#search_clear").show();
        else {
            $("#search_box").css('color', '#999').val('Search Web...').focus(function(){
                $(this).css('color', '#000').val('');
            });
        }
    }
}

var search = new Search();

function SearchAddTag(tagShow, tagCode) {
    search.addTag(tagShow, tagCode);
}
function SearchClear() {
    search.clear();
}
