/***
 *	Ares 1.01 新增功能
 **/

// 解决IE6不缓存背景图片问题
if(!window.XMLHttpRequest) {
	document.execCommand("BackgroundImageCache", false, true);
}

// 添加收藏夹
function addFavorite(url, title) {
	if (document.all) {
		window.external.addFavorite(url, title);
	} else if (window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	}
}


/**
 * 生成随机字符串，长度传参所得
 * @return {}
 */
function randomChar(len){
	var  x="0123456789qwertyuioplkjhgfdsazxcvbnm";
  	var  tmp="";
  	var leng = len||5
  	for(var i=0;i<leng;i++)  {
  	tmp += x.charAt(Math.ceil(Math.random()*100000000)%x.length);
	}
  return  tmp;
}
// html字符串转义
function htmlEscape(htmlString) {
	htmlString = htmlString.replace(/&/g, '&amp;');
	htmlString = htmlString.replace(/</g, '&lt;');
	htmlString = htmlString.replace(/>/g, '&gt;');
	htmlString = htmlString.replace(/'/g, '&acute;');
	htmlString = htmlString.replace(/"/g, '&quot;');
	htmlString = htmlString.replace(/\|/g, '&brvbar;');
	return htmlString;
}

// 设置Cookie
function setCookie(name, value) {
	var expires = (arguments.length > 2) ? arguments[2] : null;
	document.cookie = name + "=" + encodeURIComponent(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ";path=" + path;
}


// 获取Cookie
function getCookie(name) {
	var value = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
	if (value != null) {
		return decodeURIComponent(value[2]);
    } else {
		return null;
	}
}
/**
 * 将当前参数字符串转 为script 对象
 * @param {} url  全路径
 * @return {}
 */
function urlToObj(url){
    if(url!=null){
         var urlarr = url.split("?");
        if(urlarr.length >1){
           var params = urlarr[1];
           var paramm = params.split("&");
           var paramobj = {};
           for(var i=0;i<paramm.length;i++){
              var ppparam = paramm[i];
              var pps = ppparam.split("=");
              if(pps.length >1){
                   paramobj[pps[0]]=pps[1];
              }
           }
            return paramobj;
        }
        return null;
  }
}
/**
 * 将参数对象转化为参数字符串 a=c&s=c
 * @param {} param ｛a:c,s:c｝
 * @return {String}
 */
function objToParamstr(param){
   if(param != null && typeof(param)=='object'){
      var paramStr = "";
      for(var o in param){ 
          if(o != null && typeof(o)=='string' &&( typeof(param[o])=='string'||typeof(param[o])=='number')){
             paramStr += o+"="+param[o]+"&";
          }
       }  
       if(paramStr.length > 1){ 
             return paramStr.substring(0,paramStr.length-1);
        }
  }
  return "";
}
 

// 删除cookie
function deleteCookie(name) {
	var expires = new Date();
	expires.setTime(expires.getTime() - 1000 * 60);
	setCookie(name, "", expires);
}

// 浮点数加法运算
function floatAdd(arg1, arg2) {
	var r1, r2, m;
	try{
		r1 = arg1.toString().split(".")[1].length;
	} catch(e) {
		r1 = 0;
	}
	try {
		r2 = arg2.toString().split(".")[1].length;
	} catch(e) {
		r2 = 0;
	}
	m = Math.pow(10, Math.max(r1, r2));
	return (arg1 * m + arg2 * m) / m;
}

// 浮点数减法运算
function floatSub(arg1, arg2) {
	var r1, r2, m, n;
	try {
		r1 = arg1.toString().split(".")[1].length;
	} catch(e) {
		r1 = 0
	}
	try {
		r2 = arg2.toString().split(".")[1].length;
	} catch(e) {
		r2 = 0
	}
	m = Math.pow(10, Math.max(r1, r2));
	n = (r1 >= r2) ? r1 : r2;
	return ((arg1 * m - arg2 * m) / m).toFixed(n);
}

// 浮点数乘法运算
function floatMul(arg1, arg2) {
	var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
	try {
		m += s1.split(".")[1].length;
	} catch(e) {}
	try {
		m += s2.split(".")[1].length;
	} catch(e) {}
	return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}

// 浮点数除法运算
function floatDiv(arg1, arg2) {
	var t1 = 0, t2 = 0, r1, r2;    
	try {
		t1 = arg1.toString().split(".")[1].length;
	} catch(e) {}
	try {
		t2 = arg2.toString().split(".")[1].length;
	} catch(e) {}
	with(Math) {
		r1 = Number(arg1.toString().replace(".", ""));
		r2 = Number(arg2.toString().replace(".", ""));
		return (r1 / r2) * pow(10, t2 - t1);
	}
}

// 设置数值精度
function setScale(value, scale, roundingMode) {
	if (roundingMode.toLowerCase() == "roundhalfup") {
		return (Math.round(value * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale);
	} else if (roundingMode.toLowerCase() == "roundup") {
		return (Math.ceil(value * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale);
	} else {
		return (Math.floor(value * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale);
	}
}

 

$().ready( function() {
	
	var $body;
	var $select;
	
	var dialogIdNumber = 0;
	var dialogZIndex = 100;
	var messageIdNumber = 0;
	/**
	 * 传入对象参数
	 * {
	 *   id,标识
	 *   content,内容
	 *   width,宽度
	 *   modal,是否有模式 
	 *   height,高度
	 *   type,//类型，success,warn ,error
	 *   title,标题
	 *   cancelCallback,点击取消息按钮回调
	 *   okCallback,点击确认按钮回调
	 *   ok 确定按钮值
	 *   cancel 取消按钮值
	 * }
	 */
	$.dialog = function (settings) {
		
		var dialogId;
		
		if (settings.id != null) {
			dialogId = settings.id;
		} else {
			dialogId = "dialog" + dialogIdNumber;
			dialogIdNumber ++;
		}
		if (settings.content == null) {
			settings.content = "";
		}
		if (settings.width == null || settings.width == "auto") {
			settings.width = 320;
		}
		if (settings.height == null) {
			settings.height = "auto";
		}
		
		if ($body == null) {
			$body = $("body");
		}
		if(!window.XMLHttpRequest && $select == null) {
			$select = $("select");
		}
		if ($select != null) {
			$select.css("visibility", "hidden");
		}
		
		var dialogHtml = "";
		
		if (settings.modal == true) {
			dialogHtml += '<div id="dialogOverlay' + dialogId + '" class="dialogOverlay"></div>';
		}
		
		if (settings.className != null) {
			dialogHtml += '<div id="' + dialogId + '" class="baseDialog ' + settings.className + '"><div class="dialogWrap"></div><div class="dialogMain">';
		} else {
			dialogHtml += '<div id="' + dialogId + '" class="baseDialog"><div class="dialogWrap"></div><div class="dialogMain">';
		}
		
		if (settings.title != null) {
			dialogHtml += '<div id="dialogTitle' + dialogId + '" class="dialogTitle">' + settings.title + '</div><div id="dialogClose' + dialogId + '" class="dialogClose"></div>';
		} else {
			dialogHtml += '<div id="dialogClose' + dialogId + '" class="dialogClose"></div>';
		}
		
		if (settings.type != null) {
			dialogHtml += '<div id="dialogContent' + dialogId + '" class="dialogContent dialog' + settings.type + 'Icon">' + settings.content + '</div>';
		} else {
			dialogHtml += '<div id="dialogContent' + dialogId + '" class="dialogContent">' + settings.content + '</div>';
		}
		
		if (settings.ok != null || settings.cancel != null) {
			dialogHtml += '<div id="dialogButtonArea' + dialogId + '" class="dialogButtonArea">';
		}
		
		if (settings.ok != null) {
			dialogHtml += '<input type="button" id="dialogOk' + dialogId + '" class="formButton" value="' + settings.ok + '" hidefocus="true" />';
		}
		
		if (settings.cancel != null) {
			dialogHtml += '<input type="button" id="dialogCancel' + dialogId + '" class="formButton" value="' + settings.cancel + '" hidefocus="true" />';
		}
		
		if (settings.ok != null || settings.cancel != null) {
			dialogHtml += '</div>';
		}
		
		dialogHtml += '</div></div>';
		
		$body.append(dialogHtml);
		
		var isDialogTitleDown = false;
		var dialogX;
		var dialogY;
		
		var $dialogOverlay = $("#dialogOverlay" + dialogId);
		var $dialog = $("#" + dialogId);
		var $dialogTitle = $("#dialogTitle" + dialogId);
		var $dialogClose = $("#dialogClose" + dialogId);
		var $dialogOk = $("#dialogOk" + dialogId);
		var $dialogCancel = $("#dialogCancel" + dialogId);
		
		$dialog.css({"width": settings.width, "height": settings.height, "margin-left": - parseInt(settings.width / 2), "z-index": dialogZIndex ++});
		
		function dialogClose() {
			if ($select != null) {
				$select.css("visibility", "visible");
			}
			$dialogOverlay.remove();
			$dialog.remove();
		}
		
		if (settings.autoCloseTime != null) {
			setTimeout(dialogClose, settings.autoCloseTime);
		}
		
		$dialogClose.click( function() {
			if ($.isFunction(settings.cancelCallback)) {
				if (settings.cancelCallback.apply() != false) {
					dialogClose();
				}
			} else {
				dialogClose();
			}
		});
		
		$dialogOk.click( function() {
			if ($.isFunction(settings.okCallback)) {
				if (settings.okCallback.apply() != false) {
					dialogClose();
				}
			} else {
				dialogClose();
			}
		});
		
		$dialogCancel.click( function() {
			if ($.isFunction(settings.cancelCallback)) {
				if (settings.cancelCallback.apply() != false) {
					dialogClose();
				}
			} else {
				dialogClose();
			}
		});
		
		$dialogTitle.mousedown(function (event) {
			$dialog.css({"z-index": dialogZIndex ++});
			var offset = $(this).offset();
			if(!window.XMLHttpRequest) {
				dialogX = event.clientX - offset.left + 6;
				dialogY = event.clientY - offset.top + 6;
			} else {
				dialogX = event.pageX - offset.left + 6;
				dialogY = event.pageY - offset.top + 6;
			}
			isDialogTitleDown = true;
			return false;
		});
		
		$(document).mousemove(function (event) {
			if (!isDialogTitleDown) {
				return false;
			}
			$dialog.css({"top": event.clientY - dialogY, "left": event.clientX - dialogX, "margin": 0});
		}).mouseup(function () {
			isDialogTitleDown = false;
		});
		
		$dialog.keypress(function(event) {
			if(event.keyCode == 13) {
				if ($.isFunction(settings.okCallback)) {
					if (settings.okCallback.apply() != false) {
						dialogClose();
					}
				} else {
					dialogClose();
				}
			}  
		});
		
		$dialogOverlay.show();
		$dialog.show();
		$dialog.focus();
		
		return dialogId;
	}
	
	$.closeDialog = function (dialogId) {
		if ($select != null) {
			$select.css("visibility", "visible");
		}
		
		var $dialogOverlay = $("#dialogOverlay" + dialogId);
		var $dialog = $("#" + dialogId);
		
		$dialogOverlay.remove();
		$dialog.remove();
	}
	/**
	 * 供使用参数
	 * content 内容
	 * type 类型
	 * reload :true,false 是否完成需要重刷本页
	 */
	$.message = function (settings) {
	
		if (typeof(settings)=='object' && settings.content == null) {
			settings.content = "";
		}
		
		if ($body == null) {
			$body = $("body");
		}
		/**
		if(!window.XMLHttpRequest && $select == null) {
			$select = $("select");
		}
		if ($select != null) {
			$select.css("visibility", "hidden");
		}
		**/
		var messageId = "message" + messageIdNumber;
		messageIdNumber ++;
		
		var messageHtml;
		/**
		 * type 分为: warn,success,error 三种
		 * 分别表示警告，成功，错误
		 */
		if (settings.type != null) {
			messageHtml = '<div id="' + messageId + '" class="sfMessage"><div class="messageContent message' + settings.type + 'Icon">' + settings.content + '</div></div>';
		} else {
			if(typeof(settings)=='object'){
				messageHtml = '<div id="' + messageId + '" class="sfMessage"><div class="messageContent">' + settings.content + '</div></div>';
			}else{
				messageHtml = '<div id="' + messageId + '" class="sfMessage"><div class="messageContent">' + settings + '</div></div>';
			}
		}
		
		$body.append(messageHtml);
		
		var $message = $("#" + messageId);
		
		$message.css({"margin-left": "-" + parseInt($message.width() / 2) + "px"}).show();
		
		setTimeout(function() {
			$message.hide("slow",function(){
			    $message.remove(); 
			 }); 
		}, 4500); 
		return messageId;
	}

});
