// JavaScript Document

var partnershop = Class.create({
	aShops: {},
	objects: null,
	API: '/shopinfo.php',
	initDone: false,
	defaultSets: {
		province: 1,
		city: 1,
		area: 11,
		shop: 1
	},
	aEmptyShopsInfo: {
		title: '--',
		address: '--',
		opentime: '--',
		telephone: '--',
		map: '/img/passport/map/shops/empty.jpg'
	},

	initialize: function(obj, sets) { // 初始化
		// 初始化目标
		var err = '';
		if (
			(err=(obj.sltPvn=$(obj.sltPvn))?false:'sltPvn')
			||(err=(obj.sltCty=$(obj.sltCty))?false:'sltCty')
			||(err=(obj.sltArea=$(obj.sltArea))?false:'sltArea')
			||(err=(obj.lstShops=$(obj.lstShops))?false:'lstShops')
			||(err=(obj.infShopName=$(obj.infShopName))?false:'infShopName')
			||(err=(obj.infShopOpTm=$(obj.infShopOpTm))?false:'infShopOpTm')
			||(err=(obj.infShopAddr=$(obj.infShopAddr))?false:'infShopAddr')
			||(err=(obj.infShopTel=$(obj.infShopTel))?false:'infShopTel')
			||(err=(obj.lstShowAll=$(obj.lstShowAll))?false:'lstShowAll')
		) {
			return system_window_alert('初始化失败！错误信息: 无法锁定objects.'+err+'的对象，请检查传参是否正确！');
		}
		this.objects = obj;

		// 更新设置（如果存在）
		if (sets!=null&&typeof(sets)=='object') {
			for (var i in sets) {
				this.defaultSets[i] = sets[i];
			}
		}

		// 初始化方法
		this.mkObjectFunc();

		// 默认执行
		this.getProvince();
	},

	mkObjectFunc: function() { // 目标方法
		var cls = this;
		Event.observe(this.objects.sltPvn, 'change', function(){cls.getCities(this.getValue());});
		Event.observe(this.objects.sltCty, 'change', function(){cls.getAreas(this.getValue())});
		Event.observe(this.objects.sltArea, 'change', function(){cls.getShops(this.getValue(),cls.objects.sltCty.getValue(),cls.objects.sltPvn.getValue())});
		Event.observe(this.objects.lstShowAll, 'click', function(){cls.getProvince(0);cls.getShops(0,0,0)});
	},

	getProvince: function() { // 获取省、直辖市
		//var cls = this;
		Element.update(this.objects.sltCty, '<option vlaue="0">-城市-</option>');
		Element.update(this.objects.sltArea, '<option vlaue="0">-区/县-</option>');
		new _ajax({api:this.API, queryObject:{'do':'get_province'}, callFunc:[this, 'parseCode']});
	},

	getCities: function(pid) { // 获取城市
		//var cls = this;
		Element.update(this.objects.sltArea, '<option vlaue="0">-区/县-</option>');
		new _ajax({api:this.API, queryObject:{'do':'get_cities','pid':pid}, callFunc:[this, 'parseCode']});
		//_ajax({url:this.API, obj:'do=get_cities&pid='+pid, myFn:function(xml){cls.parseCode(xml)}});
	},

	getAreas: function(cid) { // 获取区、县
		//var cls = this;
		new _ajax({api:this.API, queryObject:{'do':'get_areas','cid':cid}, callFunc:[this, 'parseCode']});
		//_ajax({url:this.API, obj:'do=get_areas&cid='+cid, myFn:function(xml){cls.parseCode(xml)}});
	},

	getShops: function(aid,cid,pid) { // 获取实体店
		//var cls = this;
		//_ajax({url:this.API, obj:'do=get_shops&aid='+aid+'&cid='+cid+'&pid='+pid, myFn:function(xml){cls.parseCode(xml)}});
		new _ajax({api:this.API, queryObject:{'do':'get_shops','aid':aid,'cid':cid,'pid':pid}, callFunc:[this, 'parseCode']});
	},

	buildProvince: function(xml) { // 构建省、直辖市列表
		var lst = xml.getElementsByTagName('item');
		var s = "<option value='0'>-省/直辖市-</option>";
		for (var i=0;i<lst.length;i++) {
			s += "<option value='"+Element.nodeText(lst[i].getElementsByTagName('provinceid')[0])+"'>"+Element.nodeText(lst[i].getElementsByTagName('title')[0])+"</option>";
		}
		Element.update(this.objects.sltPvn, s);
		this.getCities(this.objects.sltPvn.getValue());
	},

	buildCities: function(xml) { // 构建城市列表
		var lst = xml.getElementsByTagName('item');
		var s = "<option value='0'>-城市-</option>";
		for (var i=0;i<lst.length;i++) {
			s += "<option value='"+Element.nodeText(lst[i].getElementsByTagName('cityid')[0])+"'>"+Element.nodeText(lst[i].getElementsByTagName('title')[0])+"</option>";
		}
		Element.update(this.objects.sltCty, s);
		this.getAreas(this.objects.sltCty.getValue());
	},

	buildAreas: function(xml) { // 构建区、县列表
		var lst = xml.getElementsByTagName('item');
		var s = "<option value='0'>-区/县-</option>";
		for (var i=0;i<lst.length;i++) {
			s += "<option value='"+Element.nodeText(lst[i].getElementsByTagName('areaid')[0])+"'>"+Element.nodeText(lst[i].getElementsByTagName('title')[0])+"</option>";
		}
		Element.update(this.objects.sltArea, s);
		this.getShops(this.objects.sltArea.getValue(),this.objects.sltCty.getValue(),this.objects.sltPvn.getValue());
	},

	buildShops: function(xml) { // 构建实体店列表
		var lst = xml.getElementsByTagName('item');
		// shop's list
		var s = '', cls = this, n = null;
		this.aShops = [];
		for (var i=0;i<lst.length;i++) {
			//s += "<li><a href='javascript:void(0)' id='sp_"+Element.nodeText(lst[i].getElementsByTagName('shopid')[0])+"'>"+Element.nodeText(lst[i].getElementsByTagName('title')[0])+"</a></li>";
			s += "<li id='sp_"+Element.nodeText(lst[i].getElementsByTagName('shopid')[0])+"'>"+Element.nodeText(lst[i].getElementsByTagName('title')[0])+"</li>";
			this.aShops[Element.nodeText(lst[i].getElementsByTagName('shopid')[0])] = {
				title: Element.nodeText(lst[i].getElementsByTagName('title')[0]),
				address: Element.nodeText(lst[i].getElementsByTagName('address')[0]),
				opentime: Element.nodeText(lst[i].getElementsByTagName('opentime')[0]),
				telephone: Element.nodeText(lst[i].getElementsByTagName('telephone')[0]),
				map: Element.nodeText(lst[i].getElementsByTagName('locationimg')[0])
			};
		}
		if (s=='') s = "<li><a>这个地区目前还没有实体门店</a></li>";
		n = lst.length==0?0:parseInt(Element.nodeText(lst[0].getElementsByTagName('shopid')[0]));
		Element.update(this.objects.lstShops, s);
		lst = this.objects.lstShops.getElementsByTagName('li');
		for (i=0;i<lst.length;i++) {
			Event.observe(lst[i], 'mouseover', function(){Element.addClassName(this, 'li_hover')});
			Event.observe(lst[i], 'mouseout', function(){Element.removeClassName(this, 'li_hover')});
			Event.observe(lst[i], 'click', function(){cls.synShopInfo(this.readAttribute("id"))});
		}
		this.synShopInfo(n);
	},

	synShopInfo: function(idx) { // 同步店铺信息
		//alert(idx+" : "+typeof(idx) + " : " + (typeof(idx)=='string' ? parseInt(idx.substring(3)) : idx));
		idx = typeof(idx)=='string' ? parseInt(idx.substring(3)) : idx;
		var a = this.aShops[idx];
		if (a==null) {
			a = this.aEmptyShopsInfo;
		}
		Element.update(this.objects.infShopName, a.title);
		Element.update(this.objects.infShopTel, a.telephone);
		Element.update(this.objects.infShopOpTm, a.opentime);
		Element.update(this.objects.infShopAddr, a.address);
		//alert(a.map);
		Element.setStyle(this.objects.infShopMap, {background:"url("+a.map+") center  no-repeat"});
	},

	setSelectOptionDefault: function(obj, val) {
		var lst = obj.getElementsByTagName('option');
		for (i=0;i<lst.length;i++) {
			if (lst[i].value==val) {
				obj.selectedIndex = i;
				lst[i].selected = true;
				break;
			}
		}
	},

	setDefaultShop: function() {

	},

	parseCode: function(xml) { // 分析返回码
		var code = Element.nodeText(xml.getElementsByTagName('parsecode')[0]);
		var bs = new Base();

		switch(code) {
			case 'shop_province_list':
			this.buildProvince(xml);
			break;

			case 'shop_cities_list':
			this.buildCities(xml);
			break;

			case 'shop_areas_list':
			this.buildAreas(xml);
			break;

			case 'shop_shops_list':
			this.buildShops(xml);
			break;

			default:
			bs._openTips("程序处理异常，请重新加载页面！\n\nERROR CODE: "+code+"\n\n\n ");
			break;
		}
	}
});