var Gallerybp_gm;
var bp_server = "http://pixie.blogdeco.jp/";
//var bp_server = "http://pixie.o/";
var gallery_bp_server = bp_server;
var gallery_elSwf = null;
var gallery_sidebarswf = null;
function Gallerybp_onContent(f){
	if(!window.__ADLE){
		var n=function(){
			if(arguments.callee.d)
				return;
			arguments.callee.d=true;
			if(window.__ADLET){
				clearInterval(window.__ADLET);
				window.__ADLET=null
			}
			for(var i=0;i<window.__ADLE.length;i++){
				window.__ADLE[i]()
			}
			window.__ADLE=null
		};
		if(document.addEventListener)document.addEventListener("DOMContentLoaded",n,false);
		// forget IE, __ie_onload defer doesnt work
		if(/WebKit/i.test(navigator.userAgent)){
			window.__ADLET=setInterval(function(){
				if(/loaded|complete/.test(document.readyState)){
					n()
				}
			},10)
		}
		window.onload=n;
		window.__ADLE=[]
	}
	window.__ADLE.push(f)
}
Gallerybp_onContent( Gallerybp_onDomLoaded );
function Gallerybp_onDomLoaded(){
	if( typeof(bp_common_loaded)!='undefined' ){
		Gallerybp.load();
	}else{
		setTimeout(Gallerybp_onDomLoaded,10);
	}
}
var Gallerybp = function(){
	// config
	var bpName			= "Gallerybp";
	var idFloatingDiv	= bpName+"_floatingDiv";
	var idFloatingSwf	= "externalInterface_"+bpName+"_floatingSwf";
	var idFloatingEmb = "externalInterface_"+bpName+"_floatingEmb";
	var idSidebarSwf  = "externalInterface_"+bpName+"_sidebarSwf";
	var idSidebarEmb  = "externalInterface_"+bpName+"_sidebarEmb";
	var elFloating;
	var swfFloating		= ["swf/Gallery.swf"];
	var swfSidebar		= "swf/Sidebar.swf";
	var lib						= "js/bp_common.js";
	var swfsize					= [600,600];
	var swfSidebarSize	= [140,105];
	var offset_top  = 0;
	var offset_left = 0;
	var aWindowSize;
	var aScrollSize;
	var aScrollPos;
	var swf_index = 0;
	var loaded = 0;		// change to 1 onload
	var b=null;	// bp_common
	var shownFromBlogparts = false;
	var active = false;	// true when showing fullscreen, false when 1x1[px]

	var params = new Object();
	var getparam = function(){
		var s = (function (e) { if(e.nodeName.toLowerCase() == 'script') return e; return arguments.callee(e.lastChild) })(document);
		var pair = s.src.match(/(style|color|sidebar)=([a-zA-Z0-9,_%]*)/g);
		if( !pair ){ return; }
		for( var i=0; i<pair.length; i++ ){
			var a = pair[i].split("=");
			params[a[0]] = a[1].split(",");
		}
	}();

	// sidebar
	document.write("<script type='text/javascript' charset='utf-8' src='"+gallery_bp_server+lib+"'></script>");
	if( params['sidebar']!='none' ){
		document.write("<object id='"+idSidebarSwf+"'"+
			" classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'"+
			" codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0'"+
			" width= '"+swfSidebarSize[0]+"px'"+
			" height='"+swfSidebarSize[1]+"px'>"+
			"<param name='movie' value='"+gallery_bp_server+swfSidebar+"'>"+
			"<param name='quality' value='high'>"+
			"<param name='wmode' value='transparent'>"+
			"<param name='allowScriptAccess' value='always'>"+
			"<param name='FlashVars' value='color="+params['color']+"'>"+
			"<embed name='"+idSidebarSwf+"'"+
			" id='"+idSidebarEmb+"'"+
			" src='"+gallery_bp_server+swfSidebar+"'"+
			" quality='high' bgcolor='#000000'"+
			" width='"+swfSidebarSize[0]+"px' height='"+swfSidebarSize[1]+"px'"+
			" type='application/x-shockwave-flash'"+
			" align='middle'"+
			" wmode='transparent'"+
			" FlashVars='color="+params['color']+"'"+
			" pluginspage='http://www.macromedia.com/go/getflashplayer'"+
			" allowScriptAccess='always' /></object>");
		cmking_ad_config = {
			partner_id  : 'ecnavi_jp_kayac_cm_kw',
			type_id     : 'KAYAC_HS_PIXIE',
			width       : 140,
			height      : 15,
			format      : 'LinkUnit',
			border_color: '#FFFFFF',
			bg_color    : '#FFFFFF',
			title_color : '#000000',
			text_color  : '#000000',
			url_color   : '#000000',
			logo_bgcolor  : '#FFFFFF'
		};
		document.write("<br/><script type='text/javascript' src='http://www.cmking.jp/ads_linkunit/cmking_ad.js'></script>");
	}
	(new Image).src = 'http://log.kayac.com/cl/?rs=i&cm=bm1100010&ln=1';

	var createStage = function(){
		if( typeof(bp_common)=='undefined' ){ throw "needs bp_common framework"; return; }
		b = bp_common;
		//b.hideScrollBars();

		if( !elFloating ){
			elFloating = new bp_elementFactory(
				document.getElementsByTagName("body")[0],
				[ {"tag" : "div",
					"attr":{"id":idFloatingDiv},
					"style":{
						"width"  :"1px",
						"height" :"1px",
						"zIndex" :"103",
						"display":"block"
					},
					"innerHTML":"<object id='"+idFloatingSwf+"'"+
						" classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'"+
						" codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0'"+
						" width= '100%'"+
						" height='100%'>"+
						"<param name='movie' value='"+gallery_bp_server+swfFloating[swf_index]+"'>"+
						"<param name='quality' value='high'>"+
						"<param name='wmode' value='transparent'>"+
						"<param name='allowScriptAccess' value='always'>"+
						"<param name='FlashVars' value='style="+params['style']+"'>"+
						"<param name='allowFullScreen' value='true'>"+
						"<embed name='"+idFloatingSwf+"'"+
						" id='"+idFloatingEmb+"'"+
						" src='"+gallery_bp_server+swfFloating[swf_index]+"'"+
						" quality='high' bgcolor='#000000'"+
						" width='100%' height='100%'"+
						" type='application/x-shockwave-flash'"+
						" align='middle'"+
						" wmode='transparent'"+
						" FlashVars='style="+params['style']+"'"+
						" allowFullScreen='true'"+
						" pluginspage='http://www.macromedia.com/go/getflashplayer'"+
						" allowScriptAccess='always' /></object>"
			} ]);
			gallery_elSwf = navigator.userAgent.match(/MSIE/) ? window[idFloatingSwf] : document[idFloatingSwf];
		}
	}

	var updatePosition = function(){
		aWindowSize = b.getWindowSize();
		aScrollSize = b.getScrollSize();
		aScrollPos	= b.getScrollPosition();
		if( active ){
			elFloating.style.width  = "100%";
			elFloating.style.height = aWindowSize[1]+"px";
			elFloating.style.left   = aScrollPos[0]+"px";
			elFloating.style.top    = aScrollPos[1]+"px";
		}else{
			elFloating.style.width  = "1px";
			elFloating.style.height = "1px";
			elFloating.style.left   = aScrollPos[0]+"px";
			elFloating.style.top    = aScrollPos[1]+"px";
		}
	}

	var showStage = function(){
		b.deleteObstacles();
		updatePosition();
		if( navigator.userAgent.match( "MSIE" ) ){
			document.onmousewheel = Gallerybp.onMouseWheel;
		}else if( navigator.userAgent.match( "Safari" ) ){
			window.onmousewheel = Gallerybp.onMouseWheel;
		}else if(window.opera){
			window.attachEvent("onmousewheel",Gallerybp.onMouseWheel);
		}else if(window.addEventListener){
			window.addEventListener( "DOMMouseScroll", Gallerybp.onMouseWheel, false );
		}
	}

	return {
		timer : null,
		laststyle : null,
		scanindex : 0,
		scantags : [],
		infected : [],
		load : function(){
			createStage();
			updatePosition();
			b.waitForFunction( "gallery_elSwf.xi_append", function(){
				Gallerybp_gm = new GalleryManager();
			} );
			b.addListener("DisplayArea","change",updatePosition,false);
			b.addListener( window,      "load",  updatePosition,false);
		},
		show : function(a){
			if( !Gallerybp_gm.initialized ){ return; }
			active = true;
			showStage();
			setTimeout( function(){
				Gallerybp_gm.showImage(a);
			},100);
		},
		end : function(){
			//b.removeListener("DisplayArea","change",updatePosition,false);
			active = false;
			if( navigator.userAgent.match( "MSIE" ) ){
				document.onmousewheel = null;
			}else if( navigator.userAgent.match( "Safari" ) ){
				window.onmousewheel   = null;
			}else if(window.opera){
				window.detachEvent("onmousewheel",Gallerybp.onMouseWheel);
			}else if(window.addEventListener){
				window.removeEventListener( "DOMMouseScroll", Gallerybp.onMouseWheel, false );
			}
			setTimeout( function(){
				updatePosition();
				//elFloating.style.width= '1px';
				//elFloating.style.height='1px';
				b.revivalObstacles();
			},0);
		},
		showThese : function(urls,focusindex){
			var newgalleryid = Gallerybp_gm.Galleries.length;
			b.waitForEval( "Gallerybp_gm.initialized", function(){
				active = true;
				if( shownFromBlogparts ){
					showStage();
					setTimeout(function(){
						gallery_elSwf.xi_showImage( newgalleryid, focusindex );
					},0);
					return;
				}
				var photoid = 0;
				urls.bpc_each( function(url){
					url = b.addDomain(url);
					gallery_elSwf.xi_append( newgalleryid, photoid, url, "" );
					photoid++;
				});
				shownFromBlogparts = true;
				Gallerybp.showThese(null,focusindex);
			} );
		},
		requestPhotos : function(){
			setTimeout( function(){
				if( b==null || typeof(Gallerybp_gm)=="undefined" || !Gallerybp_gm.initialized){
					setTimeout(Gallerybp.requestPhotos,50);
					return;
				}
				var n = 0;
				gallery_sidebarswf = navigator.userAgent.match(/MSIE/) ? window[idSidebarSwf] : document[idSidebarSwf];
				Gallerybp_gm.Galleries.bpc_each( function(g){
					g.imgs.bpc_each( function(i){
						var url = i.parentNode.href  || i.src;
						if( gallery_sidebarswf ){
							gallery_sidebarswf.xi_SidebarAppend( url );
						}
						n++;
					} );
				} );
				if( !n && gallery_sidebarswf ){
					gallery_sidebarswf.xi_showNoPhoto();
				}
			},0 );
		},
		onMouseWheel : function(e){
			var delta;
			if( navigator.userAgent.match( "MSIE" ) ){
				e = window.event;
				delta = e.wheelDelta; e.returnValue = false;
			}else if( navigator.userAgent.match( "Gecko" ) ){
				delta = e.detail * -1; e.preventDefault();
			}else if( window.opera ){
				delta = e.detail * -1; e.preventDefault();
			}else if( navigator.userAgent.match( "Safari" ) ){
				delta = e.wheelDelta; e.returnValue = false;
			}
			delta = ( delta < 0 ) ? -1 : 1;
			gallery_elSwf.xi_zoom(delta);
			if (e.preventDefault) {
				e.preventDefault();
			}
			e.returnValue = false;
		}
	};
}();


var GalleryManager = function(){
	this.initialize();
};
GalleryManager.prototype = {
	b : null,
	initialized: false,
	Galleries : [],
	initialize: function(){
		var slf = this;
		b = bp_common;
		var images = document.body.getElementsByTagName("img");
		var ai = b.asArray( images );
		var re_nodeName = new RegExp(/a/i);
		var re_imgName = new RegExp(/.+(jpg|jpeg|gif|png|bmp)$/i);
		var ai2 = ai.bpc_filter( function(i){
			var p = i.parentNode;
			if( !re_nodeName.test(p.nodeName) ){
				return false;
			}
			var href = p.getAttribute('href');
			if( !href ){
				return false;
			}
			if( re_imgName.test(href) ){
				return true;
			}else{
				return false;
			}
		});
		ai2.bpc_each( function(i){
			var p = slf.getParentDiv(i);
			var e = slf.exists( p );
			var anchor = i.parentNode;
			if( e.length==0 ){
				slf.Galleries.push( new Gallery(i,p) );
			}else{
				var iid = e[0].append( i );
			}
			anchor.onclick=function(){
				Gallerybp.show(this);
				return false;
			};
		});
		setTimeout( function(){
			this.sendToSwf();
		}.bpc_bind(this), 0 );
	},
	sendToSwf : function(){
		Gallerybp_gm.Galleries.bpc_each( function(g,idx){
			g.imgs.bpc_each( function(p,jdx){
				var txt = p.parentNode.title || p.title || p.alt;
				var url = p.parentNode.href  || p.src;
				url = b.addDomain(url);
				gallery_elSwf.xi_append(idx,jdx,url,txt);
			} );
		} );
		Gallerybp_gm.initialized = true;
	},
	showImage : function(a){
		var loc = this.getGallery(a);
		if( loc==null ){
			return false;
		}
		if( gallery_elSwf && gallery_elSwf.xi_showImage && Gallerybp_gm.initialized ){
			gallery_elSwf.xi_showImage( loc[0], loc[1] );
		}else{
			return false;
		}
	},
	getGallery : function( anchor ){
		var len = this.Galleries.length;
		for( var i=0; i<len; i++ ){
			var incl = this.Galleries[i].includes( anchor );
			if( incl != null ){
				return [i,incl];
			}
		}
		return null;
	},
	getParentDiv : function( img ){
		var p = img;
		var n = p.nodeName.toLowerCase();
		while( n != "body" && n != "div" ){
			p = p.parentNode;
			n = p.nodeName.toLowerCase();
		}
		return p;
	},
	exists : function(div){
		return this.Galleries.bpc_filter( function(g){
			if( g.div == div ){
				return true;
			}else{
				return false;
			}
		});
	}
};
var Gallery = function(img,div){
	this.initialize(img,div);
};
Gallery.prototype = {
	imgs: null,
	div: null,
	initialize: function(img,div){
		this.imgs = new Array();
		this.imgs.push( img );
		this.div = div;
	},
	// return index of added img
	append: function(img){
		this.imgs.push( img );
		return (this.imgs.length-1);
	},
	includes : function( anchor ){
		var len = this.imgs.length;
		for( var i=0; i<len; i++ ){
			if( this.imgs[i].parentNode == anchor ){
				return i;
			}
		}
		return null;
	}
};
