
var contentsearch = {
	cForm:null,boxes:null,globalBoxOnChange:null,urlRoot:null
}

contentsearch.init = function(){
	with(contentsearch){
		var box, boxesData = {BRAND:null,RANGE:null,CONTENT:null}
		contentsearch.cForm = document.getElementById("contentSearch");
		if(!cForm)return;
		urlRoot= window.location.protocol + "//" + window.location.host;
		if(window.location.toString().indexOf(urlRoot+'/buyacar/')>-1)urlRoot=urlRoot+"/buyacar"
		contentsearch.boxes = cForm.getElementsByTagName("SELECT");
		for(var b=0;b<boxes.length;b++){
			boxes[b].disabled=false;
			boxes[b].onchange=contentsearch.doEvent
			//rebuild drop boxes
			if(boxes[b].selectedIndex==0){
				box = boxes[b].id.substr(3)
				boxesData[box] = new Array();
				for(var o=1;o<boxes[b].options.length;o++){
					for(var o=1;o<boxes[b].options.length;o++){
						boxesData[box][o-1] = new selectrow(boxes[b].options[o].value , boxes[b].options[o].text )
					}
				}
				updateBoxes(boxesData,box);
			}
		}
		getBoxesData({id:"cntINIT",form:cForm})
	}
}

contentsearch.doEvent = function(e){
	if(!e){
		var e = window.event;
		obj = e.srcElement
	}else{
		obj = (e.target)? e.target : e.srcElement
	}
	if(!obj.options)return;
	var opt = obj.options[obj.selectedIndex]
	switch(e.type){
		case "change":
			contentsearch.getBoxesData(obj)
			break;
	}
}

contentsearch.setCat = function (n,v,a){
	with(contentsearch){
		var cat = document.getElementById("cat1");
		if(cat != null){
			cForm.action = urlRoot+a;
			document.getElementById("cat1").name=n;
			document.getElementById("cat1").value=v;
		}
	}
}

contentsearch.getBoxesData = function (obj){
   with(contentsearch){
	   var myUrl = urlRoot+"/research/AJAXReviewDropDown.jhtml?";
	   contentsearch.globalBoxOnChange = obj.id.substr(3);
	   switch (globalBoxOnChange){
			case "BRAND":
				setCat("categoryId",obj.form.manufacturer.value,"/cars/home.jhtml");
				myUrl += "manufacturer="+obj.form.manufacturer.value+"&range=";
				break;
			case "RANGE":
				setCat("categoryId",obj.form.range.value,"/cars/home.jhtml");
				myUrl += "manufacturer="+obj.form.manufacturer.value+"&range="+obj.form.range.value;
				break;
			case "CONTENT":
				setCat("cat",1,"/research/review.jhtml");
				return;
			case "INIT":
				if(obj.form.manufacturer.value!=0){
					myUrl += "manufacturer="+obj.form.manufacturer.value;
				}else{return}
				if(obj.form.range.value!=0)myUrl += "&range="+obj.form.range.value;
				break;
		}
		myUrl += "&t=" +(new Date().getTime()) //inhibit browser cache
		dropsearch.showLoading(cForm)
		$.getJSON(myUrl,contentsearch.gotBoxesData);	
   }
}

contentsearch.gotBoxesData = function (data){
	with(contentsearch){
		if(data['SELECTED']){
			updateBoxes(data,"BRAND",data['SELECTED'][0]['id']);
			updateBoxes(data,"RANGE",data['SELECTED'][1]['id']);
			updateBoxes(data,"CONTENT",data['SELECTED'][2]['id']);
		}else{
			updateBoxes(data,"BRAND");
			updateBoxes(data,"RANGE");
			updateBoxes(data,"CONTENT");
		}
		dropsearch.hideLoading(cForm)
	}
}	

contentsearch.updateBoxes = function (data,box,selectValue){
	var obj = document.getElementById("cnt"+box);
	if(!obj)return;
	var objOption, objnodes = obj.childNodes;
	var selectValIndex = 0;
	if(!selectValue){selectValue = obj.options[obj.selectedIndex].value;}
	for(var n=0;n<objnodes.length;n++)if(objnodes[n].nodeType == 1){
		if(objnodes[n].nodeName=="OPTION" && objnodes[n].index == 0){ //first row skip
		}else{
			obj.removeChild(objnodes[n]); //remove all other OPTION childs of select
			n--
		}
	}
	
	for(boxrow=0;boxrow<data[box].length;boxrow++){
		var oname = data[box][boxrow]['displayName']
		objOption = document.createElement('OPTION');
		obj.appendChild(objOption);
		objOption.value=data[box][boxrow]['id']
		if(box == "BRAND" || box == "RANGE"){
			objOption.text= oname.substring(0,oname.lastIndexOf("(")-1);
		}else objOption.text = oname;
		if(objOption.value==selectValue)selectValIndex=boxrow+1;
	}
	setTimeout('document.getElementById("cnt'+box+'").selectedIndex='+selectValIndex, 1) //bug in ie
}


$(document).ready(function() {
    contentsearch.init()
});
