  $(document).ready(function() {
          $('textarea.tinymce').tinymce({
            script_url : '/themes/quoralike/scripts/tinymce/jscripts/tiny_mce/tiny_mce.js',

            // General options
            theme : "advanced",
            plugins : "pagebreak,style,layer,table,save,advhr,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

            // Theme options
            theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,bullist,numlist,blockquote,|,link,unlink,image",
            theme_advanced_buttons2: "",
            theme_advanced_buttons3: "",
            theme_advanced_toolbar_location : "top",
            theme_advanced_toolbar_align : "left",
            theme_advanced_statusbar_location : "bottom",
            theme_advanced_resizing : true,
            theme_advanced_resizing_use_cookie : false,
            width: "100%"
          });
//    $('div.expander(0)').expander();
    $('div.expander(0)').expander({
        slicePoint:       150,              // the number of characters at which the contents will be sliced into two parts.
                                            // Note: any tag names in the HTML that appear inside the sliced element before
                                            // the slicePoint will be counted along with the text characters.
        widow:            4,                // a threshold of sorts for whether to initially hide/collapse part of the element's contents.
                                            // If after slicing the contents in two there are fewer words in the second part than
                                            // the value set by widow, we won't bother hiding/collapsing anything.
        expandText:       'read more',   // text displayed in a link instead of the hidden part of the element.
                                            // clicking this will expand/show the hidden/collapsed text
        expandPrefix:     '',               // text to come before the expand link
        collapseTimer:    0,                // number of milliseconds after text has been expanded at which to collapse the text again
        expandEffect:     'fadeIn',
        expandSpeed:      '',               // speed in milliseconds of the animation effect for expanding the text
        userCollapse:     true,             // allow the user to re-collapse the expanded text.
        userCollapseText: 'less',        // text to use for the link to re-collapse the text
        userCollapsePrefix: '',             // text to come before the re-collapse link
        beforeExpand: function($thisElement) {
//            $thisElement.parent().parent().append('');
        },
        afterExpand: function($thisElement) {
//            $thisElement.parent().parent().append('');
        },
        onCollapse: function($thisElement, byUser) {
            if (byUser === true) {
//                  $thisElement.parent().parent().append('');
            } else {
//                  $thisElement.parent().parent().append('');
            }
        }
    });
      $('div.expander-topic(0)').expander({
        slicePoint:       250,              // the number of characters at which the contents will be sliced into two parts.
                                            // Note: any tag names in the HTML that appear inside the sliced element before
                                            // the slicePoint will be counted along with the text characters.
        widow:            4,                // a threshold of sorts for whether to initially hide/collapse part of the element's contents.
                                            // If after slicing the contents in two there are fewer words in the second part than
                                            // the value set by widow, we won't bother hiding/collapsing anything.
        expandText:       'read more',   // text displayed in a link instead of the hidden part of the element.
                                            // clicking this will expand/show the hidden/collapsed text
        expandPrefix:     '',               // text to come before the expand link
        collapseTimer:    0,                // number of milliseconds after text has been expanded at which to collapse the text again
        expandEffect:     'fadeIn',
        expandSpeed:      '',               // speed in milliseconds of the animation effect for expanding the text
        userCollapse:     true,             // allow the user to re-collapse the expanded text.
        userCollapseText: 'less',        // text to use for the link to re-collapse the text
        userCollapsePrefix: '',             // text to come before the re-collapse link
        beforeExpand: function($thisElement) {
    //            $thisElement.parent().parent().append('');
        },
        afterExpand: function($thisElement) {
    //            $thisElement.parent().parent().append('');
        },
        onCollapse: function($thisElement, byUser) {
            if (byUser === true) {
    //                  $thisElement.parent().parent().append('');
            } else {
    //                  $thisElement.parent().parent().append('');
            }
        }
    });
  });

    $(document).ready(function() {
        var $askQuestion = $("input#askQuestion").autocomplete({
            highlight: function(match, keywords) {
                keywords = keywords.split(' ').join('|');
                return match.replace(new RegExp("("+keywords+")", "gi"),'<b>$1</b>');
            },
            source: function(request, response) {
                  var query = request.term;

                  if (!/\s$/.test(query)) {
                        query += "*";
                  }

                $.ajax({
                    url: '/search.json',
                    dataType: 'json',
                    minLength: 0,
                    delay: 30,
                    data: { q: query, page: 1, pageSize: 7, type:"question OR user OR topic" },
                    success: function(data) {
                        var $results = $.map(data.searchResults.results, function(item, index) {
                            if (item != null) {
                                if (item.type == 'question') {
                                    return {
                                        label: item.title.replace(
											new RegExp(
												"(?![^&;]+;)(?!<[^<>]*)(" +
												$.ui.autocomplete.escapeRegex(request.term) +
												")(?![^<>]*>)(?![^&;]+;)", "gi"
											), "<strong>$1</strong>" ),
//                                        label: item.title,
                                        url: "/questions/%7Bid%7D/%7Bplug%7D.html", //item.id
                                        value: item.id,
                                        plug: item.plug,
                                        type: "Question"
                                    };
                                } else if (item.type == 'topic') {
                                   return {
                                       label: item.name.replace(
											new RegExp(
												"(?![^&;]+;)(?!<[^<>]*)(" +
												$.ui.autocomplete.escapeRegex(request.term) +
												")(?![^<>]*>)(?![^&;]+;)", "gi"
											), "<strong>$1</strong>" ),
//                                        label: item.name,
                                        url: "/topics/%7Bid%7D.html", //item.id
                                        value: item.name,
                                        plug: item.plug,
                                        type: "Topic"
                                    };
                                } else if (item.type == 'user') {
                                   return {
                                        label: item.username.replace(
											new RegExp(
												"(?![^&;]+;)(?!<[^<>]*)(" +
												$.ui.autocomplete.escapeRegex(request.term) +
												")(?![^<>]*>)(?![^&;]+;)", "gi"
											), "<strong>$1</strong>" ),
                                        url: "/users/%7Bid%7D/%7Bplug%7D.html", //item.id
                                        value: item.id,
                                        plug: item.plug,
                                        type: "User",
                                        emailHash: item.emailHash
                                    };
                                }
                            }
                        });
                        $results.push({
                          label: "Search " + "\"" + $.trim(request.term) + "\"",
                          url: "/search.html?q={query}",
                          query: request.term,
                          value: request.term
                        });
                        response($results);
                    }
                });
            },
            select: function( event, ui ) {
                var url = ui.item.url.replace("%7Bid%7D", ui.item.value);
                url = url.replace("%7Bplug%7D", ui.item.plug) ;
                if (ui.item.query) {
                    url = url.replace("%7Bquery%7D", ui.item.query);
                }
                window.location = url;
                return false;
            },
        });

        if ($askQuestion.length) {
            $askQuestion.data( "autocomplete" )._renderItem = function( ul, item ) {
                return $( "<li></li>" )
				.data( "item.autocomplete", item )
				.append( "<a>"+ (item.type == 'User' ? '<img class="gravatar" align="left" width="16" height="16" style="margin-right: 5px;" src="http://www.gravatar.com/avatar/' + item.emailHash + '?s=16&amp;d=identicon&amp;r=PG"/>' : '') + " " + item.label + " " + (item.type == "Topic" ? "<span class='autocomplete-type'>Topic</span>" : '') + "</a>" )
				.appendTo( ul );
		    };
        }
    });

    function fetchMore(url, divToInsert) {
//       var m = window.location.search.match(/page=(\d+)/);
//       var curPage = m ? parseInt(m[1]) : 1;
       curPage = ++curPage;
       $("#progress-bar").toggle();

       var loading = false;
        $.ajax({
           type: "GET",
           dataType: "html",
           url: url + "" + (url.indexOf("?") == -1 ? "?" : "&") + "page=" + (curPage) + "&pageSize=" + curPageSize + "&sort="+ curSort,
           success: function( html ){
               html = $.trim( html );

               if ( html ) {
                   divToInsert.append( html );
                   $("#progress-bar").toggle();
                   $('div.expander(0)').expander({
                        slicePoint:       150,              // the number of characters at which the contents will be sliced into two parts.
                                                            // Note: any tag names in the HTML that appear inside the sliced element before
                                                            // the slicePoint will be counted along with the text characters.
                        widow:            4,                // a threshold of sorts for whether to initially hide/collapse part of the element's contents.
                                                            // If after slicing the contents in two there are fewer words in the second part than
                                                            // the value set by widow, we won't bother hiding/collapsing anything.
                        expandText:       'read more',   // text displayed in a link instead of the hidden part of the element.
                                                            // clicking this will expand/show the hidden/collapsed text
                        expandPrefix:     '',               // text to come before the expand link
                        collapseTimer:    0,                // number of milliseconds after text has been expanded at which to collapse the text again
                        expandEffect:     'fadeIn',
                        expandSpeed:      '',               // speed in milliseconds of the animation effect for expanding the text
                        userCollapse:     true,             // allow the user to re-collapse the expanded text.
                        userCollapseText: 'less',        // text to use for the link to re-collapse the text
                        userCollapsePrefix: ''             // text to come before the re-collapse link
                    });
               } else
                   curPage = 0;
           },
           error: function() {
           },
           complete: function() {
               loading = false;
           }
       });
    }

