/**
 * Initializing of the main interface on Otworlds
 */

var $viewport;
var $canvas;
jQuery(document).ready(function(){
	$viewport = jQuery("#viewport");
	$canvas = jQuery("#canvas");
	
	function showmap(id) {
		jQuery(".toolbar").animate({'opacity': 1}, 300, function(){
			Mapeditor.load(id);
			_gaq.push(['_trackPageview', '/maps/'+id]);
		});
	}
	
	if (window.location.hash.substr(1, 5) == 'mapid') {
		jQuery("#welcome").remove();
		showmap(window.location.hash.substr(7));
	}else{
		_gaq.push(['_trackPageview', '/welcome']);
		
		jQuery('#welcome a[href="#welcome-listmaps"]').click(function(){
			jQuery.ajax(Mapeditor.config.urls.backend, {
				dataType: "json",
				data: {
					'action' : 'listMaps',
				},
				success: function(data){
					var $box = jQuery("#welcome");
					var list = '<h2>Available maps</h2>';
					list += '<ul>';
					jQuery.each(data.maps, function(key, val){
						list += '<li><a href="#mapid-'+ val.id +'" class="loadmap" data-id="'+ val.id +'">'+ val.name +'</a></li>';
					});
					list += '</ul>';
					$box.html(list);
					_gaq.push(['_trackPageview', '/maps']);
				}
			});
		});
		
		jQuery("#welcome").delegate('a.loadmap', 'click', function(){
			var $this = jQuery(this);
			showmap($this.attr('data-id'));
			jQuery("#welcome").animate({'opacity': 0}, 300, function(){
				jQuery("#welcome").remove();
			});
		});
	}
	
	//Keep track of whether we need to lookup ".tile.hovered" all the time
	var hoveredElements = 0;
	$viewport.on({
		mousemove: function(e) {
			if (Mapeditor.isEditing) {
				if(hoveredElements > 0) jQuery(".tile.hovered").removeClass('hovered');
				var $target = Mapeditor.internals.figureOutTile(e);
				$target.addClass('hovered');
				hoveredElements++;
			}
		},
		//Painting when editing is active
		click: function(e) {
			if (Mapeditor.isEditing) {
				var activeBrush = Mapeditor.Materials.Brushes.active;
				var $target = Mapeditor.internals.figureOutTile(e);
				
				var Tile = $target.getTile();
				Tile.setItemid( Mapeditor.Materials.Brushes[activeBrush].server_lookid );
				
				console.log('Painting '+Mapeditor.Materials.Brushes[activeBrush].name+' on '+$target.attr('col')+', '+$target.attr('row')+', '+Mapeditor.map.currentFloor);
			}
		}
	}, '.tile');
	jQuery(window).keyup(function(e) {
		//Spacebar
		if (e.which == 32) {
			Mapeditor.toggleEdit();
			e.preventDefault();
			e.stopPropagation();
		}
	});
	jQuery('#brushes').on({
		click: function(e) {
			var $this = jQuery(this);
			if (!$this.hasClass('active')) {
				jQuery('.active').removeClass('active');
				$this.addClass('active');
				Mapeditor.Materials.Brushes.active = $this.data('name');
				
				if (!Mapeditor.isEditing) {
					Mapeditor.toggleEdit();
				}
			}
		}
	}, '.brush')
	
});
@@ -1466,16 +1466,45 @@
 ts = 0;%0A
+%09var mouseIsPressed = false;%0A
 %09$viewpo
@@ -1511,16 +1511,16 @@
 rt.on(%7B%0A
-
 %09%09mousem
@@ -1756,16 +1756,471 @@
 ents++;%0A
+%09%09%09%09%0A%09%09%09%09if (Mapeditor.isEditing && mouseIsPressed) %7B%0A%09%09%09%09var activeBrush = Mapeditor.Materials.Brushes.active;%0A%09%09%09%09var $target = Mapeditor.internals.figureOutTile(e);%0A%09%09%09%09%0A%09%09%09%09var Tile = $target.getTile();%0A%09%09%09%09Tile.setItemid( Mapeditor.Materials.Brushes%5BactiveBrush%5D.server_lookid );%0A%09%09%09%09%0A%09%09%09%09console.log('Painting '+Mapeditor.Materials.Brushes%5BactiveBrush%5D.name+' on '+$target.attr('col')+', '+$target.attr('row')+', '+Mapeditor.map.currentFloor);%0A%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D
@@ -2538,32 +2538,32 @@
 _lookid );%0A%09%09%09%09%0A
-
 %09%09%09%09console.log(
@@ -2707,24 +2707,197 @@
 loor);%0A%09%09%09%7D%0A
+%09%09%7D,%0A%09%09mousedown: function(e) %7B%0A%09%09%09if (e.which == 1) %7B%0A%09%09%09%09mouseIsPressed = true;%0A%09%09%09%7D%0A%09%09%7D,%0A%09%09mouseup: function(e) %7B%0A%09%09%09if (e.which == 1) %7B%0A%09%09%09%09mouseIsPressed = false;%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%7D, '.ti

Enable painting by dragging the mouse