// ==UserScript==
// @name           Nico multi-pop-drag-resize part
// @namespace      http://andre-garden.com
// @description    Noco muiti-pop-drag-resize support. scrollY moves when URL is ..#flvplayer_container. 
// @include        http://www.nicovideo.jp/watch*#flvplayer_container
// @include        http://www.nicovideo.jp/watch*#flvplayer_container_m
// ==/UserScript==

(function(){
    var ww = (unsafeWindow || window);
    var dc_width,dc_height;
    var init_size = true;
    var video_frame_part = {"scrollY":0,"clientH":0,"clientW":0};
    var flvp = document.getElementById("flvplayer");
    var flvp_container = document.getElementById("flvplayer_container");

 	ww.addEventListener("load", function(){
        if (document.location.href.indexOf("#flvplayer_container_m") > 0) {
	        ww.addEventListener("scroll", function(){
		        GM_setValue("video_frame_data_scrollY", ww.scrollY);
            },false);
            
			var y = GM_getValue("video_frame_data_scrollY");
            if (typeof(y) != 'undefined') {
                ww.scroll(0, y);
	        }
        } else {
 	        ww.addEventListener("resize", function(){
 	            flvp.height = Math.min(Math.floor(video_frame_part.clientH * document.documentElement.clientHeight/video_frame_part.clientH), 540);//���T�C�Y�͍����Ɉˑ�����
                flvp_container.style.height = flvp.height + "px";
                document.wrappedJSObject.location.href = document.wrappedJSObject.location.href;
	        },false);    

            if (init_size) {
                var flv_init_session = setInterval(function(){
                    if (typeof(flvp) == 'undefined' || typeof(flvp) !== 'object' || !flvp.ReadyState > 2) return;
                    if (typeof(flvp_container) == 'undefined' || typeof(flvp_container) !== 'object' || !flvp_container.ReadyState > 2) return;
                    var h = document.location.href.match(/http:\/\/www.nicovideo.jp\/watch.*?h=(.+)#flvplayer_container/);
                    if (h) {
                        video_frame_part.clientH = h[1];
                    }
                    flvp.wrappedJSObject.SetVariable('Stage.scaleMode', 'showAll');
 	                flvp.height = video_frame_part.clientH - 20;
                    flvp_container.style.height = (video_frame_part.clientH - 20) + "px";
                    
                    document.wrappedJSObject.location.href = document.wrappedJSObject.location.href;
                    clearInterval(flv_init_session);
                }, 100);
                
            }
        }

	},false);
})();

