var dxq1;
var dxq2;
var orx;
var ory;
var drgobj;
var targetobj;
var http_request = false;
var cmenu;
var defaultAction = "/visit.do";//默认请求地址
/**
	二维数组,不同的域名对应不同的请求地址
*/
var domins = new Array(
					["haodewap.com","/visit.do"]
				);

/**
	根据域名返回指定的view页面的请求地址.
	根据已知域名请参考 变量： domins是二维数组
*/
function visitAction(){

	return domins[0][1];
}


function getPosition(v){
	var tobj=v;
	ory=v.offsetTop;
	orx=v.offsetLeft;
	while (tobj=tobj.offsetParent){
		ory+=tobj.offsetTop;
		orx+=tobj.offsetLeft;
	}
  ooo=new Object();
  ooo.x=orx;
  ooo.y=ory;
	return ooo;
}

function mdown(v) {
	if (event.button==2){
	  menushow(v,"addmenu");	
	}
	if (event.button!=1) return;
	var ddiv=document.getElementById("dragdiv");
	var oos=getPosition(v);
	ddiv.style.top=oos.y	;
	ddiv.style.left=oos.x	;
	
	targetobj=v;
	ddiv.innerHTML=v.outerHTML;

	targetobj.onmousemove=mmove;
	targetobj.onmouseup=mup;
	targetobj.setCapture();
	dxq1=event.x;
	dxq2=event.y;
}

function mmove(){
	if (event.button!=1) return;
	if ((event.clientY-dxq2)*(event.clientY-dxq2)+(event.clientX-dxq1)*(event.clientX-dxq1)<9) return;
	var ddiv=document.getElementById("dragdiv");
	ddiv.style.display="block";
	ddiv.style.cursor="array";
	var adiv=parent.document.getElementById("acceptdiv");
	ddiv.style.top=parseInt(ory)+event.clientY-dxq2;
	ddiv.style.left=parseInt(orx)+event.clientX-dxq1;
}

function mup() {

	
	return;//取消了拖拽功能、这里返回不做任何操作
	/***************************/
	var reg2_layer = document.getElementById("mousehandle");
	if(reg2_layer != null)
	{
		mousehandle.style.display="none";
		mousedone.style.display="block";	
	}
	/***************************/
	if (event.button!=1) 
		return;
	var ddiv=document.getElementById("dragdiv");
	var adiv=parent.document.getElementById("acceptdiv");
	targetobj.onmousemove=null;
	targetobj.onmouseup=null;
	ddiv.style.display="none";
	targetobj.releaseCapture();

	var oos=getPosition(adiv);


	//if ((event.clientY+parent.document.body.scrollTop>=parseInt(oos.y))&&(event.clientY+parent.document.body.scrollTop<=(parseInt(oos.y)+parseInt(adiv.offsetHeight)))){
	if ((event.clientY+parent.document.body.scrollTop>=parseInt(oos.y))&&(event.clientY+parent.document.body.scrollTop<=(parseInt(oos.y)+parseInt(adiv.offsetHeight)))){
		//alert((event.clientY+parent.document.body.scrollTop) + "---" + oos.y + "====" + (event.clientY+parent.document.body.scrollTop) + "---" + (parseInt(oos.y)+parseInt(adiv.offsetHeight)));
		/**
			这里说明在if中为什么+300的原因：由于wapview.jsp实在view.jsp页面中iframe的，如果按照原先的方式不追加300，会在以iframe页面中的坐标与left值来判断的
			如果加上300最后的参考值做向左靠拢300的距离;
		*/
		if ((event.clientX+parent.document.body.scrollLeft+350>=parseInt(oos.x))&&(event.clientX+parent.document.body.scrollLeft + 350<=(parseInt(oos.x)+parseInt(adiv.offsetWidth)))){
			adiva=adiv.getElementsByTagName("a");
					for(i=0;i<adiva.length;i++) {
						if (adiva[i].href==targetobj.href){
							if (!window.confirm("您确定要添加重复的链接么？")) return;
							break;
						}
					}
			aa="<a href='" + targetobj.href + "' class='link1_5'>"+targetobj.innerHTML+"</a><br>";
			adiv.innerHTML= aa + adiv.innerHTML;
			adiva=adiv.getElementsByTagName("a");
			for(i=0;i<adiva.length;i++) {
				adiva[i].style.cssText='';
				adiva[i].oncontextmenu = new Function("menushow(this,'menu');return false");
			}
			parent.saveFavorite();
			setTimeout("parent.isLogin();",1000);
		}
	}
}
function init(){

	window.ondeactivate=function(){
		alert(event.toElement);
	}

 	var i;
	//判断对象是否存在 if(document.all.url1!=null){
   //url1.oncontextmenu=new Function("menushow(this,'addmenu');return false");}
  
	var adiv=document.getElementById("acceptdiv");
	var alla=adiv.getElementsByTagName("a");

	//重构页面中非手机窗口部分的含有wapurl链接地址的右键菜单
	for(i=0;i<alla.length;i++) {
			if (alla[i].href.indexOf("visits.do")>0){
				alla[i].oncontextmenu=new Function("menushow(this,'addmenu');return false");
				alla[i].onmousedown=new Function("mdown(this)");
				alla[i].onmousemove=new Function("mouseMoveEvent(this)");
			}
	}
}

function initdiv(){
   var div = document.getElementById("acceptdiv");
   div.innerHTML=div.innerHTML;
   var divaaa=div.getElementsByTagName("a");
   for(i=0;i<divaaa.length;i++) {
         divaaa[i].classname="ap3gbox";
		 //divaaa[i].onclick=new Function("event.returnValue=false");
		 divaaa[i].oncontextmenu = new Function("menushow(this,'menu');return false");
         	//function(){menushow();event.returnValue = false
	 }
}

function menuinit(){
	if(event.srcElement.classname=="ap3gbox" && event.button==2){
		menushow(event.srcElement);
	}
		return false;
}


//---可以实现ff浏览器中的右击效果*****实现ff事件event***************************
function __firefox(){ 
	HTMLElement.prototype.__defineGetter__("runtimeStyle", __element_style); 
	window.constructor.prototype.__defineGetter__("event", __window_event); 
	Event.prototype.__defineGetter__("srcElement", __event_srcElement); 
} 
function __element_style(){ 
	return this.style; 
} 
function __window_event(){ 
	return __window_event_constructor(); 
} 
function __event_srcElement(){ 
	return this.target; 
} 
function __window_event_constructor(){ 
	if(document.all){ 
		return window.event; 
	} 
	var _caller = __window_event_constructor.caller; 
	while(_caller!=null){ 
	var _argument = _caller.arguments[0]; 
	if(_argument){ 
		var _temp = _argument.constructor; 
		if(_temp.toString().indexOf("Event")!=-1){ 
			return _argument; 
		} 
	} 
	_caller = _caller.caller; 
	} 
	return null; 
} 
if(window.addEventListener){ 
	__firefox(); 
}
//****************之上***********
	
function menuhide(mname){
	if (!mname) return;
	var tobs=document.getElementById(mname);
	tobs.style.display="none";
}
function menushow(v,mname,tagAHrefTitle){
	menuhide(cmenu);
	event.cancelBubble = true;//阻隔事件

	if(v.tagName == "DIV"){
		targetobj=document.getElementById('hidea');
	}//这里的表示用户点击的 收藏管理中的 管理 属性、则要根据 该方法的第三个参数获取正确的标签a 对象赋给targetobj。
	else if(v.tagName == "SPAN"){
		targetobj = getTargetobj(tagAHrefTitle);
	}else{
		targetobj=v;
	}
	
	var tob=document.getElementById(mname);
	tob.style.display="block";
	tob.style.top=event.clientY+document.body.scrollTop;
	if (event.clientY+parseInt(tob.offsetHeight)>parseInt(document.body.clientHeight)) 
		tob.style.top=document.body.clientHeight+document.body.scrollTop-parseInt(tob.offsetHeight);
	tob.style.left=event.clientX+document.body.scrollLeft;
	if (event.clientX+parseInt(tob.offsetWidth)>parseInt(document.body.clientWidth) ) 
		tob.style.left=document.body.clientWidth+document.body.scrollLeft-parseInt(tob.offsetWidth);
	document.body.onclick=new Function("menuhide('"+mname+"')");
	cmenu=mname;
	
}

function alterEdit(){
	var link = targetobj.href;
	var title = targetobj.innerHTML;
	document.getElementById('resetTitle').value = title;
	document.getElementById('resetLink').value = link;
	popDialog('resitename',400,150);
}

function alterAdd(aLink,aTitle){

	var resultA = null;//最后的结果数据
	var subContent = null;//存储截取的内容字符串：站点链接地址
	var flag = "0";//0=否、1=真；含义是：是否有重复项；默认情况下没有重复的。
	
	//验证是否为空、如果为空则else分支
	if (aLink != null && aLink != ""){
		aLink=aLink.replace(/(^\s*)|(\s*$)/g, "");
		var adiv=document.getElementById("acceptdiv");
		var adiva=adiv.getElementsByTagName("a");
		//如果所输入的地址不是以：http://开头的则如下操作
		if(aLink.indexOf("http://") == -1){
				aLink = "http://" + aLink;
		}
		//验证添加重复项的链接地址、过滤
		for(i=0;i<adiva.length;i++) {
			//如果含有http://则 不等于-1，则条件成立
			if(adiva[i].href.indexOf("http://") != -1){
				//截取这个字符串的最后一次以：http://开始的到、整个字符串的总长度
				//adiva[i].href.lastIndexOf("http://")//最后一次出现的以：http://开头
				subContent = adiva[i].href.substring(adiva[i].href.lastIndexOf("http://"),adiva[i].href.length);
				//如果原始的链接地址等于当前自定义添加的标签内容地址相同则
				if(subContent == aLink){					
					//整个for循环终止。
					flag = "1";
					break;
				}
			}
		}
		//如果等于1 说明有重复项、选项.
		if(flag == "1"){
			//如果有重复项则如如下提示、如果if不成立则再次提示是否 还添加
			if (!window.confirm("您确定要添加重复的链接么？")) {
				return;
			}
		}
		//追加为符合访问标准的地址
		//aLink = "/visit.do?wapurl=" + aLink;//该代码修改为下行代码
		aLink = visitAction() + "?wapurl=" + aLink;
		//标题的验证如果为空则赋予默认值。
		if(aTitle == null || aTitle == "")
		{
			aTitle = "自定义书签";
		}
		resultA="<a href='"+ aLink +"' class='link1_5'>" + aTitle + "</a><br>";
		adiv.innerHTML= resultA + adiv.innerHTML;
		adiva=adiv.getElementsByTagName("a");
		for(i=0;i<adiva.length;i++) {
				adiva[i].style.cssText='';
				adiva[i].oncontextmenu = new Function("menushow(this,'menu');return false");
		}
	}
	saveFavorite();
	hidePopupLoginDialog('creatsitename');
}

function additem(){

	/**如果这里的targetobj 对象等于 null 说明 收藏的不是链接 而是 当前页浏览地址与标题*/
	if(targetobj == null){
		targetobj = document.getElementById("hidea");//保存了 当前所浏览的页面标题、链接
	}
	/***************************/
	var adiv=parent.document.getElementById("acceptdiv");
	adiva=adiv.getElementsByTagName("a");
	var url = targetobj.href;
	if(url == null || url.indexOf("wapurl=null") > -1 || url.indexOf("wapurl/null") > -1){
		alert("很抱歉，本页为动态生成页面，不能完成添加！");
		hidePopupLoginDialog('recomend');
		return;
	}
	
	var url = targetobj.href;
	//针对可能的重定向的URL,恢复URL原有的格式
	if(url != null && url.indexOf("visits.") > -1){
		url = urlrewrite(url);
	}
	//因为添加后的“项”全部匹配追加了visit.do、所以如果在判断是否有重复时固先匹配上visit.do在做判断。
	for(i=0;i<adiva.length;i++) {
		if (adiva[i].href==url){
			if (!window.confirm("您确定要添加重复的链接么？")) return;
			break;
		}
	}
	aa="<a href='"+ url +"' class='link1_5'>"+targetobj.innerHTML+"</a><br>";
	adiv.innerHTML= aa + adiv.innerHTML;
	adiva=adiv.getElementsByTagName("a");
	for(i=0;i<adiva.length;i++) {
			adiva[i].style.cssText='';
			adiva[i].oncontextmenu = new Function("menushow(this,'menu');return false");
	}
	parent.saveFavorite();
	/**校验是否登录、如果是非登录状态则弹出一个登录or注册层提供用户登录or注册---该函数来自 /js/util.js中*/
	parent.setTimeout("parent.isLogin();",1000);
}
/***新增*end*/


   function newwindow(v){
  		window.open(targetobj.href);
   }
   
   function helpme(){
   	window.open("/help/help.jsp");
   }
   
   function recommend(){
	  //show('recomend');
	  //return false;
		
	  //该方法实现了 窗口与div 之间的隔离
	  parent.ScreenConvert();
	  /**如果这里的targetobj 对象等于 null 说明 收藏的不是链接 而是 当前页浏览地址与标题*/
	  if(targetobj == null){
		  targetobj = document.getElementById("hidea");//保存了 当前所浏览的页面标题、链接
	  }
	  var url = targetobj.href;
	  if(url == null || url.indexOf("wapurl=null") > -1 || url.indexOf("wapurl/null") > -1){
			alert("很抱歉，本页为动态生成页面，不能完成推荐！");
			//hidePopupLoginDialog('recomend');
			return;
		}
		//取出URL有两种方法，采用不同的编码方式
		if(url.indexOf("%") > -1){//地址中含有%，是点击链接用右键取出的URL地址，已经经过浏览器转码
			url = decodeURIComponent(wapurl(url));
		}else{
			url = wapurl(url);
		}
			//url = encodeURIComponent(wapurl(url));
		//openurl="/about/recommend_submit.jsp?link_url=" + url
			//+ "&link_title=" + escape(targetobj.innerHTML);
		//window.open(openurl,"tuijian","toolbar=no,location=no,directories=no,status=no,menub ar=no,scrollbar=no,resizable=no,copyhistory=yes,width=400,height=200,left=300,top=200");
		parent.show('recomend',targetobj.innerHTML,url);
   }

   //将静态化的地址(wapurl/3g.cn.htm)重写为,(wapurl=http://3g.cn)便于与原始的规则相适应
   function urlrewrite(str){
		var i = str.indexOf("visits");
		var u;
		if(i > 0){
			//17是字符串visits.do?wapurl=的长度
			u = str.substring((i+17),str.length);
			/**
				改代码修改以下第二行代码
			*/
			//str = str.substring(0,i) + "visit.do?wapurl=" + u;
			str = str.substring(0,i - 1) + visitAction() + "?wapurl=" + u;
		}
		return str;
   }
   
   //将WEB的URL地址(wapurl/3g.cn.htm)中wap站点的URL地址提炼出来(http://3g.cn),用于网页推荐
   function wapurl(str){
		var i;
		if((i=str.indexOf("visits")) > 0){//(wapurl/3g.cn.htm)
				//17是字符串visits.do?wapurl=的长度
				if(str.indexOf("http://") > -1)
				{
					str = str.substring((i+17),str.length);
				}
		}
		return str;
  }
 document.oncontextmenu=function(e){return false;}//屏蔽自身菜单

window.defaultStatus=" 【提示】可以用鼠标将链接『拖拽』到左边的手机框中进行『收藏』!";






