  
function checkEnter(e, sProg){ //e is event object passed from function invocation
    var characterCode //literal character code will be stored in this variable
    
    if(e && e.which){ //if which property of event object is supported (NN4)
      e = e
      characterCode = e.which //character code is contained in NN4's which property
    }
    else{
      e = event
      characterCode = e.keyCode //character code is contained in IE's keyCode property
    }
    
    if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
//      subSearch(sProg,'0')
      return false 
    }
      else{
      return true 
    }
}
 
 
$(function(){
 
//  var searchResult = '<?echo $searchResult;?>';

	$('tabs').tabs({
		linkClass : 'tabs',
		containerClass : 'tab-content',
		linkSelectedClass : 'selected',
		containerSelectedClass : 'selected',
		onComplete: function(r){
      switchPage(r);
    }
	});
  
  
  	$("#sortable").sortable({axis: 'x', revert:true, tolerance: 'pointer' });
//		$("#sortable").Selection();

  
  if (showType!="") {
    showNewSearchResult(searchResult, showType);
  }

  
  function switchPage(stype){
       switch (stype)
      {
         case 'userID':
          showType = "u";
          break;
        case 'content':
          showType = "c";
          break;
        default: // 't'    
          showType = "t";          
      }
      
      if (panReady[showType]) {
        return false;
      } 
      else if (numRecord [showType]==0) {
       switch(showType)
        {
        case 'u':
          $('#tabContent_userID').html("");
          break;
        case 'c':
          $('#tabContent_content').html("")
          
          break;
        default: // 't'
          $('#tabContent_title').html("");      
        }  
        updatePageLink();        
        return false;
      }
      else {
          $('body').css('height', '100%'); // IE6 Hack
          $('<div id="overlay" />').appendTo('body').fadeIn(300);
          curPageNum = 1; 
          var queryStr = getQueryStr();
          
          $.ajax({
              url: "search.php?",  
              type: "post",
              data: queryStr + '&st=' + showType + '&p=' + curPageNum + "&getReady=1&reqType=getPage", 
              success: function (r){
              $('#overlay').fadeOut(300, function() {
                $(this).remove(); 
              });
              var Error=""; 
              var errMsg="";
              eval(r);
              if (Error) 
                  alert ("Error: " + errMsg);
              {
                 switch(showType)
                  {
                  case 'u':
                    $('#tabContent_userID').html(searchResult);
                    break;
                  case 'c':
                    $('#tabContent_content').html(searchResult)
                    
                    break;
                  default: // 't'
                    $('#tabContent_title').html(searchResult);      
                  }  
                  updatePageLink();
                  for (i=0; i < keyWords.length;i++) {
                    if (keyWords[i]!="") $('body').highlight(keyWords[i]); 
                  }
                  panReady[showType] = true;
              }
            }
          });  
          return false;                 
      }
 
  }
  
  function updatePageLink(){
    var numPage = Math.ceil(numRecord[showType] / lines_per_page[showType]);
    var str="";
    if (curPageNum < 11) {
      pg0 = 1;
      pg1 = curPageNum + 10;
      if (pg1 > numPage + 1  ) {pg1 = numPage + 1 };
    }else{
      pg0 = curPageNum - 10;
      pg1 = curPageNum + 10;
      if (pg1 > numPage + 1 ) {pg1 = numPage + 1 };
    } 
    
    if (curPageNum>1) str = '<a href = "#" id="prePage" class = "pageNotClicked">前页</a>&nbsp;';

    for (i=pg0; i<pg1; i++){
      if (i==curPageNum) {
        str += '<a href ="#" class = "pageClicked" id="'+i+'">' + i + '</a>&nbsp;'; 
      } else{
        str += '<a href ="#" class = "pageNotClicked" id="'+i+'">' + i + '</a>&nbsp;'; 
      }
    }
    if (curPageNum<numPage) str += '<a href = "#" id="nextPage" class = "pageNotClicked">下页</a>&nbsp;';
    switch(showType)
    {            
    case 'u':
      $('#navPage_userID').html(str);
      var str = "笔名中：";
      break;
    case 'c':
      $('#navPage_content').html(str);
      var str = "内容中: ";
      break;
    default: // 't'
      $('#navPage_title').html(str);
      var str = "标题中: ";
    }
    var line0 = (curPageNum - 1) * lines_per_page[showType]+1;
    var line1 = curPageNum * lines_per_page[showType];
    if (line1>numRecord[showType]) line1 = numRecord[showType];
    $('#pagenum_record').html(str + ' 第'+curPageNum+'页 ('+line0+' - ' + line1 + ')');  
  }
  
  
  function showNewSearchResult(searchResult, showType){
    // to show a new search reaults or new tab content for first time
    
    $('#resultTab').show();
    $('#bottomDiv').css('top', '-40px');
    // update tab titles with search numbers
    $('#tabTitle_title span').html('标题(' + numRecord['t']+ ')');
    $('#tabTitle_userID span').html('笔名(' + numRecord['u']+ ')');
    $('#tabTitle_content span').html('内容(' + numRecord['c']+ ')');
    
    //remove all selected items at once before new showType
    $('#tabsContainer').find('.selected').removeClass('selected');
//    $('.tab-content.selected').removeClass('selected');
//    $('.navPage.selected').removeClass('selected');

    updatePageLink();
    switch(showType)
    {            
    case 'u':
      $('#tabTitle_userID').addClass('selected');
      $('#tabContent_userID').html(searchResult).addClass('selected');
      $('#navPage_userID').addClass('selected');
      panReady['u'] = true;
      var str = "笔名中：";
      break;
    case 'c':
      $('#tabTitle_content').addClass('selected');
      $('#tabContent_content').html(searchResult).addClass('selected');
      $('#navPage_content').addClass('selected');
      panReady['c'] = true;
      var str = "内容中: ";
      break;
    default: // 't'
      $('#tabTitle_title').addClass('selected');
      $('#tabContent_title').html(searchResult).addClass('selected');      
      $('#navPage_title').addClass('selected');
      panReady['t'] = true;
      var str = "标题中: ";
    }
    
 //   searchResult = ""; 
    $('#totalRecord').html(totalRecord);
//    $('#totalPage').html(totalPage); 
    for (i=0; i < keyWords.length;i++) {
      if (keyWords[i]!="") $('body').highlight(keyWords[i]); 
    }
  };

  
  $('.searchList').live('click', function(){
    var postID = $(this).attr('post_id');
    var nw=window.open('postread.php?post_id=' + postID, 'View');
    target='View'; 
    return false;  
  });

/*  
  $('.searchList').live('mouseover', function(){
    $(this).parent().find('.abstract').slideDown('fast');
  });

 $('.searchList').live('mouseout', function(){
    $(this).parent().find('.abstract').slideUp('fast');
  });
  */ 
  
  $('#searchButton').click(function(){
    // new search
    var searchStr = trim($('#form > #q').val());
    if (searchStr == "") {
      alert ("请输入关键词");
      return false;
    }
    var queryStr = getQueryStr();
    $('body').css('height', '100%'); // IE6 Hack
    $('<div id="overlay" />').appendTo('body').fadeIn(300);

    $.ajax({
      url: "search.php?",
      type: "post",
      data: queryStr+"&getReady=1&reqType=search", 
      success: function (r){
      
        $('#overlay').fadeOut(300, function() {
          $(this).remove(); 
        });
        // data structure ( $totalRecord | $totalPage | $num_records[t] | $num_records[u] | $num_records[c] | $showType | $pagenum | $searchResult );
        // convert the reponse results to the varials; 
        var errMsg="";
        eval(r);
        if (errMsg) 
            alert (errMsg);
        else {           
            panReady['t'] = false;
            panReady['u'] = false;
            panReady['c'] = false;
            getKeywords();
            showNewSearchResult(searchResult, showType);
        }
      }
  
    }); //ajax
    return false;
  
  });
 
   

  $(".navPage a").live('click', function(){
    if (this.id=="prePage") curPageNum = curPageNum -1;
    else if (this.id=="nextPage") curPageNum = curPageNum + 1;
    else {
      var pn = parseInt (this.id);
      if (curPageNum == pn) return false;
      curPageNum = pn; 
    }
    var queryStr = getQueryStr();
    $('body').css('height', '100%'); // IE6 Hack
    $('<div id="overlay" />').appendTo('body').fadeIn(300);
    
    $.ajax({
        url: "search.php?",  
        type: "post",
        data: queryStr + '&st=' + showType + '&p=' + curPageNum + "&getReady=1&reqType=getPage", 
        success: function (r){
        $('#overlay').fadeOut(300, function() {
          $(this).remove(); 
        });
        var errMsg="";
        eval(r);
        if (errMsg) 
            alert ("Error: " + errMsg);
        {
           switch(showType)
            {
            case 'u':
              $('#tabContent_userID').html(searchResult);
//              $('#navPage_userID').html(str);
              break;
            case 'c':
              $('#tabContent_content').html(searchResult)
//              .$('#navPage_content').html(str);
              
              break;
            default: // 't'
              $('#tabContent_title').html(searchResult);      
//              $('#navPage_title').html(str);
            }  
            updatePageLink();
            for (i=0; i < keyWords.length;i++) {
              if (keyWords[i]!="") $('body').highlight(keyWords[i]); 
            }
        }
      }
    });  
    return false;     
  }); 
  
  
 
    function getQueryStr () 
    {
        var searchStr = trim($('#form > #q').val());
        var qstr = "q=" + searchStr;
        var sStr = '&s=' + $('#form > input[name=s]:checked').val();
        qstr += sStr;
        
        if ($('#form').find('input[name=t][checked]').val()=="on") {
          qstr += '&t=1';     
        } 
        
        if ($('#form').find('input[name=u]:checked').val()=="on") {
          qstr += '&u=1';
        } 
        
        if ($('#form').find('input[name=c]:checked').val()=="on") {
          qstr += '&c=1';
        } 
        
        return qstr;
    } 

 
  function getKeywords()
  {
    var searchStr = trim($('#form > #q').val()); 
    keyWords = searchStr.split(' ');  
  }

  getReady = 1;
 
}); 
 
	
function leftTrim(sString) 
{
while (sString.substring(0,1) == ' ')
{
sString = sString.substring(1, sString.length);
}
return sString;
}


function rightTrim(sString) 
{
while (sString.substring(sString.length-1, sString.length) == ' ')
{
sString = sString.substring(0,sString.length-1);
}
return sString;
}

function trim(sString) 
{
sString=leftTrim(sString);
sString=rightTrim(sString);
return sString;
}

