﻿// JScript 文件
var sightspotPageIndex = 0;
var sightspotPageSize =10;
var sightspotBufferSize = 2 * sightspotPageSize;

var sightspotDisplay;
var sightspotDisplay2;
var sightspotDisplaymore;

var isOut=false;

var area,startDate,stayDays,sightspotType,keyword,ncount=0;

var provinceList;
var cityList;
var startProvince = 0;
var startCity,endCity;
var endProvince = 0;
var inputAreaItem;
var isStartInput;

var area_loaded = false;
    /***************************************地点选择器*****************************************/
function ShowAreaSelect() {
        BOX_show("divArea");
        
        if( area_loaded ) {  
            if (isStartInput)
            {
                UpdateArea(startProvince, isStartInput);
                return false;
            }
            else
            {
                UpdateArea(endProvince, isStartInput);
                return false;
            }
        }
        area_loaded = true;
        $("#divArea").find(".area").empty();
        IIITripAJAX({
            service : AreaService().LoadCitys,
            params  : {mode:3},
            resultHanle : function(result) {
                cityList = result.Data;             
                IIITripAJAX({
                    service : AreaService().LoadProvinces,
                    params  : {},
                    resultHanle : function(result) {
                        var region_area = $("#divArea").find(".level1");
                        $.each( result.Data, function(i,n){
                            var item = $("<span/>");
                            item.text( n.AreaName );
                            item.css({"cursor":"pointer"});
                            item.bind("click", function(){
                                UpdateArea(n.Id);
                                return false;
                            });
                            item.appendTo( region_area );
                        });
                        //UpdateSchoolArea( $("#usertype").val(), null );
                        
                        var schoolsArea = $("#divArea").find(".mainContent");
                        var loading = schoolsArea.find(".loading");
                        var areas = schoolsArea.find(".area");
                        loading.hide();
                        areas.show();       
                    }
                }).execRequest_Json();  
            }
        }).execRequest_Json();        
    }  
    
    function UpdateArea(areaId) {
        $("#divArea").find(".area").empty();
        var citysArea = $("#divArea").find(".mainContent");
        var loading = citysArea.find(".loading");
        var citys = citysArea.find(".area");
        $.each( cityList, function(i,n){
            if(n.ParentId == areaId){
                var city_item = $("<li></li>").text( n.AreaName );
                city_item.css( {"cursor":"pointer"} );
                city_item.text( n.Name );
                city_item.bind("click", function(){
                    BOX_remove( "divArea" );
                    inputAreaItem.val( n.AreaName );
                    if (isStartInput)
                    {
                        startCity = n.Id;
                        startProvince = n.ParentId;
                    }       
                    else 
                    {
                        endCity = n.Id;
                        endProvince = n.ParentId;
                    }
                    return false;
                });
                citys.append( city_item ); 
            } 
        });
    }

 /*****************************************************************地点选择器******************************************************/ 



function getSightspotType(){
    var n=0;
    n=$("#checkbox").attr("checked")
        +$("#checkbox1").attr("checked")*2+$("#checkbox2").attr("checked")*4;
    return n;
}

function paraseSightspotData(divObject,oData)
{
        divObject.divContent.find("div").remove();
        divObject.divContent.append($("<div id='searchChild'></div>"));
    $.each(oData,function(i,n) {
        var row = $("#sightspottemp").clone();
        row.attr("id","sightspot_"+n.Id);
        row.attr("style","text-overflow:ellipsis;white-space:nowrap;overflow:hidden;height:150px;");
        row.find("#sightspotImage").attr("src", ImgFormat(n.ProductImgS));
        row.find("#sightspot_inroduce").find("a").text(n.SightspotName);
        row.find("#sightspot_inroduce").find("a").attr("href","../../Search/Sightspot/Sightspot_view.aspx?id="+n.Id);
        row.find("#sightspot_inroduce").find("code").text("["+n.AreaName+"]");
        row.find("#sightspot_inroduce").find("#description").html((n.Intro1).replace(new RegExp("\n","gm"),"<br>").replace(/ /g,"&nbsp;"));
        row.appendTo(divObject.divContent).show();
    });
};

function initPage()
{

        sightspotDisplay = IIITripAJAX({
        objList : [{ ID : "searchResult", onDataDisplay: paraseSightspotData}],
        service:SightspotService().Search,

        bufferSize : sightspotBufferSize,
        pagination : true,
        pageSize : sightspotPageSize
    });
        $("#mainNav").find("#sightspot_menu").find("a").attr("class","current");
      //if(window.location.search.length>0) {
            var mode=1;
            var querystrings=window.location.search.substring(1).split("&");
            startCity=querystrings[0].split("=")[1];
            //if (querystrings[1].split("=")[0]=="sightSpotType") mode=2;
            switch (mode){
                case 1:
                startDate=querystrings[1].split("=")[1];
                keyword=Request.QueryString("keyword");
                sightspotType=querystrings[3].split("=")[1];
                ncount=querystrings[4].split("=")[1];
                if(ncount == 0)//景点更多
                {
                    sightspotDisplay.setRequestHeader({
                       params : {iAreaName:0,sStartDate:startDate,keyword:keyword,iSightspotType:sightspotType,nPageIndex:sightspotPageIndex,nPageSize:sightspotBufferSize}
                    });
                     sightspotDisplay.clearData();
                     sightspotDisplay.display_Json(null);
                 }
                 else
                 {  
                    sightspotDisplaymore = IIITripAJAX({
                                objList : [{ ID : "searchResult", onDataDisplay: paraseSightspotData}],
                                service:SightspotService().GetAllMoreSightspot,
                                params : {num:ncount,nPageIndex:sightspotPageIndex,nPageSize:sightspotBufferSize},
                                bufferSize : sightspotBufferSize,
                                pagination : true,
                                pageSize : sightspotPageSize
                     });
                     sightspotDisplaymore.display_Json(null);
                 }
                break;
                case 2:
                sightspotDisplay2=IIITripAJAX({
                    objList : [{ ID : "searchResult2", onDataDisplay: paraseSightspotData}],
                    service:SightspotService().Search2,

                    bufferSize : sightspotBufferSize,
                    pagination : true,
                    pageSize : sightspotPageSize
                });
                
                sightspotType=querystrings[1].split("=")[1];
                keyword=querystrings[2].split("=")[1];
                sightspotDisplay2.setRequestHeader({
                
                params : {iAreaName:startCity,
                            iSightspotType: sightspotType ,
                            sKeyword:keyword,
                            nPageIndex:sightspotPageIndex, 
                            nPageSize:sightspotBufferSize}
                });
                // sightspotDisplay2.clearData();
                 sightspotDisplay2.display_Json(null);
            }
            
      //}
      $("#startDate").bind("focus",function () {
        WdatePicker();
      });
     $("#location").bind("focus", function() {
            inputAreaItem = $("#location");
            isStartInput = true;
            ShowAreaSelect();
      });
     
      $("#btnSearch").bind("click",function() {
           /* if ($("#location").val()=="") {
                alert("所在地不能为空");
                return;
            }
            else if ($("#startDate").val()==""){
                alert("出发时间不能为空");
                return;
            }
             if (!IsNum($.trim($("#stayDays").val())) || $.trim($("#stayDays").val()).length>2){
                alert("旅行天数不合法");
                return;
            }    */    
            if($("#keyword").attr("value").length>100){
                alert("关键词太长，请重新输入");
                return;
            }
            if($("#keyword").attr("value").length==0 || $("#keyword").attr("value")=="请输入关键字"){
                alert("关键词不能为空");
                return;
            }    
            if (isOut==false){
                area=$("#location").val();
                startDate=$("#startDate").val();
                keyword=$("#keyword").val();
                sightspotType=getSightspotType();
            }
            $("#searchResult2").remove();
            
             var querystring="";
            querystring+="location="+startCity;
            querystring+="&startDate="+startDate;
            if($("#keyword").attr("value")=="请输入关键字") querystring+="&keyword=";
            else querystring+="&keyword="+keyword;
            querystring+="&sightSpotType="+sightspotType;
            querystring +="&num=0";  
            if(!QueryStringCheck(keyword)) alert("输入含有非法字符");
            window.location.href ="../../Search/sightspot/sightspot_search_list.aspx?"+querystring;
//            sightspotDisplay.setRequestHeader({
//                
//                params : {iAreaName:startCity,
//                            sStartDate:startDate,
//                            keyword:keyword,
//                            iSightspotType: sightspotType ,
//                            nPageIndex:sightspotPageIndex, 
//                            nPageSize:sightspotBufferSize}
//                });
//            sightspotDisplay.clearData();
//            sightspotDisplay.display_Json(null);

      });
}
$(document).ready(function ()
{
    initPage();
    IIITripAJAX({
        service : SightspotService().GetHotSightspots,
        params  : {},
        resultHanle : function(result) {
            var oData = result.Data;
            var container = $("#recommand_list").find("ul");
            $.each(oData,function(i,n) {
                if (i>=9) return;
                var item = $("#recommendTemplate").clone();
                item.attr("id","recommend_" + n.Id);
                item.find("a").text(n.SightspotName);
                item.find("a").attr("href","../../Search/Sightspot/Sightspot_view.aspx?id="+n.Id);
                item.appendTo(container).show();
            });
        }
    }).execRequest_Json(null);

});





