// Dynamic Choice Images Support V6.07

// Image switcher.  Use selected animation type.
function dci_imageswitch(swapimage, newimage){
	switch (dci_animationtype)
		{
		case 'FastSwitch' :						// swap the image - immediate swap
			swapimage.attr('src',newimage);
			break;
		case 'FadeDownUp' :						// swap the image - simple fade down then up
			swapimage.fadeOut('fast',function(){swapimage.attr('src',newimage);swapimage.fadeIn()});
			break;
		case 'ImageSwitch' :						// swap the image via ImageSwitch
			swapimage.ImageStop(true,true);				// stop any existing animation
			var myswap = dci_ImageSwitchParams;			// set up our parameters
			myswap.NewImage = newimage	; 			// add in the image we want to show
			swapimage.ImageSwitch(myswap); 				// and switch images
		}	
}

// Radio Buttons
function rdclk(item, imgid, radioid){				// called from Radio button or Icon onclick
	var newimage = $('#' + item.id.replace(/^rd-/, "rim-")).attr('rel');		// get associated image
	if ( newimage )
		{
		var swapimage = $('#im-' + imgid);
		if ( swapimage.length ) dci_imageswitch(swapimage, newimage);
		}	
	if ( radioid ) $('#rd-' + imgid + '_' + radioid).attr("checked", "checked");	// if 3rd param we're calling from Icon onclick so check the appropriate button
	rehighlight();									// highlight selected radios icon
}

// Drop Down Lists 
function selectimage(selthis, imgid){							// called from onchange event in the <SELECT..> statement
	var newimage = selthis.options[selthis.selectedIndex].id;			// extract the image from option ID
	if ( newimage != '' ) 
		{
		var swapimage = $('#im-' + imgid);
		if ( swapimage.length ) dci_imageswitch(swapimage, newimage);
		}
}

// restore main image
function dcirestoremain(img, newimage){
	if ( img.src.replace(/.*\//, '') == newimage.replace(/.*\//, '') ) return;			// same image - no need to swap
	var swapimage = $(img);
	dci_imageswitch(swapimage, newimage);
}

// highlight images for selected radio buttons - called on page load and when radio changes
function rehighlight(){
	$('img[id^="rim-"]').attr('class', 'dciunselectedchoiceicon');		// clear all highlights
	$('input[id^="rd-"]:checked').each(function(i){				// all checked radios that may have icons
		if (this.className.indexOf('dcishowactive') == -1 ) return;		// don't highlight if not required
		var icon = $('#' + this.id.replace(/^rd-/, "rim-"));			// get associated icon
		if ( icon.length ) icon.attr('class', 'dciselectedchoiceicon');		//set highlight
		});
}

// initialise
$(document).ready(function() {
	rehighlight();
	$('img[id^="rim-"]').each(function(){var img = $(this).attr('rel'); if (img) $('<img/>')[0].src = img;});	// preload swap images for radio icons
	$('.dciddopt').each(function(){var img = $(this).attr('id'); if (img) $('<img/>')[0].src = img;});		// preload swap images for drop downs
});

