/*酒店的十个城市三字码*/
var hotelCitys = new Array("PEK","SHA","CAN","SZX","HKG","MAC","CTU","CKG","HGH","SYX");
/*机票的八个城市三字码*/
var _planeCitys = new Array("PEK","SHA","CAN","SZX","CTU","NKG","WUH","HGH");
/*旅游的四个城市三字码*/
var _pkgCitys = new Array("PEK","SHA","CAN","SZX");

$(function(){

    //根据客户端所在城市进行显示数据  http://www.mangocity.com/EBWEB/getCityFromIp.jsp
	$.get("http://www.mangocity.com/EBWEB/getCityFromIp.jsp", function(data){
		 setTimeout(function() {
		    var tempData = $.trim(data);
		    if(tempData!=undefined&&tempData!=null&&tempData!="")
		    {
		        tempData = filterCity(tempData);
		        var temp = "";
		        if(tempData=="无")temp="PEK";
		        else
			    temp = getThreeWordByZhong(tempData,"hotel");
			    if(tempData=="")temp="PEK";
				try{
				   initProduct(temp);
				}catch(exception){}
			}
		   }, 10);
	});
	
	/* 初始化各个产品的数据 */
    //initProduct(getCustomerIP());
    //动态加载第三方获取IP所在城市库函数
	//addScript("one","http://j.maxmind.com/app/geoip.js");
    
    // 绑定旅游度假产品选择城市控件
	$("#id_pkgProduct_city").click(function(){
		$("#id_pkgEventHander").removeClass("downNoborder").addClass("down");
		var _position = $(this).offset();
		$("#id_pkgProduct_cityList").css({left:_position.left-1+'px',top:_position.top+18+"px"}).show();
	});
	
	// 绑定旅游度假产品选择城市控件
	$("#id_planeProduct_city").click(function(){
		$("#id_planeEventHander").removeClass("downNoborder").addClass("down");
		var _position = $(this).offset();
		$("#id_planeProduct_cityList").css({left:_position.left-1+'px',top:_position.top+18+"px"}).show();
	});
	
	$(document).click(function(event){
		if( $(event.target).attr("id") != 'id_pkgProduct_city' ){
		   $("#id_pkgProduct_cityList").hide();
		   $("#id_pkgEventHander").removeClass("down").addClass("downNoborder");
		}
		if( $(event.target).attr("id") != 'id_planeProduct_city' ){
		   $("#id_planeProduct_cityList").hide();
		   $("#id_planeEventHander").removeClass("down").addClass("downNoborder");
		}
	});

    /* 初始化广告图片*/
    readyImg();
    
    //会员登陆
 	var aliasname = getCookieHistory("aliasname");
	if(aliasname!=undefined&&aliasname !=null&&aliasname!="")
	{
	   $("#id_member_a,#id_sign,#id_zuce").remove();
	   $(".css_member_li").prepend("欢迎您&nbsp;"+aliasname+",&nbsp;<a href='http://www.mangocity.com/Member/logout/logout.action'>退出</a>");
	}
	
});
function initProduct(city)
{
    /* 初始化旅游度假产品 */
    choosePkgProductCity(city);
    
    /* 初始化特推酒店产品*/
	var tempHotelCity="";
	$.each(hotelCitys,function(i,item){
	   if(city==item) tempHotelCity = item;
	});
	if(tempHotelCity!="")chooseHotelProductCity(tempHotelCity);
	else chooseHotelProductCity("PEK");
	
	/* 初始化机票产品*/
	choosePlaneProductCity(city);
}
function filterCity(city)
{
   var tempCity = $.trim(city); 
   if(tempCity!=undefined&&tempCity!=null&&tempCity!="")
   {
       if(city.indexOf("省")>=0&&city.indexOf("市")>=0)
       {
          tempCity = tempCity.substring(city.indexOf("省")+1,city.indexOf("市"));
       }
       else if(city.indexOf("市")>=0)
       {
          tempCity = tempCity.substring(0,city.indexOf("市"));
       }
   }
   else
   {
       tempCity = "无";
   }
   return tempCity
}
/*获取客户端所在城市 start*/
function getCustomerIP()
{
    var temp = defaultCityWord;
  	try
	{
	   if((typeof geoip_city)!="undefined") temp = getThreeWordByPinyin($.trim(geoip_city()));
	}catch(exception){}
	return temp
}
/*获取客户端所在城市 end*/
/*动态加载javascript函数 start*/
function addScript(id,url){ 
   var script = document.getElementById(id); 
    var head = document.getElementsByTagName("head").item(0); 
    if (script) { 
        head.removeChild(script); 
    } 
    script = document.createElement("script"); 
    script.setAttribute("src", url); 
    script.setAttribute("id",id); 
    script.setAttribute("type","text/javascript"); 
    script.setAttribute("language","javascript"); 
    head.appendChild(script); 
    return script; 
} 
/*动态加载javascript函数 end*/
/*判断字符串的长度,双字节长度为2 start*/
function getStrLength(str)
{
    var re = /[^x00-xff]/g;
    str = str.replace(re,"*");
    return str.length;
}
/*判断字符串的长度,双字节长度为2 end*/

function subStringBySize(str,size)
{
    if(getStrLength(str)>size)
    {
        str = str.substring(0,(size-1))+"..";
    }
    return str
}
/*根据汇率计算港币*/
function getHKprice(str)
{
	var price = parseInt(str);
	price = (price+100)*0.880920+40;
	price = Math.round(price);
	return price;
}
/* jQuery图片预加载插件  start */
jQuery.fn.loadthumb = function(options) {
	options = $.extend({
		 src : ""
	},options);
	var _self = this;
	_self.hide();
	var img = new Image();
	$(img).load(function(){
		_self.attr("src", options.src);
		_self.fadeIn("slow");
	}).attr("src", options.src);  //.atte("src",options.src)要放在load后面，
	return _self;
}
function readyImg()
{
	var len  = $(".num > li").length;
	var index = 0;
	var adTimer;
	$(".num li").mouseover(function(){
	index  =   $(".num li").index(this);
	showImg(index);
	}).eq(0).mouseover();	
	//滑入 停止动画，滑出开始动画.
	$('#id_ad').hover(function(){
		 clearInterval(adTimer);
	 },function(){
		 adTimer = setInterval(function(){
			index++;
			if(index==len){index=0;}
			showImg(index);				
		  } , 5000);
	}).trigger("mouseleave");
}
var catchImgs=new Array();
function showImg(index)
{
    var flagImg=false;
    for(var k=0;k<catchImgs.length;k++)
    {
       if(catchImgs[k]==index)
       {
          flagImg= true;
          break;
       }
    }//end for
    if(!flagImg)
    {
        $("#id_ad").removeClass("ad").addClass("adfirst");
        $(".class_img").hide();
		//运用图片预加载插件
		$("#id_img_"+index).loadthumb({src:  src[index] });
		catchImgs.push(index)
	}
	else
	{
	    $("#id_ad").removeClass("adfirst").addClass("ad");
	    $(".class_img").hide();
	    $("#id_img_"+index).fadeIn("slow");
	}
	
	$(".num li").removeClass("on").eq(index).addClass("on");
	$("#id_showImage").attr("href",_href[index]).attr("name","flashAd"+index);
}
/* jQuery图片预加载插件  end */

/* 
 * list: 所有数据的集合
 * city: 城市名称三字码
 * defaultCity: 默认城市名称三字码
 * num: 预计有多少条数据
 *  
 * return list: 从所有数据的集合中，根据城市三字码返回需要的城市数据
 */
function getDataForProduct(list,city,defaultCity,num)
{
   var tempList1 = new Array();
   var tempList2 = new Array();
   var length = list.length;
   var count=1;
   var count2=1;
   if(num==undefined||num==null||num=="")num=10;
   for(var i=0;i<length;i++)
   {
	   if(list[i][0]==city)
	   {
		  tempList1.push(list[i]);
		  count++;
	   }
	   if(list[i][0]==defaultCity)
	   { 
	      if(count2<=num)
	      {
		     tempList2.push(list[i]);
		     count2++;
		  }
	   }
	   if(count>num)break;
   }//end for
   
   if(tempList1.length<=1)return tempList2;
   else return tempList1;
}

/* 机票产品 start */
function choosePlaneProductCity(city,flagHK)
{
   var len =_planeCitys.length;
   var flag=false;
   for(var i=0;i<len;i++)
   {
      if(city==_planeCitys[i])
      {
         flag = true;
         break;
      }
   }
   if(!flag)city=defaultCityWord;
   
   var planeProductList = getDataForProduct(airplaneCommendArray,city,defaultCityWord,4);
   $("#id_planeList").html(getHtmlByArrayForPlane(planeProductList));
   
   if(flagHK==undefined||flagHK==null||flagHK=="")$("#id_planeList").html(getHtmlByArrayForPlane(planeProductList));
   else $("#id_planeList").html(getHtmlByArrayForPlaneHK(planeProductList));
   
   $("#id_planeProduct_cityList").hide();
   $("#id_planeEventHander").removeClass("down").addClass("downNoborder");
   
   if(city==undefined||city==null||city=="") $("#id_planeProduct_city").text(getCityByThreeWord(defaultCityWord,"flight")+"出发");
   else $("#id_planeProduct_city").text(getCityByThreeWord(city,"flight")+"出发");
   
   chooseIntelProductCity(city);
}
/* 
 * eg:airplaneCommendArray[0]=new Array('SZX','SHA','深圳,上海','1050,2009-12-21','700,2009-12-22','700,2009-12-23','700,2009-12-24','630,2009-12-25','630,2009-12-26','620,2009-12-27','620,2009-12-27');
 * http://big5.mangocity.com/TWEBB/search.action?LineType=0&BeginCity=PEK&EndCity=KMG&BeginDate=2009-12-24&BackDate=yyyy-mm-dd
 * list: 数据的集合
 * return temp: 返回组装成的HTML代码
 */
function getHtmlByArrayForPlane(list)
{
   var length = list.length;
   var temp = '<tr><td width="34%" height="25" align="center" class="biaoti">行程</td>'
              +'<td width="23%" height="25" align="center" class="biaoti">今日</td>'
              +'<td width="19%" height="25" align="center" class="biaoti">明日</td>'
              +'<td width="24%" height="25" align="center" class="biaoti">七天最低</td></tr>';
   try
   { 
	   for(var i=0;i<length;i++)
	   {
		  var tempList1 = list[i][3].split(",");
		  var tempList2 = list[i][4].split(",");
		  var tempList3 = list[i][10].split(",");
		  if(parseInt(tempList1[0])==-1)
		  {
			 tempList1[0] = "--";
			 var strTemp = getLowerPriceForFlight(list[i]);
			 tempList3[0] = strTemp.split(",")[0];
			 tempList3[1] = strTemp.split(",")[1];
		  }
		  if(parseInt(tempList2[0])==-1)
		  {
			 tempList2[0] = "--";
		  }
		  if(parseInt(tempList3[0])==-1)
		  {
			 tempList3[0] = "--";
		  }
		  temp += '<tr><td height="32" align="left" class="tdleft">'+list[i][2].replace(",","→")+'</td>'
				  +'<td height="32" align="center"><a href="http://flight.mangocity.com/jipiao-'+list[i][0]+'-'+list[i][1]+'-'+tempList1[1].replace(/-/g,'')+'.html"><span class="price">&yen;'+tempList1[0]+'</span></a><br/>'+tempList1[1].substring(5,10)+'</td>'
				  +'<td height="32" align="center"><a href="http://flight.mangocity.com/jipiao-'+list[i][0]+'-'+list[i][1]+'-'+tempList2[1].replace(/-/g,'')+'.html"><span class="price">&yen;'+tempList2[0]+'</span></a><br/>'+tempList2[1].substring(5,10)+'</td>'
				  +'<td height="32" align="center"><a href="http://flight.mangocity.com/jipiao-'+list[i][0]+'-'+list[i][1]+'-'+tempList3[1].replace(/-/g,'')+'.html"><span class="price">&yen;'+tempList3[0]+'</span></a><br/>'+tempList3[1].substring(5,10)+'</td></tr>';
	   }//end for
   }
   catch(err)
   {
      $.getJSON("http://www.mangocity.com/EBWEB/servlet/InitServlet");
   }
   return temp;
}
function getLowerPriceForFlight(list)
{
   var temp=list[4].split(",")[0];
   var temp1 = list[4].split(",")[1];
   for(var i=5;i<list.length;i++)
   {
      var tempList = list[i].split(",");
      if(parseInt(tempList[0])>0&&parseInt(tempList[0])<parseInt(temp))
      {
         temp = tempList[0];
         temp1 = tempList[1];
      }
   }
   return temp+","+temp1;
}
/* 机票产品 end */
/* 
 * eg:airplaneCommendArray[0]=new Array('SZX','SHA','深圳,上海','1050,2009-12-21','700,2009-12-22','700,2009-12-23','700,2009-12-24','630,2009-12-25','630,2009-12-26','620,2009-12-27','620,2009-12-27');
 * 
 * list: 数据的集合
 * return temp: 返回组装成的HTML代码
 */
function getHtmlByArrayForPlaneHK(list)
{
   var length = list.length;
   var temp = '<tr><td width="34%" height="25" align="center" class="biaoti">行程</td>'
              +'<td width="23%" height="25" align="center" class="biaoti">今日</td>'
              +'<td width="19%" height="25" align="center" class="biaoti">明日</td>'
              +'<td width="24%" height="25" align="center" class="biaoti">七天最低</td></tr>';
   for(var i=0;i<length;i++)
   {
      var tempList1 = list[i][3].split(",");
      var tempList2 = list[i][4].split(",");
      var tempList3 = list[i][10].split(",");
	  temp += '<tr><td height="32" align="left" class="tdleft">'+list[i][2].replace(",","→")+'</td>'
	          +'<td height="32" align="center"><a href="http://big5.mangocity.com/TWEBB/search.action?LineType=0&BeginCity='
	          +list[i][0]+'&EndCity='+list[i][1]+'&BeginDate='+tempList1[1]+'&BackDate=yyyy-mm-dd">HK$'+getHKprice(tempList1[0])+'</a><br/>'
	          +tempList1[1].substring(5,10)+'</td>'
              +'<td height="32" align="center"><a href="http://big5.mangocity.com/TWEBB/search.action?LineType=0&BeginCity='
              +list[i][0]+'&EndCity='+list[i][1]+'&BeginDate='+tempList2[1]+'&BackDate=yyyy-mm-dd">HK$'+getHKprice(tempList2[0])+'</a><br/>'
              +tempList2[1].substring(5,10)+'</td>'
              +'<td height="32" align="center"><a href="http://big5.mangocity.com/TWEBB/search.action?LineType=0&BeginCity='
              +list[i][0]+'&EndCity='+list[i][1]+'&BeginDate='+tempList3[1]+'&BackDate=yyyy-mm-dd">HK$'+getHKprice(tempList3[0])+'</a><br/>'
              +tempList3[1].substring(5,10)+'</td></tr>';
   }//end for
   return temp
}
/* 机票产品 end */
/* 国际机票产品 start */
function chooseIntelProductCity(city)
{
   var planeIntelList = getDataForProduct(intTicketArray,city,"SHA",3);
   $("#id_planeIntel").html(getHtmlByArrayForIntel(planeIntelList));
}
/* 
 * eg:intTicketArray[0] = new Array('PEK','北京','纽约','1200','2400');
 *
 * list: 数据的集合
 * return temp: 返回组装成的HTML代码
 */
function getHtmlByArrayForIntel(list)
{
   var length = list.length;
   var temp = "";
   for(var i=0;i<length;i++)
   {
	  temp += '<tr><td width="39%" height="25" align="left">'+list[i][1]+'→'+list[i][2]+'</td>'
	          +'<td width="28%" height="25" align="center">往返</td>'
	          +'<td width="33%" height="25" align="center"><a href="http://www.mangocity.com/GDSWeb/intquery/searchCondition_initCondition.action"><span class="price">&yen;'+list[i][3]+'</span></a></td></tr>';
   }//end for
   return temp
}
/* 国际机票产品 end */

/* 特推酒店产品 start */
function chooseHotelProductCity(city)
{
   if(city==undefined||city==null||city=="") setHotelCityColor(defaultCityWord);
   else setHotelCityColor(city);
   var hotelProductList = getDataForProduct(htlCommendArray,city,defaultCityWord,8);
   $("#id_hotelList").html(getHtmlByArrayForHotel2(hotelProductList,city));
   choosePinpaiProductCity(city);
   
}
function setHotelCityColor(city)
{
   var tempChildren = $(".hotelCity_");
   
   tempChildren.removeClass("current");
   tempChildren.each(function(){
      if(city == getThreeWordByCity($.trim($(this).text()),"hotel"))
      {
        $(this).addClass("current");
      }
   });
}
/* 
 * eg:htlCommendArray[0] = new Array('CAN' , '30000440' , '广州花园酒店' , '19' , 'RMB' , '770');
 *
 * list: 数据的集合
 * return temp: 返回组装成的HTML代码
 */
function getHtmlByArrayForHotel(list)
{
   var length = list.length;
   var temp = "";
   for(var i=0;i<length;i++)
   {
	  var second = ""
	  if((i+1)%2==0)second = "class='second'";
	  temp += "<li "+ second + "><a href='http://hotel.mangocity.com/jiudian-"+list[i][1]+".html"
	          +"' title='"+list[i][2]+"'>"+list[i][2]+"</a><span class='price'>&yen;"+list[i][5]+"起</span></li>";
   }//end for
   return temp
}
function getHtmlByArrayForHotel2(list,city)
{
   var length = list.length;
   var temp = "";
   var cashSign="&yen;";
   if(city=="HKG"||city=="MAC")cashSign="HK$";
   for(var i=0;i<length;i++)
   {
	  var second = ""
	  if((i+1)%2==0)second = "class='second'";
	  temp += "<li "+ second + "><a href='http://hotel.mangocity.com/jiudian-"+list[i][1]+".html"
	          +"' title='"+list[i][2]+"'>"+list[i][2]+"</a><span class='price'>"+cashSign+list[i][5]+"起</span></li>";
   }//end for
   return temp
}
/* 特推酒店产品 end */

/* 品牌酒店产品 start */
function choosePinpaiProductCity(city)
{
   var pinpaiProductList = getDataForProduct(hotelBrandArray,city,defaultCityWord,10);
   $("#id_pinpai").html(getHtmlByArrayForPinpai(pinpaiProductList));
}
/* 
 * eg:hotelBrandArray[0] = new Array('PEK','1583','如家','HOME INN');
 *
 * list: 数据的集合
 * return temp: 返回组装成的HTML代码
 */
function getHtmlByArrayForPinpai(list)
{
   var num=10;
   var length = list.length;
   var temp = "";
   for(var i=0;i<length;i++)
   {
      if(i==0) temp += '<tr>';
      else if(i>2&&i%5==0)temp += '</tr><tr>';
      temp += '<td width="20%" height="22" align="left" title="'+list[i][2]+'"><a href="http://hotel.mangocity.com/jiudian-'+list[i][1]+'-'+list[i][0].toLowerCase()+'.html">'+ subStringBySize(list[i][2],6) +'</a></td>';
   }//end for
   if(length<num)
   {
      for(var j=0;j<(num-length);j++)
      {
         var count = length-1+j;
         if(count==0) temp += '<tr>';
         else if(count>2&&count%5==4)temp += '</tr><tr>';
         temp +='<td width="20%" height="20" align="left"></td>';
      }
   }
   temp +="</tr>";
   return temp
}
/* 品牌酒店产品 start */

/* 旅游度假产品 start */
function choosePkgProductCity(city)
{
   var len =_pkgCitys.length;
   var flag=false;
   for(var i=0;i<len;i++)
   {
      if(city==_pkgCitys[i])
      {
         flag = true;
         break;
      }
   }
   if(!flag)city=defaultCityWord;
   
   var pkgProductList = getDataForProduct(intTourArray,city,defaultCityWord,9);
   $("#id_pkgProduct").html(getHtmlByArrayForPkg(pkgProductList));
   $("#id_pkgProduct_cityList").hide();
   $("#id_pkgEventHander").removeClass("down").addClass("downNoborder");
   
   if(city==undefined||city==null||city=="") $("#id_pkgProduct_city").text(getCityByThreeWord(defaultCityWord,"flight")+"出发");
   else $("#id_pkgProduct_city").text(getCityByThreeWord(city,"flight")+"出发");
}
/* 
 * eg:intTourArray[0] = new Array('PEK','1北京凤凰特色2天巴士自由行','2101','301001');
 *
 * list: 数据的集合
 * return temp: 返回组装成的HTML代码
 */
function getHtmlByArrayForPkg(list)
{
   var length = list.length;
   var temp = "";
   for(var i=0;i<length;i++)
   {
	  var first = "";
	  if(i==0)first = "class='first'";
	  temp += "<li "+ first + "><a href='"
	          +"http://www.mangocity.com/MangoPkgWeb/queryProductAction.do?operate=queryOneProduct&PkgProductLineId="+list[i][3]
	          +"' title='"+list[i][1]+"'>"+list[i][1]+"</a><span class='price'>&yen;"+list[i][2]+"起</span></li>";
   }//end for
   return temp
}
/* 旅游度假产品 end */

//搜索框button变换
function change_searchbox(obj,num) {
	var box_name = "box" + num;
	$(".css_box").hide();
	$("#"+box_name).show();
	$(".css_find_a").removeClass("current");
	$(obj).addClass("current");
}