/*
 * DivBox gallery @copyright: http://wwww.phpbasic.com @Contact:
 * w2ajax@gmail.com for lastest version.
 */
(function($) {
	$.fn.divbox = function(opt) {
		var _cfg = {
			source : '<div id="divbox_frame"><div class="mo">倍优天地精彩活动视频<div class="closed"></div></div><div><img src="images/index_05.gif" width="440" height="22"></div><div id="divbox_content"></div></div>',
			width : 450,
			height : 420,
			handle : false,
			src : 'href',
			scrollbar : 'auto',
			closed : '#divbox_frame .closed',
			prev : '#divbox_frame .prev',
			next : '#divbox_frame .next',
			path : 'players/',
			full_drag_handle : false,
			overlay : true,
			event : 'click'

		}
		if (opt)
			$.extend(_cfg, opt);
		$(this).addClass('divbox').bind(_cfg.event, function() {
			$('body').prepend('<div id="divbox"></div>' + _cfg.source);
			var requires = '#divbox,#divbox_frame,#divbox_content';
			var len = $(requires).length;
			if (len < 3) {
				$(requires).remove();
				alert('Wrong defined HTML source (_cfg.source)');
				return false;
			}
			$(_cfg.closed).hide();

			this.closed = function() {
				$('#divbox_frame').animate({
							top : format['top'],
							left : format['left'],
							width : '0px',
							height : '0px'
						}, 500, function() {
							$(this).remove();
							$('#divbox').remove();
							$('object,embed,select').show();
						});
			}
			this.animate = function(t, l, w, h, fn, closed) {
				$('#divbox_frame').animate({
							left : l,
							width : w
						}).animate({
							top : t,
							height : h
						}, 500, function() {
							if (typeof(fn) == 'function')
								fn($('#divbox_content'));
							// $(this).animate({height: h + 40});
							$(_cfg.closed).show().click(function() {
										if (typeof(closed) == 'function')
											closed($('#divbox_content'));
										obj.closed();
									});
						});
				try {
					$("#divbox_frame").draggable({
								handle : $("#divbox_frame")
							}).css({
								cursor : 'move'
							});
					if (!_cfg.full_drag_handle)
						$('#divbox_content').css({
									cursor : 'pointer'
								});
				} catch (e) { /* requires jQuery UI draggables */
				}
			}
			this.flashEmbedString = function(file, w, h, type) { // default
																	// type is
																	// FLV
				var flashvar = '';
				h = h - 70;
				w = w - 20;
				if (type == 'mp3')
					flashvar = '&provider=sound';
				var str = '<object id="player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" name="player" width="'
						+ w + '" height="' + h + '">';
				str += '<param name="movie" value="' + _cfg.path
						+ 'player.swf" />';
				str += '<param name="allowfullscreen" value="true" />';
				str += '<param name="screencolor" value="FAF3E0" />';
				str += '<param name="flashvars" value="file=' + file
						+ '&autostart=true' + flashvar + '" />';
				str += '<embed';
				str += ' type="application/x-shockwave-flash"';
				str += ' id="player2"';
				str += ' name="player2"';
				str += ' src="' + _cfg.path + 'player.swf" ';
				str += ' width="' + w + '" ';
				str += ' height="' + h + '"';
				str += ' allowfullscreen="true"';
				str += ' screencolor="#FAF3E0"';
				str += ' flashvars="file=' + file
						+ '&start=true&autostart=true' + flashvar + '" ';
				str += ' />';
				str += '</object>';
				return str;

			}
			this.viewImage = function(src) {
				$('#divbox_content').html('<img src="' + src + '" />')
						.find('img').hide();
				var Img = new Image();
				Img.onload = function() {

					$('#divbox_content img').attr('src', src);
					var top = sizesystem[3]
							+ Math.round((sizesystem[5] - Img.height) / 2);
					var left = Math.round((sizesystem[0] - Img.width) / 2);
					var width = _cfg.width ? _cfg.width : Img.width;
					var height = _cfg.height ? _cfg.height : Img.height;
					obj.animate(top, left, width, height, function(o) {
								$('#divbox_content img').fadeIn();
								$('#divbox_frame').css({
											'background' : 'none'
										});
							})
					// IE
					Img.onload = function() {
					};
				}
				Img.src = src;
			}

			this.viewDefault = function(src) {
				var winW = _cfg.width ? _cfg.width : sizesystem[0] - 100;
				var winH = _cfg.height ? _cfg.height : sizesystem[5] - 100;
				var top = sizesystem[3]
						+ Math.round((sizesystem[5] - winH) / 2);
				var left = Math.round((sizesystem[0] - winW) / 2);
				obj.animate(top, left, winW, winH, function(o) {
							$(o).html('<iframe src="' + src + '" width="'
									+ (winW - 12)
									+ '" frameborder="0" scrolling="'
									+ _cfg.scrollbar + '" height="' + winH
									+ '"></iframe>');
						});
			}
			this.viewFLV = function(src) {
				var winW = _cfg.width ? _cfg.width : 400;
				var winH = _cfg.height ? _cfg.height : 300;
				var top = sizesystem[3]
						+ Math.round((sizesystem[5] - winH) / 2);
				var left = Math.round((sizesystem[0] - winW) / 2);
				var str = obj.flashEmbedString(obj.href, winW, winH, 'flv');
				obj.animate(top, left, winW, winH, function(o) {
							$(o).html(str);
						});
			}
			this.viewWMV = function(src) {
				var winW = _cfg.width ? _cfg.width : 400;
				var winH = _cfg.height ? _cfg.height : 300;
				var top = sizesystem[3]
						+ Math.round((sizesystem[5] - winH) / 2);
				var left = Math.round((sizesystem[0] - winW) / 2);
				var str = '<object  type="application/x-oleobject" classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112"';
				str += ' width="' + winW + '" height="' + winH + '">';
				str += '<param name="filename" value="' + src + '" />';
				str += '<param name="Showcontrols" value="true" />';
				str += '<param name="autoStart" value="true" />';
				str += '<embed type="application/x-mplayer2" src="' + src
						+ '" Showcontrols="true" autoStart="true" width="'
						+ winW + '" height="' + winH + '"></embed>';
				str += '<object/>';
				obj.animate(top, left, winW, winH, function(o) {
							$(o).html(str);
						});
			}
			this.viewMP3 = function(src) {
				var winW = 320;
				var winH = 80;
				var top = sizesystem[3]
						+ Math.round((sizesystem[5] - winH) / 2);
				var left = Math.round((sizesystem[0] - winW) / 2);
				var str = obj.flashEmbedString(src, winW, winH, 'mp3');
				obj.animate(top, left, winW, winH, function(o) {
							$(o).html(str);
						});
			}
			this.viewSWF = function(src) {
				var winW = _cfg.width ? _cfg.width : 400;
				var winH = _cfg.height ? _cfg.height : 300;
				var top = sizesystem[3]
						+ Math.round((sizesystem[5] - winH) / 2);
				var left = Math.round((sizesystem[0] - winW) / 2);
				var str = obj.flashEmbedString(src, winW, winH);
				obj.animate(top, left, winW, winH, function(o) {
							$(o).html(str);
						});
			}

			this.viewElement = function() {
				var e = '#' + $(obj).attr('rel');
				var winW = _cfg.width ? _cfg.width : $(e).width();
				var winH = _cfg.height ? _cfg.height : $(e).height();
				var top = sizesystem[3]
						+ Math.round((sizesystem[5] - winH) / 2);
				var left = Math.round((sizesystem[0] - winW) / 2);
				obj.animate(top, left, winW, winH, function(o) {
							$(o).html($(e).html());
							$(e).html('');
							$('#divbox_frame').css({
										'background' : 'none',
										'background-color' : '#FAF3E0'
									});
						}, function(o) {
							$(e).html($(o).html());
						});
			}

			this.viewAjax = function(src) {
				$.post(src, {}, function(data) {
							var winW = _cfg.width ? _cfg.width : 400;
							var winH = _cfg.height ? _cfg.height : 300;
							var top = sizesystem[3]
									+ Math.round((sizesystem[5] - winH) / 2);
							var left = Math.round((sizesystem[0] - winW) / 2);
							obj.animate(top, left, winW, winH, function(o) {
										$(o).html(data);
										$('#divbox_frame').css({
													'background' : 'none',
													'background-color' : '#FAF3E0'
												});
									});

						});
			}

			var obj = this;
			var sizesystem = pageSize();
			var _click = $(obj).offset();
			$('object,embed,select').hide();
			var format = {
				'position' : 'absolute',
				'top' : _click.top,
				'left' : _click.left,
				'zIndex' : '10002',
				'width' : 0,
				'height' : 0
			}

			if (_cfg.overlay) {
				$('#divbox').css({
							'width' : '100%',// sizesystem[0]+'px',
							'height' : sizesystem[1] + 'px',
							'position' : 'absolute',
							'zIndex' : '10000',
							'left' : '0',
							'top' : '0'
						}).click(function() {
							obj.closed();
						});
			}

			$('#divbox_frame').css(format).animate({
				width : 50,
				height : 50,
				top : sizesystem[3]
						+ Math.round((sizesystem[5] - $(this).height()) / 2),
				left : Math.round((sizesystem[0] - $(this).width()) / 2)
			});
			if (_cfg.handle == 'element') {
				this.viewElement();
			} else {
				var src = $(obj).attr(_cfg.src).toString();
				if (_cfg.handle == 'ajax') {
					this.viewAjax(src);
				} else {
					var aExt = src.split('.');
					var ext = aExt[aExt.length - 1];
					var str = '';
					switch (ext.toLowerCase()) {
						case 'jpg' :
						case 'jpeg' :
						case 'gif' :
						case 'png' :
							this.viewImage(src);
							break;
						case 'flv' :
							this.viewFLV(src);
							break;
						case 'wmv' :
							this.viewWMV(src);
							break;
						case 'mp3' :
							this.viewMP3(src);
							break;
						case 'swf' :
							this.viewSWF(src);
							break;
						default :
							this.viewDefault(src);
							break;
					}
				}
			}
			return false;
		});
	}
	function pageSize() {
		var de = document.documentElement;
		var winW = window.innerWidth || self.innerWidth
				|| (de && de.clientWidth) || document.body.clientWidth;
		var winH = window.innerHeight || self.innerHeight
				|| (de && de.clientHeight) || document.body.clientHeight;
		var x = window.pageXOffset || self.pageXOffset || (de && de.scrollLeft)
				|| document.body.scrollLeft;
		var y = window.pageYOffset || self.pageYOffset || (de && de.scrollTop)
				|| document.body.scrollTop;
		var pW = window.innerWidth || document.body.scrollWidth
				|| document.body.offsetWidth;
		var pH = window.innerHeight + window.scrollMaxY
				|| document.body.scrollHeight || document.body.offsetHeight;
		var w = pW < winW ? pW : winW; // 18 = the width of window scrollbar
		var h = pH <= winH ? winH : pH + 20;
		arrayPageSize = [w, h, x, y, winW, winH];
		return arrayPageSize;
	}
})(jQuery)
