MP.setOnLoadHtml(function(){
	var uri=G.parseUri(location.href);
	G.requireCss("./skin/"+(uri.params.sk||"default")+"/skin.css");
	if(uri.params.css){
		G.requireCss(uri.params.css);
	}
	MP.BASETARGET="_self";
	if(uri.params.bt){
		/*
		G.each(G.$S("a"),function(e){
			if(!e.target){e.target=uri.params.bt;}
		});
		*/
		MP.BASETARGET=uri.params.bt;
	}
	if(uri.params.rp){
		MP.READERP=uri.params.rp;
		G.addEvent(document,"click",function(ev){
			ev=G.parseEvent(ev);
			if(!ev.click.l){return;}
			if(ev.target.tagName.toLowerCase()=="a"){
				var uri=G.parseUri(ev.target.href);
				var me=G.parseUri(location.href);
				if(me.path.replace(/index\.html$/,"reader.html")==uri.path){
					uri.path=uri.path+"?"+MP.READERP+"&_DUMMY_";
					window.open(uri.toString().replace("&_DUMMY_?","&").replace("&_DUMMY_",""),ev.target.target||MP.BASETARGET);
					return G.stopDefault(ev,true);
				}else if(uri.path!=me.path){
					window.open(uri.toString(),ev.target.target||MP.BASETARGET);
					return G.stopDefault(ev,true);
				}
			}
		})
	}

	logincheck(function onerror(type){
		G.removeClassName(document.body,"logined");
		G.addClassName(document.body,"notlogined");
		switch(type){
			case "notlogin":
			case "ng":
			case "error":
		}
		onnotlogin();
	});
	delete MP.linkClickHandlers["search"];
});

function onlogin(){
	G.removeClassName(document.body,"notlogined");
	G.addClassName(document.body,"logined");
	/**G.setElementValues(G.$("head"),{
		title:MP.login.member_id+"のプレイリスト"
	});*/
	genplaylists();
	gensubscribed();
	MP.pseudoClick(G.$("news"));
}

function onnotlogin(){
	G.removeClassName(document.body,"logined");
	G.addClassName(document.body,"notlogined");
	G.$("all").style.display = "block";
	
}

function genplaylists(plid){
	MP.plcids={};
	if(!MP.tmpl.playlists){
		MP.tmpl.playlists=new G.HtmlElementTemplater(G.$("playlists"));
	}
	MP.tmpl.playlists.start();
	G.each(MP.login.memo.mylists,function(v,i){
		if(v.id===undefined){v.id=i;}
		if(v.channel_id===undefined){
			v.channel_id=[];
		}else if(!(v.channel_id instanceof Array)){
			v.channel_id=[v.channel_id];
		}
		MP.tmpl.playlists.apply(v);
		G.each(v.channel_id,function(cid){
			MP.plcids[cid]=i;
		});
	});
	MP.tmpl.playlists.complete();
	if(plid){
		MP.pseudoClick(G.$("playlist-"+plid,true));
	}
}

function gensubscribed(){
	if(!MP.tmpl.mysubscribed){
		MP.tmpl.mysubscribed=new G.HtmlElementTemplater(G.$("mysubscribed"));
	}
	MP.tmpl.mysubscribed.start();
	MP.getMySubscribedChannels(0,10,function(){
		/*
		var list=[];
		G.each(MP.mysubscribedchannels,function(v){
			list[list.length]=v;
		});
		list=list.sort(function(a,b){
			return a.title < b.title ? -1 : 1;
		});
		*/

		var list=G.sortObj(MP.mysubscribedchannels,"title");

		G.each(list,function(v){
			MP.tmpl.mysubscribed.apply(v);
		})
		MP.tmpl.mysubscribed.complete();
	});
}

function archive() {
	var myurl=G.parseUri(location.href);
	myurl.path=myurl.path.replace(/\/([^\/]*)$/,"/reader.html");
	myurl.hash="myarchive";
	location.href=myurl.toString();
	return;
	//window.open("reader.html"+(MP.READERP?("?"+MP.READERP):"")+"#myarchive",MP.BASETARGET);
	//location.href = "reader.html#myarchive";
}

function login(){
	location.href = MP.API+"../loginp?rt="+G.esc(location.href,"U")+"&mode=iphone";
}

function logout(){
	MP.doLogout(function(){
		location.reload();
	});
}

function search(q,sc){
	if(this.tagName&&this.tagName.match(/^(a|form)$/i)){
		var a=Array.prototype.slice.call(arguments,0);
		var w=G.esc(G.$("search-global").value,"U");
		a.push("global"); a.push(w);
		q=a.join("-");
	}else{
		var w=q;
		q="global-"+G.esc(q,"U")+(sc?("-"+sc):"");
	}
	var myurl=G.parseUri(location.href);
	myurl.path=myurl.path.replace(/\/([^\/]*)$/,"/reader.html");
	myurl.hash="search-"+q;
	location.href=myurl.toString();
	return;
	//window.open("reader.html"+(MP.READERP?("?"+MP.READERP):"")+"#search-"+q,MP.BASETARGET);
	//location.href="reader.html#search-"+q;
	//return;
}
