//dts common

/**
 * javascript document
 * @author nathena
 */
(function(_ns)
{
	var _ = A;
	function openPopup(url)
	{
		if( url )
		{
			var popup = new _.Popup();
			popup.build();
			popup.setContent(_.getSource(url));
			popup.render();
			
			return popup;
		}
	}
	_ns.openPopup = openPopup;
	
	function mapPopup(url)
	{
		if( url )
		{
			var popup = openPopup(url);

			if( popup )
			{
				mapPopup.close = function()
				{
					popup.remove();
				}
				return false;
			}
		}
		return true;
	}
	_ns.mapPopup =  mapPopup;
	
	function authorPopup(author)
	{
		var url = author.href;
		
		if( url )
		{
			var popup = openPopup(url);
			
			if( popup )
			{
				authorPopup.close = function()
				{
					popup.remove();
				}
				
				return false;
			}
		}
			
		return true;
	}
	_ns.authorPopup = authorPopup;
	
	function getCity($country,container,el,defaultValue)
	{
		defaultValue = defaultValue?defaultValue:"";
		_(container).load("/api/areajs.php?op=city&area="+$country+"&el="+el+"&v="+defaultValue);
	}
	_ns.getCity = getCity;
	
	function inputbinding(inputfile,display)
	{
		InputFileImagePreview.apply(inputfile, [display]);
		
		function InputFileImagePreview(_preview)
		{
			var preview = document.getElementById(_preview);
			this.onchange = function()
			{        
				if( window.ActiveXObject )
				{
					preview.src = this.value+"?"+new Date().getTime()//not cache;
					preview.style.display="";
				}
				else if( this.files )
				{
					preview.src = this.files[0].getAsDataURL();
					preview.style.display="";
				}
				else
				{
					alert("Not Support");
					preview.style.display="none";
				}        
			}
			
		}
	}
	_ns.inputbinding = inputbinding;
	
	//遮蔽层
	var Actiondiv={
	    pop:Object,
	    id:'Adiv_pop_'+Math.random(),
	    msg:function(msgs)
	    {
	    	var msg = '<table cellpadding="0" cellspacing="0" border="0" class="tab-border">'+
					  '<tr class="p-top"><td><span class="p-span-t">'+Lang['hint']+'</span></td></tr>'+
					  '<tr><td class="p-con-warp"><div class="p-con-warm">'+msgs+'</div></td></tr></table>';
	        return msg;
	    },
	    post:function(){
	        this.pop = new A.Popup();
	        this.pop.build();
	        this.pop.setContent(this.msg(Lang['chulishuju']));//正在处理数据...
	        this.pop.render();
	    },
	    show:function(message,url,timer){
	    	timer = timer || 1000;
	        message = message || Lang['chuliwanbi'];; //处理完毕
	        
	        if(url)
	        {
	        	this.pop.setHtml(this.msg(message+Lang['guanbiyimhou'])); //,1秒钟后自动关闭
	            setTimeout(function(){document.location.href = url;},timer);
	        }
	        else
	        {
	        	this.pop.setHtml(this.msg(message+Lang['guanbiyimhou']));
	        	setTimeout(function(){Actiondiv.pop.remove();},timer)
	        }
	    }
	}
	
	_ns.Actiondiv = Actiondiv;
	
	//弹出窗口交互
	function DtsPopup()
	{
		var popup;
		var $this = this;
		this.open = function(author)
		{
			try
			{
				var url = _.isString(author)?author:author.href;
				if( url )
				{
					popup = openPopup(url);
				}
			}catch(ex){}
			return false;
		}
		this.close = function()
		{
			if( popup )
			{
				popup.remove();
				popup = null;
			}
			return false;
		}
		this.load = function(author)
		{
			var url = _.isString(author)?author:author.href;
			if( url )
			{
				Actiondiv.post();
				popup.setHtml(_.getSource(url));
				Actiondiv.show(Lang['qqcg']);
			}
			return false;
		}
		
		this.set = function(content)
		{
			popup.setHtml(content);
		}
		
		this.get = function(url)
		{
			if( url )
			{
				popup.setHtml(_.getSource(url));
			}
		}
		this.doForm = function(form)
		{
			Actiondiv.post();
			_.doForm(form,function(data)
					{
						$this.set(data);
						Actiondiv.show(Lang['ccwb']);
					});
			return false;
		}
	}
	_ns.DtsPopup = DtsPopup;
	
	var AppUpload = new DtsPopup();
	
	function UploadPhoto(op,id,url)
	{
		AppUpload.open("/api/up.php?op="+op+"&id="+id);
		
		UploadPhoto.close = function()
		{
			AppUpload.close();
			location.reload(true);
			
			return false;
		}
		return false;
	}
	_ns.UploadPhoto = UploadPhoto;
	

	//获取selectText
	function getSelectText(select)
	{
		if( !select.value ) return ""; 
		return select.options[select.selectedIndex].text;
	}
	_ns.getSelectText = getSelectText;
	
	var AjaxData={
		    popup:'',
		    getUrl:function(obj,id){
		        var url=obj.href;
		        //Actiondiv.post();
		        var con=_.getSource(url);
		        //Actiondiv.show("储存数据完毕");
		        _(id).innerHTML=con;
		        return false;
		    },
		    pop:function(obj){
		        var url=obj.href;
		        this.popup = openPopup(url);
		        return false;
		    },
		    close:function(){
		        this.popup.remove();
		        this.popup = null;
		    }
		}
	_ns.AjaxData = AjaxData;
	
	//设为首页
	function addHomePage()
	{
		var obj=document.createElement("addhomepage");
	    var homepage=document.location.href;
	    try{
	        obj.style.behavior='url(#default#homepage)';obj.setHomePage(homepage);
	    }
	    catch(e)
	    {
	        if(window.netscape) 
	        {
	            try
	            {
	                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	            }
	            catch(e)
	            {
	                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'");
	            }
	            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
	            prefs.setCharPref('browser.startup.homepage',homepage);
	        }
	    }
	}
	_ns.addHomePage = addHomePage;
	
	//收藏本站
	function addBookMark()
	{
	    var title=document.title
	    var url=document.location.href
	    if (window.sidebar)
	    {
	        window.sidebar.addPanel(title, url,"");
	    }
	    else if( window.opera && window.print )
	    {
	        var mbm = document.createElement('a');
	        mbm.setAttribute('rel','sidebar');
	        mbm.setAttribute('href',url);
	        mbm.setAttribute('title',title);
	        mbm.click();
	    }
	    else if( document.all ) window.external.AddFavorite( url, title);
	}
	_ns.addBookMark = addBookMark;
	
	//加入桌面
	function shortcut(site,title)
	{
		var iframe = document.getElementById("shortcur_iframe");
		if(!iframe)
		{
			iframe = document.createElement('iframe');
			iframe.id = "shortcur_iframe";
			iframe.style.cssText="width:0px;height:0px;position:absolute;bottom:-10px;left:-10px;z-index:-1"
			document.body.appendChild(iframe);
		}
		iframe.src = "/api/shortcut.php?site="+encodeURIComponent(site)+"&title="+encodeURIComponent(title);
	}
	_ns.shortcut = shortcut;
	
	function setLang(lang)
	{
		if( H_DOMAIN )
		{
	    	$.cookie('imagetranslang',lang,{path:"/",domain:H_DOMAIN});
		}
		else
		{
			$.cookie('imagetranslang',lang,{path:"/"});
		}
		location.reload();
	}
	_ns.setLang = setLang;

	function getLang(key)
	{
		return Lang[key]? Lang[key] : key;
	}
	_ns.getLang = getLang;
	
})(window);

(function(_w)
{
	var share,_width = 0,_height = 0,_w = 0,_h = 0,_dl = 0,_dt = 0;
	
	NetShare = {};
	
	NetShare.render = function(_url, _title, _content)
	{
		var url = _url || document.location.href, title = _title || document.title, content = _content || title
		document.write(getHTML(url, title, content));
	}
	
	NetShare.close = function()
	{
		if( share )
		{
			share.hide();
		}
		return false;
	}
	NetShare.open = function(event,_url, _title, _content)
	{
		event = $.event.fix( event || window.event );
		event.stopPropagation();
		var obj = event.target;
		if( !share )
		{
			share = $('<div style="border:1px solid #ccc;position:absolute;z-index:10000;background:#fff"></div>').appendTo("body");
			share.mousedown(function(event){event.stopPropagation();});
			$(document).click(function(){NetShare.close()});
		}
		
		var position = $(obj).offset();
		
		var url = _url || document.location.href, title = _title || document.title, content = _content || title
		
		share.html(getHTML(url, title, content));
		$("#_closeNetShare_",share).show();
		_width = share.width();
		_height = share.height();
		
		_w = $(window).width();
		_h = $(window).height();
		
		_dt = $(window).scrollTop();
		_dl = $(window).scrollLeft();
		
		var _l = position.left;
		if( _l+_width > _w+_dl )
		{
			_l = _w+_dl - _width - 2;
		}
		var _t = position.top+$(obj).height();
		if( _t+_height > _h+_dt )
		{
			_t = _h+_dt - _height - $(obj).height()-6;
		}
		share.css("top",_t).css("left",_l).show();
		
		return false;
	}
	
	
	function getHTML(url,title,content)
	{
		var html = [];
		html.push('<table border="0" style="border-collapse:separate;border-spacing:6px;">');
		html.push('<tr style="font-size:12px;color:#000;">');
		html.push('<td>分享到</td>');
		html.push('<td><a href="http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+encodeURIComponent(url)+'" target="_blank" title="分享到QQ空间"><img border="0" align="absMiddle" alt="分享到QQ空间" src="http://qzonestyle.gtimg.cn/ac/qzone_v5/app/app_share/qz_logo.png" width="16" height="16"></a></td>');
		html.push('<td><a href="http://v.t.qq.com/share/share.php?title='+encodeURIComponent(title)+'&url='+encodeURIComponent(url)+'" target="_blank" title="分享到腾讯微博"><img border="0" align="absMiddle" alt="分享到腾讯微博" src="http://file.mafengwo.net/images/login/ico_qq.png" width="16" height="16"></a></td>');
		html.push('<td><a href="http://share.renren.com/share/buttonshare.do?title='+encodeURIComponent(title)+'&link='+encodeURIComponent(url)+'&content='+encodeURIComponent(content)+'" target="_blank" title="分享到人人网"><img border="0" align="absMiddle" src="http://s.xnimg.cn/favicon-rr.ico" alt="分享到人人网" width="16" height="16"></a></td>');
		html.push("<td><a href=\"javascript:void((function(s,d,e,r,l,p,t,z,c){var%20f='http://v.t.sina.com.cn/share/share.php?appkey=639365318',u=z||d.location,p=['&amp;url=',e(u),'&amp;title=',e(t||d.title),'&amp;source=',e(r),'&amp;sourceUrl=',e(l),'&amp;content=',c||'gb2312','&amp;pic=',e(p||'')].join('');function%20a(){if(!window.open([f,p].join(''),'mb',['toolbar=0,status=0,resizable=1,width=440,height=430,left=',(s.width-440)/2,',top=',(s.height-430)/2].join('')))u.href=[f,p].join('');};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else%20a();})(screen,document,encodeURIComponent,'','','','"+title+"','"+url+"','utf-8'));\" title=\"分享到新浪微博\"><img border=\"0\" align=\"absMiddle\" src=\"http://file.mafengwo.net/images/login/ico_sina.gif\" alt=\"分享到新浪微博\"  width=\"16\" height=\"16\"></a></td>");
		html.push('<td><a href="http://www.kaixin001.com/repaste/share.php?rtitle='+encodeURIComponent(title)+'&rurl='+encodeURIComponent(url)+'&rcontent='+encodeURIComponent(content)+'" target="_blank" title="转帖到开心网"><img border="0" align="absMiddle" src="http://img1.kaixin001.com.cn/i/favicon.ico" alt="转帖到开心网"  width="16" height="16"></a></td>');
		html.push('<td><a href="http://www.douban.com/recommend/?title='+encodeURIComponent(title)+'&url='+encodeURIComponent(url)+'&rcontent='+encodeURIComponent(content)+'" target="_blank" title="分享到豆瓣"><img border="0" align="absMiddle" src="http://www.douban.com/favicon.ico" alt="分享到豆瓣" width="16" height="16"></a></td>');
		html.push('</tr>');
		html.push('</table>');
		return html.join("");
	}
})(window)


