var AdDisplayIndex;
var IsIE6 = false;
$(document).ready(function () {
    if ($.browser.msie && Number($.browser.version) <= 6)
        IsIE6 = true;
    if ($.browser.msie && Number($.browser.version) <= 8) {
        $('#CityWeather').hide();
    }
    $('#TabHotel').click(function () {
        SetTabPageHeader('#TabHotel', true);
        SetTabPageHeader('#TabJourney', false);
        $('#HotelTable').show();
        $('#JourneyTable').hide();
    });
    $('#TabJourney').click(function () {
        SetTabPageHeader('#TabJourney', true);
        SetTabPageHeader('#TabHotel', false);
        $('#JourneyTable').show();
        $('#HotelTable').hide();
    });
    $('#TabHotel').click();
    ///////////////////
    SetInputDefault('#HotelSearchCity', '中文/拼音首字母');
    SetInputDefault('#LocalJourneySearchCity', '中文/拼音首字母');
    SetInputDefault('#JourneySearchCity', '中文/拼音首字母');

    $('#HotelSearchCity').keyup(function () {
        if (IsIE6) return;
        HotelSearchCity($(this).val(), '#HotelCitySelector', '#HotelSearchCity');
    });
    $('#HotelSearchCity').blur(function () {
        $('#HotelCitySelector').fadeOut(200);
    });
    $('#LocalJourneySearchCity').keyup(function () {
        if (IsIE6) return;
        HotelSearchCity($(this).val(), '#LocalJourneyCitySelector', '#LocalJourneySearchCity');
    });
    $('#LocalJourneySearchCity').blur(function () {
        $('#LocalJourneyCitySelector').fadeOut(200);
    });
    $('#JourneySearchCity').keyup(function () {
        if (IsIE6) return;
        HotelSearchCity($(this).val(), '#JourneyCitySelector', '#JourneySearchCity');
    });
    $('#JourneySearchCity').blur(function () {
        $('#JourneyCitySelector').fadeOut(200);
    });

    $('#HotelSearchButton').click(function () {
        $('#SearchHotel').submit();
    });
    $('#JourneySearchButton').click(function () {
        $('#SearchJourney').submit();
    });
    /////////////////////////////////////
    $('#AdSelector span').click(function () {
        var i = $(this).html();
        $('#AdList').animate({ 'margin-top': '-' + ((i - 1) * 150) + 'px' });
        SetAdSelect(i - 1);
    });
    SetAdSelect(0);
    GetAdList();
});
///////////////////////////////////////////////
function SetAdSwitch(Count)
{
	if(AdDisplayIndex==Count)
	{
		AdDisplayIndex=Count-AdDisplayIndex;
	}
	$('#AdList').animate({'margin-top':'-'+((AdDisplayIndex)*150)+'px'});
	SetAdSelect(AdDisplayIndex);
	AdDisplayIndex++;
}
function RemoveAdSelect()
{
	$('#AdSelector span').each(function(){
		$(this).css({
			'background-color': '#AAA',
			'color': '#FFF'
		});
	});
}
function SetAdSelect(AdIndex)
{
	RemoveAdSelect();
	$('#AdSelector span:eq('+AdIndex+')').css({
		'background-color': '#F60',
		'color': '#000'
	});
}
function SetHotelSearchCityResult(CityName, SelectorID, InputID)
{
	$(InputID).val(CityName);
	$(SelectorID).fadeOut(200);
}
function SetInputDefault(ID, Default)
{
	$(ID).focus(function(){
		if($(this).val()==Default)
		{
			$(this).val('');
		}
	});
	$(ID).blur(function(){
		if($(this).val()=='')
		{
			$(this).val(Default);
		}
	});
}
function SetTabPageHeader(ID, Pressed)
{
	if(Pressed)
	{
		$(ID).css({
			'background-image':'none',
			'background-color':'#FFF',
			'color':'#000'
		});
	}
	else
	{
		$(ID).css({
			'background-image':'url(Image/TabHeaderNormal.jpg)',
			'background-color':'transparent',
			'color':'#305eb1'
		});
	}
}
///////////////////////////////////////////////
function HotelSearchCity(CityStr, SelectorID, InputID)
{
    var i = 0;
    if (CityStr == '')
        return;
	var CityHTMLStr='';
	$.ajax({
	    url: 'Ajax/GetArea.aspx?name=' + CityStr,
	    type: 'GET',
	    dataType: 'xml',
	    timeout: 1000,
	    error: function (xml, msg) {
	        CityHTMLStr = '<li>没有找到匹配的结果</li>'
	        $(SelectorID).html(CityHTMLStr);
	        return;
	    },
	    success: function (xml) {
	        $(xml).find('CityList').find('City').each(function () {
	            i++;
	            if (i <= 20) {
	                var it = $(this).attr('Name');
	                CityHTMLStr += '<li onclick="SetHotelSearchCityResult(\'' + it + '\', \'' + SelectorID + '\',\'' + InputID + '\')">' + it + '</li>';
	            }
	        });
	        if (CityHTMLStr == '')
	            CityHTMLStr = '<li>没有找到匹配的结果</li>'
	        $(SelectorID).html(CityHTMLStr);
	    }
	});
	$(SelectorID).fadeIn(200);
}
function GetAdList()
{
	var AdListStr='';
	var IE6AdListStr = '';
	var AdSelectorStr = '';
	var i=0;
	$.ajax({
	    url: 'Ajax/GetAd.aspx',
	    type: 'GET',
	    dataType: 'xml',
	    timeout: 1000,
	    error: function (xml, msg) {
	        return;
	    },
	    success: function (xml) {
	        $(xml).find('AdList').find('Item').each(function () {
	            var it = $(this);
	            AdListStr += '<a href="' + it.attr('LinkUri') + '" title="' + it.attr('Title') + '" target="_blank"><img alt="' + it.attr('Title') + '" src="' + it.attr('Image') + '" height="150px" width="400px"/></a>'
	            AdSelectorStr += '<span>' + (i + 1).toString() + '</span>';
	            if (i == 0) {
	                IE6AdListStr = '<a href="' + it.attr('LinkUri') + '" title="' + it.attr('Title') + '" target="_blank"><img alt="' + it.attr('Title') + '" src="' + it.attr('Image') + '" height="150px" width="400px"/></a>'
	            }
	            i++;
	        });
	        if (IsIE6) {
	            $('#DynamicAds').html(IE6AdListStr);
	        }
	        else {
	            $('#AdList').html(AdListStr);
	            // $('#AdSelector').html(AdSelectorStr);
	            AdDisplayIndex = 1;
	            setInterval('SetAdSwitch(' + i + ')', 10000);
	        }
	    }
	});
}
function GetHotelsByCityID(CityID) 
{
    var CityHTMLStr = '';
    $.ajax({
        url: 'Ajax/GetHotels.aspx?CityID=' + CityID,
        type: 'GET',
        dataType: 'xml',
        timeout: 1000,
        error: function (xml, msg) {
            return;
        },
        success: function (xml) {
            $(xml).find('HotelList').find('Hotel').each(function () {
                var name = $(this).attr('Name');
                var showName = $(this).attr('Name');
                var minprice = $(this).attr('MinPrice');
                var id = $(this).attr('ID');
                var displayMin;
                if (showName.length > 10) {
                    showName = showName.slice(0, 9) + '..';
                }

                if (minprice == -1) {
                    displayMin = '暂无数据';
                }
                else {
                    displayMin = '￥' + minprice + '起';
                }

                CityHTMLStr += '<div><a href="Hotel.aspx?ID=' + id + '" target="_blank" alt="' + name + '" title="' + name + '">' + showName + '</a><span>' + displayMin + '</span></div>';
            });
            $('#HotelList').html(CityHTMLStr + '<div class="Clear"></div>');
        }
    });
}
