import flash.display.Loader;
import flash.net.URLRequest;
var lc:LoaderContext = new LoaderContext(true);

var inid:String = stage.loaderInfo.parameters.inid; //最大上传文件　多少Ｍ
var temp_domain:String = stage.loaderInfo.parameters.my_domain;
var pic_url:String = ''; //图片地址
var news_name:String = ''; //图片地址
var xk_width:int; //选区宽度
var xk_height:int; //选取高度
var redian:String = ''; //区域热点
var mySprite:Sprite = new Sprite();  //选择区域

if(inid == null){inid = "1287712912"; }

if(temp_domain == null){temp_domain = "http://localhost/bao/admin/system/"; }
var my_domain:String = temp_domain + "";

tishi_mc.visible = false;
//初始选框
mySprite.x = 0;
mySprite.y = 0;
delDisplayList(baozhi_mc.mc_kuang)
baozhi_mc.mc_kuang.addChild(mySprite);
draw_quyu(100, 100, 100, 100);
//baozhi_mc.btm_scale.buttonMode = true;
baozhi_mc.btm_scale.x = baozhi_mc.mc_kuang.x + baozhi_mc.mc_kuang.width;
baozhi_mc.btm_scale.y = baozhi_mc.mc_kuang.y + baozhi_mc.mc_kuang.height;

var load_ban_data:URLLoader = new URLLoader();
fn_load_ban_data(); //加载数据

//加载数据--------------------------------------------------------------------------------------------------------------------------------------------------------------
function fn_load_ban_data() 
{
	
	var load_url:URLRequest = new URLRequest(my_domain + 'Newspaper_news_redian_load.php' + "?nid=" + inid + "&time=" + new Date());
	trace(my_domain + 'Newspaper_news_redian_load.php' + "?nid=" + inid + "&time=" + new Date())
	load_ban_data.load(load_url);
	load_ban_data.addEventListener (Event.COMPLETE, load_ban_data_ok);
	load_ban_data.addEventListener (IOErrorEvent.IO_ERROR, onLoadError);
	load_ban_data.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
    load_ban_data.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
	
	function securityErrorHandler(event:SecurityErrorEvent)
	{
		//dbug(load_php_1+'无法读取素材类别数据,错误代码(SECURITY_ERROR)');
	}
	function httpStatusHandler(event:HTTPStatusEvent)
	{
		//dbug(load_php_1+'正在读取产品数据，请稍后...')
	}
	function onLoadError(event:Event)
	{
		//dbug(load_php_1+'无法读取素材类别数据,错误代码(IO_ERROR)');
	}
}


function load_ban_data_ok(event:Event):void 
{
	var Temp_XML:XML=new XML();
	Temp_XML = new XML(event.target.data);

	pic_url = Temp_XML.can_shu.@pic_url; //
	news_name = Temp_XML.news_name; //
	redian = Temp_XML.can_shu.@redian;
	
	trace(get_string_array(redian, '||', 0), get_string_array(redian, '||', 1), get_string_array(redian, '||', 2), get_string_array(redian, '||', 3))
	if (redian != '')
	{
		draw_quyu(get_string_array(redian, '||', 2), get_string_array(redian, '||', 3),get_string_array(redian, '||', 0), get_string_array(redian, '||', 1));
		baozhi_mc.btm_scale.x = baozhi_mc.mc_kuang.x + baozhi_mc.mc_kuang.width;
		baozhi_mc.btm_scale.y = baozhi_mc.mc_kuang.y + baozhi_mc.mc_kuang.height;
	}
	
	title_mc.htmlText = '设置 <font color="#1974f0">' + news_name + '</font> 的链接区域';
	delDisplayList(baozhi_mc.pic_mc)
	loadImage(baozhi_mc.pic_mc,temp_domain+pic_url);	
}

//移动选区
baozhi_mc.mc_kuang.addEventListener(MouseEvent.MOUSE_DOWN, on_mc_kuang_mousedown);
function on_mc_kuang_mousedown(event:MouseEvent)
{
	baozhi_mc.mc_kuang.startDrag(false, new Rectangle(0, 0 , 450-baozhi_mc.mc_kuang.width,674-baozhi_mc.mc_kuang.height));
	baozhi_mc.btm_scale.visible = false;
}

//缩放选取
baozhi_mc.btm_scale.addEventListener(MouseEvent.MOUSE_DOWN, baozhi_mc_btm_scale_yd);
baozhi_mc.btm_scale.addEventListener(MouseEvent.MOUSE_UP, baozhi_mc_btm_scale_t);
function baozhi_mc_btm_scale_yd(event:MouseEvent)
{
	addEventListener(Event.ENTER_FRAME, baozhi_mc_btm_scale_d);
	baozhi_mc.btm_scale.startDrag();		
	trace(baozhi_mc.btm_scale.x)
		
}
		
function baozhi_mc_btm_scale_d(event:Event):void
{      	
	draw_quyu(baozhi_mc.btm_scale.x-baozhi_mc.mc_kuang.x-1,baozhi_mc.btm_scale.y-baozhi_mc.mc_kuang.y-1,baozhi_mc.mc_kuang.x,baozhi_mc.mc_kuang.y);	
}
function baozhi_mc_btm_scale_t(event:Event):void
{      	
        removeEventListener(Event.ENTER_FRAME, baozhi_mc_btm_scale_d);
		trace(baozhi_mc.btm_scale.x)
}


//保存btn_save
btn_save.addEventListener(MouseEvent.CLICK, fun_btn_save);
function fun_btn_save(event:MouseEvent)
{
	tishi_mc.note.text = '保存中,请稍候..';
	tishi_mc.dong_hua.visible = true;
	tishi_mc.visible = true;
	var params:URLVariables = new URLVariables();
	params.save_x = baozhi_mc.mc_kuang.x; 
	params.save_y = baozhi_mc.mc_kuang.y;
	params.save_w = baozhi_mc.mc_kuang.width; 
	params.save_h = baozhi_mc.mc_kuang.height;
	params.inid = inid;

	//发送数据	
	var save_url:URLLoader = new URLLoader();
	var request:URLRequest;
	request = new URLRequest(my_domain + 'Newspaper_news_redian_save.php');  
	request.method = URLRequestMethod.POST;
	request.data = params;
	save_url.load (request);
	save_url.addEventListener (Event.COMPLETE,save_complete);	
}

function save_complete(event:Event)
{
	tishi_mc.dong_hua.visible = false;
	var return_list:String = event.target.data;
	var return_data:Array = return_list.split("||"); // 0 $pass.'|1|'.$return_url|2|inid.
	trace(return_data)
	switch(return_data[0])
	{
		case "0": 			
			tishi_mc.note.text = '保存失败，请重试';
			break;
		case "1": 			
			tishi_mc.note.text = '保存成功。';
			ExternalInterface.call('go_url()');			
			break;
		default:
			tishi_mc.note.text = '无法处理您的请求，可能是以下原因：\n网络链接中断，服务器故障，请检查网络。';
       		break;
	}
	
}



//移除拖放操作
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUP_mc_stop);
function onMouseUP_mc_stop(param1:MouseEvent) : void
{
  
		baozhi_mc.mc_kuang.removeEventListener(MouseEvent.MOUSE_MOVE, on_mc_kuang_mousedown, true); //选区移动
		baozhi_mc.mc_kuang.stopDrag();
		baozhi_mc.btm_scale.visible = true;	
	
		baozhi_mc.btm_scale.stopDrag();
		baozhi_mc.btm_scale.removeEventListener(MouseEvent.MOUSE_MOVE, baozhi_mc_btm_scale_yd, true); //选区缩放
		
		baozhi_mc.btm_scale.x = baozhi_mc.mc_kuang.x + baozhi_mc.mc_kuang.width;
		baozhi_mc.btm_scale.y = baozhi_mc.mc_kuang.y + baozhi_mc.mc_kuang.height;
		
		//trace(baozhi_mc.mc_kuang.x,baozhi_mc.mc_kuang.y,baozhi_mc.mc_kuang.width,baozhi_mc.mc_kuang.height)
	
}



//画出区域选择
function draw_quyu(xk_w,xk_h,xk_x,xk_y)
{
	if (xk_w < 20){xk_w = 21;} if (xk_w >440){xk_w = 440;}
	if (xk_h < 20){xk_h = 21; }	if (xk_h > 664){xk_h = 664; }
	mySprite.graphics.clear();
	mySprite.graphics.lineStyle(1, 0x1974f0);
	mySprite.graphics.beginFill(0x1974f0,0.4);
	mySprite.graphics.moveTo(0, 0);
	mySprite.graphics.lineTo(xk_w, 0);
	mySprite.graphics.lineTo(xk_w, xk_h);
	mySprite.graphics.lineTo(0, xk_h);
	mySprite.graphics.endFill();
	baozhi_mc.mc_kuang.x = xk_x;
	baozhi_mc.mc_kuang.y = xk_y;
}





//创建容器并加载小图--------------------------------------------------------------------------------------------------------
function loadImage(mc:DisplayObjectContainer, url:String) {
	var loader:Loader = new Loader();
	loader.contentLoaderInfo.addEventListener (Event.INIT, onLoadInit);
	loader.contentLoaderInfo.addEventListener (IOErrorEvent.IO_ERROR, onLoadError);
	loader.load(new URLRequest(url),lc);
	function onLoadInit (event) {
		var emp:Bitmap = Bitmap(event.target.content);
		mc.addChild (emp);
	}
	function onLoadError (event) {
		trace ("地址："+url+" 的对象不存在.");
	}

}

//删除指定mc下的所有元素--------------------------------------------------------------------------------------------------------
function delDisplayList(container:DisplayObjectContainer,indentString:String = ""):void
{
    var child:DisplayObject;
	var cnum:uint=container.numChildren;
    for (var i:uint=0; i < cnum; i++)
    {
       	child = container.getChildAt(0);
		container.removeChildAt(0);
        //trace(indentString, child, child.name,container.numChildren,i,cnum); 		
    }
}

//取 字符 串数组  --------------------------------------------------------------------------------------------------------
function get_string_array(inString:String,informat:String = " ",i:Number=0)
{
	var temp_size_Array:Array = inString.split(informat);
	return temp_size_Array[i];
}

