/*  
 * UCK Device Selection Box JavaScript Widget
 *
 * Copyright 2009 Mobilegov - All rights reserved
 *
 * @Author   Guillaume TARTAYRE for Mobilegov - guillaume.tartayre@mobilegov.com
 * @Date     2009/08/11
 * @Version  2.0
 * 
 * @Description
 * 
 * This widget has to be used along with UCK Javascript API.
 * It makes possible to display the devices discovered by UCK Applet 
 * and let the user select the devices he/she wants to be identified with.
 * The div element containing the Device Selection Box is dynamically
 * inserted in the document using DOM Injection and thus without modifying
 * the HTML page code.
 * The style of the Device Selection Box may be customized using CSS.
 * The position of the Device Selection Box is also defined by CSS (centered)
 * A default style is provided in the file UckDeviceSelectionBox.css 
 * Please refer to the UCK Javascript API Reference Guide for more details
 * 
 * @License
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 * 3. The names of the authors may not be used to endorse or promote
 *     products derived from this software without specific prior
 *     written permission.
 *  
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * 
*/

//********************************************************//
//************* UckDeviceSelectionBox Icons **************//
//********************************************************//

var UckDeviceSelectionBoxClassIcons = new Array();
var UckDeviceSelectionBoxClassTexts = new Array();
var UckDeviceSelectionBoxUnicityIcon = new Array();
var UckDeviceSelectionBoxUnicityTexts = new Array();

UckDeviceSelectionBoxClassIcons["17"] 	= "resources/images/picto/mini-picto-cle.png";
UckDeviceSelectionBoxClassIcons["841"]	= "resources/images/picto/mini-picto-pc.png";
UckDeviceSelectionBoxClassIcons["1089"]	= "resources/images/picto/mini-picto-tel.png";

UckDeviceSelectionBoxClassTexts["17"] 	= "USB Storage Devices";
UckDeviceSelectionBoxClassTexts["841"]	= "System Manufacturer";
UckDeviceSelectionBoxClassTexts["1089"]	= "Mobile Phones";

UckDeviceSelectionBoxUnicityIcon["80"]	= "resources/images/trustable.png";
UckDeviceSelectionBoxUnicityIcon["20"]	= "resources/images/untrustable.png";

UckDeviceSelectionBoxUnicityTexts["80"]	= "This device is trustable";
UckDeviceSelectionBoxUnicityTexts["20"]	= "This device is not trustable";

//********************************************************//
//******* UckDeviceSelectionBox JavaScript Object *******//
//********************************************************//

//var saws_device_selection_box_applet;
var saws_select_device_list = new Array();
var saws_selected_device_list = new Array();

function UckDevice()
{
    this.id = "";
    this.selected = false;
}

function UckDeviceSelectionBox(_return_function)
{
    
	this.select_box_return_function = _return_function;
    this.select_box_title = "";
	
    this.open = UckDeviceSelectionBoxOpen;
    this.close = UckDeviceSelectionBoxClose;
    this.setTitle = UckDeviceSelectionBoxTitle;
    this.getSelection = UckDeviceSelectionBoxGetSelection;
}

function Trouve(expr, a) {
	var i = 0
	while (i != -1) {
		i = expr.indexOf(a, i);
		if (i >= 0) {
			return 0;
		}
	}
	return -1;
}
var locale="fr";
function UckDeviceSelectionBoxOpen(devices)
{
	saws_selected_device_list = new Array();
	
	if(document.getElementById("local")!=null)
	locale=document.getElementById("local").value;
    //set device list
    saws_select_device_list = devices;
	var j=0;
	var InternalDeviceslist = new Array();
	var USBStorageDeviceslist = new Array();
	var MobilephoneDeviceslist = new Array();

	var m = 0;
	var p = 0;
	var s = 0;
	
    for(var i=0; i < saws_select_device_list.length; ++i)
	{
    	var uckDevice = new UckDevice();
		uckDevice.id =saws_select_device_list[i].getInstanceId();
	    saws_selected_device_list[j++] = uckDevice;
	    
	    
	    
	    devClass = saws_select_device_list[i].getDeviceClass();
	    if(devClass=="841")//System manufacturer
	    {
	    	InternalDeviceslist[m] = devices[i];
			m++;
	    }
	    else
	    	 if(devClass=="17")//USB
	    	 {
	    		USBStorageDeviceslist[p] = saws_select_device_list[i];
				p++;
			 }
	    	 else if(devClass=="1089")
	    	 {
					MobilephoneDeviceslist[s] = saws_select_device_list[i];
					s++;
	    	 }
	    	 
	}

    //get device choice box element
    var device_box = document.getElementById('SawsDeviceSelectionBox');
    if(!device_box)
        return;
    device_box.innerHTML='';
    //à vérifier que ça marche avec tous les navigateurs
    if(true)
    {
		var nbdevices=0;
		if (document.getElementById('options_computer').checked == true) {
			
			SetDevicesie(InternalDeviceslist);
			nbdevices+=InternalDeviceslist.length;
		}
		if (document.getElementById('options_mobilephone').checked == true) {
			
			SetDevicesie(MobilephoneDeviceslist);
			nbdevices+=MobilephoneDeviceslist.length;

				
		}
		if (document.getElementById('options_usbkey').checked == true) {
			
			SetDevicesie(USBStorageDeviceslist);
			nbdevices+=USBStorageDeviceslist.length;

		}


		
		if (locale == "en") {
			
			device_box.innerHTML += '<div  class="text" style="padding-top:0px;">'
					+ "<font class=\"Titre_Big_Blue\">If your device is not in this list, try to reconnect your device, and then start a new scanning."
					+ ' <a href="#" onclick="resetUck();startUck();">'
					+ "Refresh" + '</a></font>.</div>';
		}else if (locale == "fr") {
			
			device_box.innerHTML += '<div  class="text" style="padding-top:0px;">'
					+ "<font class=\"Titre_Big_Blue\">Si vous ne voyez pas votre mat&eacute;riel dans cette liste, essayer de le brancher &agrave; nouveau, puis lancer une nouvelle annalyse de votre mat&eacute;riel."
					+ ' <a href="#" onclick="resetUck();startUck();">'
					+ "Actualiser" + '</a></font>.</div>';
		} 
		if(nbdevices==0)
		{
			device_box.innerHTML="";
		}
    }
	

    //display device choice box
	device_box.style.visibility = 'visible';
	device_box.style.display = 'block';
	document.getElementById("waitdna").style.display="none";
	
}
		
function SetDevicesie(deviceslist) {
	var device_box = document.getElementById('SawsDeviceSelectionBox');
	for ( var i = 0; i < deviceslist.length; ++i) {
		devClass = deviceslist[i].getDeviceClass();
		strdevicebox = '<div id="displayed"><div class="picto"><a title="'
				+ UckDeviceSelectionBoxClassTexts[devClass]
				+ '"><img style="vertical-align:top;"  src="'
				+ UckDeviceSelectionBoxClassIcons[devClass]
				+ '"  onmouseover="javascript:GetDeviceName(\''
				+ UckDeviceSelectionBoxClassTexts[devClass]
				+ '\');"  onmouseout="javascript:GetDeviceName();"  /></a></div>'
				+ '<div class="pictoname"><input title="'
				+ UckDeviceSelectionBoxClassTexts[devClass]
				+ '" type="checkbox" class="SawsDevice" id="'
				+ deviceslist[i].getInstanceId()
				+ '" onclick="javascript:UckDeviceSelectionBoxCheckDevice(this);" title="'
				+ UckDeviceSelectionBoxClassTexts[devClass]
				+ '" onmouseover="javascript:GetDeviceName(\''
				+ UckDeviceSelectionBoxClassTexts[devClass]
				+ '\');"  onmouseout="javascript:GetDeviceName();" />';
		
				if(deviceslist[i].getDescription()=="")
					strdevicebox += Unknown_PC + '</div></div>';
				else
					strdevicebox += deviceslist[i].getDescription() + '</div></div>';
		device_box.innerHTML += strdevicebox;
	}

}

		
function GetDeviceName(name) {
	//$('device_id').update(name);
}
function UckDeviceSelectionBoxTitle(title)
{
    this.select_box_title = title;
    
    //get device box title element
    var device_box_title = document.getElementById('SawsDeviceSelectionBoxTitle');
    if(!device_box_title)
        return;
    
    device_box_title.innerHTML = title;    
}

//function UckDeviceSelectionBoxGetSelection()
//{
//    return saws_device_selection_box_applet.getDeviceSelection();
//}

function UckDeviceSelectionBoxClose()
{
    //hide device choice box
    var device_box = document.getElementById('SawsDeviceSelectionBox');
    if(device_box)
    {
        if(window.opera) //opera
            device_box.innerHTML = '';
        else //mozilla, IE, safari ... 
        {
        	//remove all chidren
            if(device_box.hasChildNodes()) {
                while(device_box.childNodes.length >= 1 )
                    device_box.removeChild(device_box.firstChild);
            }
        }
        device_box.style.visibility = 'hidden';
    }   
}

function UckDeviceSelectionBoxCheckDevice(element)
{
    //change checked element's CSS class
    if(element.className == "SawsDeviceSelected")
        element.className = "SawsDevice";
    else
        element.className = "SawsDeviceSelected";
        
    //check device
    for(var i=0; i < saws_select_device_list.length; ++i){
       if(saws_select_device_list[i].getInstanceId() == element.id){
    	   saws_selected_device_list[i].selected = !saws_selected_device_list[i].selected;
           return;
       }
    }
}

function UckDeviceSelectionBoxGetSelection()
{
    var result = new Array();
	var j=0;
    for(var i=0; i < saws_selected_device_list.length; ++i)
        if(saws_selected_device_list[i].selected)
        {
        	result[j++] = saws_select_device_list[i];
        }

    return result;
}
