/**
* 弹出提示
* 方法使用示例：
*    alertBox({type:"load",title:"请稍候，正在加载数据",is_close:0,first_btn:0,second_btn:0});
*    alertBox({type:"none",title:"基本信息",height:336,is_close:1,first_btn:"保 存",second_btn:"退 出",href:"js/alertbox_feeds/bi.html"});
*    alertBox({type:"tips",title:"基本信息",content:"您输入的信息有误"});
*@param paramObj.name 必填项； 提示框的名称，用来区别提示框；
*@param paramObj.type 可填项; 提示框的类型,用来控制显示出来的图标; 默认:"tips";  可能的值: "none"(没有图标) "tips"(提示) "warn"(警告) "error"(出错) "load"(正在加载);
*@param paramObj.title 可填项; 提示框的大标题内容; 默认:"提示"; 
*@param paramObj.width 可填项; 提示框整体宽度; 默认:"436" ;
*@param paramObj.height 可填项; 当paramObj.href有值时，用来控制提示框整体高度，便与定位; 默认："400";
*@param paramObj.x 可填项; 水平位置; 默认:2;  可能的值: 1(left) 2(center) 3(right);
*@param paramObj.y 可填项; 垂直位置; 默认:2;  可能的值: 1(top) 2(middle) 3(bottom);
*@param paramObj.is_close 可填项; 是否显示右上角的关闭按钮; 默认:1;  可能的值: 0(不显示) 1(显示)
*@param paramObj.first_btn 可填项; 左侧高亮按钮; 默认:"确定";  可能的值: 0(不显示) 按钮文字( 比如"我要投票" );
*@param paramObj.second_btn 可填项; 右侧按钮; 默认:"取消";  可能的值: 0(不显示) 按钮文字( 比如"我不参加" );
*@param paramObj.href 可填项; 载入的内容(比如"feeds.html","altbox_feeds/tips.html"); 
*@param paramObj.content 可填项; 载入的文字（比如"您的操作有误"）; 
*@return 
*@throws 
*/
function alertBox(paramObj){	
	// 设定初始值 数组内共12项
    var ObjVar = new Array(paramObj.name, paramObj.type, paramObj.title, paramObj.width, paramObj.height, paramObj.x, paramObj.y, paramObj.is_close, paramObj.first_btn, paramObj.second_btn, paramObj.href, paramObj.content , paramObj.useIframe),
    ValValue = "";
    for (i = 0; i < ObjVar.length; i++) {
        switch (i) {
        case 1:
            ValValue = "tips"
            break
        case 2:
            ValValue = "提示"
            break
        case 3:
            ValValue = "440"
            break
        case 5:
            ValValue = 2
            break
        case 6:
            ValValue = 2
            break
        case 7:
            ValValue = 1
            break
        case 8:
            ValValue = "确 定"
            break
        case 9:
            ValValue = "取 消"
            break
        case 10:
            ValValue = null
            break
        case 12:
            ValValue = null
            break
        default:
            ValValue = ""
        };
        ObjVar[i] = (ObjVar[i] === undefined ? ValValue: ObjVar[i]);
    };
    var Content = ObjVar[10] === null ? "<p>" + ObjVar[11] + "</p>": "";
	
	var zindex = 9999;
	$("div[id^='alertboxbg_']").each(function(i){
		if(parseInt($(this).css("z-index"))>=zindex)
			zindex = parseInt($(this).css("z-index"))+2;
	});
	 var AlertBox = "alertbox_" + ObjVar[0],
    AlertBoxBg = "alertboxbg_" + ObjVar[0],
    BodyObj = $("body"),
    IsClose = ObjVar[7] != 0 ? "<a href='#' hidefocus = 'true' id='closebtn"+(!paramObj.name?"":"_"+paramObj.name)+"' class='icon icon-close closebtn'>关闭</a>": "",
    FistBtn = ObjVar[8] != 0 ? "<a href='#' hidefocus = 'true' id='firstbtn"+(!paramObj.name?"":"_"+paramObj.name)+"' class='dialogbtn closebtn'>" + ObjVar[8] + "</a>": "",
    SecondBtn = ObjVar[9] != 0 ? "<a href='#' hidefocus = 'true' id='secondbtn"+(!paramObj.name?"":"_"+paramObj.name)+"' class='dialogbtn closebtn'>" + ObjVar[9] + "</a>": "";
	
    $("div[id^='alertboxbg_']").each(function(i) {
		if (parseInt($(this).css("z-index")) >= zindex) zindex = parseInt($(this).css("z-index")) + 2;
	});
    // 代码写入body
    var FeedsVar = "<div id='" + AlertBox + "' class='alertbox " + ObjVar[1] + "' style=' z-index:"+(zindex+1)+"'><div class='dialog'><div class='dialogtitle'><div class='dialogpadding'><h3>" + ObjVar[2] + "<span>" + IsClose + "</span></h3><div class='dialogline'></div></div></div><div class='dialogmain'><div class='dialogpadding'><div class='dialogmainheight dialogmainheight-auto'>" + Content + "</div></div></div><div class='dialogfoot'><div class='dialogpadding'>" + FistBtn + SecondBtn + "</div></div></div></div><div id='" + AlertBoxBg + "' class='alertboxbg' style='z-index:"+ zindex +"'></div>";
    BodyObj.append(FeedsVar);	
	//设置宽高
	$("#" + AlertBox).width( ObjVar[3] -fitLayoutVar($("#" + AlertBox))[0] );
	var MainWidth = $("#" + AlertBox).width() - fitLayoutVar($("#" + AlertBox +" .dialog"))[0] - fitLayoutVar($("#" + AlertBox +" .dialogmain"))[0] - fitLayoutVar($("#" + AlertBox +" .dialogmain .dialogpadding"))[0];
	$("#" + AlertBox + " .dialogmainheight").width( MainWidth);
	if(ObjVar[4] != ""){		
		$("#" + AlertBox + " .dialogmainheight").removeClass("dialogmainheight-auto");
		$("#" + AlertBox).height( ObjVar[4] -fitLayoutVar($("#" + AlertBox))[1] );
		var MainHeight = $("#" + AlertBox).height() -fitLayoutVar($("#" + AlertBox + " .dialog"))[1] - $("#" + AlertBox + " .dialogtitle")[0].clientHeight - fitLayoutVar($("#" + AlertBox + " .dialogtitle"))[1] - $("#" + AlertBox + " .dialogfoot")[0].clientHeight - fitLayoutVar($("#" + AlertBox + " .dialogfoot"))[1]- fitLayoutVar($("#" + AlertBox + " .dialogmain"))[1] - fitLayoutVar($("#" + AlertBox + " .dialogmain .dialogpadding"))[1] - fitLayoutVar($("#" + AlertBox + " .dialogmainheight"))[1];
		$("#" + AlertBox + " .dialogmainheight").height(MainHeight);
	}
	
	// 通过load加载外部文件 计算正文大小 及 向页面load文件
    if (ObjVar[10] !== null) {        
		if(ObjVar[12] !== null){
			$("#"+AlertBox+" .dialogmainheight").wrapInner("<iframe height='100%' width='100%' frameborder='no' src='"+ ObjVar[10] +"' ></iframe>");
		}else{
			$("#" + AlertBox + " .dialogmainheight").load( ObjVar[10] );
		}
    }
	
    /* 计算并更改窗口位置属性 */
    var Obj = $("#" + AlertBox),
    ObjBg = $("#" + AlertBoxBg),
	bodyObj = eval(document.compatMode &&document.compatMode=='CSS1Compat') ? document.documentElement : BodyObj[0],
	XValue = (bodyObj.clientWidth  - ObjVar[3]) / 2,
	YValue = (bodyObj.clientHeight - fitLayoutVar(Obj)[1] - Obj[0].clientHeight) / 2;
    objPosition(Obj, ObjVar[5], "left", XValue, "right");
    objPosition(Obj, ObjVar[6], "top", YValue, "bottom");
	
	//alert(Obj.height()+","+$("#" + AlertBox +" .dialogfoot").height());
	/* 点击 移除当前提示框 */
    $(".closebtn",Obj).click(function() {
        Obj.remove();
        ObjBg.remove();	
    }).focus(function(){
		$(this).addClass("firstbtn");
	}).blur(function(){
		$(this).removeClass("firstbtn");
	});
	// 弹出窗口获得焦点
    $("#" + AlertBox).focus(); 
    $("#firstbtn" +(!paramObj.name?"":"_"+paramObj.name)).focus();
}

/**
* 定义位置
* 方法使用示例： 
*   水平位置 objPosition("$(#test)",x,"left","100px","right"); 
*   垂直位置 objPosition("$(#test)",y,"top","300px","bottom"); 
*@param obj 必填项; 需要定位位置的对象
*@param coord 必填项; 水平/垂直 可选值: x y
*@param min_value 必填项; 水平的左/垂直的头位置 可选值: left top
*@param coord_value 必填项;  水平/垂直的位置 任意值
*@param max_value 必填项; 水平的右/垂直的底位置 可选值: right bottom
**/
function objPosition(obj,coord,min_value,coord_value,max_value){		
	switch(coord){
		case 1:obj.css(min_value,0); break
		case 3:obj.css(max_value,0); break
		default:obj.css(min_value,coord_value); break
	};
}

/* 计算对象 的margin padding border的大小 
*@param a 必需 [对象] 需要计算的对象
*@return FitLayoutVarArr [数组]，0为水平位置的大小，1为垂直位置的大小
*/
function fitLayoutVar(a) {
    var FitLayoutVarArr = [],
    MarginW = parseNaN( a.css("margin-left")) + parseNaN(a.css("margin-right") ),
    PaddingW = parseNaN( a.css("padding-left")) + parseNaN(a.css("padding-right") ),
    BorderW = parseNaN( a.css("border-left-width")) + parseNaN(a.css("border-right-width") ),
	
    MarginH = parseNaN( a.css("margin-top")) + parseNaN(a.css("margin-bottom") ),
    PaddingH = parseNaN( a.css("padding-top")) + parseNaN(a.css("padding-bottom") ),
    BorderH = parseNaN( a.css("border-top-width")) + parseNaN(a.css("border-bottom-width") );
    FitLayoutVarArr[0] = MarginW + PaddingW + BorderW;
    FitLayoutVarArr[1] = MarginH + PaddingH + BorderH;
    return FitLayoutVarArr;
};
/* 如果数值为NaN，则转成数字0 
*@param x 必需 要检测的值
*/
function parseNaN(x) {
    var x = parseInt((isNaN(parseInt(x))) ? 0 : x);
    return x;
}
