/**
 * 演播厅的插件
 * 
 * ,如果需要多次播放,这在 opts中设置 :
 * 
 * studioId : 'st_123'
 * 
 */
$.fn.kyStudioPlugin = function(opts) {

	// var width=710;
	// var height=380;

	var showDialogActId = '';// 弹出窗口的 触发对象thickbox方式调用

	var studioId = '';

	if (ky.util.isEmpty(opts.studioId)) {// 如果外部设置了这个 studioId,就代表有值
		studioId = 'st_' + ky.util.getNextId();
		opts.studioId = studioId;
	} else {
		studioId = opts.studioId;
	}
	if (isdebug()) {
		
		log('studioId  init..=' + studioId);
	}
	opts = $.extend({
		theRenderElementId : $(this).attr('id')/** 进行渲染的容器id */
		,
		waterMark : '',/* 水印 */
		pcode : '',
		width : 710,
		height : 380,
		showByLayer : false,/* 是否在层中显示 */
		// studioId : 'st_123' 如果需要多次播放则设置此值,需要 字符串开头
		autoCreateLayer : false,
		/** layer显示的时候,是否自动创建层. */
		userId : 0,
		/** 用户的id,用来记录点击 */
		callback : function() {
		},
		top : 0
			// 距离上面的距离
		}, opts || {});

	return this.each(function() {
		

		/**
		 * 对js进行初始化,
		 */
		function initJs() {
			
			// alert('js...........');
			var head = document.getElementsByTagName("head")[0];

			// log(head.innerHTML);
			// log("head.length===" + head.length);

			// $('script', document.head).each(function() {
			// var t = $(this);
			// log(' ====' + t.attr('src'));
			// });
			var root = '/keyajs/jquery/';

			var array = new Array();
			var arrayPath = new Array();

			var jsName = '';

			jsName = 'tree.js';
			array.push(jsName);
			arrayPath.push(root + 'plugin/tree/' + jsName);

			// {
			// jsName = 'thickbox-compressed.js';
			// array.push(jsName);
			// arrayPath.push(root + 'plugin/thickbox/' + jsName);
			//
			// jsName = 'thickbox.css';
			// array.push(jsName);
			// arrayPath.push(root + 'plugin/thickbox/' + jsName);
			//
			// }
			var php = '/keyajs/pc/js/compressed/';

			jsName = 'client.js';
			array.push(jsName);
			arrayPath.push(php + jsName);

			if ($.browser.msie) {
				jsName = 'excanvas.js';
				array.push(jsName);
				arrayPath.push(root + 'plugin/excanvas_r3/' + jsName);
			}

			jsName = 'jquery.hoverIntent.minified.js';
			array.push(jsName);
			arrayPath.push(root + 'plugin/hoverIntent/' + jsName);

			jsName = 'jquery.bgiframe.pack.js';
			array.push(jsName);
			arrayPath.push(root + 'plugin/jdMenu-1.4.1/' + jsName);

			jsName = 'jquery.bt.min.js';
			array.push(jsName);
			arrayPath.push(root + 'plugin/bt/' + jsName);

			jsName = 'jquery.easing.1.3.js';
			array.push(jsName);
			arrayPath.push(root + 'plugin/easing/' + jsName);

			// jsName = 'tree.js';
			// array.push(jsName);
			// arrayPath.push(root + 'plugin/tree/' + jsName);

			// var php = '/keyajs/phprpc/ajs/compressed/';

			// jsName = 'studio.js';
			jsName = 'studio_jq.js';
			array.push(jsName);
			arrayPath.push(root + 'app/studio/' + jsName);

			$('script', document.head).each(function() {
						var t = $(this);

						var src = t.attr('src');
						if (src) {
							for (var i = 0; i < array.length; i++) {
								var obj = array[i];

								var index = src.indexOf(obj);

								if (index > 0) {
									// log(' 对象出现了. ' + obj);
									array.removeElement(i);
									arrayPath.removeElement(i);
								}
							}
						}
					});

			var isStart = '0';// 标识是否已经开始了演播厅
			
			for (var i in arrayPath) {
				var w = arrayPath[i];
				if (typeof(w) == 'function') {
				} else {
					// log('剩下的:' + url);
					var url = w;
					var index = url.indexOf('studio_jq.js');

					if (index > 0) {// 代表请求这个文件了
						isStart = '1';
						appendScript(url, function() {
							 
									 //log('哈哈studio.js已经加载完毕了..');
									startStudio();
								});
					} else {
						appendScript(url);
					}
				}
			}
			// 由于是异步操作,因此,
			if (isStart == '0') {
				// 启动演播厅
				// alert('启动');
				startStudio();
			}
		}
		/**
		 * 开始演播厅
		 */
		function startStudio() {
			var _t = $(this);
			_t.kyStudio(opts);

			// 弹出窗口
			openLayerWindow();
			
		}
		/**
		 * 加载一个js文件到dom中
		 */
		function appendScript(request, callback) {
			$.getScript(request, function() {
						// log("Script loaded and executed.");
						if (callback) {
							$.isFunction(callback)
									? callback.apply(this)
									: eval(callback);

						}
					});

			// var script = document.createElement('script');
			// // script.id = 'script_' + id;
			// script.src = request;
			// script.charset = "UTF-8";
			// script.defer = true;
			// script.type = 'text/javascript';
			// script.args = args;
			// script.ref = ref;
			// script.encrypt = encrypt;
			// script.callback = callback;
			// var head = document.getElementsByTagName('head');
			// if (head[0]) {
			// head[0].appendChild(script);
			// } else {
			// document.body.appendChild(script);
			// }
		}
		/**
		 * 右侧的 图片
		 */
		// function rightTopImage() {
		// log('rightTopImage........');
		// floatdivLayer("righttop");
		// floatdivLayer({
		// right : "10px",
		// top : "1px"
		// });
		// $("#studioLayer1").floatdiv({
		// right : "10px",
		// top : "100px"
		// });
		// floatdivLayer('');
		// }
		/**
		 * 右侧的 图片
		 */
		function floatdivLayer(align) {
			try {

				var id_layer = '';
				var id_close = '';
				if (opts.autoCreateLayer) {

					// log('===================================11111111111111111');
					id_layer = gid("studioLayer1");
					id_close = gid('studioLayerClose')
				} else {
					id_layer = "studioLayer1";
					id_close = 'studioLayerClose';
					// log('===================================2222222222222222222222');
				}

				var obj = $("#" + id_layer)
				if (obj) {

					// obj.floatdiv(align);
					// obj.floatdiv({
					// right : "20px",
					// top : "100px"
					// });
					//							
					// obj.floatdiv();

					// if (obj.draggable) {
					// obj.draggable({
					// // zIndex : 20,
					// ghosting : false,
					// opacity : 0.7,/* 拖拽时的透明度 */
					//
					// distance : 20, // 设置当鼠标拖动多少像素时对象才会移动
					// // scroll : false, // 设置当拖动超出整个浏览器窗口,是否滚动浏览器
					//
					// // handle : '#' + gid('studioLayer1')
					// // handle : '#studioLayer1_handle'
					// handle : '#' + id_layer
					//
					// });
					// }
					// $('#' + id_close).click(function() {
					// hiddenLayer(id_layer);
					// });

				}
			} catch (err) {
				alert(err);
			}
		}

		function initLayerB() {
			
			var theRender = gid(opts.theRenderElementId);

			// var tmp = '<input alt="#TB_inline?height=504&width=710&inlineId='

			// 510 和width=730 这两个尺寸,是用来参数 窗口方式的展现
			var tmp = '<input alt="#TB_inline?height=510&width=730&inlineId='
					+ gid('studioLayer1')
					+ '&modal=true" '
					+ ' title="" '
					+ 'id="'
					+ showDialogActId
					+ '" class="thickbox" type="button" value="Show" style="display:none" />';

			var closeId = gid('studioLayerClose');

			var html = [
					'<div id="' + gid('studioLayer1')
							+ '" class="yxybt_studio_layer">',

					'<div class="' + gid("border1") + '">',
					'  	<span class="span1">'
							+ '<a href="http://www.keyaliu.com" target="_blank"><img src="/keyaimages/studio/logo.jpg" width="105" height="27"  border="0" /></a></span>',
					'   <a href="javascript:void(0);" id="' + closeId
							+ '" style="cursor:pointer;">',
					'    <span class="span3"><img src="/keyaimages/studio/border1_img3.jpg" border="0" width="27" height="13" /></span>',
					'    <span class="span2"><img src="/keyaimages/studio/border1_img2.jpg" border="0" width="14" height="15" /></span>',
					'   </a>',
					'    <div style="height:0;overflow:hidden;"></div>',
					'    <div class="clear"></div>',
					'</div>',
					'<div class="border2"></div>',
					'<div style="height: 0; overflow: hidden;"></div>',
					'<div style="clear: both;"></div>',
					'<div style="height:0;overflow:hidden;"></div>',
					'<div class="clear"></div>',
					'<div id="' + theRender
							+ '" style="margin:0 19px 0 0;"></div>',//
					'</div>' //

			];

			var txt = html.join('');
			// log(txt);
			var parent = getS(opts.theRenderElementId);

			parent.hide();
			
			parent.html($(tmp));
			parent.append($(txt));

			// tb_init('a.thickbox, area.thickbox, input.thickbox');

			// 重置渲染器
			opts.theRenderElementId = theRender;

			getS(closeId).click(function() {
						tb_remove();// 关闭窗口
						tb_remove();//
						getS('TB_window').hide();
					});

		}
		function gid(id) {
			return studioId + '_' + id;
		}
		/**
		 * 对象页面的css进行初始化
		 */
		function initCss() {
			var imagePath = "/keyajs/jquery/plugin/tree/images/";
			var css = [

					".clear {clear: both;}",
					// 演播厅头的样式开始
					"." + gid("studio1") + "{margin:30px auto 0;}",
					".studio11{width:161px;height:40px;float:left;}",
					".studio12{width:210px;height:34px;float:right;margin:3px 0;}",

					// 演播厅头的样式结束
					"." + gid("studio2")
							+ " {margin: 0 auto;background:#D1D7E2;}",
					"."
							+ gid("studio2_main")
							+ " {overflow: hidden;background:#D1D7E2;margin: 0 auto 10px;}",
					"." + gid("studio2_main1")
							+ " {float: left;overflow-x: hidden;}",
					"." + gid("studio2_main2")
							+ " {background:#c8c8c8;;float: right;}",
					"." + gid("studio2_main2_movie") + " {background:#F2EFE6;}",
					"."
							+ gid("studio2_tag")
							+ " {line-height:1.6;overflow:hidden;font-size:12px;color:#000;background:url(/keyaimages/studio/studio2_tag_bg.jpg) left top repeat-x;border-top:1px solid #404040;}",
					"."
							+ gid("studio2_tag")
							+ "a:link,a:visited{color:#00f;text-decoration:underline;}",
					"." + gid("studio2_tag") + " a:hover{color:#00f;} ",

					".flash {position: relative;}",
					".studio_watermark {width:400px;position:absolute;overflow: hidden;left: 0px;height: 80px;text-align:left;font-weight: bold;font-size:18px;background: #000;color: #fff;filter: Alpha(opacity =70);-moz-opacity: 0.8;background-color: transparent;}",

					// --------------------------
					// ".notToRead {color: #001201;background: url(" + imagePath
					// + "cross.png) 0 0 no-repeat transparent;}",

					".studio2_main2_text{height:95%;width:96%;background:#f2efe6;font-size:12px;position:relative;overflow:auto}",
					".studio2_main2_text1{color:#999;padding-bottom:5px;border-bottom:1px solid #999;}",
					".studio2_main2_text2{color:#666;margin:10px 0 0 0;}",
					".studio2_main2_text3{color:#000;margin:15px 0 0 0;line-height:1.6;}",

					".simpleTree {margin: 0;padding: 0;}",
					".simpleTree li {list-style: none;margin: 0;padding: 0 0 0 34px;line-height: 14px;}",
					".simpleTree li div {display:inline;clear:left;font:12px Verdana, Arial, \"宋体\", serif;}",
					".simpleTree ul {margin: 0;padding: 0;list-style-type: none;}",
					// ".simpleTree .root { margin-left: -16px;background: url("
					// + imagePath + "root.gif) no-repeat 16px 0;}",

					".simpleTree .root { margin-left: -32px;}",

					// ".simpleTree .line {margin: 0 0 0 -16px;padding:
					// 0;line-height: 3px;height: 3px;font-size: 3px;background:
					// url("
					// + imagePath
					// + "line_bg.gif) 0 0 no-repeat transparent;}",

					".simpleTree .line {margin: 0 0 0 -16px;padding: 0;line-height: 3px;height: 3px;font-size: 3px;}",

					".simpleTree .line-last {margin: 0 0 0 -16px;padding: 0;line-height: 3px;height: 3px;font-size: 3px;background: url("
							+ imagePath
							+ "spacer.gif) 0 0 no-repeat transparent;}",
					".simpleTree .line-over {margin: 0 0 0 -16px;padding: 0;line-height: 3px;height: 3px;font-size: 3px;background: url("
							+ imagePath
							+ "line_bg_over.gif) 0 0 no-repeat transparent;}",
					".simpleTree .line-over-last {margin: 0 0 0 -16px;padding: 0;line-height: 3px;height: 3px;font-size: 3px;background:url("
							+ imagePath
							+ "line_bg_over_last.gif) 0 0 no-repeat transparent;}",
					".simpleTree .folder-open {margin-left: -16px;background: url("
							+ imagePath + "collapsable.gif) 0 -2px no-repeat;}",
					".simpleTree .folder-open-last {margin-left: -16px;background: url("
							+ imagePath
							+ "collapsable-last.gif) 0 -2px no-repeat;}",
					".simpleTree .folder-close { margin-left: -16px;background: url("
							+ imagePath + "expandable.gif) 0 -2px no-repeat;}",
					".simpleTree .folder-close-last {margin-left: -16px;background: url("
							+ imagePath
							+ "expandable-last.gif) 0 -2px no-repeat;}",
					// ".simpleTree .doc {margin-left: -16px;background: url("
					// + imagePath + "leaf.gif) 0 -1px no-repeat;}",
					".simpleTree .doc {margin-left: -16px;}",
					".simpleTree .doc-last {margin-left: -16px;background: url("
							+ imagePath + "leaf-last.gif) 0 -1px no-repeat;}",
					".simpleTree .trigger {display:inline;margin-left:-32px;width:28px;height:11px;border-style:none;cursor: pointer;}",
					/** width:28px;height:11px;border-style:none;没有这两句就无边框奇怪! */
					".simpleTree .text {cursor: default;}",
					".simpleTree .active {cursor: default;background-color: #F7BE77;padding: 0px 2px;border: 1px dashed #444;}"

			];

			var txt = css.join('');

			if (opts.showByLayer) {// 弹出层的 样式

				var b1 = gid("border1");
				var css1 = [

						"."
								+ gid("border")
								+ "{width:710px;height:495px;border:1px solid #606060;margin:0 auto;overflow:hidden;}",
						"."
								+ b1
								+ "{width:710px;height:39px;border-bottom:1px solid #404040;background:url(/keyaimages/studio/border1_bg.jpg) left top repeat-x;}",
						"."
								+ b1
								+ " span.span1{float:left;margin:6px 0 6px 12px;}",
						"."
								+ b1
								+ " span.span3{float:right;margin:13px 12px 13px 0;}",
						"."
								+ b1
								+ " span.span2{float:right;margin:12px 6px 12px 0;}"

				// ".yxybt_studio_layer {position: absolute;width:
				// 710px;background-color: #ccc;border: 1px solid #ccc;z-index:
				// 5;}",

				// ".yxybt_studio_layer_handle {background-color: #ccc;padding:
				// 2px;text-align: left;font-weight: bold;color: #ccc;}",

				// ".yxybt_studio_layer_close {float: right;text-decoration:
				// none;color: #FFFFFF;}"
				];
				txt += css1.join('');
			}

			if (isdebug()) {
				log("初始化了 演播厅的 css ..");

			}
			var txt1 = "<style type=\"text/css\">" + txt + "</style>";
			$(document.body).append(txt1);
		}
		/**
		 * 打开窗体
		 */
		function openLayerWindow() {
			
			if (opts.showByLayer) {
				if (opts.autoCreateLayer) {

					// var k = gid('isInitTbinit_');
					// var isInitTbinit = cacheGet(k);
					// if (ky.util.isEmpty(isInitTbinit)) {
					if (false) {
						log("xxxxxxx=================================开始初始化 tbinit  "
								+ showDialogActId);
					}
					// 重新给按钮注册tbinit
					tb_init("#" + showDialogActId);
					// cachePut(k, '1');

					// [0].scrollTop = 1000;

					// 然后触发按钮的点击事件

					// }
					if (isdebug()) {
						log('进行点击 按钮产生事件的操作');
					}
					if (ky.util.isEmpty(showDialogActId)) {
						alert('showDialogActId不存在啊');
					} else {
						$("#" + showDialogActId).click();
					}
				}
			}
		}
		// -------------------------

		function beginLoadPlugin() {

			_initCache();

			showDialogActId = gid('_show_studio_dialog');// 触发事件的按钮
			if (isdebug()) {
				log('studioId  2 =' + studioId);
			}
			var _studio_js_is_init = cacheGet('_studio_js_is_init' + studioId);
			initToLayer();
			if (ky.util.isEmpty(_studio_js_is_init)) {

				cachePut('_studio_js_is_init' + studioId, 100);
				initCss();
				initJs();

			} else {
				// opts.studioId = studioId;
				if (isdebug()) {
					log('哈哈studio.js已经加载过了,直接初始化演播厅');
				}
				var _t = $(this);
				_t.kyStudio(opts);

				// 弹出窗口
				openLayerWindow();
			}
		}

		function initToLayer() {
			// 先初始化 layer,因为如果是layer,内部需要创建 窗体!
			if (opts.showByLayer) {
				if (opts.autoCreateLayer) {
					initLayerB();
				}
				// 由于floatdiv 在ie6下面,把layer下面的html中的
				// flash也给进行了滚动处理,所以不再采用floatdiv了
				// rightTopImage();

				// TODO 可以考虑 是否进行mode选项,
				// model=true 就没有 close按钮,
				// 如果=false:需要在 button 的title上面来设置title:这个title就是 弹出窗口的标题了
				// <input
				// alt="#TB_inline?height=440&width=720&inlineId=studioLayer1"
				// title = '<img src="/keyaimages/studio/border1_img1.jpg"
				// width="105" height="27" />'
				// id="button_show" class="thickbox" type="button"
				// value="Show"
				// style="display: none"/>

			}

		}
		beginLoadPlugin();

	});
}
/**
 * 供外部的调用
 */
jQuery.extend(jQuery.fn.kyStudioPlugin, {

			'changePcode' : function(changePcode) {
				// alert('nihao');
				kyStudio.haha();
				// $.kyStudioPlugin.haha();
			}

		});
