﻿//编辑命令 去掉@
//@javascript:alert(tmp_image_tools.init());
//<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
//  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
// JavaScript Document
function Supertoolsimages() {
    //参数01
    this.img_height = 0;
    this.img_width = 0;
    this.img_left = 0;
    this.img_top = 0;

    this.img_small_src = "";
    this.img_true_src = "";

    this.max_img_height = 0;
    this.max_img_width = 0;


    this.img_id = "";
    //初始化编辑器
    this.init = function () {
        $("<div class='images_tools_menu' ><a href='javascript:void(0)' onclick='tmp_image_tools.show(this)' >编辑</a></div>").insertBefore(".img_box");
	return false;
    }

    //显示编辑窗口
    this.show = function (obj) {
        //给该图片添加上ID
        tmp_image_tools.img_id = "img_id_" + Math.random();
        $(obj).parent().next(".img_box").find("img").attr("id", tmp_image_tools.img_id);
        //获得当前页面的参数
        tmp_image_tools.max_img_height = $(obj).parent().next(".img_box").height();
        tmp_image_tools.max_img_width = $(obj).parent().next(".img_box").width();
        //获得图片的基本信息
        tmp_image_tools.img_height = $(obj).parent().next(".img_box").find("img").height();
        tmp_image_tools.img_width = $(obj).parent().next(".img_box").find("img").width();
        tmp_image_tools.img_src = $(obj).parent().next(".img_box").find("img").attr("src");

        tmp_image_tools.img_true_src = tmp_image_tools.img_src.replace(tmp_image_tools.max_img_height + "_" + tmp_image_tools.max_img_width + "_thumbnail", "");
        if (tmp_image_tools.img_src.indexOf("http://") < 0) {
            tmp_image_tools.img_src = tmp_image_tools.img_src + "http://www.mlgoo.com";
        }
        //显示窗体
        $(document.body).append(this.get_html());
        $(".img_tools").ready(function () {
            $(".img_tools").dialog({
                title: "图片编辑器！",
                width: tmp_image_tools.img_width+100,
                height: tmp_image_tools.img_height + 100,
                buttons:[{
					text:'保存图片',
					iconCls:'icon-ok',
					handler:function(){
						tmp_image_tools.save_();
                        tmp_image_tools.close_();
					}
				},{
					text:'取消图片',
					handler:function(){
						 tmp_image_tools.close_();
					}
				}]
            });
            //绑定时间

            $(".img_bg img").resizable({ alsoResize: '.img_bg,.selector img' });
            $(".selector").draggable({ containment: '.img_bg', scroll: false, grid: [1, 1] })
        })
    }

    this.get_html = function () {
        //加载图片
        //自动计算合适的位置
        var bg_width = (tmp_image_tools.max_img_width*1.5);
        var bg_height = (tmp_image_tools.max_img_height*1.5);
        var top = (tmp_image_tools.max_img_height * 0.2);
        var left = (tmp_image_tools.max_img_width * 0.2);
        return "<div class='img_tools'><div class='img_bg'  style='width:" +bg_width +"px;height:" +bg_height +"px'><img src='" + tmp_image_tools.img_true_src + "'  style='width:" + bg_width + "px;height:" + bg_height + "px'></div><div class='selector' style='width:" + tmp_image_tools.max_img_width + "px;height:" + tmp_image_tools.max_img_height + "px;z-index:1001;left:"+left+"px;top:"+top+"px'></div></div>";
    }

    this.save_ = function () {
        //更新图片大小
        tmp_image_tools.img_height = $(".img_bg img").height();
        tmp_image_tools.img_width = $(".img_bg img").width();

        //计算LEFT
        tmp_image_tools.img_top = $(".selector").offset().top - $(".img_bg img").offset().top;
        tmp_image_tools.img_left = $(".selector").offset().left - $(".img_bg img").offset().left;
        tmp_image_tools.img_true_src = tmp_image_tools.img_true_src.replace("_sy", "");
        //修复地址
        if (tmp_image_tools.img_true_src.indexOf("http:") < 0) {
            tmp_image_tools.img_true_src = "http://www.mlgoo.com/" + tmp_image_tools.img_true_src;
        }
        $.ajax({
            type: "POST",
            //注明 返回Json
            contentType: "application/json;gb2312",
            url: "http://server.mlgoo.com/Tools.asmx/edit_img?callback=?&now_height=" + tmp_image_tools.max_img_height + "&now_width=" + tmp_image_tools.max_img_width + "&now_top=" + tmp_image_tools.img_top + "&now_left=" + tmp_image_tools.img_left + "&show_height=" + tmp_image_tools.img_height + "&show_width=" + tmp_image_tools.img_width + "&img_url=" + tmp_image_tools.img_true_src,
            data: "{}",
            dataType: "json",
            success: function (result) {
                if (result.statu == 1) {
                    $("img[id$='" + tmp_image_tools.img_id + "']").replaceWith("<img sourceSize='" + Math.floor(Math.random() * 10000) + "' src='" + result.value + "?" + Math.random() + "'>");
                } else {
                    alert(result.value);
                }
            },
            error: function (data) {
                alert("调整失败！");
            }
        });

    }
    this.close_ = function () {
        $(".img_tools").dialog("close");
        $(".ui-dialog").empty();
        $(".img_tools").empty();
        $(".img_tools").remove();
    }
}

//初始化工具对象
var tmp_image_tools = new Supertoolsimages();
//页面加载完成后在初始化
// $(".img_box").ready(function () {
//		tmp_image_tools.init();
// })
