var Express = (function(){
	var template = '<div class="gridview" header="1" footer="0" actived="1">\
        <table>\
          <thead>\
            <tr>\
              <th width="120">时间</th>\
              <th>流转</th>\
            </tr>\
          </thead>\
          <tbody>\
            <tr>\
              <td align="center">$time</td>\
              <td>$context</td>\
            </tr>\
          </tbody>\
        </table>\
      </div>';

	return {
		router: function(){
			alert('快递参数载入失败，请重开软件！');
		},
		getRealExpressName: function(expressName){
			var Hash = {
				'申通': '申通快递',
				'EMS': 'EMS',
				'顺丰': '顺丰速运',
				'韵达': '韵达快递',
				'圆通': '圆通速递',
				'中通': '中通速递',
				'汇通': '汇通快运',
				'天天': '天天快递'
			};
			
			for(var express in Hash){
				if(expressName.indexOf(express) == 0){
					return Hash[express];
				}
			}
			return expressName;
		},
		showRouter2: function(type, postid, companyName){
			$.ajax({
				url: parseData2Template('http://www.kuaidi100.com/query?type=$type&postid=$postid', {
					type: type,
					postid: postid
				}),
				dataType: 'text',
				success: function(routerText){
					var Result;
					try{
						Result = (new Function('return ' + routerText))();
					}catch(e){}
					
					if(!Result || !Result.status){
						alert('快递查询失败：' + routerText);
						return false;
					}
					
					if(Result.status != 200 || !Result.data){
						alert('快递查询失败：' + Result.message);
						return false;
					}
					
					Dialog.alert({
						title: '快递跟踪 - ' + companyName + ':' + postid,
						content: template,
						style: {
							width: 550,
							height: 351
						},
						action: function($content){
							$content.find('.gridview').gridview().trigger('refresh', [{
								list: Result.data.sort(function(a, b){
									return a.time>b.time?1:-1;
								})
							}]);
						}
					});
				},
				error: function(jqXhr){
					alert('快递查询错误：' + jqXhr.responseText);
				}
			});
		},
		router: function(companyName, postid){
			companyName = this.getRealExpressName(companyName);
			var type = '';
			$.each(jsoncom.company, function(index, Company){
				if(companyName.toUpperCase() == Company.companyname){
					type = Company.code;
					return false;
				}
			});
			this.showRouter(type, postid, companyName);
		}
	}
})();

$(function(){
	$.ajax({
		url: 'http://www.kuaidi100.com/js/share/company.js',
		cache: false,
		dataType: 'script',
		success: function(){
			if(typeof(jsoncom) == 'undefined' || !jsoncom.company){
				return false;
			}else{
				Express.showRouter = Express.showRouter2;
			}
		}
	});
});