var currentLayer = "suv1",currentSubModel = "",currentRadio,currentFirstRadio,currentSecondRadio;
var whichLayer,whichInput,whichSpan;
var check = false,disable = false;
function showLayer(id, name) {
	var element = $(id);
	try{
		var refEl = $('pMasthead').getElement('div.hook');
		var refPos = refEl.getCoordinates();
		refEl.setStyle('display', 'none');
		if (id == "submissionAlert") {
			element.setStyles( {
				left : 150,
				top : 160
			});
		} else {
			
			element.setStyles( {
				left : refPos.left,
				top : refPos.top,
				width : refPos.width
			});
			var brandObject = allBrands[name];
			var info = element.getElement('div[class=overlayLeftColumn]');
			info.getElement('div[class=year]').set('html', brandObject.year);
			info.getElement('div[class=model]').set('html', brandObject.displayName);
			info.getElement('img[class=image]').set('src', BASE_CONTEXT + '/assets/en/images/vehicles/overlay/' + brandObject.year + brandObject.name + '.jpg');
			info.getElement('img[class=image]').set('alt', brandObject.displayName);
		}
		
	}catch(e){}
	element.setStyle('display', 'block');
}
function hideLayer(id){
	try {
		$(id).setStyle('display', 'none');
	} catch (e) {}
}
function closeLayer(){
	$(currentLayer).setStyle('display', 'none');
	$('pMasthead').getElement('div.hook').setStyle('display', 'block');;
}

function initLayer(subModel,layerId,myArray,currentCombo,searchParams){//open the layer
	hideLayer("submissionAlert");
	hideLayer(currentLayer);
	showLayer(layerId,subModel);
	currentSearchParams = searchParams;
	currentSubModel = subModel;
	currentLayer = layerId;
	initRadio(layerId);
	loadRadio(layerId, myArray, subModel, currentCombo);
}
function initRadio(layerId){//make all radio active
	whichLayer = document.getElementById(layerId);
	whichInput = whichLayer.getElementsByTagName("input");
	whichSpan = whichLayer.getElementsByTagName("span");
	for ( var i = 0; i < whichInput.length - 1; i++) {
		whichInput[i].disabled = false;
		whichInput[i].checked = false;
		whichSpan[i].className = "active";
	}
}

function loadRadio(layerId,myArray,subModel,currentCombo){//first time load the layer, preload the popular choices, currentCombo is the number of the combo order of this subModel, which is the popular one when first load
	if(layerArray[layerId].length==1){//1 level case
		for(var i=0; i<whichInput.length-1; i++){
			if(whichInput[i].value==myArray[currentCombo][0]){ //preload the first popular case
				whichInput[i].checked=true;
				whichSpan[i].className = "active";
			}
		}
	}else if (layerArray[layerId].length==2){//2 levels case
		for(var i=0; i<whichInput.length-1; i++){
			if(whichInput[i].value==myArray[currentCombo][0] || whichInput[i].value==myArray[currentCombo][1]){ //preload the first popular case
				whichInput[i].checked=true;
				whichSpan[i].className = "active";
			}
			if(whichInput[i].value==myArray[currentCombo][0]) currentRadio = whichInput[i];
		}
		toggleRadio(currentRadio, layerId);//just like click the level1

	}else{//3 levels case
		for(var i=0; i<whichInput.length-1; i++){
			if(whichInput[i].value==myArray[currentCombo][0] || whichInput[i].value==myArray[currentCombo][1] || whichInput[i].value==myArray[currentCombo][2]){//preload the first popular case
				whichInput[i].checked=true;
				whichSpan[i].className = "active";
			}
			if(whichInput[i].value==myArray[currentCombo][0]) currentRadio = whichInput[i];
			//if(whichInput[i].value==myArray[currentCombo][1]) currentSecondRadio = whichInput[i];

		}
		toggleRadio(currentRadio, layerId);//just like click the level1
		//toggleRadio(currentSecondRadio, layerId, "0");//just like click the level2, but the first level 0 always keeps active when first load page
	}
}

var comboExist=false;
var specialCase=false;
var currentLevel, specialLevelName, vanCase;

function toggleRadio(thisRadio, layerId, filterKeepActive){
	for(var k=0; k<layerArray[layerId].length; k++){
		if(layerArray[layerId][k]==thisRadio.name){
			currentLevel = k; // the level which has the current radio picked
		}
	}

	for(var n=0;  n<whichInput.length-1; n++){
		if(whichInput[n].name==layerArray[layerId][0]){//make the first level active at first
			whichInput[n].disabled=false;
			whichSpan[n].className = "active";
		}
	}
	if(currentLevel!=2){//drive type does not toggle the other levels
		for(var k=0; k<layerArray[layerId].length; k++){
			if(layerArray[layerId][k]!=thisRadio.name){//find the other level except the currentLevel clicked
				specialCase=false; // each level might have radio checked but need to be disabled in the non-combo case
				for(var j=0; j<whichInput.length-1; j++){
					if(whichInput[j].name==layerArray[layerId][k]){//get the radios in the k level
						if(whichInput[j].disabled){
							whichInput[j].disabled = false;
							whichSpan[j].className = "active";
						}
						comboExist=false;
						for(var m=0; m<subModelArray[currentSubModel][3].length; m++){//check combo
							if(subModelArray[currentSubModel][3][m][k]==whichInput[j].value && subModelArray[currentSubModel][3][m][currentLevel]==thisRadio.value){
								comboExist=true;
							}
						}
						if(!comboExist){//this input radio has non-combo with the currentLevel radio clicked
							if(whichInput[j].checked){
								whichInput[j].checked=false;
								specialCase=true; // need to check the other available radio button
								specialLevelName=whichInput[j].name;
							}
							whichInput[j].disabled=true;
							whichSpan[j].className = "grey";
						}
					}
				}
				if(specialCase){ //pick the first available radio to check in this k level
					for(var n=0;  n<whichInput.length-1; n++){
						if(whichInput[n].name==specialLevelName && !whichInput[n].disabled){
							whichInput[n].checked=true;
							whichSpan[n].className = "active";
							break;
						}
					}
				}
			}
		}
	}
	if(currentLevel==0 && layerArray[layerId].length>2){//when click the first level, make the second level radio call to check the combo between 2nd and 3rd level, so disable not exist combo
		for(var i=0; i<whichInput.length-1; i++){
			if(whichInput[i].name==layerArray[layerId][1]&&whichInput[i].checked)
				currentSecondRadio = whichInput[i]; //find the radio checked in the second level layerArray[layerId][1]
		}
		for(var j=0; j<whichInput.length-1; j++){
			if(whichInput[j].name==layerArray[layerId][0]&&whichInput[j].checked)
				currentFirstRadio = whichInput[j]; //find the radio checked in the first level layerArray[layerId][1]
		}
		toggleRadio(currentSecondRadio, layerId, "0");
		disable3rdLevelRadio(currentFirstRadio.value, currentSecondRadio.value, layerId);
	}
	if(filterKeepActive!=null && filterKeepActive!=""){
		for(var p=0;  p<whichInput.length-1; p++){
			if(whichInput[p].name==layerArray[layerId][filterKeepActive]){//make the filterKeepActive level active to solve the dead end case
				whichInput[p].disabled=false;
				whichSpan[p].className = "active";
			}
		}
	}
	vanCase = true;
	if(layerId=="van1"){//express vehicle specail case
		for(var i=0; i<whichInput.length-1; i++){
			if(whichInput[i].name==layerArray[layerId][2]&&whichInput[i].checked)
				vanCase = false;
		}
		if(vanCase){//nothing checked in the 3rd level
			for(var i=0; i<whichInput.length-1; i++){
				if(whichInput[i].name==layerArray[layerId][2]&&!whichInput[i].disabled){
					whichInput[i].checked=true; break;
				}
			}
		}
	}
}

function disable3rdLevelRadio(v1, v2, layerId){
	var tempV="";
	for(var mm=0; mm<subModelArray[currentSubModel][3].length; mm++){//check combo
		if(subModelArray[currentSubModel][3][mm][0]==v1 && subModelArray[currentSubModel][3][mm][1]==v2){
			tempV = tempV +','+ subModelArray[currentSubModel][3][mm][2];
		}
	}
	for(var jj=0; jj<whichInput.length-1; jj++){
		if(whichInput[jj].name==layerArray[layerId][2]){
			if(tempV.indexOf(whichInput[jj].value)==-1){
				whichInput[jj].disabled=true;
				whichSpan[jj].className = "grey";
			}
		}
	}
}
