function show_box(box, show){
	
	// trigger
	if (show == 3) {
		if (box == 'authbox') {
			var top = $(".authbox").css('top');
			if (top.slice(0,top.length-2) > 0) show = 0;
			else show = 1;
		}
		else if (box == 'infobox') {
			if ($("#infobox").css('display') == 'block') show = 0;
			else show = 1;
		}
	}
	
	if (show == 1) {
		if (box == 'authbox') {
			
			// hide infobox
			if ($("#infobox").css('display') == 'block') $("#infobox").fadeOut("fast");
			
			// show authbox
			$(".authbox").css('left', $(window).width()/2-$(".authbox").width()/2); 
			$(".authbox").animate({top: $(window).height()/2-$(".authbox").height()/2 + "px"});
			
		}
		else if (box == 'infobox') {
		
			// hide authbox
			$(".authbox").animate({top: ($(window).height()/2-$(".authbox").height()/2 + $(".authbox").height()) * -1  + "px" });
			
			// show infobox
			if ($("#infobox").css('display') == 'block') $("#infobox").fadeOut("fast");
		  $("#infobox").css('width', $(window).width()-165);
		  $("#infobox").css('height', $(window).height()-200);
		  $("#infobox").fadeIn("fast");
		  
		  $("#infobox_content").css('height', $("#infobox").height() - 20);
      $("#infobox_opt").css('left', $("#infobox").width() - $("#infobox_opt").width() + 20);
      		
		}
	} else if (show == 0) {
		if (box == 'authbox') {
			$(".authbox").animate({top: ($(window).height()/2-$(".authbox").height()/2 + $(".authbox").height()) * -1  + "px" });
		}
		else if (box == 'infobox') {
			$("#infobox").fadeOut("fast");
		}
	}
}

function load_page(page, ajax) {

	if (page != false) {
	  // load page in div using ajax calls
		if (ajax == true) {
			//change href
			location.href = "#"+page;
			
			show_box('infobox', 1);
		
		  // resize infobox
		  $("#infobox_content").html('<img src="img/loading.gif" alt="loading ..." />');
		  $("#infobox_content").load('x.php?'+ page, {}, function (responseText, textStatus, XMLHttpRequest) {
		    
		  });
	  }
	  // redirect page
		else {
			location.href = page;
		}
	}
	
	return false;
}

function close_page(){
	show_box('infobox', 0);
}

// --- users
function acceptuser(field, sid, acc) {
  $.post('x.php?t=users&s=2&u='+sid+'&a='+acc,{ },
  function () {
    field.parentNode.innerHTML=''; 
    load_page('t=users',true);
  });	
}

function post_form(form_id){
  var action = $('#'+form_id).attr('action');
  var form_elemen = $('#'+form_id).map(function(){ return $.makeArray(this.elements); }); 
  
  location.href = "#"+action.substr(1);
			
  show_box('infobox', 1);
		
  $("#infobox_content").html('<img src="img/loading.gif" alt="loading ..." />');
  $("#infobox_content").load('x.php'+ action, form_elemen, function (responseText, textStatus, XMLHttpRequest) {
    
  });
  //jQuery.each(form_elemen, function() {
  //  alert(this.name);
  //});
 
  return false;
}

function post_form_device(form_id){
	var action = $('#'+form_id).attr('action');
	
	// resize infobox
  if ($("#infobox").css('display') == 'block') $("#infobox").fadeOut("fast");
  $("#infobox").css('width', $(window).width()-165);
  $("#infobox").css('height', $(window).height()-200);
  $("#infobox").fadeIn("fast");
  
  $("#infobox_content").css('height', $("#infobox").height()-20);
  $("#infobox_opt").css('left', $("#infobox").width() - $("#infobox_opt").width()); //aizveert poga
  
  var form_name = $('input[name=form_name]').val()
  var form_onscreen = $('input[name=form_onscreen]:checked').val()
  var form_record = $('input[name=form_record]:checked').val()
  var form_public = $('input[name=form_public]:checked').val()
  
  $("#infobox_content").html('<img src="img/loading.gif" alt="loading ..." />');
  $("#infobox_content").load('x.php'+ action, {
		form_name: form_name,
		form_onscreen: form_onscreen,
		form_record: form_record,
		form_public: form_public
	}, function (responseText, textStatus, XMLHttpRequest) {
    
  });
 
  return false;
}

function help_box(tip) {
	alert(tip);
}

function device_key_gen(devid){
	var action = '?t=devices&s=2&d=' + devid;
	$("#device_key").html('<img src="img/loading2.gif" alt="loading ..." />');
  $("#device_key").load('x.php'+ action);	
  
  return false;
}

function get_positions() {
	//$('#debug').load('x.php?t=get_positions');
	if (get_positions_running == false) {
	  get_positions_running = true;
	  $.getJSON('x.php?t=get_positions',
    	function(data){
        jQuery.each(data, function(devid,device) {
        	
        	// trail
        	if (device.showtrail == 'y') {
          	if (typeof(window["poly_"+devid]) != 'undefined') {
					    mapstraction.removePolyline(window["poly_"+devid]);
				    }	
          	var poly = new Array;
          	jQuery.each(device.position, function(i,position) {
					    poly[poly.length] = new LatLonPoint(position.lat,position.lon);
      			});
      			
      			window["poly_"+devid] = new Polyline(poly);
				    mapstraction.addPolyline(window["poly_"+devid]);
          }      	
        	
        	// draw markers
        	jQuery.each(device.position, function(i,position) {
					  //if (i != 0) {
					  if (device.position.position_0 != position){
						  if (typeof(window["markerpos_"+devid+"_"+i]) != 'undefined') {
							  mapstraction.removeMarker(window["marker_"+devid+"_"+i]);
						  }	
						  window["markerpos_"+devid+"_"+i] = new LatLonPoint( position.lat, position.lon); 
						  window["marker_"+devid+"_"+i] = new Marker(window["markerpos_"+devid+"_"+i]);	
						  window["marker_"+devid+"_"+i].setIcon("img/marker_tailpos.png", [10,17], [5,17]);
						  window["marker_"+devid+"_"+i].setShadowIcon('img/marker_shadow.png', [17,17]);
            	
		          
						  window["marker_"+devid+"_"+i].setInfoBubble('<b>' + device.name + "</b><br />Laiks: " + position.time + "<br />" + "Platums: " + position.lat + "<br />" + "Garums: " + position.lon  + "<br />" + "Augstums: " + position.alt + "<br />" + "Ātrums: " + position.spd + " km/h");

						  mapstraction.addMarker(window["marker_"+devid+"_"+i]);
					  }
    			});
    			
    			// last marker
        	if (device.position.position_0 != undefined) {
        	
        		if (typeof(window["markerpos_"+devid]) != 'undefined') {
						  mapstraction.removeMarker(window["marker_"+devid]);
					  }	
					  window["markerpos_"+devid] = new LatLonPoint( device.position.position_0.lat, device.position.position_0.lon); 
					  window["marker_"+devid] = new Marker(window["markerpos_"+devid]);	
					  window["marker_"+devid].setIcon("img/marker_currpos.png", [20,34], [10,34]);
					
					  window["marker_"+devid].setLabel('"' + device.name + '"');
      			window["marker_"+devid].setInfoBubble('<b>' + device.name + "</b><br />Laiks: " + device.position.position_0.time + "<br />" + "Platums: " + device.position.position_0.lat + "<br />" + "Garums: " + device.position.position_0.lon  + "<br />" + "Augstums: " + device.position.position_0.alt + "<br />" + "Ātrums: " + device.position.position_0.spd + " km/h");
					  mapstraction.addMarker(window["marker_"+devid]);
					
        	}
        	
        	// now we can update
        	get_positions_running = false;
    			
			  });
    	});
  	}
}

function locate_device(devid) {
	if (typeof(window["markerpos_"+devid]) != 'undefined') {
		mapstraction.setCenterAndZoom(window["markerpos_"+devid], 14);
		window["marker_"+devid].openBubble();
	}
}
