			<!-- GA START -->
			var _gaq = _gaq || [];
			_gaq.push(['_setAccount', 'UA-17614686-5']);
			_gaq.push(['_trackPageview']);
			(function()
			{
				var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
				
				ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
				var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
			})();
			eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('$(\'[8-9=c]\').3(\'4\',5(a,b){6{7(2.1){0=2.1}d{0=\'e-f-g.h\'}i.j([\'k\',0])}l(m){}});',23,23,'url|hash|location|live|pageshow|function|try|if|data|role|||page|else|jquery|mobile|example|html|_gaq|push|_trackPageview|catch|err'.split('|'),0,{}))
		<!-- GA END -->

		    var isLoggedIn = false;
            var trackerInterval = undefined;
            var mrk = Array();
            var currentCollection = null;
            var map = null;
            var bounds = null;
            var balloons = null;
            var panorama;

            function logout() {
                isLoggedIn = false;
                mrk = null;
                mrk = Array();
                $('#select-choice-1').remove();
                $.mobile.changePage('#login');
                clearInterval(trackerInterval);
                trackerInterval = undefined;
                try {
                    $('#map_canvas_2').gmap('destroy');
                }
                catch (err) { }
            }

            function getAll()
            {
                $.ajax({ method: 'POST', cache: false, url: '/db.aspx', data: { action: 'getAll' }, dataType: 'json', success: function (data) {
                    if (data[0]['Key'] == 'error') {
                        if (data[0]['Value'] == 'login') {
                            logout();
                        }
                        else {alert(data[0]['Value']);}
                    }
                    else {
                        currentCollection = data;
                        isLoggedIn = true;

                        if (trackerInterval == undefined)
                            trackerInterval = setInterval(getAll, 5000);
                        if ($('#select-choice-1').length == 0) {
                            var s = '<option value="-1">All devices</option>';
                            for (var v = 0; v < data.length; v++)
                                s = s + '<option value="' + v + '">' + data[v][1]['Value'] + '</option>';
                            $('#select-container').html('<select data-theme="b" data-dividertheme="a" data-native-menu="false" name="select-choice-1" id="select-choice-1">' + s + '</select>');
                            $('#select-choice-1').selectmenu();
                            $('#select-choice-1').change(function () {
                                try {
                                    panorama.setVisible(false);
                                } catch(exc){}
                                if ($(this).val() == -1) {
                                    try { m.fitBounds(bounds); } catch (exc) { }
                                    $('#details-wrapper').attr('trkid', '');
                                    //$('#details-wrapper').hide();
                                    $('#commands-wrapper').hide();
                                    $('#details-header .ui-btn-text').html('Select Tracker to View Details');
                                    $('#details-speed').html('');
				    $('#details-location').html('');
				    $('#details-lastupdate').html('');
				    $('#details-lat').html('');
                                    $('#details-lon').html('');
                                     $("#streetViewToggle").hide();
                                }
                                else {
                                    $('#details-wrapper').attr('trkid', currentCollection[$(this).val()][0]['Value']);
                                    $('#map_canvas_2').gmap('option', 'center', new google.maps.LatLng(currentCollection[$(this).val()][2]['Value'], currentCollection[$(this).val()][3]['Value']));
                                    $('#map_canvas_2').gmap('option', 'zoom', 18);
                                    var streetViewCheck = new google.maps.StreetViewService();  
                                    streetViewCheck.getPanoramaByLocation(m.center, 50, function(result, status) {
                                        if (status != google.maps.StreetViewStatus.ZERO_RESULTS)
                                            $("#streetViewToggle").show();
                                        else
                                            $("#streetViewToggle").hide();
                                    });
                                    $('#details-header .ui-btn-text').html('Details');
                                    $('#details-speed').html(currentCollection[$(this).val()][7]['Value']);
                                    $('#details-location').html('Location: ' + currentCollection[$(this).val()][4]['Value']);
                                    $('#details-lastupdate').html('Last Update: ' + currentCollection[$(this).val()][5]['Value']);
                                    $('#details-lat').html('Latitude: ' + currentCollection[$(this).val()][2]['Value']);
                                    $('#details-lon').html('Longitude: ' + currentCollection[$(this).val()][3]['Value']);
                                    $('#details-wrapper').show();
                                    $("html").animate({ scrollTop: $(document).height() }, "slow");
                                    if (currentCollection[$(this).val()][10].length > 0) {
                                        $('#commands-inner').empty();
                                        for (var i = 0; i < currentCollection[$(this).val()][10].length; i++) {
                                            $('#commands-inner').append('<button id="cmdBtn' + i + '" devid="' + currentCollection[$(this).val()][0]['Value'] + '" cmd="' + currentCollection[$(this).val()][10][i]['Value'] + '" data-role="button">' + currentCollection[$(this).val()][10][i]['Key'] + '</button>');
                                            $('#cmdBtn' + i).button();
                                            $('#cmdBtn' + i).parent().bind('click', function () {
                                                var elementId = new Date().getTime();
                                                $(this).append('<span class="' + elementId + '"> ..Sending</span>');
                                                $.ajax({ method: 'POST', cache: false, url: '/db.aspx', data: { action: 'command', device: $(this).find('button').attr('devid'), command: $(this).find('button').attr('cmd') }, dataType: 'json', success: function (data) {
                                                    if (data[0]['Key'] != 'success') {
                                                        $('.' + elementId).css('color', 'red');
                                                        $('.' + elementId).html(data[0]['Value']);
                                                    }
                                                    else {
                                                        $('.' + elementId).css('color', 'green');
                                                        $('.' + elementId).html('OK');
                                                    }
                                                    setTimeout("$('." + elementId + "').remove();", 3000);
                                                }, error: function () {
                                                    $('.' + elementId).css('color', 'red');
                                                    $('.' + elementId).html('Network Error: Try Again');
                                                    setTimeout("$('." + elementId + "').remove();", 3000);
                                                }
                                                });
                                            });
                                        }
                                        $('#commands-wrapper').collapsible();
                                        $('#commands-wrapper').show();
                                    }
                                    else $('#commands-wrapper').hide();
                                }
                            });
                             $("#streetViewToggle").hide();
                         //   $('#details-wrapper').hide();
                        }
                        var isOnList = mrk.length;
                        for (var i = 0; i < data.length; i++) {
                            if($('#details-wrapper').attr('trkid')==data[i][0]['Value']) {
                                    $('#map_canvas_2').gmap('option', 'center', new google.maps.LatLng(data[i][2]['Value'], data[i][3]['Value']));
                                    $('#details-speed').html(data[i][7]['Value']);
                                    $('#details-location').html('Location: ' + data[i][4]['Value']);
                                    $('#details-lastupdate').html('Last Update: ' + data[i][5]['Value']);
                                    $('#details-lat').html('Latitude: ' + data[i][2]['Value']);
                                    $('#details-lon').html('Longitude: ' + data[i][3]['Value']);
                            }
                            for (var y = 0; y < mrk.length; y++) {
                                if (mrk[y].get('idx') == data[i][0]['Value']) {
                                    if (mrk[y].getPosition().lat() == data[i][2]['Value'] && mrk[y].getPosition().lng() == data[i][3]['Value']) {}
                                    else mrk[y].setPosition(new google.maps.LatLng(data[i][2]['Value'], data[i][3]['Value']));
                                    isOnList = true;
                                }
                            }

                            if (isOnList == 0) {
                                if (bounds == null) bounds = new google.maps.LatLngBounds();
                                balloons = new Array();
                                bounds.extend(new google.maps.LatLng(data[i][2]['Value'], data[i][3]['Value']));
                                try {
                                    $('#map_canvas_2').gmap('addMarker', { 'position': data[i][2]['Value'] + ',' + data[i][3]['Value'], 'draggable': false, 'bounds': false, 'icon': 'http://www.mytrackpanel.com/icons/' + data[i][6]['Value'] + '.png' },
                                        function (map, marker) {
                                            mrk[i] = marker;
                                            mrk[i].set('idx', data[i][0]['Value']);
                                            mrk[i].set('index', i);
                                    }).click(function() {
                                        $('#select-choice-1').val(this.get('index'));
                                        $('#select-choice-1').change();
                                    });
                                }
                                catch (exc) {
                                    $('#map_canvas_2').gmap({ 'zoom': 1 }).bind('init', function (evt, map) {
                                        m = map;
                                        try { m.fitBounds(bounds); } catch (exc) { }
                                    });
                                    $('#map_canvas_2').gmap('addMarker', { 'position': data[i][2]['Value'] + ',' + data[i][3]['Value'], 'draggable': false, 'bounds': false, 'icon': 'http://www.mytrackpanel.com/icons/' + data[i][6]['Value'] + '.png' },
                                        function (map, marker) {
                                            mrk[i] = marker;
                                            mrk[i].set('idx', data[i][0]['Value']);
                                            mrk[i].set('index', i);
                                    }).click(function() {
                                        $('#select-choice-1').val(this.get('index'));
                                        $('#select-choice-1').change();
                                    });
                                }
                            }
                        }
                    }
                }
                });
            }

            $("#streetViewToggle").live("tap", function () {
                var streetViewCheck = new google.maps.StreetViewService();  
                streetViewCheck.getPanoramaByLocation(m.center, 50, function(result, status) {
                    if (status == google.maps.StreetViewStatus.ZERO_RESULTS) {}
                    else {
                        panorama  = m.getStreetView();
                        panorama.setPosition(m.center);
                        panorama.setPov({ heading: 265, zoom:1, pitch:0 });
                        var toggle = panorama.getVisible();
                        if (toggle == false) 
                            panorama.setVisible(true);
                        else 
                            panorama.setVisible(false);
                   }
                });
            });

            $("#login_submit").live("tap", function () {

                $('#login_message').css('color', 'black');
                $('#login_message').html('Connecting...');
                $.ajax({ method: 'POST', cache: false, url: '/db.aspx', data: { action: 'login', user: $('#user').val(), pass: $('#pass').val() }, dataType: 'json', success: function (data) {
                    $('#login_message').html('');
                    $('#login_message').css('color', 'red');
                    if (data[0]['Key'] == 'error') {
                        if (data[0]['Value'] == 'Incorrect password' && $('#password-input').is(':hidden'))
                            $('#password-input').show();
                        else $('#login_message').html(data[0]['Value']);
                    }
                    else if (data[0]['Key'] == 'success') {
                        isLoggedIn = true;
                        $.mobile.changePage('#map');
                    }
                    else $('#login_message').html('Unable to connect. Please try again');
                }
                });
            });

            $("#logout").live("pageshow", function () {
                $.ajax({ method: 'POST', url: '/db.aspx', data: { action: 'logout' }, success: function (data) {
                    logout();
                }
                });
            });

		    // PAGE EVENTS

            $('#home').live("pageshow", function () {
                if (isLoggedIn == true) {
                    $('#login_link').hide();
                    $('#map_link').show();
                    $('#logout_link').show();
                }
                else {
                    $('#login_link').show();
                    $('#map_link').hide();
                    $('#logout_link').hide();
                }
            });

            $('#login').live("pageshow", function () {
                $('#pass').val('');
                $('#password-input').hide();
            });

            $('#map').live("pageshow", function () {
               // $('#details-wrapper').hide();
               // $('#commands-wrapper').hide();
                getAll();
                try { $('#map_canvas_2').gmap('refresh'); } catch (exc) { }
            });

		    $('#map').live("pagecreate", function () {
		        getAll();
		    });

		    $('#home').live("pagecreate", function () {
		        if($('.ui-page-active').attr('id')==undefined)
		            $.mobile.changePage('#map');
		    });


