﻿// JavaScript Document
//Jquery Center Plugin
(function($){
	$.fn.center=function(f){
		return this.each(function(){
			//设定父元素的position
			if($(this).parent().css("position")=="static"){
				$(this).parent().css("position","relative");
			};
			
			//设定元素的位置方式
			$(this).css("position","absolute");
			
			//如果参数为空,或者参数为水平居中
			if(!f || f == "horizontal" || f == "h" || f == "x") {
				var left=($(this).parent().width()-$(this).width())/2;
					left=(left<0)?0:left;
					$(this).css("left",left+"px");
			}
			if(!f || f == "vertical"|| f == "v" || f == "y") {
				var top=($(this).parent().height()-$(this).height())/2;
					top=(left<0)?0:top;
					$(this).css("top",top+"px");	
			}
			
		});
	}
})(jQuery);

//Jquery 移动和Resize插件扩展
(function($){
	$.fn.drag=function(r){$.jqDnR.init(this,r,'d'); return this;};
	$.fn.resize=function(r){$.jqDnR.init(this,r,'r'); return this;};
	$.jqDnR={
		init:function(w,r,t){ r=(r)?$(r,w):w;
			r.bind('mousedown',{w:w,t:t},function(e){ var h=e.data; var w=h.w;
			hash=$.extend({oX:f(w,'left'),oY:f(w,'top'),oW:f(w,'width'),oH:f(w,'height'),pX:e.pageX,pY:e.pageY},h);
			$().mousemove($.jqDnR.drag).mouseup($.jqDnR.stop);
			return false;});
		},
		drag:function(e) {var h=hash; var w=h.w[0];
			if(h.t == 'd') h.w.css({left:h.oX + e.pageX - h.pX,top:h.oY + e.pageY - h.pY});
			else h.w.css({width:Math.max(e.pageX - h.pX + h.oW,0),height:Math.max(e.pageY - h.pY + h.oH,0)});
			return false;},
		stop:function(){var j=$.jqDnR;$().unbind('mousemove',j.drag).unbind('mouseup',j.stop);},
		h:false};
		var hash=$.jqDnR.h;
		var f=function(w,t){return parseInt(w.css(t)) || 0};
})(jQuery);

//图像自动缩放
(function($){
	$.fn.ImageAutoSize = function(width,height)
	{
		$("img",this).each(function()
		{
			var image = $(this);
			if(image.width()>width)
			{
				image.width(width);
				image.height(width/image.width()*image.height());
			}
			if(image.height()>height)
			{
				image.height(height);
				image.width(height/image.height()*image.width());
			}
		});
	}
})(jQuery);
//切换class
(function($){
	$.fn.SwapClass = function(c1,c2) {
		return this.each(function(){
			if($(this).attr("class").indexOf(c1)>=0){
				$(this).removeClass(c1);
				$(this).addClass(c2);
			}else if($(this).attr("class").indexOf(c2)>=0){
				$(this).removeClass(c2);
				$(this).addClass(c1);
			}				  
		});
	};
})(jQuery);

(function($){
	$.fn.bgIframe = $.fn.bgiframe = function(s) {
		// This is only for IE6
		if ( $.browser.msie && /6.0/.test(navigator.userAgent) ) {
			s = $.extend({
				top     : 'auto', // auto == .currentStyle.borderTopWidth
				left    : 'auto', // auto == .currentStyle.borderLeftWidth
				width   : 'auto', // auto == offsetWidth
				height  : 'auto', // auto == offsetHeight
				opacity : true,
				src     : 'javascript:false;'
			}, s || {});
			var prop = function(n){return n&&n.constructor==Number?n+'px':n;},
				html = '<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+
						   'style="display:block;position:absolute;z-index:-1;'+
							   (s.opacity !== false?'filter:Alpha(Opacity=\'0\');':'')+
							   'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+
							   'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+
							   'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+
							   'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+
						'"/>';
			return this.each(function() {
				if ( $('> iframe.bgiframe', this).length == 0 )
					this.insertBefore( document.createElement(html), this.firstChild );
			});
		}
		return this;
	};
})(jQuery);

//meta
(function($) {
	$.extend({
		metadata : {
			defaults : {
				type: 'class',
				name: 'metadata',
				cre: /({.*})/,
				single: 'metadata'
			},
			setType: function( type, name ){
				this.defaults.type = type;
				this.defaults.name = name;
			},
			get: function( elem, opts ){
				var settings = $.extend({},this.defaults,opts);
				// check for empty string in single property
				if ( !settings.single.length ) settings.single = 'metadata';
				
				var data = $.data(elem, settings.single);
				// returned cached data if it already exists
				if ( data ) return data;
				
				data = "{}";
				
				if ( settings.type == "class" ) {
					var m = settings.cre.exec( elem.className );
					if ( m )
						data = m[1];
				} else if ( settings.type == "elem" ) {
					if( !elem.getElementsByTagName )
						return undefined;
					var e = elem.getElementsByTagName(settings.name);
					if ( e.length )
						data = $.trim(e[0].innerHTML);
				} else if ( elem.getAttribute != undefined ) {
					var attr = elem.getAttribute( settings.name );
					if ( attr )
						data = attr;
				}
				
				if ( data.indexOf( '{' ) <0 )
				data = "{" + data + "}";
				
				data = eval("(" + data + ")");
				
				$.data( elem, settings.single, data );
				return data;
			}
		}
	});
	$.fn.metadata = function( opts ){
		return $.metadata.get( this[0], opts );
	};
})(jQuery);

//example 插件
(function(A){A.fn.example=function(D,C){var B=A.extend({},A.fn.example.defaults,C,{example_text:D});var E=A.isFunction(B.example_text);if(!A.fn.example.bound_class_names[B.class_name]){A(window).unload(function(){A("."+B.class_name).val("")});A("form").submit(function(){A(this).find("."+B.class_name).val("")});A.fn.example.bound_class_names[B.class_name]=true}return this.each(function(){var G=A(this);var H=A.metadata?A.extend({},B,G.metadata()):B;if(A.browser.msie&&!G.attr("defaultValue")&&(E?G.val()!="":G.val()==H.example_text)){G.val("")}if(G.val()==""){G.addClass(B.class_name);G.val(E?H.example_text.call(this):H.example_text)}if(B.hide_label){var F=A("label[@for="+G.attr("id")+"]");F.next("br").hide();F.hide()}G.focus(function(){if(A(this).is("."+B.class_name)){A(this).val("");A(this).removeClass(B.class_name)}});G.blur(function(){if(A(this).val()==""){A(this).addClass(B.class_name);A(this).val(E?H.example_text.call(this):H.example_text)}})})};A.fn.example.defaults={example_text:"",class_name:"example",hide_label:false};A.fn.example.bound_class_names=[]
})(jQuery);

//textlimit
//jQuery("Textarea").textlimit('span.counter',256)
jQuery.fn.textlimit=function(counter_el, thelimit, speed) {
	var charDelSpeed = speed || 15;
	var toggleCharDel = speed != -1;
	var toggleTrim = true;
	
	var that = this[0];
	updateCounter();
	
	function updateCounter(){
		jQuery(counter_el).text(thelimit - that.value.length);
	};
	
	this.keypress (function(e){ if( this.value.length >= thelimit && e.charCode != '0' ) e.preventDefault() })
	.keyup (function(e){
		updateCounter();
		if( this.value.length >= thelimit && toggleTrim ){
			if(toggleCharDel){
				// first, trim the text a bit so the char trimming won't take forever
				that.value = that.value.substr(0,thelimit+100);
				var init = setInterval
					( 
						function(){ 
							if( that.value.length <= thelimit){ init = clearInterval(init); updateCounter() }
							else{ that.value = that.value.substring(0,that.value.length-1); jQuery(counter_el).text('trimming...  '+(thelimit - that.value.length)); };
						} ,charDelSpeed 
					);
			}
			else this.value = that.value.substr(0,thelimit);
		}
	});
	
};

/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 9/11/2008
 * @author Ariel Flesler
 * @version 1.4
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(h){var m=h.scrollTo=function(b,c,g){h(window).scrollTo(b,c,g)};m.defaults={axis:'y',duration:1};m.window=function(b){return h(window).scrollable()};h.fn.scrollable=function(){return this.map(function(){var b=this.parentWindow||this.defaultView,c=this.nodeName=='#document'?b.frameElement||b:this,g=c.contentDocument||(c.contentWindow||c).document,i=c.setInterval;return c.nodeName=='IFRAME'||i&&h.browser.safari?g.body:i?g.documentElement:this})};h.fn.scrollTo=function(r,j,a){if(typeof j=='object'){a=j;j=0}if(typeof a=='function')a={onAfter:a};a=h.extend({},m.defaults,a);j=j||a.speed||a.duration;a.queue=a.queue&&a.axis.length>1;if(a.queue)j/=2;a.offset=n(a.offset);a.over=n(a.over);return this.scrollable().each(function(){var k=this,o=h(k),d=r,l,e={},p=o.is('html,body');switch(typeof d){case'number':case'string':if(/^([+-]=)?\d+(px)?$/.test(d)){d=n(d);break}d=h(d,this);case'object':if(d.is||d.style)l=(d=h(d)).offset()}h.each(a.axis.split(''),function(b,c){var g=c=='x'?'Left':'Top',i=g.toLowerCase(),f='scroll'+g,s=k[f],t=c=='x'?'Width':'Height',v=t.toLowerCase();if(l){e[f]=l[i]+(p?0:s-o.offset()[i]);if(a.margin){e[f]-=parseInt(d.css('margin'+g))||0;e[f]-=parseInt(d.css('border'+g+'Width'))||0}e[f]+=a.offset[i]||0;if(a.over[i])e[f]+=d[v]()*a.over[i]}else e[f]=d[i];if(/^\d+$/.test(e[f]))e[f]=e[f]<=0?0:Math.min(e[f],u(t));if(!b&&a.queue){if(s!=e[f])q(a.onAfterFirst);delete e[f]}});q(a.onAfter);function q(b){o.animate(e,j,a.easing,b&&function(){b.call(this,r,a)})};function u(b){var c='scroll'+b,g=k.ownerDocument;return p?Math.max(g.documentElement[c],g.body[c]):k[c]}}).end()};function n(b){return typeof b=='object'?b:{top:b,left:b}}})(jQuery);

