﻿/*
 * Thickbox 2.1 - One Box To Rule Them All.
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2006 cody lindley
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 * Thickbox is built on top of the very light weight jQuery library.
 */

Date.prototype._toUTCString = Date.prototype.toUTCString;
Date.prototype.toUTCString = function(csharp_mode)
{
	if (!csharp_mode)
		return this._toUTCString();
	var	f = function(n)
	{
		var s = "0"+n;
		return s.substr(s.length-2, 2);
	};
	var d = this;
	return d.getUTCFullYear()+f(d.getUTCMonth())+f(d.getUTCDate())
		+f(d.getUTCHours())+f(d.getUTCMinutes())+f(d.getUTCSeconds());
};

$$ = function(id)
{
	return document.getElementById(id);
};

$.fn.setShown = function(ok)
{
	if (ok)
		return this.show();
	else
		return this.hide();
};

$.fn.setEnabled = function(ok)
{
	return this.each(function()
	{
		if (ok == undefined)
		{
			if (this.save_disabled != undefined && this.save_disabled.constructor == Boolean)
			{
				this.disabled = this.save_disabled;
				this.save_disabled = undefined;
			}
		}
		else
		{
			this.save_disabled = this.disabled;
			this.disabled = !ok;
		}
	});
};

$.keyServer = function(opt)
{
	if (!opt)
		opt = {};
	if (opt.enter == undefined)
		opt.enter = true;
	if (opt.thickbox == undefined)
		opt.thickbox = false;

	var parseCode = function(s)
	{
		if (/^\s*<b>\s*(enter|esc|\d)\s*<\/b>/i.test(s))
			s = RegExp.$1;
		else if (/^\s*(enter|esc|\d)\s+/i.test(s))
			s = RegExp.$1;
		else
			return null;
		if (/^enter$/i.test(s))
			return 13;
		else if (/^esc$/i.test(s))
			return 27;
		else if (/^\d$/.test(s))
			return s*1 + 48;
		return null;
	};
	
	var fillCodeObjs = function(code, code_obj)
	{
		if (code == null || (!opt.enter && code == 13))
			return;
		var obj = that.code_objs[code];
		if (!obj)
			obj = code_obj;
		else if (obj.constructor == Array)
			obj.push(code_obj);
		else
			obj = [obj, code_obj];
		that.code_objs[code] = obj;
	};

	var that = this;
	that.code_objs = [];
	that.fo = (opt.thickbox ? $.thickbox.$("a,button") : $("a,button"));
	that.fo.focus(function()
	{
		this.blur();
	});
	that.fo.each(function()
	{
		var code = parseCode(this.innerHTML);
		fillCodeObjs(code, this);
		if (code >= 48 && code <= 48+9)
			fillCodeObjs(code+48, this);
	});
	that.onkeyup = function(e)
	{
		if (!opt.thickbox && $.thickbox.isShown())
			return;
		if (e.shiftKey||e.ctrlKey||e.altKey)
			return;
		var obj = that.code_objs[e.keyCode];
		if (!obj)
			return;
		if (obj.constructor == Array)
		{
			for (var i=0; i<obj.length; i++)
			{
				var jo = $(obj[i]);
				for (var j=0; j<2; j++)
				{
					if (jo.is(":disabled") || jo.is(":hidden"))
					{
						jo = null;
						break;
					}
					jo = jo.parent();
				}
				if (jo)
				{
					obj[i].click();
					return false;
				}
			}
		}
		else
		{
			obj.click();
			return false;
		}
	};
	that.start = function()
	{
		$(document).keyup(that.onkeyup);
	};
	that.end = function()
	{
		$(document).unbind("keyup", that.onkeyup);
	};
};

$.icon = 
{
	id: null,
	all: null,
	current: null,
	go: function(id)
	{
		var icons = $(".icon");
		var tips = $("div.tip");
		$.icon.id = id;
		$.icon.all = icons;

		var mouseover = function()
		{
			icons.removeClass("hover");
			$.icon.id = icons.index(this);
			$.icon.current = $(this).addClass("hover");
			tips.hide();
			if ($.icon.id < tips.length)
				$(tips[$.icon.id]).show();
		};
		var mouseout = function()
		{
// 			icons.removeClass("hover");
// 			$.icon.id = null;
// 			$.icon.current = null;
// 			tips.hide();
		};
		
		icons.css("text-decoration", "none").hover(mouseover, mouseout).focus(function()
		{
			this.blur();
// 			this.hideFocus = true;
// 			$(this).mouseover();
		});
		
		$(icons[id]).mouseover();
		
		tips.corner();
		
		var go = function(offset)
		{
			var id = $.icon.id;
			if (id == null || id == undefined)
				id = (offset<0?0:icons.length-1);
			id+=offset;
			if (id < 0)
				id = icons.length-1;
			if (id >= icons.length)
				id = 0;
			var icon = icons[id];
			if (icon)
				$(icon).mouseover();
			return false;
		};

		$(document).keydown(function(e)
		{
			if ($.thickbox.isShown())
				return;
			if (e.keyCode == 9)
				return go(e.shiftKey?-1:1);
		});
		$(document).keyup(function(e)
		{
			if ($.thickbox.isShown())
				return;
			switch (e.keyCode)
			{
			case 13:
				try { icons[$.icon.id].click(); } catch(e) {}
				return false;
			case 37: // left
				return go(-1);
			case 39: // right
				return go(1);
			default:
				break;
			}
		});
	}
};

$.PageModel =
{
	x: function()
	{
		var de = document.documentElement;
		var db = document.body;
		return (de&&de.scrollLeft) || (db&&db.scrollLeft) || window.pageXOffset || self.pageXOffset || 0;
	},
	y: function()
	{
		var de = document.documentElement;
		var db = document.body;
		return (de&&de.scrollTop) || (db&&db.scrollTop) || window.pageYOffset || self.pageYOffset || 0;
	},
	w: function()
	{
		var de = document.documentElement;
		var db = document.body;
		return (de&&de.clientWidth) || (db&&db.clientWidth) || window.innerWidth || self.innerWidth || 0;
	},
	h: function()
	{
		var de = document.documentElement;
		var db = document.body;
		return (de&&de.clientHeight) || (db&&db.clientHeight) || window.innerHeight || self.innerHeight || 0;
	}
};

// add thickbox to href elements that have a class of .thickbox
$(function()
{
	$("a.thickbox").click(function()
	{
		var t = this.title || this.name || null;
		var g = this.rel || false;
		$.thickbox.show(t, this.href, g);
		this.blur();
		return false;
	});
});

$.thickbox = 
{
	padding: 10,
	title_height: 18,
	button_height: 32,
	
	// is shown
	isShown : function()
	{
		return $.thickbox.jo && $.thickbox.jo.window;
	},

	$: function(a)
	{
		return $(a, $.thickbox.jo.window);
	},
	
	// called when the user clicks on a thickbox link
	show: function (caption, url, image_group)
	{	
		var opt = {};
		if (url == undefined)
		{
			opt = caption;
			caption = opt.caption;
			url = opt.url;
			image_group = opt.image_group;
		}
		$.thickbox.ajax_callback = opt.ajax_callback;
		$.thickbox.remove_callback = opt.remove_callback;
		
		var close_flag = "&#x00d7;"; // "X"
		
//		try
		{
			if (!$.thickbox.jo)
				$.thickbox.jo = {};
			if (!$.thickbox.jo.hide_select)
			{
				$.thickbox.jo.hide_select = $("<iframe id='TB_HideSelect'></iframe>");
				$.thickbox.jo.overlay = $("<div id='TB_overlay' onclick='$.thickbox.remove()'></div>");
				$.thickbox.jo.load = $("<div id='TB_load'><img src='jq/loading3.gif'/></div>");
				$.thickbox.jo.window = $("<div id='TB_window'></div>");
				$(document.body)
					.append($.thickbox.jo.hide_select)
					.append($.thickbox.jo.overlay)
					.append($.thickbox.jo.load)
					.append($.thickbox.jo.window);
				$.thickbox.resizeOverlay();
				$.thickbox.positionLoad();
				$(document).keydown($.thickbox.onkeydown);
				$(window).resize($.thickbox.onresize).scroll($.thickbox.onscroll);
			}
			
			if (!caption)
				caption = "";
			
			if (url.indexOf("?")>0) // If there is a query string involved
				var base_url = url.substr(0, url.indexOf("?"));
			else
				var base_url = url;
			var url_string = /\.jpg|\.jpeg|\.png|\.gif|\.bmp/g;
			var url_type = base_url.toLowerCase().match(url_string);
			if (url_type == '.jpg' || url_type == '.jpeg' || url_type == '.png'
				|| url_type == '.gif' || url_type == '.bmp')
			{
				// code to show images
				$.thickbox.prev_caption = "";
				$.thickbox.prev_url = "";
				$.thickbox.prev_html = "";
				$.thickbox.next_caption = "";
				$.thickbox.next_url = "";
				$.thickbox.next_html = "";
				$.thickbox.image_count = "";
				$.thickbox.url_found = false;
				if (image_group)
				{
					var temp_array = $("a[@rel="+image_group+"]").get();
					for (var i = 0; ((i < temp_array.length) && ($.thickbox.next_html == "")); i++)
					{
						if (!(temp_array[i].href == url))
						{	
							if ($.thickbox.url_found)
							{
								$.thickbox.next_caption = temp_array[i].title;
								$.thickbox.next_url = temp_array[i].href;
								$.thickbox.next_html =
									"<span id='TB_next'>&nbsp;&nbsp;<a href='#'>Next &gt;</a></span>";
							}
							else
							{
								$.thickbox.prev_caption = temp_array[i].title;
								$.thickbox.prev_url = temp_array[i].href;
								$.thickbox.prev_html =
									"<span id='TB_prev'>&nbsp;&nbsp;<a href='#'>&lt; Prev</a></span>";
							}
						}
						else
						{
							$.thickbox.url_found = true;
							$.thickbox.image_count = "Image " + (i+1) + " of " + (temp_array.length);
						}
					}
				}

				var img_preloader = new Image();
				img_preloader.onload = function()
				{
					img_preloader.onload = null;
					
					// Resizing large images - orginal by Christian Montoya edited by me.
					var w = $.PageModel.w() - 150;
					var h = $.PageModel.h() - 150;
					var image_w = img_preloader.width;
					var image_h = img_preloader.height;
					if (image_w > w)
					{
						image_h = image_h * (w / image_w);
						image_w = w;
						if (image_h > h)
						{
							image_w = image_w * (h / image_h); 
							image_h = h; 
						}
					}
					else if (image_h > h)
					{ 
						image_w = image_w * (h / image_h); 
						image_h = h; 
						if (image_w > w)
						{ 
							image_h = image_h * (w / image_w); 
							image_w = w;
						}
					}
					// End Resizing
					
					$.thickbox.width = image_w + $.thickbox.padding*2;
					$.thickbox.height = image_h + $.thickbox.padding*2+$.thickbox.title_height;
					$.thickbox.jo.window.append(
"<a href='#' id='TB_ImageOff' title='Close' onclick='$.thickbox.remove()'>"
+"<img id='TB_Image' src='"+url+"' width='"+image_w+"' height='"+image_h+"' alt='"+caption+"'/></a>"
+"<div id='TB_caption'>"+caption+"<div id='TB_secondLine'>"
+$.thickbox.image_count+$.thickbox.prev_html+$.thickbox.next_html+"</div></div>"
+"<div id='TB_closeWindow'>"
+"<a href='#' id='TB_closeWindowButton' onclick='$.thickbox.remove()'>"+close_flag+"</a></div>");
					
					$.thickbox.onkeyup = function(e)
					{
						switch (e.keyCode)
						{
						case 27:
							$.thickbox.remove();
							break;
						case 37: // left
							if ($.thickbox.prev_html != "")
								goPrev();
							break;
						case 39: // right
							if($.thickbox.next_html != "")
								goNext();
							break;
						default:
							break;
						}
					};
					$(document).keyup($.thickbox.onkeyup);

					if ($.thickbox.prev_html != "")
					{
						function goPrev()
						{
							$(document).unbind("keyup", $.thickbox.onkeyup);
							$.thickbox.jo.window.empty();
							$.thickbox.show($.thickbox.prev_caption, $.thickbox.prev_url, image_group);
							return false;
						}
						$.thickbox.$("#TB_prev").click(goPrev);
					}
					
					if ($.thickbox.next_html != "")
					{
						function goNext()
						{
							$(document).unbind("keyup", $.thickbox.onkeyup);
							$.thickbox.jo.window.empty();
							$.thickbox.show($.thickbox.next_caption, $.thickbox.next_url, image_group);
							return false;	
						}
						$.thickbox.$("#TB_next").click(goNext);
					}

					$.thickbox.position();
					$.thickbox.jo.load.hide();
					$.thickbox.jo.window.show();
					$.thickbox.doAfterShow();
				};
		  		img_preloader.src = url;
			}
		
			else
			{	
				// code to show html pages
				var query_string = url.replace(/^[^\?]+\??/,'');
				var params = $.thickbox.parseQuery(query_string);
				
				$.thickbox.ajax_w = params['width']*1;
				$.thickbox.ajax_h = params['height']*1;
				$.thickbox.width = $.thickbox.ajax_w+$.thickbox.padding*2;
				$.thickbox.height = $.thickbox.ajax_h+$.thickbox.padding*2+$.thickbox.title_height;
				
				$.thickbox.jo.window.append(
					"<div id='TB_title'>"
					+"<span id='TB_ajaxWindowTitle'>"+caption+"</span>"
					+"<span id='TB_closeAjaxWindow'>"
					+"<a href='#' id='TB_closeWindowButton' onclick='$.thickbox.remove()'>"+close_flag+"</a>"
					+"</span></div>");
				
				if (url.indexOf('TB_iframe') != -1)
				{
					var url_no_query = url.split('TB_');
					$.thickbox.jo.window.append(
						"<iframe frameborder='0' hspace='0' id='TB_iframeContent' name='TB_iframeContent'"
						+" onload='$.thickbox.onIframeLoad()' src='"+url_no_query[0]+"'"
						+" style='width:"+($.thickbox.ajax_w+29)+"px;height:"+($.thickbox.ajax_h+17)+"px;'"
						+"></iframe>");
				}
				else
				{
					$.thickbox.jo.ajax_content = $("<div id='TB_ajaxContent'"
						+" style='width:"+$.thickbox.ajax_w+"px;height:"+$.thickbox.ajax_h+"px;'></div>");
					$.thickbox.jo.window.append($.thickbox.jo.ajax_content);
				}
						
				if (url.indexOf('TB_inline') != -1)
				{
					if (!opt.content)
						opt.content = $("#"+params['inlineId']).html();
					$.thickbox.jo.ajax_content.html(opt.content);
					$.thickbox.position();
					$.thickbox.jo.load.hide();
					$.thickbox.jo.window.show();
					window.setTimeout($.thickbox.doAfterShow, 0);
				}
				else if(url.indexOf('TB_iframe') != -1)
				{
					$.thickbox.position();
					if (!frames['TB_iframeContent']) // be nice to safari
						$.thickbox.onIframeLoad();
				}
				else
				{
					$.thickbox.jo.ajax_content.load(url, function()
					{
						$.thickbox.position();
						$.thickbox.jo.load.hide();
						$.thickbox.jo.window.show();
						$.thickbox.doAfterShow();
					});
				}

				$.thickbox.onkeyup = function(e)
				{ 	
					if (e.keyCode == 27)
						$.thickbox.remove();
				};
				$(document).keyup($.thickbox.onkeyup);
			}
		}
// 		catch(e)
// 		{
// 			_alert(e.message);
// 		}
	},
	
	// helper functions below
	onkeydown : function(e)
	{
		if (e.keyCode == 9)
		{
			if ($.thickbox.jo.fo_text.length < 1)
				return false;
			var first = $.thickbox.jo.fo_text[0];
			var last = $.thickbox.jo.fo_text[$.thickbox.jo.fo_text.length-1];
			if (first == last)
				return false;
			var e1 = e.shiftKey?first:last;
			var e2 = e.shiftKey?last:first;
			if (e.srcElement == e1)
			{
				e2.focus();
				return false;
			}
		}
	},
	
	doAfterShow: function()
	{
		$.thickbox.jo.fo = $.thickbox.$("a,button,input[@type=text],input[@type=password]");
		$.thickbox.jo.fo_text = $.thickbox.$("input[@type=text],input[@type=password]");
		if ($.thickbox.jo.fo_text.length > 0)
			$.thickbox.jo.fo_text[0].focus();
		else
			window.focus();
		$.thickbox.key_server = new $.keyServer({thickbox:true});
		$.thickbox.key_server.start();
		if ($.thickbox.ajax_callback)
			$.thickbox.ajax_callback();
	},
	
	onIframeLoad: function()
	{
		$.thickbox.jo.load.hide();
		$.thickbox.jo.window.show();
		$.thickbox.doAfterShow();
 		if (frames['TB_iframeContent'])
 			$.thickbox.$("#TB_ajaxWindowTitle").html(frames['TB_iframeContent'].document.title);
	},

	remove: function(not_callback)
	{
		$(document).unbind("keydown", $.thickbox.onkeydown).unbind("keyup", $.thickbox.onkeyup);
  		$(window).unbind("resize", $.thickbox.onresize).unbind("scroll", $.thickbox.onscroll);
  		$.thickbox.jo.load.remove();
  		$.thickbox.jo.window.remove();
  		$.thickbox.jo.overlay.remove();
  		$.thickbox.jo.hide_select.remove();
  		delete $.thickbox["jo"];
  		$.thickbox.key_server.end();
  		delete $.thickbox["key_server"];
		if (not_callback !== false && $.thickbox.remove_callback)
			$.thickbox.remove_callback();
		return false;
	},

	resizeOverlay: function()
	{
		var db = document.body;
		var pw = $.PageModel.w();
		var ph = $.PageModel.h();
 		var w = db.scrollWidth || db.offsetWidth || (window.scrollMaxX+pw) || 0;
 		var h = db.scrollHeight || db.offsetHeight || (window.scrollMaxY+ph) || 0;
 		if (w < pw) w = pw;
 		if (h < ph) h = ph;
 		$.thickbox.jo.overlay.width(w+"px").height(h+"px");
 		$.thickbox.jo.hide_select.width(w+"px").height(h+"px");
	},

	positionLoad: function()
	{
		$.thickbox.jo.load.css(
		{
			left: ($.PageModel.x()+($.PageModel.w()-100)/2)+"px",
			top: ($.PageModel.y()+(($.PageModel.h()-100)/2))+"px",
			display: "block"
		});
	},

	position: function()
	{
		$.thickbox.jo.window.css(
		{
			left: ($.PageModel.x()+($.PageModel.w()-$.thickbox.width)/2)+"px",
			top: ($.PageModel.y()+($.PageModel.h()-$.thickbox.height)/2)+"px",
			width: $.thickbox.width+"px"
		});
	},
	
	onresize: function()
	{
		$.thickbox.resizeOverlay();
		$.thickbox.position();
	},
	
	onscroll: function()
	{
		$.thickbox.positionLoad();
		$.thickbox.position();
	},

	parseQuery: function(query)
	{
		var params = {};
		if (!query)
			return params;
		var pairs = query.split(/[;&]/);
		for (var i = 0; i < pairs.length; i++)
		{
			var key_val = pairs[i].split('=');
			if (! key_val || key_val.length != 2)
				continue;
			var key = unescape(key_val[0]);
			var val = unescape(key_val[1]);
			val = val.replace(/\+/g, ' ');
			params[key] = val;
		}
		return params;
	}
};

// replace window alert and confirm
window._alert = window.alert;
window.alert = function(msg, remove_callback)
{
	if (typeof Sleep != "function")
	{
		if (msg)
			msg = msg.replace(/<br\s*\/?\s*>/g, "\n");
		window._alert(msg);
		return;
	}
	if (msg)
		msg = msg.replace(/\n+/g, "<br/>");
	
	var width = 260;
	var height = 110;
	$.thickbox.show(
	{
		caption: document.title,
		url: "#TB_inline?width="+width+"&height="+height,
		content: '<div style="height:'+(height-$.thickbox.button_height)
			+'px;margin-left:10px;line-height:1.4em">'+msg+'</div><div>'
			+'<button class="button float_right"><b>Enter</b>确定</button></div>',
		remove_callback:remove_callback
	});
	
	$.thickbox.jo.ajax_content.css("overflow", "visible");
	if ($.thickbox.jo.ajax_content.height() > height)
	{
		$.thickbox.ajax_w = width*1.6;
		$.thickbox.width = $.thickbox.ajax_w+$.thickbox.padding*2;
		$.thickbox.jo.ajax_content.width($.thickbox.ajax_w+"px");
	}
	$.thickbox.position();
	var button = $.thickbox.$("button");
	button.click($.thickbox.remove);
	
	while (!remove_callback && $.thickbox.isShown())
	{
		var msg = CaptureMSG();
		if (msg && msg.message == parseInt("0x0101",16) && (msg.wParam == 13 || msg.wParam == 27))
			button.click();
		else
			Sleep(10);
	}
};

window._confirm = window.confirm;
window.confirm = function(msg)
{
	if (typeof Sleep != "function")
	{
		if (msg)
			msg = msg.replace(/<br\s*\/?\s*>/g, "\n");
		return window._confirm(msg);
	}
	if (msg)
		msg = msg.replace(/\n+/g, "<br/>");
	
	var width = 260;
	var height = 110;
	$.thickbox.show(
	{
		caption: document.title,
		url: "#TB_inline?width="+width+"&height="+height,
		content: '<div style="height:'+(height-$.thickbox.button_height)
			+'px;margin-left:10px;line-height:1.4em">'+msg+'</div><div>'
			+'<button class="float_right" id="no_button"><b>Esc</b>否</button>'
			+'<button class="float_right" id="yes_button"><b>Enter</b>是</button>'
			+'</div>'
	});
	
	$.thickbox.jo.ajax_content.css("overflow", "visible");
	if ($.thickbox.jo.ajax_content.height() > height)
	{
		$.thickbox.ajax_w = width*1.6;
		$.thickbox.width = $.thickbox.ajax_w+$.thickbox.padding*2;
		$.thickbox.jo.ajax_content.width($.thickbox.ajax_w+"px");
	}
	$.thickbox.position();
	var return_value;
	var yes_button = $.thickbox.$("#yes_button");
	yes_button.click(function()
	{
		return_value = true;
		$.thickbox.remove();
	});
	var no_button = $.thickbox.$("#no_button");
	no_button.click(function()
	{
		return_value = false;
		$.thickbox.remove();
	});
    while ($.thickbox.isShown())
	{
		var msg = CaptureMSG();
		if (msg && msg.message == parseInt("0x0101",16))
		{
			switch (msg.wParam)
			{
			case 13:
				yes_button.click();
				break;
			case 27:
				no_button.click();
				break;
			default:
				break;
			}
		}
		else
			Sleep(10);
	}
    return return_value;
};
