﻿	$(document).ready(function(){
		objs=$(".item_pic");
		for(lop=0;lop < $(objs).length;lop++){
			fix_mb_pic($(objs[lop]),45,45);
		}

		if($("#county").val() == ""){
			initCounty();
		}
		$("#city").change(function(){
			initCounty();
		});
	});
	
	function initCounty(){
		$("#county").removeAttr("disabled");
		$("#county > option").remove();
		$("#county").append("<option value=\"\"> </option>");
		if( $("#city").val() == "新竹市" || $("#city").val() == "嘉義市" ){
			$("#county").attr("disabled","disabled");
		}else if( $("#city").val() == "" || $("#city").val() == " " ){
		}else{
			$.ajax({
				type: "POST",
				url: "/public/get_city.asp",
				data: "city="+$("#city").val(),
				async: false,
				error: function(){alert("鄉鎮市區資料載入錯誤！");},
				success: function(CountyData){
					arrConuties=CountyData.split(",");
					for(lop=0;lop < arrConuties.length;lop++){
						$("#county").append("<option value=\""+arrConuties[lop]+"\">"+arrConuties[lop]+"</option>");
					}
				}
			});
		}
	}
	
	function fix_mb_pic(obj,fixW,fixH){
		var image = new Image();
		obj_img = $(obj).find("img");
		image.src = $(obj_img).attr("src");
		width = image.width;
		height = image.height;
		if(width > fixW && height > fixH){
			fixRatioW = width / fixW;
			fixRatioH = height / fixH;
			fixRatio=fixRatioW > fixRatioH?fixRatioH:fixRatioW;
			width = Math.round(width / fixRatio);
			height = Math.round(height / fixRatio);
			
			$(obj_img).css("width",width);
			$(obj_img).css("height",height);
		}
		
		pic_W=parseInt($(obj).css("width"));
		pic_H=parseInt($(obj).css("height"));
		pic_SW=parseInt($(obj).attr("scrollWidth"));
		pic_SH=parseInt($(obj).attr("scrollHeight"));
		pic_SL=parseInt((pic_SW-pic_W)/2);
		pic_ST=parseInt((pic_SH-pic_H)/2);
		$(obj).attr("scrollLeft",pic_SL);
		$(obj).attr("scrollTop",pic_ST);
	}
