﻿// JavaScript Document
(function($) {
/*-- code start ---------------------------------------------------------------------------------------------------------*/
		  
		  
	$.fn.edhPanel = function(cfg){
		/*HTML:
		<div class="edhUI_panel">
			<div class="cap">Caption</div>
			<div class="tbar"></div> <!--可选-->
			<div class="box">content</div>
			<div class="bbar"></div> <!--可选-->
		</div>
		*/
		/*配置参数*/
		cfg=$.extend({
			 collapsible:true	/*是否可收缩*/
			,collapsed:false	/*初始是否收缩*/
		},cfg||{});
		
		this.each(
		function(i){
			var _UI_=this;
			/*公开属性*/
			_UI_.p={};
			/*公开方法*/
			_UI_.f={};
			/*公开事件*/
			_UI_.e={};
			
			if (cfg.collapsible){
				if (cfg.collapsed) $(".box:first,.tbar:first,.bbar:first",_UI_).hide();
				
				var _click=function(e){
					var $in=$(this).siblings(".box:first,.tbar:first,.bbar:first");
					$in.slideToggle("fast");
				};
				
				$(".cap:first",_UI_).unbind("click",_click).click(_click);
			}
		});
		return this;   
	};
	
	
	
	$.fn.edhTabPage = function(cfg){
		/*HTML:
		<div class="edhUI_tabPage">
			<div class="cap">
				<span><label>Caption_1</label></span>
				<span><label>Caption_2</label></span>
			</div>
			<div class="box">  <div>Content_1</div><div>Content_2</div> </div>
			<div class="bbar"></div> <!--可选-->
		</div>
		*/
		/*配置参数*/
		cfg=$.extend({
			 activeIndex:0	/*初始的激活页id*/
			,beforeChange:null	/*页改变前*/
			,afterChange:null	/*页改变后*/
		},cfg||{});
		
		this.each(
		function(i){
			var _UI_=this;
			/*公开属性: activeIndex */
			_UI_.p={activeIndex:cfg.activeIndex};
			/*公开方法: showActive*/
			_UI_.f={};
			/*公开事件: beforeChange,afterChange*/
			_UI_.e={
				 beforeChange:cfg.beforeChange	/* toId,fromId */
				,afterChange:cfg.afterChange	/* toId */
			};
			
			var _showActive=function(id){
				
				//激活e.beforeChange
				if ($.isFunction(_UI_.e.beforeChange)){
					if (!_UI_.e.beforeChange(id,_UI_.p.activeIndex)) return;
				}
				
				$(".box:first>div",_UI_).hide().eq(id).show();
				$(".cap:first>span",_UI_).removeClass("active").eq(id).addClass("active");
				_UI_.p.activeIndex=id;
				
				//激活e.afterChange
				if ($.isFunction(_UI_.e.afterChange)) _UI_.e.afterChange(id)
			}; _UI_.f.showActive=_showActive;
			
			_showActive(cfg.activeIndex);
			
			
			var _click=function(e){
				var id=$(".cap:first>span",_UI_).index(this);
				_showActive(id);
			};
			
			$(".cap>span",_UI_).unbind("click",_click).click(_click);
		});
		
		return this;   
	};
	
	$.fn.edhPageCtrl = function(cfg){
		/*
		<div class="edhUI_pageCtrl">
			<span class="pageInfo">
				<label></label>
				<input class="pageSize" type="text" />
				<label></label>
			</span>
			<a class="pageFirst"></a>
			<a class="pagePrev"></a>
			<label class="pageNum"></label>
			<label class="pageCount"></label>
			<a class="pageNext"></a>
			<a class="pageLast"></a>
			<span class="pageTo">
				<label></label><select></select><label></label>
			</span>
		</div>
		*/
		/*配置参数*/
		cfg=$.extend({
			 txtLabels:	['第一页','上一页','下一页','最后页']
			,txtInfos:	['每页','条']
			,txtJumps:	['到','页']
			,pageCount:1, pageNum:1 ,pageSize:10
			,hideParent:false	/*当没有数据时,是不是同时隐藏父元素*/
			,afterChange:null	/*页改变后*/
		},cfg||{});
		
		this.each(
		function(i){
			var _UI_=this;
			/*公开属性: activeIndex */
			_UI_.p={
				 pageCount	:cfg.pageCount
				,pageNum	:cfg.pageNum
				,pageSize	:cfg.pageSize
			};
			/*公开方法: goPage,hide,show,refresh,paint*/
			_UI_.f={
				 goPage	:null
				,hide		:null	,show		:null
				,refresh	:null	/*需要刷新控件上的分页信息时，调用这个*/
				,paint		:null	/*需要重绘控件时，调用这个*/
			};
			/*公开事件: afterChange*/
			_UI_.e={
				afterChange:cfg.afterChange	/* toPage */
			};
			
			var _goPage=function(id){
				//-1表示最后一页
				var id=parseInt(id);
				if (isNaN(id)) id=1;	//return false;
				if (id<1&&id!=-1) id=1;
				if (id>_UI_.p.pageCount) id=-1;
				//激活e.afterChange
				if ($.isFunction(_UI_.e.afterChange)) _UI_.e.afterChange(id,_UI_.p.pageSize)
			}; _UI_.f.goPage=_goPage;
		
			var _paint=function(){
				$("a.pageFirst",_UI_).text(cfg.txtLabels[0]);
				$("a.pagePrev",_UI_).text(cfg.txtLabels[1]);
				$("a.pageNext",_UI_).text(cfg.txtLabels[2]);
				$("a.pageLast",_UI_).text(cfg.txtLabels[3]);
				$("span.pageInfo",_UI_)
					.find("label:eq(0)").text(cfg.txtInfos[0]).end()
					.find("label:eq(1)").text(cfg.txtInfos[1]).end();
				$(".pageTo",_UI_)
					.find("label:eq(0)").text(cfg.txtJumps[0]).end()
					.find("label:eq(1)").text(cfg.txtJumps[1]).end();
				_refresh();
			}; _UI_.f.paint=_paint;
			
			var _refresh=function(){
				$("span.pageInfo input:text.pageSize",_UI_).val(_UI_.p.pageSize)
				$("label.pageNum",_UI_).text(_UI_.p.pageNum);
				$("label.pageCount",_UI_).text(" / "+_UI_.p.pageCount);
				
				var $sel=$(".pageTo select",_UI_)
				$sel.find("option").remove();
				for(var i=1;i<=_UI_.p.pageCount;i++){
					$("<option></option")
					.val(i).text(i).attr("selected",i==_UI_.p.pageNum)
					.appendTo($sel);
				}				
			}; _UI_.f.refresh=_refresh;
			
			var _bind=function(){
				$("a.pageFirst",_UI_).unbind("click")
					.click(function(e){_goPage(1); return false;});
				$("a.pagePrev",_UI_).unbind("click")
					.click(function(e){_goPage(_UI_.p.pageNum-1); return false;});
				$("a.pageNext",_UI_).unbind("click")
					.click(function(e){_goPage(_UI_.p.pageNum+1); return false;});
				$("a.pageLast",_UI_).unbind("click")
					.click(function(e){_goPage(-1); return false;});
				$(".pageTo select",_UI_).unbind("change")
					.change(function(e){_goPage($(this).val());});
				$("span.pageInfo input:text.pageSize",_UI_).unbind("keypress")
					.keypress(function(e){
						if(e.keyCode==13){
							var s=parseInt($(this).val());
							if (isNaN(s)) s=-1;
							if (s>0){
								_UI_.p.pageSize=s; _goPage(1);
							}else{
								$(this).val(_UI_.p.pageSize);
							}
						}
					});
			};
			
			var _hide=function(){
				$(_UI_).hide(); if (cfg.hideParent) $(_UI_).parent().hide();
			}; _UI_.f.hide=_hide;
			
			var _show=function(){
				$(_UI_).parent().show(); $(_UI_).show();
			}; _UI_.f.show=_show;

			_bind();
			_paint();
		});
		
		return this;   
	};



	$.fn.edhPicShow = function(cfg){
		/*HTML: 动态图片框
		<div class="edhUI_picShow1 edhUI_picShow">
			<!---图片显示层--->
			<div class="imgs">
				<!---<img src="/v2img/temp/02.jpg"/><img src="/v2img/temp/01.jpg"/>--->
			</div>
			<!---控制--->
			<div class="ctrl">
				<table class="info" cellpadding="0" cellspacing="0">
				<tr>
				<!---文字说明--->
				<td class="text" align="left" valign="middle">
					<div class="title">大家好</div>
				</td>
				<!---按钮--->
				<td class="btns" align="left" valign="bottom">
					<!---<a href="#"><div>1</div></a><a href="#"><div>9</div></a>--->
				</td></tr></table>
			</div>
		</div>
		*/
		/*配置参数*/
		cfg=$.extend({
			 file	:[]		/*图片文件数组,元素结构 {src:'',page:'#',target:'_blank',title:'[无标题]'} */
			 ,wait	:5000	/*默认5秒换一幅*/
			 ,speed	:1000	/*默认1秒完成交换动画*/
		},cfg||{});

		this.each(
		function(i){
			var _UI_=this;
			/*公开属性*/
			_UI_.p={};
			/*公开方法*/
			_UI_.f={};
			/*公开事件*/
			_UI_.e={};
			
			_UI_.loopId=0; _UI_.loopVal=null; 
			
			var _init=function(){
				$(_UI_).addClass("edhUI_picShow");
				var A_Tag;
				for(var i=0; i<cfg.file.length; i++){
					cfg.file[i]=$.extend({src:'',page:'#',target:'_blank',title:'[无标题]'},cfg.file[i]||{});
					A_Tag='<a href="'+cfg.file[i].page+'" title="'+cfg.file[i].title+'" target="'+cfg.file[i].target+'">';
					
					$(_UI_).find(".imgs").append($(A_Tag+'<img src="'+cfg.file[i].src+'" title="'+cfg.file[i].title+'" border="0"/></a>').hide());
					$(_UI_).find(".text").append($('<div class="title">'+cfg.file[i].title+'</div>').hide());
					$(_UI_).find(".btns").append(
						$(A_Tag+'<div>'+(i+1)+'</div></a>')
						.mouseover(function(e){
							_stop();
							_goto(parseInt($(this).text())-1);	
						})
						.mouseout(function(e){
							_run();			   
						})
					);
					
				}
				_goto(0); _run();
			};
			
			var _run=function(){
				_stop();
				_UI_.loopVal=window.setInterval(function(){_goto(_UI_.loopId+1);},cfg.wait);
			};
			var _stop=function(){
				try{
					window.clearInterval(_UI_.loopVal);
				}catch(e){}
				_UI_.loopVal=null;
			};
			var _goto=function(id){
				if (id>=cfg.file.length) id=0;
				var $img=$(_UI_).find(".imgs a");
				var $title=$(_UI_).find(".text div.title");
				var $btn=$(_UI_).find(".btns a");
				
				$img.not(":eq("+id+")").fadeOut(parseInt(cfg.speed/1.5));
				$img.filter(":eq("+id+")").fadeIn(cfg.speed);

				$title.not(":eq("+id+")").hide();
				$title.filter(":eq("+id+")").fadeIn(cfg.speed/2);
							
				$btn.not(":eq("+id+")").find(">div").removeClass("active");
				$btn.filter(":eq("+id+")").find(">div").addClass("active");
				_UI_.loopId=id;
			};
			_init();

		});
		
		
		return this;   
	};


/*-- code end ---------------------------------------------------------------------------------------------------------*/
})(jQuery);	