function select_paint (id)
{
    duration = 0.5;
    
    morphTin(paint[id].tin, paint[id].alt, paint[id].colour, duration); // morph the tin

    // morph the roomset colour
    $('room-container-1').morph('background-color: ' + paint[id].colour + ';', {duration: duration});
    $('room-container-2').morph('background-color: ' + paint[id].colour + ';', {duration: duration});
    
    // reveal the product details
    $('p-' + prev_id).style.display = 'none';
    $('p-' + id).style.display      = 'block';
    
    prev_id = id;   // store the current id for next time
}



function rotate_brands (off_num, on_num)
{
    var max_slots   = 9;                                                    // max number of logo slots
    var off_cnt     = off_num;                                              // off counter
    var on_cnt      = on_num;                                               // on counter
    
    for (var i = 0; i < max_slots; i++) {                                   // fill the slots
        if (off_num != on_num) {                                            // if not the first run
            off_cnt++;                                                      // increment the off count
            
            if (off_cnt >= homebrands.length) {                             // if you run out of slots go back to start
                off_cnt = 0;
            }
            
            $('brand_' + (homebrands[off_cnt])).style.display = 'none';     // turn the slot display off
        }
        
        on_cnt++;                                                           // increment the on count
        
        if (on_cnt >= homebrands.length) {                                  // if you run out of slots go back to start
            on_cnt = 0;
        }
        
        $('brand_' + (homebrands[on_cnt])).style.display = 'inline-block';  // turn the slot display on
    }
    
    setTimeout('rotate_brands(' + off_cnt + ', ' + on_cnt + ')', 5000);     // set to change after time
}


//Functions for controlling the roomsets block
	function fetch_product_roomset_data(palette_id) {
		var url = '/lib/ajax/roomset_data.php?palette_id='+palette_id;
		new Ajax.Request(url, {
			method: 'get',
			onComplete: function(transport) {
				if (200 == transport.status) {
					var data = transport.responseText.evalJSON();
					paint = new Array();
					paint = data.paint;
					roomsets = data.roomsets;

					//preload the roomset images
					var roomset_img = new Image(roomsets[0]['img_width'], roomsets[0]['img_height']);
					for (i=0;i<roomsets.length;i++) {
						roomset_img.src = roomsets[i]['src'];
					}

					//change paint daubs
					$('palette-paint-daubs').update(data.paint_daubs);

					//change the paint details
					$('paint-details-container').update(data.paint_details);

					//update the palette details
					$('palette_heading').update(data.palette_info['name']);
					$('palette_description').update(data.palette_info['description']);

					//Start rotating at the first image
					rotate_roomsets(0);

					//update the roomset with the first paint colour
					for(paint_id in paint) {
						prev_id = paint_id;		//store the id of the paint we are on
						select_paint(paint_id);	//display the paint

						break;
					}
					
					Event.fire(document, "img:replace"); // replace any img headings

				} else {	//If the page doesn't return correctly reload the entire page
					window.location.href = '/index.php?palette_id='+palette_id;
				}
			}
		});
	}

