﻿var provinceList;
var cityList;

var startProvince = 0;
var endProvince = 0;
var startCity = 0;
var endCity = 0;

var trafficType ="";
var hotelStar = "";
var startDate ="";
var backDate ="";

var inputAreaItem;
var isStartInput;

var area_loaded = false;


/***************************************地点选择器*****************************************/
   function ShowAreaSelect() {
        BOX_show("divArea");
        
        if( area_loaded ) {  
            if (isStartInput)
            {
                $("#divArea").find(".level1").hide();
                UpdateArea();
                return false;
            }
            else
            {
                $("#divArea").find(".level1").show();
                UpdateArea(endProvince);
                return false;
            }
        }
        area_loaded = true;
        $("#divArea").find(".area").empty();
        IIITripAJAX({
            service : AreaService().LoadCitys,
            params  : {mode:isStartInput?6:2},
            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();
                        if (isStartInput) 
                        {
                            $("#divArea").find(".level1").hide();
                            UpdateArea();
                        }
                        else
                        {
                             $("#divArea").find(".level1").show();
                        }
                        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");
        if (isStartInput)
        {
            $.each( cityList, function(i,n){
                if(n.IfStart == 1){
                    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 );
                        startCity = n.Id;   
                        return false;
                    });
                    citys.append( city_item ); 
                } 
            });
        }
        else
        {
            $.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 );
                        endCity = n.Id;
                        endProvince = n.ParentId;
                        return false;
                    });
                    citys.append( city_item ); 
                } 
            });
        }
        
        
    }

 /*****************************************************************地点选择器******************************************************/ 
 
 
 function initPage() 
 {
      $("#mainNav").find("#selfplay_menu").find("a").attr("class","current");
      $("#startCity").bind("focus", function() {
            inputAreaItem = $("#startCity");
            isStartInput = true;
            ShowAreaSelect();
      });
      
 /*     $("#endCity").bind("focus",function() {
            inputAreaItem = $("#endCity");
            isStartInput = false;
            ShowAreaSelect();
      });*/
      
      $("#startDate").bind("focus",function () {
        WdatePicker();
      });
      
      $("#endDate").bind("focus", function () {
        WdatePicker();
      });
      
      $("#btnSearch").bind("click", function() {
        trafficType = "";
        hotelStar = "";
        
        $("input[@name='trafficType']").each(function () {
            if ($(this).attr("checked") == true)
            {
                if (trafficType != "")
                {
                    trafficType += ",";
                }
                trafficType = trafficType + Math.pow(2,$(this).attr("value")-1);
            }
        });
        
        $("input[@name='hotelStar']").each(function () {
            if ($(this).attr("checked") == true)
            {
                if (hotelStar != "")
                {
                    hotelStar += ",";
                }
                hotelStar = hotelStar + $(this).attr("value");
            }
        });
             if ($.trim($("#endCity").val()) != "")
            {
                var sEndCity = $.trim($("#endCity").val());
            }       
        if ($.trim($("#startDate").val()) != "")
        {
            startDate = $.trim($("#startDate").val());
        }
        
        if ($.trim($("#endDate").val()) != "")
        {
            backDate = $.trim($("#endDate").val());
        }
        
        window.location.href =  "../../Search/SelfPlay/Search.aspx?sc=" + startCity + "&ec=" + sEndCity + "&sd=" + startDate + "&bd=" + backDate + "&tt=" + trafficType + "&hs=" + hotelStar;
      });
 }
 
 $(document).ready(function() {
    initPage();
    //加载热门线路
    IIITripAJAX({
        service : SelfPlayService().GetHotProductDiy,
        params  : {nCount : 0, maxCount : 10},
        resultHanle : function(result) {
            var oData = result.Data;
            var container = $("#HotContainer").find("ul");
            $.each(oData,function(i,n) {
                var item = $("#hotTemplate").clone();
                item.attr("id","hot_" + n.Id);
                item.find("a").text(n.DiyName);
                item.find("a").attr("href", "../../Search/SelfPlay/view.aspx?id=" + n.Id);
                item.appendTo(container).show();
            });
        }
    }).execRequest_Json(null);
    
    //加载特惠路线
    IIITripAJAX({
        service : SelfPlayService().GetPromoteProductDiy,
        params  : {nCount : 0, maxCount : 10},
        resultHanle : function(result) {
            var oData = result.Data;
            var container = $("#promoteContainer").find("ul");
            $.each(oData,function(i,n) {
                var item = $("#promoteTemplate").clone();
                item.attr("id","promote_" + n.Id);
                item.find("a").text(n.DiyName);
                item.find("a").attr("href", "../../Search/SelfPlay/view.aspx?id=" + n.Id);
                item.appendTo(container).show();
            });
        }
    }).execRequest_Json(null);
    
    
    //加载精品路线
    IIITripAJAX({
        service : SelfPlayService().GetGreatProductDiy,
        params  : {nCount : 0, maxCount : 14},
        resultHanle : function(result) {
            var oData = result.Data;
            var container = $("#GreatContainer").find("p");
            $.each(oData,function(i,n) {
                var item = $("#<a style=\"display:inline\"/>");
                item.text(n.DiyName);
                item.attr("href", "../../Search/SelfPlay/view.aspx?id=" + n.Id);
                item.appendTo(container);
            });
        }
    }).execRequest_Json(null);
    
    //加载推荐路线
    IIITripAJAX({
        service : SelfPlayService().GetRecommandProductDiy,
        params  : {nCount : 2, maxCount : 14},
        resultHanle : function(result) {
            var oData = result.Data;
            var firstContainer = $("#firstRecommandContainer").find("dl");
            var secondContainer = $("#secondRecommandContainer").find("dl");
            
            $.each(oData,function(i,n) {
                if (i == 0)
                {
                    if(n.ProductImgS =="")
					{
					    $("#firstImg").attr("src",ImgFormat("/images/view_img.gif"));
					}
					else
					{
						$("#firstImg").attr("src",ImgFormat(n.ProductImgS));
						$("#firstImg").error(function(){ $("#firstImg").attr("src",ImgFormat("/images/view_img.gif")); });
					}
					
                    $("#firstImg").bind("click",function() {
                       window.location.href =  "../../Search/SelfPlay/view.aspx?id=" + n.Id;
                    });
                    $("#firstName").text(n.DiyName).bind("click", function() {
                        window.location.href =  "../../Search/SelfPlay/view.aspx?id=" + n.Id;
                    });
                    if (n.Intro1.length>120)
                        $("#firstIntro").html((n.Intro1.substring(0,120)+"...").replace(new RegExp("\n","ggm"),"<br>").replace(/ /g,"&nbsp;"));
                    else $("#firstIntro").html((n.Intro1).replace(new RegExp("\n","ggm"),"<br>").replace(/ /g,"&nbsp;"));
					
                }
                else if (i == 1)
                {
                    if(n.ProductImgS =="")
					{
					    $("#secondImg").attr("src",ImgFormat("/images/view_img.gif"));
					}
					else
					{
						$("#secondImg").attr("src",ImgFormat(n.ProductImgS));
						$("#secondImg").error(function(){ $("#secondImg").attr("src",ImgFormat("/images/view_img.gif")); });
					}
                    
                     $("#secondImg").bind("click",function() {
                        window.location.href =  "../../Search/SelfPlay/view.aspx?id=" + n.Id;
                    });
                    $("#secondName").text(n.DiyName).bind("click", function() {
                        window.location.href =  "../../Search/SelfPlay/view.aspx?id=" + n.Id;
                    });
                    if (n.Intro1.length>120)
                        $("#secondIntro").html((n.Intro1.substring(0,120)+"...").replace(new RegExp("\n","ggm"),"<br>").replace(/ /g,"&nbsp;"));
                    else $("#secondIntro").html((n.Intro1).replace(new RegExp("\n","ggm"),"<br>").replace(/ /g,"&nbsp;"));
               
                }
                else if (i < 7)
                {
                    var item = $("#recommandTemplate").clone();
                    item.attr("id", "recommand_" + n.Id);
                    //item.find("#price").text(n.ReferencePrice);
                    item.find("#diyName").text(n.DiyName);
                    item.find("a").attr("href", "../../Search/SelfPlay/view.aspx?id=" + n.Id);
                    item.appendTo(firstContainer).show();
                }
                else 
                {
                    var item = $("#recommandTemplate").clone();
                    item.attr("id", "recommand_" + n.Id);
                    item.find("#price").text(n.ReferencePrice);
                    item.find("#diyName").text(n.DiyName);
                    item.find("a").attr("href", "../../Search/SelfPlay/view.aspx?id=" + n.Id);
                    item.appendTo(secondContainer).show();                    
                }
            });
        }
    }).execRequest_Json(null);
 
 });
