/////////////////////////////////////////////
//				//
//	Blue Show v1.0		//
//	   Longbill		//
//     http://www.longbill.cn		//
//				//
/////////////////////////////////////////////

var bs_title	= "<span style='font-size:22px;color:#4499ee;font-family:宋体;'>Blue Show v1.0 我的图片浏览器!!</span>";	//标题信息
var bs_trans 	= 50;				//背景透明度
var bs_imgtypes 	= "|jpg|jpeg|gif|png|bmp|pic|";		//符合条件的图片后缀（前后都要以"|"结束）
var bs_bg_color	= "#333333";			//半透明背景颜色
var bs_win_color	= "#d6e9fc";			//主窗口背景色
var bs_lang = 					//语言设置
{
	first: "<span style='font-size:12px;color:#333333'>这是第一张</span>",
	last : "<span style='font-size:12px;color:#333333'>这是最后一张</span>",
	notfound : "<span style='font-size:12px;color:#00ff00'>Not Found</span>",
	previous : "上一张图片",
	next     : "下一张图片",
	close    : "退出BlueShow",
	open     : "双击打开此图片",
	title    : "标题:",
	alt      : "点击使用BlueShow浏览此图"	//自动添加到< a>标签的title和 < img>标签的alt里的文字
}

var bs_auto 	= 1;		//默认的自动运行，对应调用时的参数  autorun
var bs_hidden 	= 1;		//默认的锁定网页，对应调用时的参数  lock
var bs_urlencode	= 1;		//是否处理非常规url（如果你的空间支持中文的路径名，最好设置成0）
var bs_height 	= 400;		//主窗口高
var bs_width 	= 650;		//主窗口宽
var bs_max_width 	= 400;		//主图片最大宽
var bs_max_height 	= 300;		//主图片最大高
var bs_max_width_s	= 100;		//两侧小图片最大宽
var bs_max_height_s	= 200;		//两侧小图片最大高
var bs_padding	= 4;		//边距
var bs_move = 			//移动的提示窗口的一些参数
{
	display : false,
	bg   : "#e7f2fd",
	border: "1px solid #eeeeee",
	trans :70,
	color:"#333333"
}

//保存网页原有事件
var bs_onload = window.onload;
var bs_onscroll = window.onscroll;
var bs_onmousemove = document.body.onmousemove;
//内部全局变量
var bs_win 	= null;
var bs_imgs 	= [];
var bs_titles	= [];
var bs_total 	= 0;
var bs_i 		= 0;
var debug 		= false;
var bs_get_vars 	= [];
var bs_out_html	= "";
var bs_body	= document.body;
var bs_out	= document.createElement("div");
with(bs_out.style)
{
	display = "none";
	zIndex = 1001;
	position = "absolute";
	height = bs_height;
	width = bs_width;
	backgroundColor = bs_win_color;
	padding = bs_padding*2;
	top = 0;
	left = 0;
	border = "0px";
	textAlign = "center";	
}
bs_out.id = "bs_main";

function bs_w(s)
{
	bs_out_html+=s;
}

//以write的方式创建dom树（比直接的dom操作好）
bs_w("<table style='border-collapse: separate; border-spacing: 4px;caption-side: middle;text-align:center;' id='bs_table'><tr><td colspan=3 id='bs_title'></td></tr>");
bs_w("<tr><td width=100 id='bs_img_left' align=center></td><td id='bs_img' align=center>&nbsp;</td><td width=100 id='bs_img_right' align=center>&nbsp;</td></tr>");
bs_w("<tr><td align=center style='font-size:12px;color:#333333;'>Powered By <a href='http://www.longbill.cn' target=_blank style='color:#8ec2f5;text-decoration:none;'>Longbill</a></td>");
bs_w("<td align=center><a href='javascript:bs_show(1)' style='text-decoration:none;color:#333333' title='Go to the first one'><<</a>&nbsp;&nbsp;<a href='javascript:bs_pre()' style='text-decoration:none;color:#333333' title='Previous'><-</a>&nbsp;&nbsp;");
bs_w("<span style='width:20px;font-size:16px;' id='bs_num'></span>&nbsp;&nbsp;<a href='javascript:bs_next()' style='text-decoration:none;color:#333333' title='Next'>-></a>&nbsp;&nbsp;");
bs_w("<a href='javascript:bs_show(bs_total)' style='text-decoration:none;color:#333333' title='Go to the last one'>>></a></td><td align=center>");
bs_w("<a href='javascript:bs_close()' style='text-decoration:none;color:#333333;font-size:16px;font-weight:bold;font-family:Arial;background-color:auto;' title='Close Blue Show'>CLOSE</a></td></td>");
bs_w("</tr></table>");
bs_w("<div id='bs_move_div' style='position:absolute;padding:3;z-index:1002;width:auto;height:auto;display:none;word-wrap:break-word;word-break:break-all;font-size:12px;'>&nbsp;</div>");

var bs_a = document.createElement("div");
with(bs_a.style)
{
	position = "absolute";
	padding = 3;
	zIndex = 1002;
	width = "auto";
	height = "auto";
	display = "none";
	wordWrap = "break-word";
	wordBreak = "break-all";
	fontSize = "12px";
}
bs_a.id = "bs_move_div";
bs_body.appendChild(bs_a);

var bs_a = document.createElement("div");
with(bs_a.style)
{
	left = 0;
	top  = 0;
	position = "absolute";
	margin = 0;
	zIndex =1000;
	display = "none";
	backgroundColor = bs_bg_color;
	border = "0px";
}
bs_a.id = "bs_bg";
bs_body.appendChild(bs_a);

bs_out.innerHTML = bs_out_html;
bs_body.appendChild(bs_out);

// 取得调用参数
var bs_scripts = document.getElementsByTagName('script');
var bs_url = bs_scripts[bs_scripts.length-1].src;
if (bs_url && bs_url.indexOf("\?") !=-1)
{
	var bs_arr = bs_url.split("\?");
	if (bs_arr[1])
	{
		var bs_reqs = bs_arr[1].split("&");
		for(var bs_i=0;bs_i<bs_reqs.length;bs_i++)
		{
			var bs_req = bs_reqs[bs_i].split("=");
			eval("bs_get_vars['"+bs_req[0]+"']="+bs_req[1]+";");
		}//author : longbill  www.longbill.cn
		for(var bs_i in bs_get_vars)
		{
			var bs_j = bs_i.toString().toLowerCase();
			switch(bs_j)
			{
				case "lock"    : bs_hidden = bs_get_vars[bs_i];
				case "autorun" : bs_auto = bs_get_vars[bs_i];
				case "move"    : bs_drag = bs_get_vars[bs_i];
			}
		}
	}
}

window.onload = (!bs_auto)?bs_onload:function ()	//自动初始化
{
	if (debug) alert('window.onload');
	if (typeof bs_onload == "function") bs_onload();
	BlueShow();
}

function BlueShow()	//初始化
{
	var imgs = document.getElementsByTagName("a");
	for(var i=0;i<imgs.length;i++)
	{
		var href = imgs[i];
		var src = href.href;
		if (!src) continue;
		var arr = src.split(".");
		var type = "|"+arr[arr.length-1].toLowerCase()+"|";
		if ( bs_imgtypes.indexOf(type) != -1)
		{
			bs_total++;
			href.i = bs_total;
			var alt = (href.title)?href.title+"\n":"";
			bs_titles[href.i] = (href.innerText)?href.innerText:(href.title)?href.title:href.href;
			href.title = alt + bs_lang["alt"];
			href.onclick = bs_showme;
			href.target = "_blank";
			bs_imgs[bs_total] = bs_escape(href.href);
		}
	}
	var imgs = document.getElementsByTagName("img");
	for(var i=0;i<imgs.length;i++)
	{
		var img = imgs[i];
		if (img.className && img.className.indexOf("blueshow") !=-1)
		{
			bs_total++;
			img.i = bs_total;
			bs_titles[img.i] = (img.alt)?img.alt:img.src;
			img.onclick = bs_showme;
			var alt = (img.alt)?img.alt+"\n":"";
			img.alt = alt + bs_lang["alt"];
			img.style.cursor = "hand";
			bs_imgs[bs_total] = img.src;
		}
	}

	with( $('bs_img').style)
	{
		width = bs_max_width + bs_padding*2;
		height = bs_max_height + bs_padding*2+5;
	}

	if (bs_hidden)
	{//author : longbill  www.longbill.cn
		with($('bs_bg').style)
		{
			width = parseInt((document.body.scrollWidth<document.body.clientWidth)?document.body.clientWidth:document.body.scrollWidth);
			height = parseInt((document.body.scrollHeight<document.body.clientHeight)?document.body.clientHeight:document.body.scrollHeight);
			backgroundColor = bs_bg_color;
			if (isIE()) 
				filter = " Alpha(Opacity="+bs_trans+")";
			else
				opacity = bs_trans/100;
		}
	}//author : longbill  www.longbill.cn
	bs_center($('bs_main'));
	window.onscroll = function ()
	{
		if (typeof bs_onscroll == "function") bs_onscroll();
		bs_center($('bs_main'));
	}
	
	var movdiv = $('bs_move_div');
	with (movdiv.style)
	{
		backgroundColor = bs_move["bg"];
		border = bs_move["border"];
		color = bs_move["color"];
		if (isIE()) 
			filter = " Alpha(Opacity="+bs_move["trans"]+")";
		else
			opacity = bs_move["trans"]/100;
	}
	document.body.onmousemove = function (event)
	{
		event = event ? event : window.event;
		if (typeof(bs_onmousemove)== "function") bs_onmousemove();
		if (bs_move["display"])
		{
			with ($('bs_move_div').style)
			{
				left = event.clientX + parseInt(document.body.scrollLeft)+10;
				top = event.clientY + parseInt(document.body.scrollTop);
				display = "";
			}
		}
	}
	$('bs_title').innerHTML = bs_title;
}

function $(obj) { return document.getElementById(obj); }  // a very useful function

function bs_escape(s) //处理urlencode过后的url
{
	if (!s) return "";
	if (!bs_urlencode) return s;
	var r = "";
	if (s.toLowerCase().substr(0,7) == "http://")
	{
		s = s.substr(7,s.length);
		r += "http://";
	}
	var arr = s.split("/");
	r += arr[0];
	for(var i=1;i<arr.length;i++) r+="/"+escape(arr[i]);
	return r;
}

function bs_showme(event)
{
	if (!bs_show(this.i) && bs_hidden)  $('bs_bg').style.display = "";
	return false;
}

function bs_pre()
{
	if (bs_i>1) bs_show(bs_i-1);
}

function bs_next()
{
	if (bs_i<bs_total) bs_show(bs_i+1);
}

function bs_show(bsi)		//显示图片
{
	bs_i = bsi;
	if (bs_hidden) $('bs_bg').style.display = "";
	var win = $('bs_main');
	win.style.display = "";

	if (bsi>1 && bs_imgs[bsi-1])
	{
		bs_create_img("bs_img_left",bs_imgs[bsi-1],bs_max_width_s,bs_max_height_s,bs_titles[bsi-1]);
	}
	else
	{
		with ($('bs_img_left'))
		{
			innerHTML = bs_lang["first"];
			onmouseover = null;
			onclick = null;
			bs_move_off();
		}
		
	}
	
	if (bs_imgs[bsi])
	{
		bs_create_img("bs_img",bs_imgs[bsi],bs_max_width,bs_max_height,bs_titles[bsi]);
		$('bs_num').innerHTML = ""+bsi+"/"+bs_total;
	}
	else
	{
		$('bs_img').innerHTML = bs_lang["notfound"];
	}//author : longbill  www.longbill.cn

	if (bsi<bs_total && bs_imgs[bsi+1])
	{
		bs_create_img("bs_img_right",bs_imgs[bsi+1],bs_max_width_s,bs_max_height_s,bs_titles[bsi+1]);
	}
	else
	{
		with ($('bs_img_right'))
		{
			innerHTML = bs_lang["last"];
			onmouseover = null;
			onclick =null;
			bs_move_off();
		}
	}
	bs_center($('bs_main'));
}

function bs_create_img(id,src,max_width,max_height,title)	//创建img
{
	var imgdiv = $(id);
	if (!imgdiv) return false;
	imgdiv.innerHTML = "";
	imgdiv.t = title;
	var img = document.createElement("img");
	img.mw = max_width;
	img.mh = max_height;
	img.onreadystatechange = bs_resize;
	img.onload = bs_resize;
	imgdiv.onmouseover = function ()
	{
		var fid = this.id;
		var html = (fid == "bs_img_left")?bs_lang["previous"]+"<br/>"+bs_lang["title"]+this.t:(fid == "bs_img")?bs_lang["title"]+this.t+"<br/>"+bs_lang["open"]:bs_lang["next"]+"<br/>"+bs_lang["title"]+this.t;
		bs_move_on(html);
	}
	imgdiv.onmouseout = bs_move_off;
	imgdiv.onclick = function ()
	{
		var fid = this.id;
		if (fid == "bs_img_left") bs_pre();
		else if (fid == "bs_img_right") bs_next();
	}
	if (id == "bs_img") imgdiv.ondblclick = function (){open(this.firstChild.src,"_blank","");}
	imgdiv.appendChild(img);
	img.src = src;
	var w=img.width,h=img.height;
	var pw = max_width/w, ph = max_height/h;
	if (pw<1 || ph<1)
	{
		var p = (ph>pw)?pw:ph;
		img.width = parseInt(w*p)-1;
		img.height = parseInt(h*p)-1;
	}
}

function bs_close()	//退出blueshow
{
	$('bs_main').style.display = "none";
	if (bs_hidden) $('bs_bg').style.display = "none";
	bs_move_off();
}

window.onerror = function()
{
	window.status = "Script Error!!!!!!!!!!!!";
	return false;
}

function isIE()
{
	return (document.all && window.ActiveXObject && !window.opera) ? true : false;
}

function bs_resize()
{
	var w = parseInt(this.width);
	//author : longbill  www.longbill.cn
	var h = parseInt(this.height);
	if (!this.mw || !this.mh) return;
	var pw = this.mw/w;
	var ph = this.mh/h;
	if (pw>=1 && ph>=1) return;
	var p = (ph>pw)?pw:ph;
	this.width = w*p-1;
	this.height = h*p-1;
}

function bs_center(win)	//将主窗口置于网页正中
{
	var d = document.body;
	var s1 = parseInt(d.scrollTop + d.clientHeight/2 - bs_height/2);
	win.style.top = (s1<parseInt(d.scrollHeight)-bs_height)?s1:d.scrollHeight;
	//author : longbill  www.longbill.cn
	var s1 = parseInt(d.scrollLeft) + parseInt(d.clientWidth/2) - parseInt(bs_width/2);
	win.style.left = (s1<parseInt(d.scrollWidth)-bs_width)?s1:d.scrollWidth;
}

function bs_move_on(s)
{
	bs_move["display"] =1;
	$('bs_move_div').innerHTML =s;
}

function bs_move_off()
{
	bs_move["display"] = false;
	$('bs_move_div').innerHTML = "";
	$('bs_move_div').style.display = "none";
}