/**
 * @author richard
 */
 
 document.write("<style> .nojs, .reqd { display: none;} #field14 * {display:inline;} </style>");
 
 function $(id){
 	return document.getElementById(id);
 }
 
var promoData = [
	{
		'href': "/ratings/features.html?feature=vehicle-air-bags.php",
		'title' : "Demand side curtain airbags",
		'src' :"/gfx/0240-LTI-Curtain_1.gif"			
	}, 
	{
		'href': "/ratings/features.html?feature=vehicle-esc.php",
		'title' : "Ensure your next car has Electronic stability control",
		'src' :"/gfx/0240-LTI_Stability_1.gif"			
	}
];
// delay for image switch
var seconds = 7;

 
/* set up table cells in he search box */ 
 function processTableRow(el){
 
	var i = el.getElementsByTagName('TD')[1] || el.getElementsByTagName('TD')[0];
	debug(i);
		
	var input =  i.getElementsByTagName('select')[0] || i.getElementsByTagName('input')[0] || i.getElementsByTagName('a')[0];
	el.input = input;
		
 
	if (el.className && el.className.substring(0, 4) == "req_"){
 		// id of parent element
 		var id = el.className.replace("req_node_type_", "").replace(" reqd", "");
 		

		// get parent as an object
		var parent = $("row" + id);
		
		if (parent.dependantChildren){
 			parent.dependantChildren.push(el);
 		} else {
	 		parent.dependantChildren = new Array();
 			parent.dependantChildren.push(el); 				
 		}
		
		var i = parent.getElementsByTagName('TD')[1] || parent.getElementsByTagName('TD')[0] ;
		debug(i);
		
		//console.log(i);
		
		var input =  i.getElementsByTagName('select')[0] || i.getElementsByTagName('input')[0] || i.getElementsByTagName('a')[0];
		
		parent.input = input;
 		// assign event handlers
		
		if (input.tagName == "A"){
		
		//todo: need to make this = 1 if any children are showing.
			if ((parent.input && parent.input.value == 1) || !(el.input.value == 0 || el.input.value == "")){
				/*console.log(parent.input && parent.input.value == 1) 
				console.log(el.input.value == 0 || el.input.value == "")
				console.log("seetting to 1 ", parent, input, el);*/
				parent.input.value = 1;
			} else {
				//console.log("seetting to 0 ", parent, input, el);			
				parent.input.value = 0;
			}
		
			input.onclick = function(){
				parent.input.value = (parent.input.value==0) ? 1 : 0;
				
				change(parent);
			}
		
		} else {
		
			
			
			
			input.onchange = function(){
				change(parent);
			}
			input.onkeyup = function(){
				change(parent);
			}
		}
		// initial call 
		change(parent);
		//console.log("parent", parent)
 		
 	}
 
 }


function hide(e){
	//if (e.style){
		//console.log(e.input);
		
		
		
		
		e.input.value = e.input.tagName == "SELECT" ? 0 : "";
		
		
		e.style.display = "none";
		//}
}

function show(e){
	if (e.style){
		//IE doesn't suppor table-cell and block displays incorrectly in firefox
		e.style.display = !(window.attachEvent && !window.opera) ? "table-row" : "block";
	}
}



// call this function when a form element changes

 function change(e){
	//console.log(e.input)
 	if (e.input.value == 0 || e.input.value == ""){
		debug(e.input)
		if (e.input.tagName == "A"){
			e.input.innerHTML = 'More options';
			e.input.className = 'more';
			//var img = e.getElementsByTagName('IMG')[0];
			//img.src="gfx/more-options.gif";

		} else {
		//console.log(e.input);
		$('field2').innerHTML = '<input type="text" id="field_2" name="cs_2"/> ';
		} 
		for (var i= 0; i < e.dependantChildren.length; i++){
 			//e.dependantChildren[i].style.background = "#FFEEEE";
			hide(e.dependantChildren[i]);
 		}
 	} else {
		if (e.input.tagName == "A"){
			e.input.innerHTML = 'Fewer options';
			e.input.className = 'fewer';
			
			//debug(e);
			//var img = e.getElementsByTagName('IMG')[0];
			//img.src="gfx/fewer-options.gif";
		} else {
			if ($('field_1').value != '' && $('field_1').value != 0){
				var x = createXHR();
				x.onreadystatechange=function(){
	  				if(x.readyState==4){
	    				$('field2').innerHTML=x.responseText;
	    			}
	  			}
	  			x.open("GET","models.php?make=" + $('field_1').value + '&selected=' + $('field_2').value,true);
				x.send(null);
			} else {
				$('field2').innerHTML = '<input type="text" id="field_2" name="cs_2"/> ';
			}
		}
		
 		for (var i= 0; i < e.dependantChildren.length; i++){
			//e.dependantChildren[i].style.background = "#FFFFFF";
 			show(e.dependantChildren[i]);
 			var prev  = e.dependantChildren[i].previousSibling;
 			while(prev && prev.tagName !== "TD"){
 				prev = prev.previousSibling;
 			}
			if(prev){
				show(prev);
				}
 			
  		}
 	}
  }
function createXHR(){
	var x;
	try {
		// Firefox, Opera 8.0+, Safari
		x=new XMLHttpRequest();
	} catch (e) {
    // Internet Explorer
    try {
		x=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
      try
        {
        x=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        document.write("Your browser does not support AJAX!");
        return false;
        }
      }
    }
    return x;
 }

function models_dropdown(){
	$('field_1').onchange = function(){
		if ($('field_1').value != '' && $('field_1').value != 0){
			var x = createXHR();
			x.onreadystatechange=function(){
  				if(x.readyState==4){
    				$('field2').innerHTML=x.responseText;
    			}
  			}
  			x.open("GET","models.php?make=" + $('field_1').value,true);
			x.send(null);
		}
	
	}

}
  
 var webx = new Object();

function debug(m){
//console.log(m);
}

 
 /* initialise the search box inputs */
function initialise(){
	

	try {
	 	var table = $("group1"); //.getElementsByTagName("table")[0];
	 	var rows = table.getElementsByTagName("tr");
	 	for(var i=0; i < rows.length; i++){
	 		processTableRow(rows[i]);		
	 	}
		
	} catch (e) {
		//console.log(e);
		//alert('error on searchbox');
	} 	
	
	
	var links = document.getElementsByTagName('A');
	for(var i = 0; i < links.length; i++){
		fixlink(links[i]);
	}
	
	opera_labels();
	setTimeout("swap_adds()", seconds * 1000);
	//models_dropdown();

	
}



function opera_labels(){
	var labels = document.getElementsByTagName('LABEL');
	for(var i = 0; i < labels.length; i++){
		var l = labels[i];
		if (l.hasAttribute && l.hasAttribute('for')){
			debug(l);
			
			if (window.opera || navigator.vendor.indexOf('Apple') >= 0){
				//alert('sdc');
				opera_label(l);
			}
		}
	}
}

function opera_label(l) {
	l.onclick = function(e){
		if(e){
			e.stopPropagation();
		}
		var input = document.getElementById(l.getAttribute('for'));
		//console.log(input);
		//alert('p'); 
		debug(l, input);
		input.click();
		return true;
	}
}


function fixlink(l){
	//console.log(l);
	if (l.href.substring(l.href.length -1) =="#"){
		l.onclick = function(){alert('Coming Soon'); return false;}
	}
}



function swap_adds(){
	var promo = $('promo');
	
	if (!promo) return;
	
	var link = promo.getElementsByTagName("A")[1];
	var image = link.getElementsByTagName("IMG")[0];
	
	var link_ref = link.href.replace("http://" + window.location.host, '');
	var promoIndex = link_ref == promoData[0].href ? 1 : 0;
	//console.log(link, image, promoIndex, link_ref);
	
	link.href = promoData[promoIndex].href;
	image.title  = promoData[promoIndex].title;
	image.src = promoData[promoIndex].src;

	setTimeout("swap_adds()", seconds * 1000);

	
}

 
 
window.onload = initialise;


