// jQuery Isometric Mapper 0.5
// March 2009.
// Alexander Sidko (mortiy@gmail.com)

var dragging = false;
(function($) {
	$.fn.extend({
		gridMove : function(x, y, map_width, w, l, h, road) {

			return this.each(function() {

						// Creating a reference to the object
						
						$(this).css({
									"left" : Math.round(map_width / 2 + (x-l) * 32),
									"top" : Math.round(map_width / 2 + (y-w-l-h+1) * 16)
								});
								if (h<0) {h=0;}
								$(this).css("z-index", parseInt(map_width+y+h) );
								
								if (road) {$(this).css("z-index","15");}
								if ($(this).hasClass("water") ||  $(this).hasClass("watebo")) {$(this).css("z-index",10);}
								if (!road && (typeof($(this).data("params")) !== 'undefined')) {
								bid = $(this).data("params").bid;
								
								if ( bid == 1 || bid == 4 || bid == 57 || bid == 58 || bid == 73 || bid == 74 || bid == 75 || bid == 76 || bid == 112 || bid == 578 || bid == 579 || (bid>=564 && bid<=573) ) {$(this).css("z-index",3);}
								if (!inPoly(x,y)){$(this).css("z-index","-2"); $("#outside").append($(this)); }
								}
					});
		},
	
		gridGetCo : function() {
// Setting up the map coordinates according to element position
			return this.each(function() {


						var obj = $(this);
						data = obj.data("params");
						offset_left = (parseInt(obj.css("left")) - map_width/2)/32;
						offset_top  = (parseInt(obj.css("top"))  - map_width/2)/16;
						data.x = offset_left + data.l;
						data.y = offset_top + data.w + data.l + data.h - 1;
					});
		},
		
				gridSave : function() {
// Saving object
			return this.each(function() {


						var obj = $(this);
						data = obj.data("params");
						$.get("http://internetmap.info/cgi-bin/i-map.pl", {
		                a: "add_object",
		                id: data.id,
		                x: data.x,
		                y: data.y,
						type: ""+data.type,
						block_id : data.bid,
						city_id : city_id,
						wars : wars
		            	});
			});
		},
						gridRemove : function() {
// Removing object
			return this.each(function() {


						var obj = $(this);
						
						data = obj.data("params");
						$.post("http://internetmap.info/cgi-bin/i-map.pl", {
		                a: "remove_object",
		                id: data.id,
		                wars : wars
		            	});
						obj.remove();
						$("#land_control").hide();
			});
		},
		
		roadRemove : function() {
// Removing object
			return this.each(function() {


						var obj = $(this);
						
						data = obj.data("params");
						$.post("http://internetmap.info/cgi-bin/i-map.pl", {
		                a: "delete_road",
		                id: data.road
		            	});
						deleteRoad(data.road);

			});
		},
		
			pointRemove : function() {
// Removing road point
			return this.each(function() {


						var obj = $(this);
						
						data = obj.data("params");
						$.post("http://internetmap.info/cgi-bin/i-map.pl", {
		                a: "split_road",
		                pid: data.id,
						rid: data.road
		            	});
						deleteRoad(data.road);
						where = data.road;
						$.getScript("http://internetmap.info/cgi-bin/i-map.pl?a=get_roads&where="+where, function(){
    $(".mark_up").css("z-index", 5);
    $("#info_panel").html("LOADING: Marks...");
    
});
								
	
			});
		},
		
		
						gridUpdate : function(intX, intY) {
// Update object position
			return this.each(function() {
						var obj = $(this);
						data = obj.data("params");
           				 $.post("http://internetmap.info/cgi-bin/i-map.pl", {
              		     a: "update_object",
               			 id: data.id,
               			 wars : wars,
	             	     x: data.x,
             		     y: data.y
            });
			});
		},
		
					collisionCheck : function(xc,yc) {
// Update object position
			return this.each(function() {
				var string = "";
						var obj = $(this);
						data = obj.data("params");
					
						if (data.type=="traf") {collision =0;  return false;}
						
						if (typeof(xc) == 'undefined') {xc = data.x}
						if (typeof(yc) == 'undefined') {yc = data.y}
						width = data.w;
						length = data.l;
						if (length-Math.floor(length)>0.15){length=Math.ceil(length)} else {length=Math.floor(length)}
						if (width-Math.floor(width)>0.15){width=Math.ceil(width)} else {width=Math.floor(width)}
           				for(i=0;i<width;i++)
						{
							
							for (z=0;z<length;z++) {
								x = xc - z + i;
								y = yc - z - i;
								
								if (RoadMap[half_size+x][half_size+y].length>1) {collision =  1; $("#info_panel").html("Collision!");return true;}
							}
						}
						
					
			});
		},
		
						fillMap : function(what) {
// Update object position
			return this.each(function() {
			
						var obj = $(this);
						data = obj.data("params");
           				for(i=0;i<data.w;i++)
						{
							for (z=0;z<data.l;z++) {
								x = data.x - z + i;
								y = data.y - z - i;
								RoadMap[half_size+x][half_size+y]=what;
							}
						}
					
			});
		},
		
		roadDraggable : function(intX, intY, intW, intL, intH, intID, strType,blockID,RoadID) {
			return this.each(function() {
						var obj = $(this);
						obj.draggable({
						grid  : [32, 16],
						start : function() {
								dragging = true;
								$("#sector").hide();
							},

							stop : function() {
								dragging = false;
								$("#sector").show();
							//	$(this).gridUpdate();
							data = obj.data("params");
								
								road_id=data.road;
							
								RoadX = $("#road"+road_id).data("points").x;
				
								RoadX[data.id-1]= data.x
	
								RoadY = $("#road"+road_id).data("points").y;

								RoadY[data.id-1]= data.y;
								
								
								 $.post("http://internetmap.info/cgi-bin/i-map.pl", {
              		     a: "update_road",
               			 id: data.id-1,
	             	     x: data.x,
             		     y: data.y,
						 rid: road_id
            });
									deleteRoad(road_id);
				
								roadBuild(road_id,data.type);
								
							},
					//		revert: 'valid', 
							drag : function() {
								
								data = $(this).data("params");
								$(this).gridGetCo();
								$(this).css({
									"z-index": map_width+data.y-data.h
									
								});
							//	if (data.bid == 1 || data.bid == 4) {$(this).css("z-index",half_size);}
							//	$("#info_panel").html(data.type + ": " + cur_x + ";" + cur_y);

							}
						})
						.data("params",{x:intX,y:intY,w:intW,l:intL,h:intH,id:intID,type:strType,bid:blockID,road:RoadID})
						//.hover(function(){});
						
            });
			},
		gridInit : function(intX, intY, intW, intL, intH, intID, strType,blockID) {
// intX, intY 		 - cordinates;
// intW, intL, intH  - width, length, height;
// intID 			 - object id;
// strType 		     - object type; 
			return this.each(function() {

				// Creating a reference to the object
				
				
				obj = $(this);
				
			//	
		//		.draggable({
					//	  handle: 'img',
			//				grid  : [32, 16],
					//		cancel: "div",
			//				start : function() {
			//					data = $(this).data("params");
			//					dragging = true;
			//					$(this).fillMap(0);
			//					select_start_x = data.x;
			//					select_start_y = data.y;
			//					$("#sector").hide();
			//				},
// s
				//			stop : function() {
				//				dragging = false;
				//				data = $(this).data("params");
				//				$("#sector").show();
				///				if (!(((data.x % 2) && (data.y % 2)) || (!(data.x % 2) && !(data.y % 2)))) {
				//				data.y = data.y-1;
				//				}
				//				$(this).collisionCheck();
					//			
				//				if (!collision) {
				//					$(this).gridMove(data.x,data.y,map_width,data.w,data.l,data.h)
				//					$(this).gridUpdate();
				//					$(this).fillMap("obj");
				//				} else {
				//					$(this).gridMove(select_start_x,select_start_y,map_width,data.w,data.l,data.h)
				//					.gridGetCo();
			//						
			//						
				//					collision = 0;
				//				}
				//			},
				//	//		revert: 'valid', 
				//			drag : function() {
				//				
				//				data = $(this).data("params");
				//				$(this).gridGetCo();
				//				$(this).css({
				///					"z-index": map_width+data.y-data.h
				//					
				//				});
				//				if (data.bid == 1 || data.bid == 4) {$(this).css("z-index",100);}
				//				$("#info_panel").html(data.type + ": " + cur_x + ";" + cur_y);//
//
				//			}
				//		})
				obj.css({width: obj.find("img").width(),height:obj.find("img").height(),position:"absolute"})
				.data("params",{x:intX,y:intY,w:intW,l:intL,h:intH,id:intID,type:strType,bid:blockID})
				 .gridMove(intX,intY,map_width,intW,intL,intH)
				 data = obj.data("params");
				 for(i=0;i<data.w;i++)
						{
							for (z=0;z<data.l;z++) {
								x = data.x - z + i;
								y = data.y - z - i;
								RoadMap[half_size+x][half_size+y] = data.type;
							}
						} 
				 tickProgressBar(); 
				 
				
			});
		}
	});
})(jQuery);