document.onselectstart= function(event){
	if(window.event) {
	event =    window.event;
}
try {
	var the = event.srcElement ;
	if( !( ( the.tagName== "INPUT" && the.type.toLowerCase() == "text" ) || the.tagName== "TEXTAREA" ) )
	{
		return false;
	}
	return true ;
} catch(e) {
		return false;
	}
}

function kjax(msg)
{
	var partten = {"popmsg":msg};
	var Url = "./wz_include/index_json_url.php";
	$.post(Url,partten,function(data)
	{
		alert(data);
	});
}

function GridIsSale(start, end)
{
	var Sales = $("#Grid .apos");
	for (var i = 0; i < Sales.length; i++)
	{
		var element = $(Sales[i]);
		var RowStart = $(element).attr("rs");
		var RowEnd = $(element).attr("re");
		var CellStart = $(element).attr("cs");
		var CellEnd = $(element).attr("ce");		
		if ((start.Row <= RowEnd && end.Row >= RowStart) &&
		(start.Cell <= CellEnd && end.Cell >= CellStart))
		{
			return true;
		}

	}
	return false;
}

function moveForm_front()
{	
	var formStr = "<div style='width:400px;height:100px;'><input id='movetarget' type='text' value='' size='40'/><br><input type='button' value='submit'></div>";
	openWindow({'content':formStr,'type':'movediv'});
}

$(document).ready(function() {	
// 格子		
	var MouseDown = false;	
	$("#Grid,#SelectDiv").livequery(function(){
		
		$("#Grid .miaomiao").mouseenter(function(){
			var W = $(this).width();
			$(this).css({'z-index':'1009'});
			//$(this).find(".des_area").css({'left':W-2,'top':0,'display':'block','position':'absolute'});
			//$(this).find(".des_area").css({'left':W-2,'top':0,'position':'absolute'}).show(70);

			$(this).find(".des_area").css({'margin-left':'0px'}).show(70);
		}).mouseleave(function(){			
			$(this).css({'z-index':'990'});	
			//$(".des_area").css({"display":"none"});
			$(".des_area").css({'margin-left':'-2999px'}).hide(50);
		});

		$(this).mousedown(function(pos){
			if (pos.which == 1)	   MouseDown = true;	
			pos.stopPropagation();
		}).mouseup(function(pos){
			MouseDown = false;
		});

		$("#Grid").mousedown(function(pos){
			$("#MenuDiv").css({'display':'none'});
			if (pos.which == 1)
			{
				var SelectDiv = $("#SelectDiv");
				var startrow = parseInt(((pos.pageY - $("#Grid").offset().top - 3) / 13), 0);
				var startcell = parseInt(((pos.pageX - $("#Grid").offset().left - 3) / 13), 0);
				SelectDiv.css("top", startrow * 13);
				SelectDiv.css("left", startcell * 13);
				SelectDiv.width(1 * 11);
				SelectDiv.height(1 * 11);
				SelectDiv.show();
				SelectDiv.data("Start", { Row: startrow + 1, Cell: startcell + 1 });
				SelectDiv.data("End", { Row: startrow + 1, Cell: startcell + 1 });
			}
		});

		$(this).mousemove(function(pos){
			if (MouseDown)
			{
				var SelectDiv = $("#SelectDiv");
				var width = (parseInt((pos.pageX - SelectDiv.offset().left - 3) / 13, 0) + 1) * 13 - 2;
				var height = (parseInt((pos.pageY - SelectDiv.offset().top - 3) / 13, 0) + 1) * 13 - 2;
				var endrow = parseInt(((pos.pageY - $("#Grid").offset().top - 3) / 13), 0) + 1;
				var endcell = parseInt(((pos.pageX - $("#Grid").offset().left - 3) / 13), 0) + 1;
				var start = SelectDiv.data("Start");
				if (width <= 0)		{		width = 11;		}
				if (height <= 0)	{		height = 11;	}
				if (endrow < start.Row)	{	endrow = start.Row;	}
				if (endcell < start.Cell){	endcell = start.Cell;	}
				SelectDiv.width(width);
				SelectDiv.height(height);
				SelectDiv.data("End", { Row: endrow, Cell: endcell });
			}
		});

	});

	$(".miaomiao").live("contextmenu",function(e){
		
		$(this).css({'z-index':'1020'});
		var fn = $('#ated').attr('fn');
		var tb = $('#ated').attr('tb');
		var ai = $(this).attr('ai');
		var aj = $(this).attr('aj');
		$(this).append("<div class='edit_line'><a onclick=oW('"+fn+"','抢占',"+ai+",'"+tb+"')>抢占</a><a onclick=oW('"+fn+"','抢占',"+aj+",'"+tb+"')>A抢占</a><a onclick=oW('"+fn+"','抢占',-1,'"+tb+"')>全改</A></div>");		
		
		var Ml = e.pageX;               var Mt = e.pageY;		var W = $(this).width(); var H = $(this).height();
		var Dl = $(this).offsetParent().left; 	var Dt = $(this).offsetParent().top;
		
		$(this).find(".edit_line").css({'left':0,'top':0,'display':'block','position':'absolute'});
		//if($(this).find(".edit_line:has(.movdiv)").length >0 )	$(this).find(".edit_line").css({'left':0,'top':0,'display':'block','position':'absolute'});
		//else 		$(this).find(".edit_line").prepend("<a class='movdiv' onclick='moveForm_front()'>Moveit</a>").css({'left':0,'top':0,'display':'block','position':'absolute'});
		
		return false;
	});

	$('#SelectDiv').live("contextmenu",function(e)
	{
		var Ml = e.pageX;               var Mt = e.pageY;			var start = $(this).data("Start");		var end = $(this).data("End");		
		$("#right_main").data("Start",start);	$("#right_main").data("End",end);
		if (GridIsSale(start, end))	{			
			kjax("包含选定格子");			return false;		}
		var number = (end.Row - start.Row + 1) * (end.Cell - start.Cell + 1);
		var start = $(this).data("Start");
		
		//if(number>6*6){
		//	kjax("地球都是你的，你选了"+number+"格子,only 36 OK!");				return false;		}
		//confirm("您好：<br /><span>您选定了" + number + "个格子，可以上传一张 " + (SelectDiv.width() + 2) + "*" + (SelectDiv.height() + 2) + " 像素的图片，价值" + number * 50 + "元，即" + number * 50 * 100 + "点券。<br />你确定现在购买吗？</span>");
		$("#Gslected").html(start.Row+","+start.Cell+"<br>"+number); $("#Gpix").html( ($(this).width() + 2) + "*" + ($(this).height() + 2));		
		$("#MenuDiv").css({'top': Mt ,'left': Ml,"display":"block",'position':'absolute','z-index':'1000'});
		return false;
	});	

	$("#MenuDiv li").live("mouseover",function(){
		$(this).css({'background-color':'blue'});
		$(this).find("a").css({'color':'#fff'});
	}).live("mouseout",function(){
		$(this).css({'background-color':'#fff'});
		$(this).find("a").css({'color':'#000'});
	});

});
