$(document).ready(function(){
	
	// Animate range & model selection boxes
	$('.spec-model, .spec-range').animate({opacity: 0.0},100);
	$('.spec-range').each(function(){
		dindex = $(".spec-range").index(this);
		dwaittime = dindex * 190;
		$(this).animate({opacity: 0.0},dwaittime).animate({opacity: 1.0},600);
	});
	$('.spec-model').each(function(){
		dindex = $(".spec-model").index(this);
		dwaittime = dindex * 190;
		$(this).animate({opacity: 0.0},dwaittime).animate({opacity: 1.0},600);
	});
	
	// Set exterior image caption opacity
	$('#extimgcaption').css('opacity','0.85');
	
	// What to do when exterior image radio buttons are clicked
	$('.extradio').click( function() {
		updateExterior();
	});
	
	// What to do when exterior image radio buttons are clicked
	$('.intradio').click( function() {
		updateInterior();
	});
	
	// What to do when transmission radio buttons are clicked
	$('.transradio').click( function() {
		updateTransmission();
	});
	
	// What to do when feature checkboxes are clicked
	$('.featcheck').click( function() {
		updateFeatures();
	});
	
	// Options+Accessories Collapse
	$('.h3features').click( function () {
		$(this).next().slideToggle("fast",function(){
			$(this).height("auto");
		});
	});
	


	// Update images on page load
	if ($('input[name="Exterior"]').length > 0) {
		updateExterior();
	}
	if ($('input[name="Interior"]').length > 0) {
		updateInterior();
	}
	if ($('input[name="Transmission"]').length > 0) {
		updateTransmission();
	}
	if ($('input[name="Accessories"]').length > 0) {
		updateFeatures();
	}
	
	
	// IE LABEL IMG fix
	//if(document.all && navigator.appVersion.indexOf("MSIE")>-1 && navigator.appVersion.indexOf("Windows")>-1) {
	//	$('.feat-div').click( function () {
			//alert('1');
	//		$(this).children('.featcheck').click();
	//	});
	//}
	
	  if(document.all && navigator.appVersion.indexOf("MSIE")>-1 && navigator.appVersion.indexOf("Windows")>-1)
	  {
		var a = document.getElementsByTagName("label");
		for(var i=0,j=a.length;i<j;i++){
		  if(a[i].hasChildNodes)
		  {
			a[i].onclick = function(){
				var e = document.getElementById(this.htmlFor);
				switch(e.type){
				   case "radio": e.click(); break;
				   case "checkbox": e.click(); break;
				   case "text": case "password": case "textarea": e.focus(); break;
				}
			}
		  }
		}
	  }
	
});

function updateExterior() {
	selectedext = $('input[name="Exterior"]:checked').val();
	selectedextcode = striptexttocode(selectedext);
	newimgurl = 'build/gallery/' + range_code + model_code + '-exterior-' + selectedextcode + '.jpg';
	$('#extimg').fadeTo(400,0);
	$('#extimg').attr('src',newimgurl);
	$('#extimg').fadeTo(600,1);
	$('#extimgcaption').slideUp().html($('input[name="Exterior"]:checked').val()).slideDown();
	$('.selectedextlabel').removeClass('selectedextlabel');
	$('input[name="Exterior"]:checked').prev().addClass('selectedextlabel');
	
	$('#spec-info-pic').attr('src',newimgurl);
	$('#spec-info-exterior').animate({opacity: 0.0},250).html('<b>Colour:</b><br/>' + selectedext).animate({opacity: 1.0},350);
}

function updateInterior() {
	selectedint = $('input[name="Interior"]:checked').val();
	$('#spec-info-interior').animate({opacity: 0.0},250).html('<b>Trim:</b><br/>' + selectedint).animate({opacity: 1.0},350);
	
	$('.int-div').removeClass('int-div-selected');
	$('input[name="Interior"]:checked').parent().addClass('int-div-selected');
}

function updateTransmission() {
	selectedtrans = $('input[name="Transmission"]:checked').val();
	$('#spec-info-transmission').animate({opacity: 0.0},250).html('<b>Transmission:</b><br/>' + selectedtrans).animate({opacity: 1.0},350);
	
	$('.trans-div').removeClass('trans-div-selected');
	$('input[name="Transmission"]:checked').parent().addClass('trans-div-selected');
}

function updateFeatures() {
	featureshtml = '';
	$('.featcheck').parent().removeClass('feat-div-selected');
	$('input[name="Accessories"]:checked').each(
		function() {
			$(this).parent().addClass('feat-div-selected');
			featureshtml += ' - ' + $(this).val() + '<br/>';
		}
	);
	if (featureshtml != '') {
		featureshtml = '<b>Options + Accessories:</b><br/>' + featureshtml;
	}
	$('#spec-info-options').animate({opacity: 0.0},250).html(featureshtml).animate({opacity: 1.0},350);
}




function striptexttocode(what) {
	var out;
	out = what;
	out = out.replace(/[^a-zA-Z0-9]+/g,'').toLowerCase();
	return out;
}
