var $current;
$().ready(function(){
   
   $current = $('#tab2');
   var imagespath = $current.attr('imagespath');
   
   $current.find('a').css('background-image', 'url('+ imagespath +'/tableft3.gif)');
   $current.find('span').css('background-image', 'url('+ imagespath +'/tabright3.gif)');


   $('#tabs1 li').click(function(){

       var vochid = $(this).attr('vochid');
       var promoid = $(this).attr('promoid');
       var cityid = $(this).attr('cityid');
       var rootpath = $(this).attr('rootpath');
//       var imagespath = $(this).attr('imagespath');

       $current.find('a').css('background-image', 'url('+ imagespath +'/tableft1.gif)');
       $current.find('span').css('background-image', 'url('+ imagespath +'/tabright1.gif)');
//        alert(imagespath) ;
       $(this).find('a').css('background-image', 'url('+ imagespath +'/tableft3.gif)');
       $(this).find('span').css('background-image', 'url('+ imagespath +'/tabright3.gif)');

       $current = $(this);

       $(vochid).load(rootpath + "/hotelsearch/vochhotelsearch.action", {"cityselect":cityid});
       $(promoid).load(rootpath + "/hotelsearch/promohotelsearch.action", {"cityselect":cityid});
   });

});

function searchSpecHotel(cityid, rootpath) {
    $("#taginfolist").load(rootpath + "/hotelsearch/tagsearch.action", {"cityselect":cityid});
    $("#promohotellist").load(rootpath + "/hotelsearch/promohotelsearch.action", {"cityselect":cityid});
}

function searchVochHotel(vochid, promoid, cityid, rootpath) {
  
    $(vochid).load(rootpath + "/hotelsearch/vochhotelsearch.action", {"cityselect":cityid});
    $(promoid).load(rootpath + "/hotelsearch/promohotelsearch.action", {"cityselect":cityid});
}

function searchPCD(ptarg, ctarg, dtarg, rootpath) {
    $.getJSON(rootpath + "/hotelsearch/provinvce.action", {"countryid":"CN"}, function(json) {
        var obj = $(ptarg);
        obj.children().remove();
        for (var i = 0; i < json.length; i++) {
            var opt = json[i].split("#");
            if (opt[0] == 'Shanghai')
                obj.append("<option value='" + opt[0] + "' selected='selected'>" + opt[1] + "</option>");
            else
                obj.append("<option value='" + opt[0] + "'>" + opt[1] + "</option>");
        }
        obj.attr("disabled", "");
        var provenceid = json[0].split("#")[0];
        $.getJSON(rootpath + "/hotelsearch/provinvce.action", {"provenceid":"Shanghai"}, function(json) {
            var obj2 = $(ctarg);
            obj2.children().remove();
            for (var i = 0; i < json.length; i++) {
                var opt = json[i].split("#");
                obj2.append("<option value='" + opt[0] + "'>" + opt[1] + "</option>");
            }
            obj2.attr("disabled", "");


            var cityid = json[0].split("#")[0];
            if (cityid == null || cityid == "") return;
            $.getJSON(rootpath + "/hotelsearch/provinvce.action", {"cityid":"Shanghai"}, function(json) {
                if (json == null || json == "") return;
                var obj3 = $(dtarg);
                obj3.children().remove();
                obj3.append("<option value=''>---全部---</option>");
                for (var i = 0; i < json.length; i++) {
                    var opt = json[i].split("#");
                    obj3.append("<option value='" + opt[0] + "'>" + opt[1] + "</option>");
                }
                obj3.attr("disabled", "");
            })
        })
    });
}


function searchPCDSelected(ptarg, ctarg, dtarg, provinceCode, cityCode, districtCode, rootpath) {
    $.getJSON(rootpath + "/hotelsearch/provinvce.action", {"countryid":"CN"}, function(json) {
        var obj = $(ptarg);
        obj.children().remove();
        if (provinceCode == '')
            provinceCode = 'Shanghai';
        for (var i = 0; i < json.length; i++) {
            var opt = json[i].split("#");
            if (opt[0] == provinceCode)
                obj.append("<option value='" + opt[0] + "' selected='selected'>" + opt[1] + "</option>");
            else
                obj.append("<option value='" + opt[0] + "'>" + opt[1] + "</option>");
        }
        obj.attr("disabled", "");
        var provenceid = json[0].split("#")[0];

        if (cityCode == '')
            cityCode = 'Shanghai';
        $.getJSON(rootpath + "/hotelsearch/provinvce.action", {"provenceid":provinceCode}, function(json) {
            var obj2 = $(ctarg);
            obj2.children().remove();
            for (var i = 0; i < json.length; i++) {
                var opt = json[i].split("#");
                if (opt[0] == cityCode)
                    obj2.append("<option value='" + opt[0] + "' selected='selected'>" + opt[1] + "</option>");
                else
                    obj2.append("<option value='" + opt[0] + "'>" + opt[1] + "</option>");
            }
            obj2.attr("disabled", "");

            var cityid = json[0].split("#")[0];
            if (cityid == null || cityid == "") return;
            $.getJSON(rootpath + "/hotelsearch/provinvce.action", {"cityid":cityCode}, function(json) {
                if (json == null || json == "") return;
                var obj3 = $(dtarg);
                obj3.children().remove();
                obj3.append("<option value=''>---全部---</option>");
                for (var i = 0; i < json.length; i++) {
                    var opt = json[i].split("#");
                    if (opt[0] == districtCode)
                        obj3.append("<option value='" + opt[0] + "' selected='selected'>" + opt[1] + "</option>");
                    else
                        obj3.append("<option value='" + opt[0] + "'>" + opt[1] + "</option>");
                }
                obj3.attr("disabled", "");
            })
        })
    });
}

function setClickEvent(elementname, targElementName, rootpath) {
    $(elementname).click(function() {
        searchSpecHotel(elementname);
        if (this.checked) {
            var cityid = this.value;
            searchSpecHotel(cityid, rootpath);
        }

        if (!$("#selectProvince").attr("checked")) {
            $("#province").attr("disabled", true);
            $("#city").attr("disabled", true);
        }

        var cityid = this.value;
        if (cityid == null || cityid == "") return;
        $.getJSON(rootpath + "/hotelsearch/provinvce.action", {"cityid":cityid}, function(json) {
            var obj = $(targElementName);
            obj.children().remove();
            obj.append("<option value=''>---全部---</option>");
            for (var i = 0; i < json.length; i++) {
                var opt = json[i].split("#");
                obj.append("<option value='" + opt[0] + "'>" + opt[1] + "</option>");
            }
            obj.attr("disabled", "");
        })

    });
}

function selectProvince(sourceElementName, targElementName, targElementName2, targElementName3, rootpath) {
    $(sourceElementName).click(function() {
        $.getJSON(rootpath + "/hotelsearch/provinvce.action", {"countryid":"CN"}, function(json) {
            var obj = $(targElementName);
            obj.children().remove();
            for (var i = 0; i < json.length; i++) {
                var opt = json[i].split("#");
                obj.append("<option value='" + opt[0] + "'>" + opt[1] + "</option>");
            }
            obj.attr("disabled", "");

            var provenceid = json[0].split("#")[0];
            $.getJSON(rootpath + "/hotelsearch/provinvce.action", {"provenceid":provenceid}, function(json) {
                var obj2 = $(targElementName2);
                obj2.children().remove();
                for (var i = 0; i < json.length; i++) {
                    var opt = json[i].split("#");
                    obj2.append("<option value='" + opt[0] + "'>" + opt[1] + "</option>");
                }
                obj2.attr("disabled", "");

                var cityid = json[0].split("#")[0];
                $.getJSON(rootpath + "/hotelsearch/provinvce.action", {"cityid":cityid}, function(json) {
                    var obj = $(targElementName3);
                    obj.children().remove();
                    obj.append("<option value=''>---全部---</option>");
                    for (var i = 0; i < json.length; i++) {
                        var opt = json[i].split("#");
                        obj.append("<option value='" + opt[0] + "'>" + opt[1] + "</option>");
                    }
                    obj.attr("disabled", "");
                })

            })
        });
    })
}


function selectCity(sourceElementName, targElementName, targElementName2, rootpath) {
    $(sourceElementName).change(function() {
        var provinceid = this.value;
        $.getJSON(rootpath + "/hotelsearch/provinvce.action", {"provenceid":provinceid}, function(json) {
            var obj = $(targElementName);
            obj.children().remove();
            for (var i = 0; i < json.length; i++) {
                var opt = json[i].split("#");
                obj.append("<option value='" + opt[0] + "'>" + opt[1] + "</option>");
            }
            obj.attr("disabled", "");

            var cityid = json[0].split("#")[0];
            $.getJSON(rootpath + "/hotelsearch/provinvce.action", {"cityid":cityid}, function(json) {
                var obj = $(targElementName2);
                obj.children().remove();
                obj.append("<option value=''>---全部---</option>");
                for (var i = 0; i < json.length; i++) {
                    var opt = json[i].split("#");
                    obj.append("<option value='" + opt[0] + "'>" + opt[1] + "</option>");
                }
                obj.attr("disabled", "");
            })
        })
    })
}


function searchDistrict(targElementName, cityid, rootpath) {
    $.getJSON(rootpath + "/hotelsearch/provinvce.action", {"cityid":cityid}, function(json) {
        var obj = $(targElementName);
        obj.children().remove();
        obj.append("<option value=''>---全部---</option>");
        for (var i = 0; i < json.length; i++) {
            var opt = json[i].split("#");
            obj.append("<option value='" + opt[0] + "'>" + opt[1] + "</option>");
        }
        obj.attr("disabled", "");
    })
}

function selectDistrict(sourceElementName, targElementName, rootpath) {
    $(sourceElementName).change(function() {
        var cityid = this.value;
        searchDistrict(targElementName, cityid, rootpath);
    })
}

//封装日期

function datetoggle(obj, dateobj) {
    if (dateobj._showing) dateobj.hide();
    dateobj.onchange = function() {
        obj.value = this.formatDate();
    }
    dateobj.defaultInit("zh");
    dateobj.toggle(obj);
}

function setDateClickEvent(e1, e2, dateobj) {
    if (e1 != null) {
        $(e1).click(function() {
            datetoggle(this, dateobj);
        })
    }
    if (e2 != null) {
        $(e2).click(function() {
            datetoggle(this, dateobj);
        })
    }
}


function formateDate(cd, day) {
    if (day != 0)
        cd.setDate(cd.getDate() + day);

    var month = (cd.getMonth() + 1) + "";
    if (month.length < 2) month = "0" + month;

    var date = cd.getDate() + "";
    if (date.length < 2) date = "0" + date;

    return  cd.getFullYear() + "-" + month + "-" + date;
}

function setHotelQueryDefault(provence, city, district, indate, outdate, pricerange, hotelname, hotelstar, tag) {
    /*设置省份默认值*/
    setSelectElDefaultSelected("provence", provence);
    /*设置城市默认值*/
    setSelectElDefaultSelected("city", city);
    /*设置行政区*/
    setSelectElDefaultSelected("district", district);
    /*设置入住日期*/
    document.getElementById("indate").value = indate;

    /*设置离店日期*/
    document.getElementById("outdate").value = outdate;
    /*设置价格范围*/
    setSelectElDefaultSelected("pricerange", pricerange);
    /*设置酒店名称*/
    document.getElementById("hotelname").value = outdate;
    /*设置酒店星级*/
    setSelectElDefaultSelected("star", hotelstar);
    /*设置地标*/
    document.getElementById("tag").value = tag;
}

function setSelectElDefaultSelected(elementname, defaultvalue) {
    if (!isNaN(defaultvalue) && defaultvalue != null) {
        var optionlist = doucment.getElementById(elementname).options;
        for (var i = 0; i < optionlist.length; i++) {
            if (defaultvalue == optionlist[i].value) {
                optionlist[i].selected = true;
                break;
            }
        }
    }
}