
var Drag = {
	"moveDiv": null,
	"dragObj": null,
	"draging": false,
	"start": function(ev) {
		Drag.dragObj = Utils.getTarget(ev);
		if (Drag.isdragable()) {
			Drag.createDiv();
			Drag.draging = false;
		}
		return false;
	},
	"draging": function(ev) {
		ev = ev || window.event;
		var el = $(".managLink").el;
		if (!Drag.isdragable() || Drag.moveDiv == null) {
			return;
		}
		// 设置被选定对象的鼠标跟随效果
		if (!Drag.draging) {
			var move = Drag.dragObj.cloneNode(true);
			Drag.moveDiv.innerHTML=move.innerHTML;
			//Drag.moveDiv.style.cssText = Drag.dragObj.style.cssText;


		}
		// 使用DOM操作，替换拖动过程中元素的位置
		var mouseOverObj = Utils.getTarget(ev);
		if (mouseOverObj.getAttribute("dragable") || mouseOverObj.getAttribute("container")) {
			if (Drag.dragObj.parentNode != mouseOverObj.parentNode) {
				if (mouseOverObj.nextSibling && mouseOverObj.nextSibling.id!="drag") {
					mouseOverObj.parentNode.insertBefore(Drag.dragObj, mouseOverObj);
				} else {
					mouseOverObj.parentNode.appendChild(Drag.dragObj);
					//mouseOverObj.parentNode.insertBefore(Drag.dragObj, mouseOverObj);
				}
			} else {
				if (mouseOverObj.nextSibling && mouseOverObj.nextSibling.id!="drag") {
					Drag.dragObj.parentNode.insertBefore(Drag.dragObj, mouseOverObj);
				} else {
					Drag.dragObj.parentNode.appendChild(Drag.dragObj);
					//Drag.dragObj.parentNode.insertBefore(Drag.dragObj, mouseOverObj);
				}
			}
		}
		//Drag.moveDiv.style.top = (Utils.getPosition(ev)).top + 'px';
		//Drag.moveDiv.style.left = (Utils.getPosition(ev)).left + 'px';
		Drag.moveDiv.style.top = (Utils.getMousePos(ev)).y - 305 + 'px';
		Drag.moveDiv.style.left = (Utils.getMousePos(ev)).x - document.body.offsetWidth/2 - el.scrollLeft + el.offsetLeft + 'px';
		//Drag.moveDiv.style.top = ev.pageY + el.scrollTop + 'px';
		//Drag.moveDiv.style.left = ev.pageX-240  + 'px';
		//alert($(".managLink").el.offsetLeft);
		//alert(document.body.offsetWidth - el.offsetLeft);
		//alert(document.body.offsetWidth );offsetHeight

		// 设置状态为拖动中
		Drag.draging = true;
		return false;
	},
	"end": function() {
		Drag.dragObj = null;
		Drag.removeDiv();
		Drag.draging = false;
		return false;
	},
	"removeDiv": function() {
		// 移除拖动时跟随鼠标移动的虚拟DIV层
		if (Drag.moveDiv != null) {
			Drag.moveDiv.style.display = "none";
			Drag.moveDiv.parentNode.removeChild(Drag.moveDiv);
			Drag.moveDiv = null;
		}
	},
	"createDiv": function() {
		// 初始化拖动时跟随鼠标移动的虚拟DIV层
		Drag.removeDiv();
		Drag.moveDiv = document.createElement("li");
		Drag.moveDiv.id = "drag";
		Drag.moveDiv.style.cssText = "position:absolute;z-index:10;";
		//document.body.appendChild(Drag.moveDiv);
		$(".managLink .argulis").el.appendChild(Drag.moveDiv);
		//$(".managLink").el.insertBefore(Drag.moveDiv,$(".managLink").el.firstChild);
	},
	"isdragable": function() {
		// 验证当前对象是否为可拖动的对象
		if (Drag.dragObj == null || !Drag.dragObj.getAttribute("dragable")) {
			return false;
		}
		return true;
	}
};
var Utils = {
	"getTarget": function(ev) {
		// 获得当前的目标源对象
		ev = ev || window.event;
		var tget = ev.target || ev.srcElement;
		return tget;
	},
	"getPosition": function(ev) {
		// 设置坐标
		// 模拟一个鼠标跟随的效果
		ev = ev || window.event;
		return {
			top: ev.clientY + document.body.scrollTop -305,
			//top:  ev.screenY,
			//top: ev.clientY +(document.documentElement.scrollTop || document.body.scrollTop)-(document.documentElement.clientTop || document.body.clientTop),
			//left: ev.screenX
			left: ev.clientX +(document.documentElement.scrollLeft || document.body.scrollLeft)
		};
	},
	"getMousePos":function(event) {
            var e = event || window.event;
            var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
            var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
            var x = e.pageX || e.clientX + scrollX;
            var y = e.pageY || e.clientY + scrollY;
            //alert('x: ' + x + '\ny: ' + y);
            return { 'x': x, 'y': y };
   },
   "getPos":function(e) {
    e = e || window.event;
    var x = e.pageX || (e.clientX + (e.scrollLeft || e.scrollLeft));
    var y = e.pageY || (e.clientY + (e.scrollTop  || e.scrollTop));
    return {'l':x+300,'t':y};
  }
};
var atxt = false,canAddurl = false;
 (function() {
	function b(i, h) {
		var j = '<div class="usealert" num="' + h + '">' + i + "</div>";
		var g = document.createElement("div");
		g.setAttribute("num", h);
		g.setAttribute("class", "usealert");
		g.innerHTML = i;
		$(".mtop").el.appendChild(g);
		$(".usealert").show();
		new Animate($(".usealert").get(0), "left", {
			from: 0,
			to: 120,
			time: 200
		}).start();
		setTimeout(function() {
			$(".usealert").each(function(b) {
				b.parentNode.removeChild(b);
				//$(".usealert").hide();
			})
		},
		3000)
	};
	function getDomain(r) {
		var p = /[\w-]*\.*[\w][\w-]*\.(?:com\.cn|net\.cn|org\.cn|gov\.cn|com\.hk|com|cn|co|net|org|gov|cc|biz|info|hk)/ig;
		//alert(p.exec(r));
		//alert(r.match(p))
		if (p.exec(r)) {
			return r.match(p)
		} else {
			return "icon.png";
		}
	};
	function geticon(r) {
		var p = /\((.*)\)/ig;
		if (p.exec(r)) {
		var t=r.match(p);
		var t1=p.exec(r)[1];
			return t1;
		} else {
			return "icon.png";
		}
	};
	function edit() {
		$(".argulis li .edit").on("click",function(b) {
		b = b || window.event;
        var target = b.target || b.srcElement;
			$("#operUrl").html("修 改");
			var c = $(b.parentNode);
			//alert(p);
			$("#edtitle").el.value = c.el.innerHTML.replace(/<.+?>/gim,'');
			$("#edurl").el.value = c.el.getAttribute("data-url"); //($(this).parent().attr("data-url"));
			$(".inedits button").el.setAttribute("data-id", c.el.getAttribute("data-id"));
			h = true;
		}); //alert(k);
		$(".argulis li .cancel").on("click",function(b) {
			var c = $(b.parentNode);
			var q = c.el.getAttribute("data-id");
			var u = c.el.getAttribute("data-url");
			//alert(q)
			b.parentNode.parentNode.removeChild(b.parentNode);
			$(".ucom li").each(function(e) {
				var d = e.getAttribute("data-id");
				var v = e.getAttribute("data-url");
				if (q == d && u == v) {
					$(e).removeClass("added");
					//alert(d);
				}
			});
			h = false;
			atxt = true;
		});
	};
	$("#js_add,.add").each(function(b) {
		$(b).on("click",function(c) {
			$("#globalMask").show();
			$(".managLink").show();
		})
	});
	if (window.localStorage) {
		var l = localStorage.getItem("urlTemp_manage")
	} else {
		var l = Cookie.get("urlTemp_manage")
	}
	if (!l) {
		
		var n = [],
		j = "";
		$("#use_url li a").each(function(a) {
			var q = a.getAttribute("href"),
			y = a.style.cssText,
			//y = 'background:'+a.style.background,
			t = a.innerHTML,
			u = a.parentNode.getAttribute("data-id"),
			m = a.getAttribute("qtcms"),
			s = {};
			s.href = q;
			s.title = t;
			s.style = y;
			//s.icon = geticon(y);
			s.id = u;
			if(m=="1"){
				n.push(s);
			}
			//alert(a.getStyle("background"));
			//alert(y.match(/\([^\)]*\)/gim));
			//alert(geticon(y));
		});
		if (window.localStorage) {
			localStorage.setItem("urlTemp", JSON.stringify(n))
		} else {
			if(qingtiancms_com.Browser.ie6){
				Cookie.set("urlTemp", JSON.stringify(n))
			}else{
				Cookie.set("urlTemp", JSON.stringify(n))
			}
		}
		var m = "";
		$(n).each(function(q) {
			if (q.style) {
				m += '<li data-id="' + q.id + '" data-url="' + q.href + '" style=\'' + q.style + '\' dragable="true">' + q.title + '<i class="edit" title="编辑"></i><i class="cancel" title="删除"></i></li>'
			} else {
				m += '<li data-id="' + q.id + '" data-url="' + q.href + '" style=\'background:url(' + q.icon + ') no-repeat left;\' dragable="true">' + q.title + '<i class="edit" title="编辑"></i><i class="cancel" title="删除"></i></li>'
			}
		});
		$(".argulis").html(m);
		edit();
		//$(".argulis").el.innerHTML = "";
	} else {
		$("#reset").setStyle("visibility", "visible");
		atxt = true;
		var i = JSON.parse(l);
		c(i);
		edit();
		$("#js_add,.add").each(function(b) {
			$(b).on("click",
			function(c) {
				$("#globalMask").show();
				$(".managLink").show()
			})
		});
	};
	var p = $(".argulis li").size(),
	k = $("#operUrl"),
	h = false;
	k.on("click",function(e) {
		var t = $("#edtitle").el.value,
		r = $("#edurl").el.value,
		s = e.getAttribute("data-id");
		if (h) {
			$(".argulis li").each(function(e) {
				var d = e.getAttribute("data-id");
				if (s == d) {
					e.innerHTML = t + '<i class="edit" title="编辑"></i><i class="cancel" title="删除"></i>';
					e.setAttribute("data-url", $("#edurl").el.value);
					//alert(s);
				}
			});
			$("#edtitle").el.value = "";
			$("#edurl").el.value = "";
			k.html("添 加");
			h = false;
			$("#reset").setStyle("visibility", "visible");
			atxt = true;
			edit();
			return false
		} else {
			if (p >= qtcmsdd.a0605.a) {
				if (!canAddurl) {
					b("登录后可添加更多网址，在不同电脑都能看到收藏记录~",4);
					return false
				}
			};
			if (t == "") {
				b("请输入网站名称~", 1);
				$("#edtitle").el.focus();
				return false
			} else {
				if (r == "") {
					b("请输入网站地址~", 2);
					$("#edurl").el.focus();
					return false
				} else {
					if (r.indexOf("http://") == -1 && r.indexOf("https://") == -1) {
						b("请输入正确的网址~", 3);
						$("#edurl").el.focus();
						return false
					}
				}
			};
			//var q = '<li data-url="' + r + '" data-id="' + s + '" selectable="true" group="0" dragobj="0"><span>' + t + '</span><i class="edit" title="编辑"></i><i class="cancel" title="删除"></i></li>';
			var y = 'background:url(/icon/' + getDomain(r) + ') no-repeat left;';
			var y = 'background:url(' + qtcmsdd.it+'ico/ie.gif' + ') no-repeat 1px;padding-left:20px;';
			var g = document.createElement("li");
			g.setAttribute("data-id", Date.parse(new Date()));
			if (r.indexOf("http://") == -1 && r.indexOf("https://") == -1) {
				r = "http://" + r;
			} else {}
			//alert(r.indexOf("http://"));
			g.setAttribute("data-url", r);
			g.style.cssText = y;
			g.setAttribute("dragable", "true");
			var q = t + '<i class="edit" title="编辑"></i><i class="cancel" title="删除"></i>';
			g.innerHTML = q;
			$(".argulis").el.appendChild(g);
			//alert(q);
			$("#edtitle").el.value = "";
			$("#edurl").el.value = "";
			e.setAttribute("data-id",Date.parse(new Date()));
			$("#reset").setStyle("visibility", "visible");
			edit();
			atxt = true
		};
		edit();
		return false;
	});
	$("ol.ucom li").each(function(a) {
		$(a).hover(function(t) {
			$(t).addClass("hover")
			},
		function(t) {
			$(t).removeClass("hover")
		});
		var s = a.getAttribute("data-id"),
		r = a.getAttribute("data-url"),
		t = a.innerHTML;
		//alert("0");
		$(a).on("click",function(c) {
			$(a).hasClass("added",function(h) {
				if (h) {
					b("您已经添加过了···", 1);
					//alert("0");
					return false
				} else {
					if ($(".argulis li").size() >= qtcmsdd.a0605.a) {
						if (!canAddurl) {
							//o("登录后可添加更多网址，在不同电脑都能看到收藏记录~");
							b("您好，您最多只能保存24个网址~",5);
							return false
						}
					}
					//var q = '<li data-id="' + $(this).attr("data-id") + '" data-url="' + $(this).attr("data-url") + '" dragobj="0" selectable="true" group="0"><span>' + $(this).html() + '</span><i class="edit" title="编辑"></i><i class="cancel" title="删除"></i></li>';
					var g = document.createElement("li");
					var y = 'background:url(/icon/' + getDomain(r) + ') no-repeat left;';
					var y = 'background:url(' + qtcmsdd.it+'ico/ie.gif' + ') no-repeat 1px;padding-left:20px;';
					g.setAttribute("data-id", s);
					g.setAttribute("data-url", r);
					g.style.cssText = y;
					g.setAttribute("dragable", "true");
					var q = t + '<i class="edit" title="编辑"></i><i class="cancel" title="删除"></i>';
					g.innerHTML = q;
					$(".argulis").el.appendChild(g);
					$(c).addClass("added");
					c.setAttribute("title", "已经添加");
					atxt = true;
				}
			});
			edit();
			return false;
		});
		$("#reset").setStyle("visibility", "visible");
	});
	function c(j) {
		var i = "",
		h = "";
		$(j).each(function(k) {
			if (k.style) {
				i += '<li data-id="' + k.id + '"><a href="' + k.href + '" style=\'' + k.style + '\'>' + k.title + "</a></li>";
				h += '<li data-id="' + k.id + '" data-url="' + k.href + '" style=\'' + k.style + '\' dragable="true">' + k.title + '<i class="edit" title="编辑"></i><i class="cancel" title="删除"></i></li>'
			} else {
				i += '<li data-id="' + k.id + '"><a href="' + k.href + '" style=\'background:url(' + k.icon + ') no-repeat left;\'>' + k.title + "</a></li>";
				h += '<li data-id="' + k.id + '" data-url="' + k.href + '" dragable="true">' + k.title + '<i class="edit" title="编辑"></i><i class="cancel" title="删除"></i></li>'
			}
			$(".ucom li").each(function(e) {
				if (e.getAttribute("data-id") == k.id) {
					$(e).addClass("added");
					e.setAttribute("title", "已经添加")
				}
			})
		});
		if (j.length %6==0) {
			i += '<li class="add" id="js_add"><span title="添加网址"></span></li>';
		} else {
			i += '<li class="add-normal" id="js_add"><span class="normal" title="添加网址">添加网址</span></li>';
		};
		$("#use_url").html(i);
		$(".argulis").html(h);
	};
	function o(r) {
		//var q = '<div class="useAlert"><div class="useTitle"><h5>提示</h5><a href="javascript:;"></a></div><p class="umsg"><i></i><em>' + r + '</em></p><div class="useSelt"><a id="openLogin">登 录</a> <a class="c">取 消</a></div></div>';
		//var g = document.createElement("div");
		//g.setAttribute("class", "useAlert");
		$("#useAlert").show();
		var q = '<div class="useTitle"><h5>提示</h5><a href="javascript:;"></a></div><p class="umsg"><i></i><em>' + r + '</em></p><div class="useSelt"><a id="openLogin">登 录</a> <a class="c">取 消</a></div>';
		$("#useAlert").html(q);
		//g.innerHTML = q;
		//$(".managLink").el.appendChild(g);
		//$(".managLink").append(q)
	};
	var itip = document.getElementById("itip");
	//$(itip).hide();
	/*$(uu).on("click",function(c) {
		$("#globalMask").hide();
		$(".managLink").hide()
});*/
	$(__$("uclose")).each(function(e) {
		e.onmouseover = function() {
			if (atxt) {
			window.setTimeout(function() {
				$("#utip").show()
			},
			300);
			}
		};
		e.onmouseout = function() {
			window.setTimeout(function() {
				$("#utip").hide();
			},
			300);

		};
		e.onclick = function() {
			var i = [],
			j = "";
			$(".argulis li").each(function(e) {
				var k = e.getAttribute("data-url"),
				//n = e.getElementsByTagName("span")[0].innerHTML,
				n = e.innerHTML.replace(/<.+?>/gim,''),
				p = e.getAttribute("data-id"),
				y = e.style.cssText,
				m = {};
				m.href = k;
				m.title = n;
				m.style = y;
				//m.icon = geticon(y);
				m.id = p;
				i.push(m)
			});
			var h = "";
			$(i).each(function(k) {
				if (k.style) {
					h += '<li data-id="' + k.id + '"><a href="' + k.href + '" style=\'' + k.style + '\'>' + k.title + "</a></li>"
				} else {
					h += '<li data-id="' + k.id + '"><a href="' + k.href + '" style=\'background:url(' + k.icon + ') no-repeat left;\'>' + k.title + "</a></li>"
				}
			});
			if (i.length %6==0) {
				h += '<li class="add" id="js_add"><span title="添加网址"></span></li>';
			} else {
				h += '<li class="add-normal" id="js_add"><span class="normal" title="添加网址">添加网址</span></li>';
			};
			$("#use_url").html(h);
			$(".managLink").hide();
			//$("#globalMask").remove();
			if (window.localStorage) {
				localStorage.setItem("urlTemp_manage", JSON.stringify(i))
			} else {
				if(qingtiancms_com.Browser.ie6){
					Cookie.set("urlTemp_manage", JSON.stringify(i))
					
				}else{				
					Cookie.set("urlTemp_manage", JSON.stringify(i))
				}
			}
			$("#globalMask").hide();
			$("#js_add,.add").each(function(b) {
											
				$(b).on("click",function(c) {
					$("#globalMask").show();
					$(".managLink").show()
				})
			});
			//$(".managLink").hide()
		}
	});
	//var reset = document.getElementById("reset");

	$(__$("reset")).on("click",	function() {
		//var h = '<div class="useAlert"><div class="useTitle"><h5>提示</h5><a href="javascript:;"></a></div><p class="umsg"><i></i><em>是否要恢复为默认网址?</em></p><div class="useSelt"><a href="javascript:;" id="rc">确 定</a> <a href="javascript:;" class="c">取 消</a></div></div>';
		//var g = document.createElement("div");
		//g.setAttribute("class", "useAlert");
		var q = '<div class="useTitle"><h5>提示</h5><a title=""></a></div><p class="umsg"><i></i><em>是否要恢复为默认网址?</em></p><div class="useSelt"><a id="rc" href="javascript:void(0)" target="_self">确 定</a> <a class="c" href="javascript:void(0)" target="_self">取 消</a></div>';
		//g.innerHTML = q;
		//$(".managLink").el.appendChild(g);
		//var q = '<div class="useTitle"><h5>提示</h5><a href="javascript:;"></a></div><p class="umsg"><i></i><em>' + r + '</em></p><div class="useSelt"><a id="openLogin">登 录</a> <a class="c">取 消</a></div>';
		$("#useAlert").el.innerHTML = q;
		$("#useAlert").show();
		$(".useSelt a.c, .useTitle a").each(function(b) {
			$(b).on("click",function(c) {
				//b.parentNode.parentNode.parentNode.removeChild(b.parentNode.parentNode);
				//$(".useAlert").remove();
				$("#useAlert").hide();
			})
		});
		var rc = document.getElementById("rc");
		$(__$("rc")).on("click",function(b) {
			if (window.localStorage) {
				localStorage.removeItem("urlTemp_manage")
			} else {
				Cookie.clear("urlTemp_manage")
			};
			window.location.reload();
		});
		//$(".managLink").append(h)
	});
	//$(".managLink .argulis").el.onmousemove = Drag.draging;
//$(".managLink .argulis").el.onmouseup = Drag.end;
//$(".managLink .argulis").el.onmousedown = Drag.start;
$(".managLink .argulis").each(function(e) {
var flag = false;
//e.onmouseout = function(){clearTimeout(flag);}
e.onmousemove = Drag.draging;
e.onmouseup = Drag.end;
e.onmousedown = Drag.start;
});
})();
