/**
 * @author lovemaggie
 */
var apiUrl = 'http://api.125car.com';
var jsonpCallbackArg = '&jsoncallback=?';

//var listArray = ['country', 'brand', 'series', 'model'];
//var listStringArray = ['国别', '品牌', '系列', '车型'];
var listArray = ['brand', 'series', 'model'];
var listStringArray = ['品牌', '系列', '车型'];

var haveCountryArea = false;
var haveBrandArea = false;
var haveSeriesArea = false;
var haveModelArea = false;

function blankFunc() {};

function getIndexInList(appName) {
	
	for ( i = 0; i < listArray.length; i++ ) if ( listArray[i] == appName ) return i;
	return -1;
}

function getParentName(appName) {
	
	var indexOf = getIndexInList(appName);
	if ( indexOf < 1 ) return '';
	else return listArray[indexOf - 1];
}

function getChildName(appName) {
	
	var indexOf = getIndexInList(appName);
	if ( indexOf == listArray.length - 1 ) return '';
	else return listArray[indexOf + 1];
}

function getList(url, callback) { getUrlByAjax(url, callback); }


function baseCBSM(appName, data) {
	
	this.list = listArray;
	
	this.data = $.evalJSON($.toJSON(data));
	
	this.name = appName;
	this.index = getIndexInList(this.name);
	this.isTop = this.index == 0;
	this.isBottom = this.index == this.list.length - 1;
	
	this._areaId = 'InnerArea';
	this._htmlVId = 'VId';
	this._htmlId = 'Id';
	this._elementName = 'Id';
	this._elementValue = 'Name';
	this._elementString = listStringArray[this.index];
	
	
	
	this.parentName = this.isTop ? null : getParentName(this.name);
	this.parentAreaId = this.isTop ? null : this.parentName + this._areaId;
	this.parentIndex = this.isTop ? null : getIndexInList(this.parentName);
	this.parentHtmlVId = this.isTop ? null : this.parentName + this._htmlVId;
	this.parentHtmlId = this.isTop ? null : this.parentName + this._htmlId;
	
	this.childName = this.isBottom ? null : getChildName(this.name);
	this.childAreaId = this.isBottom ? null : this.childName + this._areaId;
	this.childIndex = this.isBottom ? null : getIndexInList(this.childName);
	this.childHtmlVId = this.isBottom ? null : this.childName + this._htmlVId;
	this.childHtmlId = this.isBottom ? null : this.childName + this._htmlId;
	
	this.areaId = this.name + this._areaId;
	this.htmlVId = this.name + this._htmlVId;
	this.htmlId = this.name + this._htmlId;
	this.elementName = this.name + this._elementName;
	this.elementValue = this.name + this._elementValue;
	
	this.apiUrl = apiUrl + '/' + this.name + '/getlisttojson' + ( this.isTop ? '?' : '/' + this.parentHtmlId + '/0?' ) + jsonpCallbackArg;
	
	this.haveParentArea = false;
	this.haveChildArea = false;
	
	this.getListCallbackFunc = blankFunc;
	this.getListAgainCallbackFunc = blankFunc;
	this.runParentChangeCallbackFunc = blankFunc;
	this.haveChildAreaFunc = blankFunc;
	
	this.getListCount = 0;
	this.haveResponseData = false;
	
	this.init = function() { /*this.data = $.evalJSON($.toJSON(data));*/ }
	this.writeArea = function() { document.write('<span id="' + this.areaId + '">data loading...</span>'); }
	this.getOneValue = function(a, b) { return $('#' + a).attr('value') ? $('#' + a).attr('value') : $('#' + b).attr('value'); }
	this.getUrl = function(id) { return parseInt(id) ? this.apiUrl.replace('0', parseInt(id)) : this.apiUrl; }
	this.getList = function() {

		var parentVIdObjValue = parseInt($('#' + this.parentHtmlVId).attr('value'));
		var data = this.data;
		
		if ( parentVIdObjValue && this.haveParentArea ) {
			
			data = ((this.data[parentVIdObjValue] != null && typeof(this.data[parentVIdObjValue]) != "undefined") ? this.data[parentVIdObjValue] : new Array());
			
		} else if ( !this.haveParentArea ) {
		} else {
			
			data = new Array();
		}
		
		this.runCallback(data);
		
		/**
		this.getListCount++;
		getList(this.getUrl(this.getOneValue(this.parentHtmlId, this.parentHtmlVId)), this.getListCallbackFunc);
		*/
	 }
	this.setHtmlIdAndHtmlVId = function() {
		
		var objVId = $('#' + this.htmlVId);
		var objId = $('#' + this.htmlId);
	
		if ( this.getListCount > 1 ) {

			if ( !this.haveResponseData ) objId.attr('value', '');
			
			if ( objVId.attr('value') && this.haveResponseData ) objId.attr('value', objVId.attr('value'));
			else if ( objId.attr('value') ) objVId.attr('value', objId.attr('value'));
			else objId.attr('value', '')
			
		} else {

			if ( !this.haveResponseData ) objId.attr('value', '');
			
			if ( objId.attr('value') ) objVId.attr('value', objId.attr('value'));
			else if ( objVId.attr('value') && this.haveResponseData  ) objId.attr('value', objVId.attr('value'));
			else objId.attr('value', '');
		}
	}
	this.runCallback = function(data) {
		
		if ( Object == data.constructor || ( Array == data.constructor && data.length > 0 ) ) {
			
			this.haveResponseData = true;
		} else {
			
			this.haveResponseData = false;
		}

		var objAreaId = $('#' + this.areaId);
		objAreaId.html(jsonToHtmlSelect(data, this.htmlVId, this.elementName, this.elementValue, this._elementString));
		
		this.setHtmlIdAndHtmlVId();
		
		var objVId = $('#' + this.htmlVId);
		var objId = $('#' + this.htmlId);
		
		objVId.change(function() {
			
			objId.attr('value', objVId.attr('value'));
		});
		
		//run once on init
		if ( this.haveChildArea ) {

			this.haveChildAreaFunc();
		}
		
		this.runParentChange(this);
	}
	this.runParentChange = function(thisObj) {

		if ( !this.isTop ) {
		
			var objParentAreaId = $('#' + this.parentAreaId);
			var objParentVId = $('#' + this.parentHtmlVId);
			var objParentId = $('#' + this.parentHtmlId);
			
			objParentVId.unbind();
			objParentVId.change(function() {
				
				if (objParentVId.attr('value')) objParentId.attr('value', objParentVId.attr('value'));
				else objParentVId.attr('value', objParentId.attr('value'));
				
				$('#' + thisObj.areaId).html('data re loading...');
				thisObj.getList();
			});
		}
	}
	this.runChildInit = function() {}
}