var App = {};

var brand_models=[];

// select an option by marking the 'selected' attribute of the 'option' 
App.selectValue = function(obj, value){
	var has = false;
	$("option", obj).each(function(){
		if($(this).val() == value){
			$(this).attr("selected", true);
			has = true;
		}
	});
	return has;
};

App.changeBrand = function(brand,preModel){
	var ret = false;
	try{
		ret = App.selectValue(App.brandSel, brand);
	} catch(e){
		ret = true;
	}
	if(preModel!=null && !ret){//需要初始化默认选项
		ret=true;
	}
	
	if(ret){
		var logosrc = "logo_nokia.gif";		
		var netprovider = "G"; // use the 'G' as net provider constantly 
		
		for(var i=0;i<cm_brands_models.length;i++){
			if(cm_brands_models[i].name==brand){
				// cm_brands_models will be created by mobile2009/jsinc/brandlist.jsp
				logosrc=cm_brands_models[i].logo;
			}
		}
		//ajax拉取机型数组回来
		$.get(
		  'mobile2009/modellist.jsp',
		  {
			brand: brand
		  },
		  function(res){
			if(res){
				try{
					res = eval("(" + res + ")");		
					brand_models[netprovider+"_"+brand]=res;					
					var mobiles = brand_models[netprovider+"_"+brand];
					if(logosrc){
						$("#brand_img").attr("src",MOBILE_IMAGES + logosrc);
						$("#brand_img").show();
					}						
					if(mobiles){
						// create options array to hold list of models
						var options_arr = [];
						for(var i=0;i<mobiles.length;i+=1){
							var mobile  = mobiles[i];
							options_arr.push("<option mqqversion=\""+mobile.mqqversion+"\" mqqtime=\""+mobile.mqqtime+"\" logo=\""+mobile.logo+"\" value=\""+ mobile.name +"\">"+ mobile.name + "</option>");
						}
						options_arr.push("<option value='others'>其他</option>");
						App.modelSel.html(options_arr.join(""));
				
						var initmodel = mobiles[0].name;
						App.changeModel(initmodel);
						
						//selecte the model according to default value
						if(preModel!=null){
							App.changeModel(preModel);
						}
					}	
				}catch(e){}
			}//end if res
		  });
	}//end if ret
};

App.changeModel = function(model){

	var ret = false;
	try{
		ret = App.selectValue(App.modelSel, model)
	} catch(e){
	
		ret = true;
	}

	// set value into the option list
	if(ret){
		$("#model_img").hide();
		var logo = $("option[@selected]", App.modelSel).attr("logo");
		var mqqversion = $("option[@selected]", App.modelSel).attr("mqqversion");
		var mqqtime = $("option[@selected]", App.modelSel).attr("mqqtime");
		$("#model_img").attr("src", MOBILE_IMAGES + logo);
		$("#model_img").show("slow");
		$("#mqqname_show").text(mqqversion);
		$("#mqqtime_show").text(mqqtime);
	}
};

App.initBrands = function(){

	var options_arr = [];
	var brands = cm_brands_models;
	
	// create the array to hold the list of brands
	for(var i=0;i<brands.length;i++){
		options_arr.push("<option logo=\""+brands[i].logo+"\" value='"+ brands[i].name +"'>"+ brands[i].alias + "</option>");
			
	}
	
	options_arr.push("<option value='iPhone'>iPhone</option>");
	options_arr.push("<option value='yulong'>宇龙</option>");
	options_arr.push("<option value='others'>其他</option>");
	
	App.brandSel.html(options_arr.join(""));
	
	//var initbrand = brands[0].name;
	//App.changeBrand(initbrand);
	
};

/*
 * Initialize function will be called after dom is ready*/
App.init = function () {
	/* start initialize view elements */
	App.modelSel = $("#model_sel");
	App.brandSel = $("#brand_sel");
	

	var preBrand = prechoise.brand;
	var preModel = prechoise.model;
    
	// init the brands list 
	App.initBrands();
	// select the brand/model according to default values
	App.changeBrand(preBrand,preModel);

	App.brandSel.bind("change", function(){
		var brand = App.brandSel.val();
		if(brand=="none"){
			App.modelSel.html("");
			$("#mqqname_show").text("");
			$("#brand_img").hide();
			$("#model_img").hide();
		} else if(brand == "others"){
			window.location.href="./download2.jsp";
		} else if(brand == "iPhone"){
			window.location.href="/2009/iphone/index.html";
		}else if(brand == "yulong"){
			window.location.href="/news/news20071025.jsp";
		} else{//common
			App.changeBrand(brand);
		}
	});

	App.modelSel.bind("change", function(){
		var model = App.modelSel.val();
		if(model=="others"){
			window.location.href="/m/"+App.brandSel.val()+"/"+model;
			return;
		}
		App.changeModel(model);
	});
	/* end initialize view elements */
	
	$("#nextstep").bind("click", function(){
		if(App.modelSel.val()&&App.brandSel.val()){
			if("Sony Ericsson"==App.brandSel.val()){
			window.location.href="./m/se/"+App.modelSel.val();
			}else{
			window.location.href="./m/"+App.brandSel.val()+"/"+App.modelSel.val();
			}
		}
	});
};

$(document).ready(App.init);