displayGroups = {"Technical Specs":{"technicalspecsdp:developer_platform":["dp:developer_platform","Developer Platform"],"technicalspecsdp:operating_system":["dp:operating_system","Operating System"],"technicalspecsresolution":["Resolution","Resolution"],"technicalspecstechnicalspecsdisclaimer":["Technical Specs Disclaimer","Notes"]},"General":{"generalresolution":["Resolution","Resolution"],"generaldp:bits_per_pixel":["dp:bits_per_pixel","Color Depth"],"generalsecondarydisplayresolution":["Secondary Display Resolution","Secondary Display Resolution"],"generaldp:secondary_bits_per_pixel":["dp:secondary_bits_per_pixel","Secondary Display Color Depth"],"generalsize":["Size","Size"],"generaldp:volume":["dp:volume","Volume"],"generaldp:weight":["dp:weight","Weight"],"generaldp:navigation_key":["dp:navigation_key","Input Methods"],"generaldp:band":["dp:band","Frequency Band"],"generaldp:region":["dp:region","Regional Availability"],"generaldp:data_bearer":["dp:data_bearer","Data Bearer"],"generaldp:cpu_count":["dp:cpu_count","CPU Count"],"generaldp:hardware_cpu_type":["dp:hardware_cpu_type","CPU Type"],"generaldp:hardware_cpu_clock_rate":["dp:hardware_cpu_clock_rate","CPU Clock Rate"],"generaldp:hardware_graphics_processor":["dp:hardware_graphics_processor","Graphics Processor"],"generaluaproflink":["UAProf Link","UAProf Link"],"generalconsumerlink":["Consumer Link","Consumer Link"],"generaldeveloperlink":["Developer Link","Developer Link"],"generalremotedeviceaccessservice":["Remote Device Access Service","Remote Device Access Service"],"generalgeneraldisclaimer":["General Disclaimer","Notes"]},"Extra Features":{"extrafeaturesdp:feature":["dp:feature","Extra Features"],"extrafeaturesdp:gps_feature":["dp:gps_feature","GPS Features"],"extrafeaturesextrafeaturesdisclaimer":["Extra Features Disclaimer","Notes"]},"APIs":{"apisdp:java_api":["dp:java_api","Java Technology"],"apisjavaapiaccesspermissionslink":["Java API Access Permissions Link","Java API Access Permissions"],"apisdp:other_api":["dp:other_api","Other APIs"],"apisdp:product_id":["dp:product_id","Product ID"],"apisdp:sensor_api_channel":["dp:sensor_api_channel","Sensor API Channels"],"apisdp:root_certificate":["dp:root_certificate","Certificates"],"apisapisdisclaimer":["APIs Disclaimer","Notes"]},"Browser & Flash":{"browserflashdp:browser":["dp:browser","Browser"],"browserflashdp:flash_lite":["dp:flash_lite","Flash Lite"],"browserflashdp:flash_lite_feature":["dp:flash_lite_feature","Flash Lite Feature"],"browserflashbrowserflashdisclaimer":["Browser & Flash Disclaimer","Notes"]},"Multimedia":{"multimediacameraresolution":["Camera Resolution","Camera Resolution"],"multimediadp:camera_digital_zoom":["dp:camera_digital_zoom","Digital Zoom"],"multimediadp:camera_optical_zoom":["dp:camera_optical_zoom","Optical Zoom"],"multimediadp:camera_sensor_cmos":["dp:camera_sensor_cmos","Sensor"],"multimediadp:camera_focal_length":["dp:camera_focal_length","Focal length"],"multimediadp:camera_f_number":["dp:camera_f_number","F-Stop\/Aperture"],"multimediadp:camera_minimum_focus_range":["dp:camera_minimum_focus_range","Focus range"],"multimediadp:camera_image_format":["dp:camera_image_format","Image Format"],"multimediadp:camera_feature":["dp:camera_feature","Camera Features"],"multimediacameravideoresolution":["Camera Video Resolution","Video Resolution"],"multimediadp:camera_video_frame_rate":["dp:camera_video_frame_rate","Video Frame Rate"],"multimediadp:camera_video_zoom":["dp:camera_video_zoom","Video Zoom"],"multimediadp:camera_video_format":["dp:camera_video_format","Video Format"],"multimediasecondarycameraresolution":["Secondary Camera Resolution","Secondary Camera Resolution"],"multimediadp:secondary_camera_digital_zoom":["dp:secondary_camera_digital_zoom","Secondary Camera Digital Zoom"],"multimediadp:secondary_camera_optical_zoom":["dp:secondary_camera_optical_zoom","Secondary Camera Optical Zoom"],"multimediadp:secondary_camera_sensor_cmos":["dp:secondary_camera_sensor_cmos ","Secondary Camera Sensor"],"multimediadp:secondary_camera_focal_length":["dp:secondary_camera_focal_length","Secondary Camera Focal length"],"multimediadp:secondary_camera_f_number":["dp:secondary_camera_f_number","Secondary Camera F-Stop\/Aperture"],"multimediadp:secondary_camera_minimum_focus_range":["dp:secondary_camera_minimum_focus_range","Secondary Camera Focus range"],"multimediadp:secondary_camera_image_format":["dp:secondary_camera_image_format","Secondary Camera Image Format"],"multimediadp:secondary_camera_feature":["dp:secondary_camera_feature","Secondary Camera Camera Features"],"multimediasecondarycameravideoresolution":["Secondary Camera Video Resolution","Secondary Camera Video Resolution"],"multimediadp:secondary_camera_video_frame_rate":["dp:secondary_camera_video_frame_rate","Secondary Camera Video Frame Rate"],"multimediadp:secondary_camera_video_zoom":["dp:secondary_camera_video_zoom","Secondary Camera Video Zoom"],"multimediadp:secondary_camera_video_format":["dp:secondary_camera_video_format","Secondary Camera Video Format"],"multimediadp:video_feature":["dp:video_feature","Video Features"],"multimediadp:video_format":["dp:video_format","Video Formats"],"multimediadp:local_video_playback":["dp:local_video_playback","Local Video Playback"],"multimediadp:graphics_format":["dp:graphics_format","Graphics Formats"],"multimediadp:audio_feature":["dp:audio_feature","Audio Features"],"multimediadp:audio_format":["dp:audio_format","Audio Formats"],"multimediamultimediadisclaimer":["Multimedia Disclaimer","Notes"]},"Memory Functions":{"memoryfunctionsdp:shared_memory":["dp:shared_memory","Max User Storage"],"memoryfunctionsdp:nand_memory":["dp:nand_memory","NAND Memory"],"memoryfunctionsdp:free_executable_memory":["dp:free_executable_memory","Free Executable RAM Memory"],"memoryfunctionsdp:memory_card":["dp:memory_card","Memory Card"],"memoryfunctionsdp:maximum_memory_card_size":["dp:maximum_memory_card_size","Max Memory Card Size"],"memoryfunctionsdp:heap_size":["dp:heap_size","Maximum Heap Size"],"memoryfunctionsdp:maximum_jar_size":["dp:maximum_jar_size","Maximum JAR Size"],"memoryfunctionsdp:hard_disk":["dp:hard_disk","Hard Disk"],"memoryfunctionsdp:mass_storage_memory":["dp:mass_storage_memory","Mass Storage Memory"],"memoryfunctionsdp:nor_memory":["dp:nor_memory","NOR memory"],"memoryfunctionsmemoryfunctionsdisclaimer":["Memory Functions Disclaimer","Notes"]},"Connectivity":{"connectivitydp:pc_connectivity":["dp:pc_connectivity","Local Connectivity"],"connectivitydp:wlan_support":["dp:wlan_support","WLAN support"],"connectivitydp:bluetooth_profile":["dp:bluetooth_profile","Bluetooth Profiles"],"connectivityconnectivitydisclaimer":["Connectivity Disclaimer","Notes"]},"Messaging":{"messagingdp:messaging":["dp:messaging","Messaging"],"messagingdp:email_protocol":["dp:email_protocol","Email Protocol"],"messagingdp:email_solution":["dp:email_solution","Email Solution"],"messagingdp:messaging_feature":["dp:messaging_feature","Messaging Feature"],"messagingdp:document_format":["dp:document_format","Document Formats"],"messagingmessagingdisclaimer":["Messaging Disclaimer","Notes"]},"Power Management":{"powermanagementdp:power_management":["dp:power_management","Power Management"],"powermanagementdp:battery":["dp:battery","Battery"],"powermanagementdp:maximum_talk_time":["dp:maximum_talk_time","Maximum Talk Time"],"powermanagementdp:maximum_gsm_talk_time":["dp:maximum_gsm_talk_time","GSM Talk Time"],"powermanagementdp:maximum_wcdma_talk_time":["dp:maximum_wcdma_talk_time","WCDMA Talk Time"],"powermanagementdp:maximum_voip_talk_time":["dp:maximum_voip_talk_time","VoIP Talk Time"],"powermanagementdp:maximum_standby_time":["dp:maximum_standby_time","Maximum Standby Time"],"powermanagementdp:maximum_gsm_standby_time":["dp:maximum_gsm_standby_time","GSM Standby Time"],"powermanagementdp:maximum_wcdma_standby_time":["dp:maximum_wcdma_standby_time","WCDMA Standby Time"],"powermanagementdp:maximum_video_playback_time":["dp:maximum_video_playback_time","Max Video Playback Time"],"powermanagementdp:maximum_video_recording_time":["dp:maximum_video_recording_time","Max Video Recording Time"],"powermanagementdp:maximum_music_playback_time":["dp:maximum_music_playback_time","Max Music Playback Time"],"powermanagementpowermanagementdisclaimer":["Power Management Disclaimer","Notes"]},"Other":{"otherdp:device_management":["dp:device_management","OMA Device Management"],"otherdp:data_synchronization":["dp:data_synchronization","OMA Data Synchronization"],"otherdp:oma_drm_level":["dp:oma_drm_level","Digital Rights Management"],"otherdp:oma_drm_delivery_method":["dp:oma_drm_delivery_method","DRM Delivery Method"],"otherdp:web_service":["dp:web_service","Web Service"],"otherotherdisclaimer":["Other Disclaimer","Notes"]},"Recommended Resources":{"recommendedresourcestools":["Tools","Tools"],"recommendedresourcessdks":["SDKs","SDKs"],"recommendedresourcesdocuments":["Documents","Documents"],"recommendedresourcesrecommendedresourcesdisclaimer":["Recommended Resources Disclaimer","Notes"]}};


// ===================================
// =        Class DeviceTables       =
// ===================================
var FnDeviceTables = new Class({

    /**
     * Adds the device selected by the device chooser to the table
     */
    addSelectedDevice: function() {
        var selectedDevice = $('deviceList').getSelected()[0].value;
        // Have to do this because of Safari :(
        if (selectedDevice != '0') {
            this.addDevice(selectedDevice);
        }
    },

    /**
     *  Adds the indicated device to the Device Table
     */
    addDevice: function(deviceModel) {
        // Request Selected Device Data by an AJAX Call
        var requestUrl = 'http://www.forum.nokia.com/piazza/devices/devices/get-devices';
        
        // Checking weather the device is already added in IE because current versions of IE does not support select- option disabled
        if (navigator.appName == "Microsoft Internet Explorer" && fnDeviceTable.selectItemState($('deviceList'), deviceModel) == true) {
            // Reset back to our selection message
            $('deviceList').selectedIndex = 0;
            return;
        }
                 
        var jsonRequest = new Request.JSON({url: requestUrl,
            onComplete: function(responseObject) {
                if (responseObject == null) {
                    alert("Request Failed. Please try again");
                }
                else {                
                    // Adding to deviceHeader
                    var headerRow = $('FnDeviceTableHeader').getElements('tr');
                    var headerCell = new Element('td', {'id': 'device_' + deviceModel});

                    // This span is used to resolve IE not treating TD width but treats SPAN width so we can make the
                    // table cell fixed width
                    var deviceHeaderSpan = new Element('span', {'class': 'headerSpan'});
                    var deviceHeaderWrapper = new Element('div', {'id': 'FnDeviceTableHeaderWrapper_' + deviceModel});
                    var deviceHeaderImagesWrapper = new Element('div', {'id': 'FnDeviceTableHeaderImagesWrapper_' + deviceModel});
                    var deviceHeaderTextWrapper = new Element('div', {'id': 'FnDeviceTableHeaderTextWrapper_' + deviceModel});

                    deviceHeaderWrapper.appendChild(deviceHeaderImagesWrapper);
                    deviceHeaderWrapper.appendChild(deviceHeaderTextWrapper);

                    var deviceGridImageHolder = new Element('div', {'id': 'deviceGridImageHolder_' + deviceModel});
                    deviceHeaderImagesWrapper.appendChild(deviceGridImageHolder);

                    var deviceGridImage = new Element('img', {'src': responseObject[0]['dp:image_grid'], 'alt': deviceModel, 'class': 'deviceImage'});

                    deviceGridImageHolder.appendChild(deviceGridImage);
                    deviceHeaderTextWrapper.appendText(responseObject[0]['Title'] + '  ');

                    var removeDevice = new Element('input', {
                        'type': 'image',
                        'src': 'http://www.forum.nokia.com/piazza/devices/images/close.gif',
                        'value': deviceModel
                    });

                    // Adding those nice fadein/fadeout effet to the remove button
                    removeDevice.setOpacity(0);
                    headerCell.addEvent('mouseover', removeDevice.fade.bind(removeDevice, [1]));
                    headerCell.addEvent('mouseout', removeDevice.fade.bind(removeDevice, [0]));

                    // Adding things to the row
                    deviceHeaderTextWrapper.appendChild(removeDevice);
                    deviceHeaderSpan.appendChild(deviceHeaderWrapper);
                    headerCell.appendChild(deviceHeaderSpan);

                    // Add it to the next to last position
                    headerCells = headerRow[0].getElements('td');
                    var lastHeaderCell = headerCells[headerCells.length - 1];
                    headerRow[0].insertBefore(headerCell, lastHeaderCell);

                    // Registering the remove event and calling it off
                    removeDevice.addEvent('click', function() {
                        var deviceTDs = $$('td');

                        for (var x=0; x < deviceTDs.length; x++) {
                            var tdId = deviceTDs[x].getProperty('id');
                            if (tdId != null && tdId.match(deviceModel) != null) {
                                deviceTDs[x].dispose();
                            }
                        }

                        // Setting the colspan for the main cat column
                        var headerCols = $$('th');

                        for (var y=0; y < headerCols.length; y++) {
                            var colSpn = headerCols[y].getProperty('colspan');
                            if (colSpn != null || parseInt(colSpn) != 0) {
                                colSpn = parseInt(colSpn) - 1;
                                headerCols[y].setAttribute('colspan', colSpn);
                            }
                        }

                        // Enabling the select option for the selected device
                        fnDeviceTable.toggleSelectItemState($('deviceList'), deviceModel, true);
                        // Hilight the different device features from the first one
                         if (hilightEnabled == true) {
                            fnDeviceTable.hilightFeatures();
                         }
  						fnDeviceTable.resetEmptyRows();
                    });


                    // Adding the device to the Device Table
                    $each(displayGroups, function(catProperties, category) {
                        var mainCatTbodyId = 'tbody_' + category.replace(/[^A-Za-z0-9\-_:\.]+/g,'');
                        var headerRow = $(mainCatTbodyId).getElements('th');
                        var headerColSpan = headerRow[0].getProperty('colspan');

                        headerColSpan = parseInt(headerColSpan) + 1;
                        headerRow[0].setProperty('colspan', headerColSpan);

                        $each(catProperties, function(attributes, nodeId) {
                            var displayName = attributes[0];
                            var propRow = document.getElementById(nodeId);
                            var cellValue = responseObject[0][displayName];
                            var tableColumn = new Element('td', {'id': nodeId + '_' + deviceModel, 'class': 'deviceTableSubCat'});

                            // We insert before the last cell because it's used to stretch the page to 100%
                            var tableRowCells = propRow.getElementsByTagName('td');
                            var lastCell = tableRowCells[tableRowCells.length-1];

                            var spanHilighter = new Element('span');
                            if (cellValue == null) {
                                spanHilighter.innerHTML = '-';
                                tableColumn.appendChild(spanHilighter);
                                propRow.insertBefore(tableColumn, lastCell);
                            }
                            else if (cellValue == true) {
                                var tick = new Element('img', {'src': 'http://www.forum.nokia.com/piazza/devices/images/tick.gif', 'alt': 'True'});
                                spanHilighter.appendChild(tick);
                                tableColumn.appendChild(spanHilighter);
                                propRow.insertBefore(tableColumn, lastCell);
                            }
                            else {
                                spanHilighter.innerHTML = cellValue;
                                tableColumn.appendChild(spanHilighter);
                                propRow.insertBefore(tableColumn, lastCell);
                            }
                        });
                    });

                    // Hilight the different device features from the first one
                    if (hilightEnabled == true) {
                       fnDeviceTable.hilightFeatures();
                    }

					fnDeviceTable.resetEmptyRows();
					
                    // Disabling the select option for the selected device
                    fnDeviceTable.toggleSelectItemState($('deviceList'), deviceModel, false);
                    // Reset back to our selection message
                    $('deviceList').selectedIndex = 0;
                    window.focus();
                }
            }
            }).get({'device': deviceModel}
        );
     },

	/**
	* Returns true if the content of searchTermElement is contained  
	* in element or nested span inside element.
	*/
	isInElement: function(element, searchTermElement) {
		var found = false;
		elementChildren = element.childNodes;
		// test for content match
		if(element.innerHTML == searchTermElement.innerHTML) {
			found = true;
		}
		else {
			// test for match inside a nested span
			for (var i=0; i < elementChildren.length; i++) {
				var currentChild = elementChildren[i];
				if (currentChild != null && currentChild.nodeType == 1 && currentChild.nodeName == "SPAN"
				&& currentChild.innerHTML == searchTermElement.innerHTML) {										
					found = true;
				}			
			}
		}
		return found;
	},
	
    /**
     *  Hilight devices features that differ from those in the device in the first column.
     */
     hilightFeatures: function() {	
       $each (displayGroups, function(value, key) {
         var madeUpKey = key.replace(/[^A-Za-z0-9\-_:\.]+/g,'');
         var catRows = $('tbody_' + madeUpKey).getElements('tr');

         for (var q = 0; q < catRows.length; q++) {
            var row = $(catRows[q].getProperty('id')).getElements('td');
            // (row.length -  1) because we dont need the td with 100%  width to be hilighted
            for (var r = 1; r < (row.length -  1); r++) {
                var firstDeviceSpanElement = row[1].getElement('span');
                var compareDeviceSpanElement = row[r].getElement('span');							
				var compareChild = compareDeviceSpanElement.firstChild;
				var rowTitle = row[0].innerHTML;
				
				// links are excluded 
				if (rowTitle != "UAProf Link" && rowTitle != "Consumer Link" && rowTitle != "Developer Link" && rowTitle != "Notes") {
					// This tests for differences in items where there is a single line (no nested span)
					if (compareChild.nodeType == 3) {
						if (compareChild.nodeName == "A") {						
							compareDeviceSpanElement.className = 'noHilight';						
						}
						else if (compareDeviceSpanElement.innerHTML != firstDeviceSpanElement.innerHTML) {
							compareDeviceSpanElement.className = 'hilight';						
						}
						else {
							compareDeviceSpanElement.className = 'noHilight';						
						}
					}
					// This tests for differences where there are multiple items (nested span for each item)
					else {
						var compareChildren = compareDeviceSpanElement.childNodes;
						for (var i = 0; i < compareChildren.length; i++) {
							var currentChild = compareChildren[i];
							if (currentChild.nodeType == 1 && compareChild.nodeName == "SPAN") {
								if (!this.isInElement(firstDeviceSpanElement, currentChild)) {
									currentChild.className = 'hilight';
								}
								else {
									currentChild.className = 'noHilight';							
								}				
							}
						}										
					}					
				}
            }
         }
       }, this);
     },

	/**
	* Sets the display classes for a section of the device table (contained in the given tbody). 
	* This includes the tbody, initial tr (header) and main data trs.
	* Classes are used as follows:
	* tbody: 					  FnDeviceCollapsible: this section can be collapsed.
	* 		  					  FnTableGroupHidden/FnTableGroupNotHidden: indicates if the section is hidden because it contains no data.
	*						   	  FnTableGroupExpanded/FnTableGroupClosed: this section is expanded/closed because the user has expanded/closed it.
	* first tr (section header) : FnTableGroupHeaderOpen: the section is open.
	*							  FnTableGroupHeaderClosed: the section is closed because the user has closed it or never opened it.
	*							  FnTableGroupHeaderHidden: the section is hidden because it contains no data.
	* subsequent trs (features) : FnDeviceRowOpen: The row is open.
	* 							  FnDeviceRowClosed: Closed if the section is closed or if the row is hidden due to not containing any data.
	* Note that the tbody classes are the only ones that distinguish all possible states of open/closed hidden/not hidden.
	*/
	setSectionDisplay: function(tbody, expanded) {

		// ignore the header in the device comparison table.
		if(tbody.id != "FnDeviceTableHeader") {
			var rows = tbody.getElementsByTagName("TR");
			var sectionContainsData = false;
			var collapsible = this.includesClassName(tbody, "FnDeviceCollapsible");		
			if(!collapsible) {
				expanded = true;
			}

			// first process the main data rows.
			// skipping the top row because it is the section header
			for (var i = 1; i < rows.length; i++) {
				var currentRow = rows[i];
				var rowContainsData = this.rowContainsData(currentRow);

				if (rowContainsData) {
					sectionContainsData = true;
				}

				if (expanded && rowContainsData) {					 
					rows[i].className = 'FnDeviceRowOpen';
				}
				else {
					rows[i].className = 'FnDeviceRowClosed';
				}
			}
			this.setSectionHeaderDisplay(tbody, expanded, sectionContainsData, collapsible);
		}
	},

	/**
	* Sets the classes for the section tbody and header trs in the device table.
	*/
	setSectionHeaderDisplay: function(tbody, expanded, sectionContainsData, collapsible) {
		var rows = tbody.getElementsByTagName("TR");
    
		if(sectionContainsData) {
			if(expanded) {
				rows[0].className = "FnTableGroupHeaderOpen";
				tbody.className = "FnTableGroupNotHidden FnTableGroupExpanded";	
				var expandImageTag = rows[0].getElementsByTagName('IMG');
				if(expandImageTag.length>0) { // it is 0 if it's not collapsible
					expandImageTag[0].src = 'http://www.forum.nokia.com/images/icon_arrow_open.gif';								
				}
			}
			else {
				rows[0].className = "FnTableGroupHeaderClosed";
				tbody.className = "FnTableGroupNotHidden FnTableGroupClosed";
				var expandImageTag = rows[0].getElementsByTagName('IMG');				
				if(expandImageTag.length>0) { // it is 0 if it's not collapsible
					expandImageTag[0].src = 'http://www.forum.nokia.com/images/icon_arrow_closed.gif';													
				}
			}
		}
		else{
			rows[0].className = "FnTableGroupHeaderHidden";
			if(expanded) {
				tbody.className = "FnTableGroupHidden FnTableGroupExpanded";								
			}
			else {
				tbody.className = "FnTableGroupHidden FnTableGroupClosed";														
			}
		}	
		if(collapsible) {
			tbody.className = tbody.className + " FnDeviceCollapsible";
		}
	},
	
	/**
 	* Reset empty row/section hiding.
 	*/
     resetEmptyRows: function() {	
		var table = document.getElementById("FnDeviceTable");
		var tbodys = table.getElementsByTagName("TBODY");		
		
		for (var k = 0; k < tbodys.length; k++) {
			var currentBody = tbodys[k];
			var expanded = this.includesClassName(currentBody, "FnTableGroupExpanded");
			this.setSectionDisplay(currentBody, expanded);
		}
	 },

     toggleSelectItemState: function(selectList, value, isEnabled) {
         var options = selectList.getElements('option');
         for (var i=0; i < options.length; i++) {
             if (options[i].getProperty('value') == value) {
                 if (isEnabled == true) {
                    options[i].disabled = false;
                 }
                 else {
                     options[i].disabled = true;
                 }
             }
         }
     },
     
     // We need this function only for IE because ie does not allow SELECT tag options to be disabled
     // See usage in addDevice function   
     selectItemState: function(selectList, value) {
         var options = selectList.getElements('option');
           for (var i=0; i < options.length; i++) {
               if (options[i].getProperty('value') == value) {
                   return options[i].disabled;
               }
         }
     },

	/**
	* Toggles the section for the given header between expanded and not expanded.
	*/
     toggleDisplay: function(header) {
         // Use mootools to get the parent and then the children tr elements
         // mootools helps us with incompatabilities between IE and the rest of the world
         // Otherwise, we'd use straight DOM
         var tableRoadId = header.getAttribute('id');
         var parentTbody = $(tableRoadId).getParent();
         var tbodyRows = parentTbody.getElements('tr');

         // If we're collapsed, expand. Otherwise, collapse.
         var expandImageTag = tbodyRows[0].getElements('img');
         if (header.className == 'FnTableGroupHeaderOpen') {
             this.setDisplay(header, false);
         }
         else {
             this.setDisplay(header, true);
         }
     },

	/**
	* Sets the display classes for the section with the given header.
	*/
     setDisplay: function(header, expanded) {
         var tableRowId = header.getAttribute('id');
         var parentTbody = $(tableRowId).getParent();
		this.setSectionDisplay(parentTbody, expanded);
     },
	
	/**
	* Returns true if the given className is found in the className property of the given element.
	*/
	includesClassName: function(element, className) {
		if (element.className.indexOf(className) >-1) {
			return true;
		}
		else {
			return false;
		}
	},

	/**
	* Returns true if the given row contains any cells with more than a "-". 
	* Note - excludes the first cell in the row from the test as it is the row title.
	*/
	rowContainsData: function(row) {
		var containsData = false;
		var cells = row.getElementsByTagName("td");
		// skip the first cell because it is the feature name				
		for (var j = 1; j < cells.length-1; j++) {
			var currentCell = cells[j];
			if (!(currentCell.innerHTML.toUpperCase().indexOf(">-</SPAN>") > -1 || (currentCell.innerHTML == ""))) {
				containsData = true;
			} 
		}					
		return containsData;
	},

	/**
	* Expands all the sections in the device table.
	*/
     expandAll: function(expand) {
         // Get all tbody elements in the device table
         var catTbodies = $('FnDeviceTable').getElements('tbody');

         // Iterate over all the tbody elements
         for (var a=1; a < catTbodies.length; a++) {
             var tableRows = catTbodies[a].getElements('tr');
             var expandImageTag = tableRows[0].getElements('img');
             var headerCell = tableRows[0].getElements('th');
             var headerCellRow = headerCell.getParent();
             
             this.setDisplay(tableRows[0], expand);
         }
     },

     addDeviceToComparison: function(deviceModel) {
         deviceWindow = this.getDeviceWindow(deviceModel);
     },

     processDevices: function() {
         if (typeof deviceQueue != 'undefined') {
             while (device = deviceQueue.shift()) {
                 this.addDevice(device);
             }
         }
         this.processTimer = window.setTimeout("fnDeviceTable.processDevices();", 1000);
     },

     getDeviceWindow: function(initialDevice) {
         var width  = 815;
         var height = 600;
         var left   = (screen.width  - width)/2;
         var top    = (screen.height - height)/2;
         var params = 'width='+width+', height='+height;
         params += ', top='+top+', left='+left;
         params += ', directories=no';
         params += ', location=no';
         params += ', menubar=no';
         params += ', resizable=yes';
         params += ', scrollbars=yes';
         params += ', status=no';
         params += ', toolbar=no';

         // Null if we never had one, closed if we did but it's been closed
         if (this.deviceWindow == null || this.deviceWindow.closed) {
             queryString = '';
             if (initialDevice != null) {
                 queryString = "?device=" + initialDevice;
             }
             this.deviceWindow = window.open('http://www.forum.nokia.com/piazza/devices/devices/show-comparison' + queryString, 'showcomparison', params);
         }

         // focus the window if we are the currently focused window
         if (window.focus) {
             this.deviceWindow.focus()
         }

         return this.deviceWindow;
     },

 	/**
 	* Remove all the devices from the device comparison table
 	*/     
     removeAllDevices: function() {
         // Getting all the td's
         var deviceTDs = $$('td');
         
         for (var x=0; x < deviceTDs.length; x++) {
             // Removing all the cells with an id  because the first cell does not have a id
             var tdId = deviceTDs[x].getProperty('id');
             if (tdId != null) {
                 deviceTDs[x].dispose();
             }
         }
         // Resetting the empty rows
         fnDeviceTable.resetEmptyRows();
         // Reset the disable option in all select elements
         var comboBox = $("deviceList").getElementsByTagName("option");
          for (var y=0; y<comboBox.length; y++) {
              if (comboBox[y].disabled == true) {
                  comboBox[y].disabled = false;
              }
          }
     }
 });

// ===========================================================================
// =        Create an object and run the thing when the page is loaded       =
// ===========================================================================

// Our class object
fnDeviceTable = null;
hilightEnabled = true;;


jQuery(function() {
    fnDeviceTable = new FnDeviceTables();
    fnDeviceTable.processTimer = window.setTimeout("fnDeviceTable.processDevices();", 300);
	fnDeviceTable.resetEmptyRows();
});
