var devices;
// -------------------------------------------------------------------
// hasOptions(obj)
//  Utility function to determine if a select object has an options array
// -------------------------------------------------------------------
function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
	}

    function removeAllOptions(from) { 
	if (!hasOptions(from)) { return; }
	for (var i=(from.options.length-1); i>=0; i--) { 
		from.options[i] = null; 
		} 
	from.selectedIndex = -1; 
	} 

// -------------------------------------------------------------------
// addOption(select_object,display_text,value,selected)
//  Add an option to a list
// -------------------------------------------------------------------
function addOption(obj,text,value,selected) {
	if (obj!=null && obj.options!=null) {
		obj.options[obj.options.length] = new Option(text, value, false, selected);
		}
	}

    var control;
    //create a call-back listener class
    var listener = Class.create();
    listener.prototype = { 
        initialize: function() { },

   		onException: function(json) {
   			alert("Exception: " + json.msg);
			new Ajax.Request('/errorreport.php', {
				method: 'post',
				parameters: {
				subject: 'Garmin communicator Error',
				gpx: $('gpx').value,
				text: json.msg
				}
			});
    	},

		onStartFindDevices: function(json) {
            if($('status') != null) {
            	$('status').innerHTML = "Hľadám zariadenie";
			}
        },

		onFinishWriteToDevice: function(json) {
      		$('status').innerHTML = "Zapísaný súbor";
		},

		onWaitingWriteToDevice: function(json) { 
		        if(confirm(json.message.getText())) {
	    	 		$('status').innerHTML = "Prepisujem súbor";
		            json.controller.respondToMessageBox(true);
		        } else {
		     		$('status').innerHTML = "Neprepisujem súbor";
		            json.controller.respondToMessageBox(false);
		        }
		    },

		onFinishFindDevices: function(json) {
           if(document.all.find != undefined)
           		document.all.find.disabled = false;
            if(json.controller.numDevices > 0) {
	            if(document.all.upload != undefined)
	            	document.all.upload.disabled = false;
                str = "nasiel som " + json.controller.numDevices + " pristroje.";
                devices = json.controller.getDevices();
//				alert(document.all.devices);
                for( var i=0; i < devices.length; i++ ) {
	                if(document.all.devices == undefined) {
	                }
	                else {
	                	document.all.devices.options[document.all.devices.options.length] = new Option(devices[i].getDisplayName(), i,false);
	                }
					reportdevice(devices[i]);
	                    str += "<li>"+devices[i].getDisplayName()+"</li>";
                }
                
                str += "</ol>";
            if($('status') != null) {
                $('status').innerHTML = 'Koniec vyhľadávania';
            }

            } else {
            if($('status') != null)
                $('status').innerHTML = "Nebol nájdený žiaden prístroj. Ak máte prístroj pripojený, skúste vyhľadať znovu.";
            }
        }
    }

    function load() {
       	if(PluginDetect.detectGarminCommunicatorPlugin()) {
            control = new Garmin.DeviceControl();
            control.register(new listener());
            var unlocked = control.unlock( ["http://www.geocaching.sk","c9ac0f9a0323ce6baa7bb3aca8837062"] );
//            $('msg').innerHTML = "<h2>Našiel som plugin, unlocked="+unlocked+"</h2>";
		} else {
            //$('status').innerHTML = "<strong>Pre používanie tejto funkcie, je potrebné mať nainštalovaný Garmin Communicator Plugin, ktorý môžete bezplatne stiahnúť na stránke <a href=http://www8.garmin.com/products/communicator/>Garminu</a>.";
		}
//    	alert('OK');
    }

	function searchDevices() {
       	if(PluginDetect.detectGarminCommunicatorPlugin()) {
            control.findDevices();
		}	
	}

    function findDevices() {
       	if(PluginDetect.detectGarminCommunicatorPlugin()) {
            $('status').innerHTML = "Plugin je nainstalovany.";
		    removeAllOptions(document.all.devices);
		    document.all.find.disabled = true;
		    document.all.upload.disabled = true;
            control.findDevices();
		} else {
            $('status').innerHTML = "<strong>Pre používanie tejto funkcie, je potrebné mať nainštalovaný Garmin Communicator Plugin, ktorý môžete bezplatne stiahnúť na stránke <a href=http://www8.garmin.com/products/communicator/>Garminu</a>.";
		}
    }
