/*date:2013/11/2
**developer:linxiongwei
*/
$("document").ready(function(){
   dropMenu(".detail_nav ul li");
   function dropMenu(obj){
		$(obj).each(function(){
			var theSpan = $(this);
			var theMenu = theSpan.find(".secondNav");
			var tarHeight = theMenu.height();
			theMenu.css({height:0,opacity:0});
			theSpan.hover(function(){
				theSpan.addClass("navHover");
				theMenu.stop().show().animate({height:tarHeight,opacity:0.95},400);
			},function(){
				theSpan.removeClass("navHover");
				theMenu.stop().animate({height:0,opacity:0},400,function(){
					$(this).hide();
				});
			});
		});
	}
});

$(function(){
	var hb=["石家庄市","唐山市","秦皇岛市","邯郸市","邢台市","保定市","张家口市","承德市","沧州市","廊坊市","衡水市"],
		sx=["太原市","大同市","阳泉市","长治市","晋城市","朔州市","晋中市","运城市","忻州市","临汾市","吕梁市"],
		nmg=["呼和浩特市","包头市","乌海市","赤峰市","通辽市","鄂尔多斯市","呼伦贝尔市","巴彦淖尔市","乌兰察布市","兴安盟","锡林郭勒盟","阿拉善盟"],
		ln=["沈阳市","大连市","鞍山市","抚顺市","本溪市","丹东市","锦州市","营口市","阜新市","辽阳市","盘锦市","铁岭市","朝阳市","葫芦岛市"],
		jl=["长春市","吉林市","四平市","辽源市","通化市","白山市","松原市","白城市","延边自治州"],
		hlj=["哈尔滨市","齐齐哈尔市","鸡西市","鹤岗市","双鸭山市","大庆市","伊春市","佳木斯市","七台河市","牡丹江市","黑河市","绥化市","大兴安岭地区"],
		js=["南京市","无锡市","徐州市","常州市","苏州市","南通市","连云港市","淮安市","盐城市","扬州市","镇江市","泰州市","宿迁市"],
		zj=["杭州市","宁波市","温州市","嘉兴市","湖州市","绍兴市","金华市","衢州市","舟山市","台州市","丽水市"],
		ah=["合肥市","芜湖市","蚌埠市","淮南市","马鞍山市","淮北市","铜陵市","安庆市","黄山市","滁州市","阜阳市","宿州市","六安市","亳州市","池州市","宣城市"],
		hj=["福州市","厦门市","莆田市","三明市","泉州市","漳州市","南平市","龙岩市","宁德市"],
		jx=["南昌市","景德镇市","萍乡市","九江市","新余市","鹰潭市","赣州市","吉安市","宜春市","抚州市","上饶市"],
		sd=["济南市","青岛市","淄博市","枣庄市","东营市","烟台市","潍坊市","济宁市","泰安市","威海市","日照市","莱芜市","临沂市","德州市","聊城市","滨州市","菏泽市"],
		hn=["郑州市","开封市","洛阳市","平顶山市","安阳市","鹤壁市","新乡市","焦作市","济源市","濮阳市","许昌市","漯河市","三门峡市","南阳市","商丘市","信阳市","周口市","驻马店市"],
		hub=["武汉市","黄石市","十堰市","宜昌市","襄樊市","鄂州市","荆门市","孝感市","荆州市","黄冈市","咸宁市","随州市","恩施自治州","仙桃市","潜江市","天门市","神农架林区"],
		hun=["长沙市","株洲市","湘潭市","衡阳市","邵阳市","岳阳市","常德市","张家界市","益阳市","郴州市","永州市","怀化市","娄底市","湘西自治州"],
		gd=["广州市","韶关市","深圳市","珠海市","汕头市","佛山市","江门市","湛江市","茂名市","肇庆市","惠州市","梅州市","汕尾市","河源市","阳江市","清远市","东莞市","中山市","潮州市","揭阳市","云浮市"],
		gx=["南宁市","柳州市","桂林市","梧州市","北海市","防城港市","钦州市","贵港市","玉林市","百色市","贺州市","河池市","来宾市","崇左市"],
		han=["海口市","三亚市","五指山市","琼海市","儋州市","文昌市","万宁市","东方市","定安县","屯昌县","澄迈县","临高县","白沙县","昌江县","乐东县","陵水县","保亭县","琼中县","西沙群岛","南沙群岛","中沙群岛"],
		sc=["成都市","自贡市","攀枝花市","泸州市","德阳市","绵阳市","广元市","遂宁市","内江市","乐山市","南充市","眉山市","宜宾市","广安市","达州市","雅安市","巴中市","资阳市","阿坝自治州","甘孜自治州","凉山自治州"],
		gz=["贵阳市","六盘水市","遵义市","安顺市","铜仁地区","黔西南自治州","毕节地区","黔东南自治州","黔南自治州"],
		yn=["昆明市","曲靖市","玉溪市","保山市","昭通市","丽江市","普洱市","临沧市","楚雄自治州","红河自治州","文山自治州","西双版纳州","大理自治州","德宏自治州","怒江傈自治州","迪庆自治州"],
		xz=["拉萨市","昌都地区","山南地区","日喀则地区","那曲地区","阿里地区","林芝地区"],
		sgx=["西安市","铜川市","宝鸡市","咸阳市","渭南市","延安市","汉中市","榆林市","安康市","商洛市"],
		gs=["兰州市","嘉峪关市","金昌市","白银市","天水市","武威市","张掖市","平凉市","酒泉市","庆阳市","定西市","陇南市","临夏自治州","甘南自治州"],
		qh=["西宁市","海东地区","海北自治州","黄南自治州","海南自治州","果洛自治州","玉树自治州","海西自治州"],
		nx=["银川","石嘴山","吴忠","固原","中卫"],
		xj=["乌鲁木齐市","克拉玛依市","吐鲁番地区","哈密地区","昌吉自治州","博尔塔拉州","巴音郭楞州","阿克苏地区","克孜勒苏州","喀什地区","和田地区","伊犁自治州","塔城地区","阿勒泰地区","石河子市","阿拉尔市","图木舒克市","五家渠市"],
		tw=["台北","高雄","台南","台中","金门","基隆","新竹","嘉义","新北","南投县","宜兰县","桃园县","苗栗县","彰化县","云林县","屏东县","台东县","花莲县","澎湖县"],
		xg=["香港岛","九龙","新界"],
		am=["澳门半岛","离岛"];
	var array=[hb,sx,nmg,ln,jl,hlj,js,zj,ah,hj,jx,sd,hn,hub,hun,gd,gx,han,sc,gz,yn,xz,sgx,gs,qh,nx,xj,tw,xg,am];
	var array2=["hb","sx","nmg","ln","jl","hlj","js","zj","ah","hj","jx","sd","hn","hub","hun","gd","gx","han","sc","gz","yn","xz","sgx","gs","qh","nx","xj","tw","xg","am"];
	var currArea=$(".area_name").text();
	
	$(".addr_city a").click(function(){
		_this=$(this);
		areaShow(_this);
	});

	function areaShow(obj){
		var index=obj.parent().index();
		var currIndex=parseInt(index/7);
		$(".addr_city a").removeClass("addr_select");
		obj.addClass("addr_select");
		$(".addr_second").hide();
		for(var i in array2){
			if(obj.attr("addr")==array2[i]){
				$(".addr_second ul").empty().removeAttr("addr");
				$(".addr_city").find(".addr_second").eq(currIndex).children().attr("addr","hb").parent().show();
				for(var j in array[i]){
					$(".addr_city").find(".addr_second").eq(currIndex).children().append("<li><a addr='"+array[i][j]+"'>"+array[i][j]+"</a></li>");
				}
			}
		}
	}
	
	function updateFreight()
	{
		var region = [];
		$(".area_box a.addr_select").each(function(){
			region.push($(this).text());
		});
		var shop_id = $("input[name=shop_id]").val();
		if (region) {
			$.post('/product/ajax_upload_freight/', {'region':region.join(','), 'shop_id':shop_id}, function(json){
				if (json.flag == 1) {
					$("#shippingFreightDiv").html(json.data);
				}
			}, 'json');
		}
	}

	$(".area_name").click(function(){
		currArea=$(".area_name").text();
		if(!$(".area_box").hasClass("arew_show")){
			$(".area_box").addClass("arew_show");
		}else{
			$(".area_box").removeClass("arew_show");
		}
	});
	$(".area_close").click(function(){
		$(".area_box").removeClass("arew_show");
	});
	$(".addr_second a,.addr_zxCity a").live("click",function(){
		var val=$(this).text();
		var addr=$(this).parents("ul").attr("addr");
		$(".addr_second a").removeClass('addr_select');
		$(this).addClass('addr_select');
		$(".area_name").text(val).attr("addr",addr);
		$(".area_box").removeClass("arew_show");
		updateFreight();
	});
	
	$(".addr_city a").click(function(){
		$(".addr_city a").removeClass('addr_select');
		$(".addr_zxCity a").removeClass('addr_select');
		$(this).addClass('addr_select');
	});
	$(".addr_zxCity a").click(function(){
		$(".addr_city a").removeClass('addr_select');
		$(".addr_zxCity a").removeClass('addr_select');
	});
	
	for(var i in array){
		for(var j in array[i]){
			if(array[i][j]==currArea){
				$(".addr_city a[addr="+array2[i]+"]").trigger("click");
				$(".addr_second a[addr="+currArea+"]").addClass('addr_select');
				return false;//终止,注意这循环之后写的代码不再执行
			}
		}
	}
});