
//Dingtai.Search NameSpace
Dingtai.Search = {};

Dingtai.Search.dosearch = function(type,val) {
    
};

//根据URL获取URL的参数 并构造JSON返回
Dingtai.Search.GetParams = function(_href){
    var start = _href.indexOf("?");
    if (start <=0 ) {
        return null;
    }
    var params = _href.substring(start+1);
    if (params.lenght<0) {
        return null;
    }
    params =params.split("&");
    var json_params = "";
    json_params = "{";
    for (var i=0;i<params.length;i++){
        var param = params[i];
        param = param.split("=");
        if(param.length<1) { continue ;}
       
        json_params = json_params +"\""+ param[0] +"\":\""+ param[1] +"\"";
//        json_params = json_params +"\""+ param[0] +"\":\""+decodeURIComponent(param[1]) +"\"";
        if (i != params.length-1) { json_params = json_params + ","; }
    }
    json_params = json_params + "}";
//   alert(json_params.toString ());
    //构造JSON对象
//    json_params = eval("(" + json_params + ")");      
    json_params = jQuery.parseJSON(json_params);
  
    return json_params;
  
    //http://www.vpod.com:4557/html/search.htm?type=a&val=aaa
}



Dingtai.Search.Output = function(type,data,targetObject,sort) {
     //jQuery对JSON排序 按Point逆序
     data.sort(function(a,b){
            return a[sort]<b[sort]?1:(a[sort]==b[sort]?(a["ID"]<b["ID"]?1:-1):-1);
        });
    
    for (var i = 0;i<data.length;i++){    
//         alert(i);
           
             
                 
            var url =  data[i].PathUrl ;
            var imgurl = data[i].ImgUrl; 
  
             var name = "";
            name = data[i].Name;
            var detail = data[i].Detail;
            var html =   "<p class=\"img\"><b class=\"img_hover\"><a href=\"" + url + "\"  target=\"_blank\"><img src=\""+ imgurl +"\" width=\"120\" height=\"90\" alt=\"鼎太播客\" /><span></span></a></b></p><span class=\"txt\"><b>" + name + "</b>&nbsp; &nbsp;" + detail + "<a href=\"" + url + "\" target=\"_blank\">播放视频</a> </span>";
            targetObject.find("li").eq(i).html(html);
           

    }     
};

Dingtai.Search.OutputUser = function(type,data,targetObject,sort) {
     //jQuery对JSON排序 按Point逆序
     data.sort(function(a,b){
            return a[sort]<b[sort]?1:(a[sort]==b[sort]?(a["ID"]<b["ID"]?1:-1):-1);
        });
    for (var i = 0;i<data.length;i++){    
//         alert(i);
            var name = "";
            name = data[i].UserName;
            if (typeof(name) == undefined) {name = "";}         
            var nick = data[i].Nick;
            if (typeof(nick) == undefined || nick == "" ) {nick = "";}
             var Province = "";
            Province = data[i].Province;
            if (typeof(Province) == undefined) {Province = "";}
             var City = "";
            City = data[i].City;
            if (typeof(City) == undefined) {City = "";}
            
            var url =  data[i].PathUrl ;
            var imgurl = data[i].ImageUrl; 
                             
            var html =   "<p class=\"img\"><b class=\"img_hover\"><a href=\"" + url + "\"  target=\"_blank\"><img src=\""+ imgurl +"\" width=\"120\" height=\"90\" alt=\"鼎太播客\" /><span></span></a></b></p><span class=\"txt\"><b>" + name + "</b>&nbsp; &nbsp;昵称 : " + nick + "&nbsp; &nbsp;<br />省份 : " + Province + "&nbsp; &nbsp;城市 : " + City + "<a href=\"" + url + "\" target=\"_blank\">进入主页</a> </span>";
            targetObject.find("li").eq(i).html(html);
         

    }     
};

Dingtai.Search.PutPage = function(type,val,pageIndex,pageCount,pageSize) {
    var url = "search.htm?" + Dingtai.Const("search_type") + "=" + type +"&" + Dingtai.Const("search_val") + "=" + val+"&" + Dingtai.Const("search_pageSize") + "="+pageSize+"&" + Dingtai.Const("search_page") + "=";
   //添加分页         
    var ul_page = jQuery(".video_page ul");
    ul_page.html("");
    

    
    if (pageIndex == 1) { ul_page.append("<li class='previous'><span>上一页</span></li>"); } 
    else { ul_page.append("<li class='previous'><a href='"+url + (pageIndex -1)+"'>上一页</a></li>"); }
    
    var preMore = false;
    var count = 10;
    if ((pageCount > count && pageIndex > count) || pageCount - pageIndex < count) { preMore = true;}
    var nextMore = false;
    if (pageCount > count && pageIndex <= pageCount - count) { nextMore = true;}
//    if (preMore) { ul_page.append("<li class='pages'><a href='"+  url + (pageIndex - count) +"'>...</a></li>"); }
    
    var start = 0;
    if(pageIndex > count) { start = pageIndex - count + 3; }
    else { start = 1; }
    var end = 0;
    if(pageIndex > pageCount - count) { end = pageCount; if (pageCount > count) { start = pageCount - count + 1;}}
    else if(pageIndex < 5) { end =count; }
    else { end =pageIndex + count - 3; }
    
     for(var i = start; i<=end;i++){
        if (i==pageIndex) { ul_page.append("<li class='pages'><span>" + i +"</span></li>"); }
        else { ul_page.append("<li class='pages'><a href='"+ url+i +"'>" + i +"</a></li>"); }
     }
     
//     if (nextMore) { ul_page.append("<li class='pages'><a href='"+  url + (pageIndex + count)  +"'>...</a></li>"); }

    
//    for(var i = 1; i<=pageCount;i++){
//        if (i==pageIndex) { ul_page.append("<li class='pages'><span>" + i +"</span></li>"); }
//        else { ul_page.append("<li class='pages'><a href='"+url+i+"'>" + i +"</a></li>"); }        
//    }
    
    if (pageIndex == pageCount) { ul_page.append("<li class='next'><span>下一页</span></li>"); } 
    else { ul_page.append("<li class='next'><a href='"+url + (pageIndex +1)+"'>下一页</a></li>"); }
};

Dingtai.Search.PutList = function(type,data,dataLength,commonParamsCount) {
    var control = ".tab_content"
    var lis = jQuery(control + " > li");
    jQuery(control).html("");
    //为UL添加 LI
    if (lis.length<dataLength){ 
        for(var j = lis.length;j<dataLength;j++){
            jQuery(control).append("<li></li>");
        }
    }
    lis = jQuery(control);
     //去除通用参数对象  MSG , pageCount
    for (var k = 0 ; k<commonParamsCount; k++) {data.pop(); }
            
//        $.each(data, function(i) {
//            alert(data[i].ID+" " +data[i].Name+"  sort:"+data[i].Point);
//        });

   
       if (type == Dingtai.Const("search_hdmedia") || type == Dingtai.Const("search_media")) {      
            jQuery(".Search_title ul").css("display","block");
             var sort ="Point";  
            Dingtai.Search.Output(type,data,lis.eq(0),sort);
            
            sort ="UploadDate";  
            Dingtai.Search.Output(type,data,lis.eq(1),sort);
            sort ="PointCount";  
            Dingtai.Search.Output(type,data,lis.eq(2),sort);
               
        }
        else if( type == Dingtai.Const("search_user") ) {
            jQuery(".Search_title ul").css("display","none");
            
            sort ="CreationDate";  
            Dingtai.Search.OutputUser(type,data,lis.eq(0),sort);
        }
};

Dingtai.Search.decode = function(detail) {
    detail = decodeURIComponent(detail);
    return detail;
};

Dingtai.Search.encode = function(detail) {
    detail = encodeURIComponent(detail);
    return detail;
};


//请求Search.aspx 页面返回后的回调除数
Dingtai.Search.SearchCallback = function (data,textstatus){
     
        var max = data.length;
        //常规参数数量  msg , pageIndex , pageCount
        var commonParamsCount = 7;
        var dataLength = 0;
        if (max > commonParamsCount){
            dataLength = max - commonParamsCount;
        }
        else if (max == commonParamsCount){
            alert(data[0].msg);
            return;
        }
        else {
       
        }
    
        var type = data[max-(commonParamsCount - 1)].searchType;
        var val = data[max-(commonParamsCount - 2)].q;        
        var pageIndex = data[max-(commonParamsCount - 3)].page;
        var pageSize = data[max-(commonParamsCount - 4)].pageSize;
        var pageCount = data[max-(commonParamsCount - 5)].pageCount;
        var recordCount = data[max-(commonParamsCount - 6)].recordCount;

        //设置搜索栏
        jQuery("#datasearch").html(val);
        jQuery("#datacount").html(recordCount);
        
        //添加分页
        Dingtai.Search.PutPage(type,val,pageIndex,pageCount,pageSize);  
        //添加列表显示
       Dingtai.Search.PutList(type,data,dataLength,commonParamsCount);
        
};

/*
function sort(data){ 
    var t;
    for (var i = 0; i < data.length - 1 ; i++) {
        for(var j = 1; j < data.length; j++) {
            if (data[i].Point < data[j].Point) {
                t = data[i];
                data[i] = data[j];
                data[j] = t;
            }
        }  
    }   
    return data;
}
*/

//执行Search.htm的搜索
jQuery(document).ready(function() {   
        var href = location.href;
//       alert(unescape(href));
//alert(decodeURIComponent (href));
//alert(decodeURI (href));
        href = decodeURI(href);
 
        var params = Dingtai.Search.GetParams(href);
        var searchPage = Dingtai.Const("searchPage");  
          
        jQuery.getJSON(searchPage, params , Dingtai.Search.SearchCallback)
        jQuery("#logo").ajaxError(function(event,request, settings){
            alert("Error: " + event.toString() + "  " + settings.toString());
        });;    
             
});
