/**
 * @author shom
 */

function logout(){
	MP.doLogout(function(){
		location.reload();
	});
}


/*
 * アイディア
 * c=<cid>,q=<#search query>があったら、フィード選択しない
 * b=icon|bookmarklet|blogparts|embedがあったら、生成先自動選択
 * -アイコン
 * 　　RSS登録用アイコン/単純なアイコンorプルダウン
 * -ブックマークレット
 *   押したら小さい窓でエントリー集
 * -blog parts
 *   上のやつをiframeにつっこむ
 * -HTML貼り付け
 *   HTMLコード
 */

var URI;
function onlogin(){

/*
	tinyMCE.init({
		mode : "exact",
		elements:"embededit",
		plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
		theme : "simple"
	});
*/

	// logincheck.jsから呼び出し
	// これとは別に <a href="#cmd..." class="onlogin"></a>も実行されている

	G.setAutoInputSelectAll();
	
	URI=G.parseUri(location.href);
	
	MP.pseudoClick([
		"#create-wizard@iconwiz",
		"#create-wizard@bookmarkletwiz",
		"#create-wizard@blogpartswiz",
		"#create-wizard@embedwiz",
		"#exec-initwizards"
	].join("&&"));

	if (G.isMSIE) {
		G.each(G.$S("input",G.$("dispoptform")),function(e){
			G.addEvent(e,"click",dispopthandler);
		});
	}else{
		G.addEvent(G.$("dispoptform"),"change",dispopthandler);
	}

	G.each([document,window],function(target){
		G.addEvent(target,"resize",function(){
			if(MP._resizeTimer){return;}
			MP._resizeTimer=setTimeout(function(){
				fitelements();
				delete MP._resizeTimer;
			},200);
		});
	});
	
	fitelements.later(1000);
	
	setInterval(fitelements,3000);

}

function fitelements(){
	var cs=G.clientSize();
	var fp=G.pos(G.$("foot-area"));
	var mlp=G.pos(G.$("main-left-area"));
	var mh=cs.height-fp.height-mlp.top; // main-area,right-area,left-area
	var margins=7*2+5*2;
	G.each(["main","left","right"],function(v){
		G.$(v+"-area").style.height=mh+"px";
		var deco=MP.decorated[v];
		if(deco){
			var did=deco.id;
			G.$(did+"-cont").style.height=mh-margins+"px";
			if(v!="main"){
				G.$(did+"-cont").style.overflow="auto";
			}
		}
	});

	G.each(MP.tabs.mode.contents,function(e){
		try{
			e.style.height=Math.max(50,mh-margins-e.offsetTop)+"px";
		}catch(ex){CONSOLE.error("fitelements:"+e.id+"\n"+G.dump(ex)+"\n"+margins);}
	});

}

function initwizards(){
}

function genmyfeeds(){
	if(!MP.tmpl.myfeeds){MP.tmpl.myfeeds=new G.HtmlElementTemplater(G.$("myfeeds"));}
	if(MP.tmpl.myfeeds.busy){
		genmyfeeds.poll(function(){return !MP.tmpl.myfeeds.busy;})
		return;
	}
	MP.tmpl.myfeeds.start();
	//order_key:date(pubdate),title,count
	//channel_group:0:通常,1:prof,2:archive,3:draft
	MP.myfeeds={
		all:{},
		archives:{},
		draft:null,
		auth:{"public":{},"private":{}},
		rsstype:{normal:{},podcast:{}},
		source:{raw:{},rss:{},html:{},mail:{}}
	};

	function getmyfeedsmore(offset,limit){
		MP.doModiphi("channel/getMyChannelList",{offset:offset,limit:limit,order_key:"title"},function(r){
			G.each(r.RESULTS,function(v){
				var className=[];
				G.each(["regist","pub","update","newest_item_pub"],function(p){
					if(v[p+"_date"]){
						try{
							v[p+"_date_local"]=MP.getUserTZDateStr(v[p+"_date"]);
						}catch(ex){
							CONSOLE.error("getmyfeedsmore:getUserTZDateStr:"+p+":"+v[p+"_date"]+G.dump(ex));
							v[p+"_date_local"]=v[p+"_date"];
						}
					}
				});
				if(v.channel_group==2){ // archive
					v._className="archive";
					MP.myfeeds.archives[v.channel_id]=v;
				}else if(v.channel_group==3){ // draft;
					v._className="draft";
					MP.myfeeds.draft=v;
				}else{
					MP.myfeeds.all[v.channel_id]=v;
					if(v.private_flg!=0){
						MP.myfeeds.auth["private"][v.channel_id]=v;
						className.push("auth_private");
					}else{
						MP.myfeeds.auth["public"][v.channel_id]=v;
						className.push("auth_public");
					}
					if(v.channel_type==1){
						MP.myfeeds.rsstype.podcast[v.channel_id]=v;
						className.push("rss_podcast");
					}else{
						MP.myfeeds.rsstype.normal[v.channel_id]=v;
						className.push("rss_normal");
					}
					if(v.n2_flg==1){
						MP.myfeeds.source.rss[v.channel_id]=v;
						className.push("source_html");
					}else if(v.n3_flg==1){
						MP.myfeeds.source.html[v.channel_id]=v;
						className.push("source_rss");
					}else{
						MP.myfeeds.source.raw[v.channel_id]=v;
						className.push("source_raw");
					}
					v._className=className.join(" ");
					if(v._className.match("auth_public")){
						MP.tmpl.myfeeds.apply(v);
					}
				}
			});
			if(r.RESULTS.length!=0){
				getmyfeedsmore.later(50,this,offset+limit,limit);
			}else{
				MP.tmpl.myfeeds.complete();
			}
		},function (err,mode){
			CONSOLE.error("getmyfeeds:"+mode+":"+G.dump(err));
			var msg;
			switch(mode){
				case "resulterr":
					msg="サーバーエラー"; break;
				case "resultng":
					msg="入力が間違っています"; break;
				case "connectionerror":
					msg="サーバー接続エラー"; break;
				default:
					msg="エラー"; break;
			}
			MP.tmpl.myfeeds.abort(function(tmpl){
				var t=G.$S(".for-template-abort",tmpl.e)[0];
				if(t){
					t.innerHTML=msg+"\n"+G.dump(err);
				}else{
					alert(msg+"\n"+G.dump(err));
				}
			});
		},false);
	}
	
	getmyfeedsmore(0,10);
	
}

function genplaylists(){
	if(!MP.tmpl.playlists){
		MP.tmpl.playlists=new G.HtmlElementTemplater(G.$("playlist"));
	}
	MP.tmpl.playlists.start();
	G.each(MP.login.memo.mylists,function(v,i){
		if(v.id===undefined){v.id=i;}
		MP.tmpl.playlists.apply(v);
	});
	MP.tmpl.playlists.complete();
}

function gensubscribed(){
	if(!MP.tmpl.mysubscribed){
		MP.tmpl.mysubscribed=new G.HtmlElementTemplater(G.$("subscribed"));
	}
	MP.tmpl.mysubscribed.start();
	MP.getMySubscribedChannels(0,10,function(){
		var list=G.sortObj(MP.mysubscribedchannels,"title");
		G.each(list,function(v){
			MP.tmpl.mysubscribed.apply(v);
		})
		MP.tmpl.mysubscribed.complete();
	});
}

function setfeededit(mode){
	var uri=G.parseUri(location.href);
	uri.path=uri.path.replace(/\/[^\/]+$/,"/edit_feed_"+mode+".html");
	uri.params._callback="feedselected";
	uri.params._dummy=(new Date()).valueOf().toString(36);
	var ifr=MP["_ifr_"+mode];
	if(!ifr){
		ifr=new G.IFrame(G.$("ifr-from"+mode));
		MP["_ifr_"+mode]=ifr;
	}else{
		clearInterval(ifr._s);
	}
	ifr.load(uri.toString(),function(win,doc){
		this._s=this.resizeToContent(true);
	});
}

function moveelement(srcid,dstid){
	G.$(dstid).appendChild(G.$(srcid));
}

function feedselected(cid,o){
	switch(cid){
		case "myfeeds": case "subscribed": case "playlist":
			var v=G.getElementValues(G.$(cid));	
			cid=v.channel_id;
			if(v._channel_id_single){cid=v._channel_id_single};
			break;
	}
	//alert(cid);
	if(cid){
		if(cid instanceof Array){cid=cid.join(",");}
		cid=cid.replace(/,?\${channel_id}$/,"");
		cid=cid.split(",");
		var wizid=G.$SA(G.$("selectfeeds"),".wizard").id;
		var wiz=MP.wizards[wizid];
		if(wizid=="iconwiz"&&cid.length>1){
			alert("フィードが複数選択されています");
			return;
		}else{
			if(cid.length>200){
				alert("選択できるフィードは200個までです");
				return;
			}
		}
		wiz._v={channel_id:cid};
		wiz.next();
	}else{
		alert("フィードを選択してください");
	}
}

function seticonresult(){
	var v=MP.wizards.iconwiz._v;
	//var uri=G.parseUri(MP.API);
	MP.getFeedsInfo(v.channel_id,function(r){
		if(!r[0]){
			alert("フィードが見つかりませんでした");
			return;
		}
		var img=MP.wizards.iconwiz._img.img;
		if(MP.wizards.iconwiz._img.img=="url"){
			img=MP.wizards.iconwiz._img.imgurl;
		}
		var rss1="<a href=\""+G.esc(r[0].channel_url)+"#subscribe\"><img src=\""+G.esc(img,"A")+"\" style=\"border:none;\"/></a>"
		var src="<script type=\"text/javascript\" charset=\"utf-8\" src=\""+G._BASEPATH+"parts.js\"></script>"+rss1;
		G.setElementValues(G.$("iconresults"),{rss1:rss1,src:src})
	});
}

// dispopt onchange
function dispopthandler(ev){
	ev=G.parseEvent(ev);
	if(ev.target&&ev.target.tagName&&ev.target.tagName.toLowerCase()=="input"){
		if(G.isClass(ev.target,"preventupdatepreview")){return;};
		if(ev.type=="click"&&ev.target.type=="text"){return;}
		if(ev.target.name=="_type"){
			switch(ev.target.value){
				case "entries.html":
					G.removeClassName(G.$SA(ev.target,".openable"),"opened");
					G.addClassName(G.$SA(ev.target,".openable"),"closed");
					break;
				case "entries-p.html":
					G.removeClassName(G.$SA(ev.target,".openable"),"closed");
					G.addClassName(G.$SA(ev.target,".openable"),"opened");
					MP.pseudoClick(G.$("entries-p-scrollintoview"));
					break;
			}
		}
		var wizdiv=G.$SA(ev.target,".wizard");
		RES[wizdiv.id.replace(/wiz$/,"")](true);
	}
}

// #dispopt init 
var orgtitles=null;

var PVINI={
	"bookmarklet":function(){
		G.$("dispoptform").reset();
		var v=MP.wizards.bookmarkletwiz._v;
		if(v.channel_id.push){
			v.channel_id=v.channel_id.join(",");
		}
		var cids=v.channel_id.split(",");
		orgtitles=null;
		MP.getFeedsInfo(cids,function(r){
			var channels=[],titles=[];
			G.each(r,function(v){
				channels.push(v.channel_id);
				titles.push(v.title);
			});
			titles=shorten_l(titles.join(","),100);
			orgtitles=titles;
			G.setElementValues(G.$("dispoptform"),{
				t:titles,
				_ifr_w:"",
				_ifr_h:"",
				fs:"",
				_type:"entries.html",	// or "entries-p.html"
										// for _type="entries-p.html"
					as:"",
					lm:10,				// limit of entry in 1page
					_t_lim:"true",			// show count     : top
					_t_reg:"false",			// show region    : top
					_t_pg:"true",			// show paging    : top
					_t_np:"false",			// show next/prev : top
					_b_np:"true",			// show next/prev : bottom
					_b_pg:"false",			// show paging    : bottom
					_b_reg:"false",			// show region    : bottom
					_b_lim:"false",			// show count     : bottom
				ds:"1l",				// or "" : entries wrap
				_feedname:"true",		// entries display element	
				_pubdate:"true",		// entries display element
				ed:"ov",				// or "2p","ot","pl" : entry display (overlay,2pane,outerwindow,permalink)
										// for ed="ov"
					_esov:80,			// entry size %
					_etov:"true",		// show entry title
					_sfov:"false",		// show first entry automatically
										// for ed="2p"
					_es2p:50,			// entry size %
					_et2p:"true",		// show entry title
										// for ed="il"
					_etil:"true",		// show entry title
					_sfil:"false"		// show first entry automatically
			});
		});
	},
	"blogparts":function(){
		G.$("dispoptform").reset();
		var v=MP.wizards.blogpartswiz._v;
		if(v.channel_id.push){
			v.channel_id=v.channel_id.join(",");
		}
		var cids=v.channel_id.split(",");
		orgtitles=null;
		MP.getFeedsInfo(cids,function(r){
			var channels=[],titles=[];
			G.each(r,function(v){
				channels.push(v.channel_id);
				titles.push(v.title);
			});
			titles=shorten_l(titles.join(","),100);
			orgtitles=titles;
			G.setElementValues(G.$("dispoptform"),{
				t:titles,
				_ifr_w:200,
				_ifr_h:200,
				fs:"",
				_type:"entries.html",	// or "entries-p.html"
										// for _type="entries-p.html"
					as:"",
					lm:10,				// limit of entry in 1page
					_t_lim:"false",			// show count     : top
					_t_reg:"false",			// show region    : top
					_t_pg:"false",			// show paging    : top
					_t_np:"false",			// show next/prev : top
					_b_np:"false",			// show next/prev : bottom
					_b_pg:"false",			// show paging    : bottom
					_b_reg:"false",			// show region    : bottom
					_b_lim:"false",			// show count     : bottom
				ds:"1l",				// or "" : entries wrap
				_feedname:"false",		// entries display element	
				_pubdate:"false",		// entries display element
				ed:"ov",				// or "2p","il","ot","pl" : entry display (overlay,2pane,inline,outerwindow,permalink)
										// for ed="ov"
					_esov:90,			// entry size %
					_etov:"true",		// show entry title
					_sfov:"false",		// show first entry automatically
										// for ed="2p"
					_es2p:50,			// entry size %
					_et2p:"true",		// show entry title
										// for ed="il"
					_sfil:"false"		// show first entry automatically
			});
		});
	},"embed":function(){
	}
};

// gen preview and result
var RES={
	"bookmarklet":function(preview){
		var v=MP.wizards.bookmarkletwiz._v;
		if(v.channel_id.push){
			v.channel_id=v.channel_id.join(",");
		}
		//if(v.t!="<初期化中...>"||v.t==orgtitles.bookmarklet){delete v.t;}
		setdispopt(v.channel_id.split(","),function(uri,v){
			if (uri.params.t) {
				uri.params.t = uri.params.t.replace(/^\s+/, "").replace(/\s+$/, "");
				if(uri.params.t==""){
					uri.params.t="MODIPHIT - Gadget";
				}
			}
			uri.params.uc=MP.login.contract_type;
			var link=G.tmpl("javascript:(function(){window.open(\"${uri}\",null,\"width=500,height=200,resizable=yes,menubar=no,toolbar=no,location=no,status=no\")})()",
				{uri:G.esc(uri.toString(),"J")});
			G.setElementValues(G.$("bookmarkletresults"),{
				link:{"@href":link,_value:G.esc(uri.params.t||orgtitles)},
				rawlink:{"@target":"_blank","@href":uri.toString(),_value:G.esc(uri.params.t||orgtitles)}
			});
		},preview);
		return;
	},
	"blogparts":function(preview){
		var v=MP.wizards.blogpartswiz._v;
		if(v.channel_id.push){
			v.channel_id=v.channel_id.join(",");
		}
		//if(v.t!="<初期化中...>"||v.t==orgtitles.blogparts){delete v.t;}
		var vf=G.getElementValues(G.$("blogpartsdisp"));
		var w=G.atoi(vf._ifr_w,200);
		var h=G.atoi(vf._ifr_h,200);
		MP._blogpartspv_style=G.setStyleSheet(G.tmpl("#blogpartswiz #setoptpv iframe { width:${w}px; height:${h}px }",{w:w,h:h}),MP._blogpartspv_style);
		setdispopt(v.channel_id.split(","),function(uri,v){
			uri.params.uc=MP.login.contract_type;
			var code=G.tmpl("<iframe src=\"${uri}\" width=\"${w}\" height=\"${h}\" frameborder=\"0\"></iframe>",{uri:uri.toString(),w:w,h:h});
			var code2=G.tmpl("<iframe src=\"${uri}\" width=\"${w}\" height=\"${h}\" name=\""+new Date().valueOf().toString(36)+"\" frameborder=\"0\"></iframe>",{uri:uri.toString(),w:w,h:h}); // for safari iframe cache clear
			G.setElementValues(G.$("blogpartsresults"),{
				code:code2,
				src:code
			});
		},preview);
		return;
	},
	"embed":function(preview){
		var v=MP.wizards.embedwiz._v;
		if(v.channel_id.push){
			v.channel_id=v.channel_id.join(",");
		}
		var vf=G.getElementValues(G.$("embedpvform"));
		vf.channel_id=v.channel_id;
		v=vf;

		if(v.entries_lim<1||v.entries_lim>50){
			alert("エントリー表示件数は1～50件です");
		}

		var tmp=(new Date()).valueOf().toString(36);
		var entriestmplid="mpentries_"+tmp;
		var entrytmplid="mpentry_"+tmp;

		var entries=v.entries_src.replace(/__ENTRIESID__/g,entriestmplid).replace(/__ENTRYID__/g,entrytmplid).replace(/\s+$/,"").replace(/^\s+/,"")||"[エントリー一覧に表示項目を追加してください]";
		var entry=v.entry_src.replace(/__ENTRIESID__/g,entriestmplid).replace(/__ENTRYID__/g,entrytmplid).replace(/\s+$/,"").replace(/^\s+/,"")||"[エントリー内容に表示項目を追加してください]";
		
		var head=G.tmpl(
			'<script type="text/javascript" charset="utf-8" src="${parts}"></script>\r\n'+
			'<style type="text/css" >.template,.pageinit {display:none;}</style>\r\n'+
			'<a href="#search-${entriesquery}-0,${limit}@${entriestmplid}" class="pageinit">for modiphi parts</a>\r\n',
			{
				parts: G._BASEPATH + "parts.js",
				entriestmplid: entriestmplid,
				entrytmplid: entrytmplid,
				entriesquery: "feed-" + v.channel_id,
				limit: Math.min(50,Math.max(1,G.atoi(v.entries_lim,5)))
			}
		);

		var entries_box=({
			"div":["<div id='${entriestmplid}' class='mp-entries'>","</div>"],
			"span":["<span id='${entriestmplid}' class='mp-entries'>","</span>"],
			"tr":["<table><tbody id='${entriestmplid}' class='mp-entries'>","</tbody></table>"],
			"li":["<ul id='${entriestmplid}' class='mp-entries'>","</ul>"]
		})[v.entries_block];
		entries_box[0]=G.tmpl(entries_box[0],{entriestmplid:entriestmplid});
		var td=(v.entries_block=="tr"?["<td>","</td>"]:["",""]);
		var td2=(v.entries_block=="tr"?["<td style='display:none;'>","</td>"]:["",""]);
		var code_entries=[
			entries_box[0],
				"<"+v.entries_block+" class='template mp-entries-all ${_oddeven} ${_className}'>",
					td2[0]+"<a __href='#entry-${item_id}@"+entrytmplid+"' style='display:none;' class='mp-entries-toentry'></a>"+td2[1],
					entries,
				"</"+v.entries_block+">",
				"<"+v.entries_block+" class='for-template-processing mp-entries-all'>",
					td[0]+v.entries_loading+td[1],
				"</"+v.entries_block+">",
				"<"+v.entries_block+" class='for-template-notfound mp-entries-all'>",
					td[0]+v.entries_notfound+td[1],
				"</"+v.entries_block+">",
				"<"+v.entries_block+" class='for-template-abort mp-entries-all'>",
					td[0]+v.entries_abort+td[1],
				"</"+v.entries_block+">",
				"<"+v.entries_block+" style='display:none'>",
					td[0]+(v.entries_autoclick=="1"?"<a href='#click@.mp-entries/.mp-entries-toentry[0]' class='templatecomplete'></a>":"")+td[1],
				"</"+v.entries_block+">\n",
			entries_box[1]
		].join("\r\n")+"\r\n";

		var entry_box=({
			"div":["<div id='${entrytmplid}'>","</div>"],
			"span":["<span id='${entrytmplid}'>","</span>"]
		})[v.entry_block];
		entry_box[0]=G.tmpl(entry_box[0],{entrytmplid:entrytmplid});

		var code_entry=[
			entry_box[0],
				"<"+v.entry_block+" class='for-template-processing mp-entry-all'>",
					v.entry_loading,
				"</"+v.entry_block+">",
				"<"+v.entry_block+" class='for-template-notfound mp-entry-all'>",
					v.entry_notfound,
				"</"+v.entry_block+">",
				"<"+v.entry_block+" class='for-template-abort mp-entry-all'>",
					v.entry_abort,
				"</"+v.entry_block+">",
				"<"+v.entry_block+" class='template mp-entry-all'>",
					entry,
				"</"+v.entry_block+">",
			entry_box[1]
		].join("\r\n")+"\r\n";

		var code=[
			"<!-- MODIPHI Widgets created : head -->",
			head,
			"<!-- ****************************** -->\r\n\r\n",
			"<!-- MODIPHI Widgets created : entries -->",
			code_entries,
			"<!-- ****************************** -->\r\n\r\n",
			"<!-- MODIPHI Widgets created : entry -->",
			code_entry,
			"<!-- ****************************** -->\r\n\r\n"
		].join("\r\n");
		G.setElementValues(G.$("embedresults"),{
			code:code
		});
		if(preview){
			G.setElementValues(G.$("embedpv"),{
				//code:code,
				head:head,
				code_entries:code_entries,
				code_entry:code_entry
			});
			MP.processPageInit(G.$("embedpv"));
			EPW&&EPW.location.reload();
		}else{
			MP.processPageInit(G.$("embedres"));
		}
	}
};

EPW=null;
function embedpvwindow(){
	RES.embed(true);
	var win=window.open("widgets-preview.html?bodyf=pvbody","_mpembedpv");
}

var pdoc=document;
function pvbody(){
	//doc.body.innerHTML=G.$("embedpvcont").innerHTML;
	return G.$("embedrescode").value;
}

function setdispopt(cids,f,preview){
	MP.getFeedsInfo(cids,function(r){
/*
		var channels=[],titles=[];
		G.each(r,function(v){
			channels.push(v.channel_id);
			titles.push(v.title);
		});
*/
		var channels=[];
		G.each(r,function(v){
			channels.push(v.channel_id);
		});
		
		var uri=G.parseUri(location.href);
		uri.hash=""; uri.params={};
		uri.params.c=channels.join(",");
		
		var v=G.getElementValues(G.$("dispoptform"));
		
		if(v._type=="entries-p.html"){
			uri.path=uri.path.replace(/\/[^\/]*$/,"/gadget/entries-p.html");
			uri.params.nv=
				((v._t_lim?"tl,":"")+
				(v._t_reg?"tr,":"")+
				(v._t_pg?"tp,":"")+
				(v._t_np?"tnp,":"")+
				(v._b_np?"bnp,":"")+
				(v._b_pg?"bp,":"")+
				(v._b_reg?"br,":"")+
				(v._b_lim?"bl,":"")).replace(/,$/,"");
		}else{
			delete v.lm;
			uri.path=uri.path.replace(/\/[^\/]*$/,"/gadget/entries.html");
		}
		
		switch(v.ed){
			case "ov":
				v.es=v._esov;
				v.sf=(v._sfov=="true"?"1":"");
				break;
			case "2p":
				v.es=v._es2p;
				break;
			case "il":
				v.sf=(v._sfil=="true"?"1":"");
				break;
		}
		
		
		var dn=[]; // display:none;
		if(!v._feedname){
			dn.push("fd");
		}
		if(!v._pubdate){
			dn.push("pd");
		}
		if(v.ed=="ov"&&v._etov!="true"){
			dn.push("et");
		}
		if(v.ed=="2p"&&v._et2p!="true"){
			dn.push("et");
		}
		if(v.ed=="il"&&v._etil!="true"){
			dn.push("et");
		}
		if(dn.length>0){
			v.dn=dn.join(",");
		}
		
		if(v.t=="<初期化中...>"||v.t==orgtitles){delete v.t;}
		
		G.each(v,function(v,p){
			if(p.indexOf("_")!=0){
				uri.params[p]=v;
			}
		})
		
		if(preview){
			G.$("dispoptpvifr").src=uri.toString();
		}
		
		f&&f(uri,v);
		
	});
}

function shorten_l(str,len){
	str=str.replace(/^\s+/,"").replace(/\s+/g," ");
	if(str.length+3>len){
		return str.substring(0,len)+"...";
	}
	return str;
}
