﻿/**
 * jonche.extend
 * @requires jQuery v1.4.2
 *
 * @author Wayjet Wong
 * @site http://www.jonche.com
 * @licence LGPL(http://www.opensource.org/licenses/lgpl-license.php)
 *
 * @Version: 1.0.0 beta (build 101119)
 */

/*
获取对象在数组中的下标值
@value	匹配的对象

例子:
var arr = ['aa','bb','cc'];
var index = arr.indexOf('bb');
*/
[].indexOf || (Array.prototype.indexOf = function (value) {
	for (var i = this.length; i-- && this[i] !== value; );
	return i;
});

/*
根据条件搜索第一个匹配到的对象,如果匹配失败返回null
@option 搜索的参数，可以是一个函数方法，也可以是一个选项对象，选项对象格式为:
{ predicate:函数方法名, start:搜索的起始位置 }

例子:
var str = arr.first(function(item){
	return item=='cc';
});
var str2 = arr.first({
	start: 1,			//开始搜索的下标
	predicate: function(item){
		return item=='cc';
	}
});
alert([str,str2].join(','));
*/
[].first || (Array.prototype.first = function (options) {
	var settings = { predicate: null, start: 0 };
	if ($.isFunction(options)) {
		$.extend(settings, { predicate: options });
	} else {
		$.extend(settings, options);
	}

	if (!settings.start) settings.start = 0;

	for (var i = settings.start; i < this.length; ++i) {
		var item = this[i];
		if (settings.predicate(item)) return item;
	}
	return null;
});

[ ].where || (Array.prototype.where = function (options) {
	var settings = { predicate: null, start: 0 };
	if ($.isFunction(options)) {
		$.extend(settings, { predicate: options });
	} else {
		$.extend(settings, options);
	}

	if (!settings.start) settings.start = 0;
	var result = [];
	for (var i = settings.start; i < this.length; ++i) {
		var item = this[i];
		if (settings.predicate(item)) result.push(item);
	}
	return result;
});

(function ($) {
	$.extend({
		/*
		产生GUID
		@split	分割符号,默认使用"-"来分割
		*/
		jguid: function (split) {
			function s4() {
				return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
			}
			if (split == null) split = "-";
			return (s4() + s4() + split + s4() + split + s4() + split + s4() + split + s4() + s4() + s4());
		},
		/*
		验证传入的字符串是否是电子邮箱格式
		@value 要验证的字符串
		*/
		isEmail: function (value) {
			var re = /^(?:[\w\!\#\$\%\&\'\*\+\-\\\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\\\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/
			return re.test(value);
		},
		/*
		 * 验证传入的字符串是否是ISO日期格式
		 * @value 要验证的字符串
		 */
		isIsoDate: function (value) {
			var mt = value.match(/^(\d{4})\-(\d{1,2})-(\d{1,2})$/);
			if(!mt) return false;
			try {
				var dt = new Date(RegExp.$1, RegExp.$2, RegExp.$3);
				return true;
			} catch (e) {
				return false;
			}
		},
		ajaxUpload: function (fromfile, tourl, callback, onError) { //style="visibility:hidden" 
			var uid = new Date().getTime(), idIO = 'jUploadFrame' + uid;
			var jIO = $('<iframe name="' + idIO + '" class="xheHideArea" style="display:none" />').appendTo('body');
			var jForm = $('<form action="' + tourl + '" target="' + idIO + '" method="post" enctype="multipart/form-data" style="display:none"></form>').appendTo('body');
			var jOldFile = $(fromfile);
			alert(jOldFile.length);
			var jNewFile = jOldFile.clone().attr('disabled', 'true');
			alert('abc');
			jOldFile.before(jNewFile).appendTo(jForm);
			jForm.submit();
			alert('submit');
			jIO.load(function () {
				setTimeout(function () {
					jNewFile.before(jOldFile).remove();
					jIO.remove(); jForm.remove();
				}, 100);
				var strText = $(jIO[0].contentWindow.document.body).text(), data = Object;
				try { data = eval('(' + strText + ')'); }
				catch (ex) {
					if (onError) onError(ex);
					else alert(ex);
					return;
				}
				if (callback) callback(data);
			});
		},
		postTo: function (opt) {
			if (!opt.type) opt.type = 'POST';
			if (!opt.dataType) opt.dataType = 'json';
			if (!opt.async) opt.async = false;
			$.ajax(opt);
		},
		 /*
		 * 获取页面的宽度
		 */
		pageWidth: function (){ 
			if($.browser.msie){ 
				return document.compatMode == "CSS1Compat"
					? document.documentElement.clientWidth 
					: document.body.clientWidth; 
			}else{
				return self.innerWidth; 
			}
		},
        /*
		 * 获取页面的高度
		 */
        pageHeight: function () {
			if ($.browser.msie) {
				return document.compatMode == "CSS1Compat"
					? document.documentElement.clientHeight 
					: document.body.clientHeight; 
			}else{
				return self.innerHeight;
			} 
		},
		getTimestamp: function(){
			d = new Date();
			return d.toGMTString();
		}
	});

	/*
		序列化对象
		参考DEMO：http://tobiascohen.com/files/stackoverflow/jquery-form-serializeObject.html
		示例：$.toJSON($('form').serializeObject())
	*/
	$.fn.serializeObject = function() {
		var o = {};
		var a = this.serializeArray();
		$.each(a, function() {
			if (o[this.name]) {
				if (!o[this.name].push) o[this.name] = [o[this.name]];
				o[this.name].push(this.value || ''); 
			} else {
				o[this.name] = this.value || '';
			}
		});
		return o;
	};
	/*
	调用示例
		$('.tabbar').tabPage({event:'click'});
	*/
	$.fn.tabPage = function (option) {
		function switchTab($sender) {
			var targetId = $($sender).attr('href').substr(1),
				$holder = $($($sender).attr('href')).parent();

			$sender.closest('ul').find('>li>a').removeClass('cur');
			$holder.children().each(function () {
				if ($(this).attr('id') != targetId) {
					$(this).hide();
				} else {
					$sender.addClass('cur');
					$(this).show();
				}
			});
		}

		var config = { event: 'click' };
		config = $.extend(config, option);
		return this.each(function () {
			var $single = $('>li a:first', this),
				$holder = $($single.attr('href')).parent();
			$('>li a', this).bind(config.event, function (e) {
				e = e || event;
				if (e.isDefaultPrevented()) return; //判断事件是否被取消
				e.preventDefault();
				switchTab($(this));
			});
			$single.trigger(config.event);
			/*
			$(this).bind('switch', function (event,$cur) {
				alert($cur);
				//switchTab($cur); 
			});*/
		})
	};

	/*
	调用示例
		$('.ly_gallery .img_wrapper img').adjustImage({ maxWidth: 310, maxHeight: 310 });
	*/
	$.fn.adjustImage = function (option) {
		var config = { maxWidth: 100, maxHeight: 100, calcMagrin: false };
		config = $.extend(config, option);

		return this.each(function () {
			//display:table-cell; vertical-align:middle; /*非IE内核,居中对齐*/
			var $wrap = $(this).parent(),
				oWidth = $(this).get(0).width || 0,
				oHeight = $(this).get(0).height || 0,
				scaleW = config.maxWidth / oWidth,
				scaleH = config.maxHeight / oHeight;
			var minScale = Math.min(scaleW, scaleH);
			if (oWidth == 0 || oHeight == 0) {
				oWidth = config.maxWidth;
				oHeight = config.maxHeight;
			}

			//计算缩放后的尺寸
			var w = Math.min(config.maxWidth, Math.ceil(oWidth * minScale));
			var h = Math.min(config.maxHeight, Math.ceil(oHeight * minScale));
			/*var reg = /(\d+)\s*px/;
			
			var padding = {
			left: $wrap.css('paddingLeft'),
			top: $wrap.css('paddingTop'),
			right: $wrap.css('paddingRight'),
			bottom: $wrap.css('paddingBottom')
			}*/
			/*$wrap.css({
				paddingLeft: (config.maxWidth - w) / 2 + 'px',
				paddingRight: (config.maxWidth - w) / 2 + 'px',
				paddingTop: (config.maxHeight - h) / 2 + 'px',
				paddingBottom: (config.maxHeight - h) / 2 + 'px'
			});
			$(this).css({
				width: w + 'px',
				height: h + 'px'
			});*/

			$wrap.css({ 'position': 'relative' });
			$(this).css({
				width: w + 'px',
				height: h + 'px',
				'position': 'absolute',
				left: (config.maxWidth - w) / 2 + 'px',
				top: (config.maxHeight - h) / 2 + 'px'
			});
		});
	};
	/*
	调用示例
		var gallery = $('.thumbGallery').thumbGallery({
			back: $('.thumbGallery a.back'),
			forward: $('.thumbGallery a.forward') 
		});
		//gallery.trigger('goto', 2); 触发
	*/
	$.fn.thumbGallery = function (options) {
		function repeat(str, num) {
			return new Array(num + 1).join(str);
		}
		var config = { back: null, forward: null };
		var options = $.extend(config, options);
		return this.each(function () {
			var $wrapper = $('> div', this).css('overflow', 'hidden'),
				$slider = $wrapper.find('> ul'),
				$items = $slider.find('> li'),
				$single = $items.filter(':first'),
				singleWidth = $single.outerWidth(),
				visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
				currentPage = 1,
				pages = Math.ceil($items.length / visible);

			// 1. Pad so that 'visible' number will always be seen, otherwise create empty items
			if (($items.length % visible) != 0) {
				$slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
				$items = $slider.find('> li');
			}

			// 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
			$items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
			$items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
			$items = $slider.find('> li'); // reselect

			// 3. Set the left position to the first 'real' item
			$wrapper.scrollLeft(singleWidth * visible);

			// 4. paging function
			function gotoPage(page) {
				var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;

				$wrapper.filter(':not(:animated)').animate({
					scrollLeft: '+=' + left
				}, 500, function () {
					if (page == 0) {
						$wrapper.scrollLeft(singleWidth * visible * pages);
						page = pages;
					} else if (page > pages) {
						$wrapper.scrollLeft(singleWidth * visible);
						// reset back to start position
						page = 1;
					}

					currentPage = page;
				});

				return false;
			}
			$(config.back).click(function () { return gotoPage(currentPage - 1); });
			$(config.forward).click(function () { return gotoPage(currentPage + 1); });
			$(this).bind('goto', function (event, page) { gotoPage(page); }); 
			
			/*
			$wrapper.after('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');
			// 5. Bind to the forward and back buttons
			$('a.back', this).click(function () { return gotoPage(currentPage - 1); });
			$('a.forward', this).click(function () {
				return gotoPage(currentPage + 1);
			});
			// create a public interface to move to a specific page
			$(this).bind('goto', function (event, page) {
				gotoPage(page);
			});*/
		});
	};
	
	$.fn.autoHeight = function(){
		if(arguments.length==0) return;
		var elements=[];
		for (i=0; i<arguments.length; i++) {
			elements.push($(arguments[i]).outerHeight(true));
		}
		var max = elements[0];
		for(i=1; i<elements.length; ++i) {
			if(max < elements[i]) max = elements[i];
		}
		$(this).height(max);
	};

	$.fn.dropDownList = function () {
		return this.each(function () {
			$(this).mouseover(function () {
				$(this).children('ul').width($(this).width() - 2).show();
				$(this).children('a').addClass('hover');
			}).mouseleave(function (e) {
				e = e || event;
				if (e.isDefaultPrevented()) return; //判断事件是否被取消
				e.preventDefault();
				$(this).children('ul').hide();
				$(this).children('a').removeClass('hover');
			}).addClass('dropdownList');
		});
	}

	function setCursorPosition(ctrl, pos) {
        if(ctrl.setSelectionRange){
            ctrl.focus();
            ctrl.setSelectionRange(pos,pos);
        } else if (ctrl.createTextRange){
            var range = ctrl.createTextRange();
            range.collapse(true);
            range.moveEnd('character', pos);
            range.moveStart('character', pos);
            range.select();
        }
    }
	function getCursorPosition (ctrl) {//获取光标位置函数
	    var CaretPos = 0;	// IE Support
	    if (document.selection) {
	        ctrl.focus ();
		    var Sel = document.selection.createRange ();
		    Sel.moveStart ('character', -ctrl.value.length);
		    CaretPos = Sel.text.length;
	    }
	    // Firefox support
	    else if (ctrl.selectionStart || ctrl.selectionStart == '0')
		    CaretPos = ctrl.selectionStart;
	    return (CaretPos);
    }

	$.fn.uint_input = function(defValue) {
        this.each(function(i){
            $(this).bind('keyup', function(e) {
                var val = $(this).val();

                if (e.keyCode == 8 || e.keyCode==12 || e.keyCode == 46 || (e.keyCode >= 33 && e.keyCode <= 40)) {
                    if(defValue && val=='') $(this).val(defValue);
                    return true;
                }
                var pos = getCursorPosition($(this).get(0));
                
                var newPos = pos;
                
                var result = '';
                for(i=0;i<val.length;++i){
                    var chr = val.substring(i,i+1); //IE6不支持val[index]

                    if(chr>='０' && chr<='９') {
                        result+=String.fromCharCode(chr.charCodeAt(0)-0xFEE0);
                    } else if(!isNaN(chr) && !isNaN(parseInt(chr,10))) {
                        result+=chr;
                    } else {
                        if(i<pos) --newPos;
                    }
                }
                if(defValue && result=='') result=defValue; 
                $(this).val(result);
                setCursorPosition($(this).get(0),newPos);
            });
        });
	}

	$.fn.int_input = function(defValue) {
        this.each(function(i){
            $(this).bind('keyup', function(e) {
                if (e.keyCode == 8 || e.keyCode==12 || e.keyCode == 46 || (e.keyCode >= 33 && e.keyCode <= 40)) return true;
                var pos = getCursorPosition($(this).get(0));
                var newPos = pos;
                var val = $(this).val();
                var result = '';
                for(i=0;i<val.length;++i){
                    var chr = val.substring(i,i+1); //IE6不支持val[index]
                    if(chr=='-' && i==0) {
                        result+=chr;
                    } else if(chr>='０' && chr<='９') {
                        result+=String.fromCharCode(chr.charCodeAt(0)-0xFEE0);
                    } else if(!isNaN(chr) && !isNaN(parseInt(chr,10))) {
                        result+=chr;
                    } else {
                        if(i<pos) --newPos;
                    }
                }
                if(defValue && result=='') { result=defValue; }
                $(this).val(result);
                setCursorPosition($(this).get(0),newPos);
            });
        });
	}
	/*
	$.fn.reg_input = function (reg) {
		this.each(function () {
			$(this).bind('keypress', function (e) {
				e = e || event;
				var dom = $(this).get(0);

				if ($.browser.msie)
					return regInput(dom, reg, String.fromCharCode(e.keyCode));
				else {
					if (e.keyCode == 8 || e.keyCode == 46 || (e.keyCode >= 37 && e.keyCode <= 40)) return true;
					var val = $(this).val();
					var newVal = val.substring(0, dom.selectionStart) +
								 String.fromCharCode(e.charCode) +
								 val.substring(dom.selectionEnd, val.length);
					return (reg.test(newVal));
				}
			}).bind('paste', function (e) {
				e = e || event;
				if ($.browser.msie)
					return regInput(this, reg, window.clipboardData.getData('Text'));
				else
					return false;
			}).bind('drop', function (e) {
				e = e || event;
				var val = $(this).val();
				if ($.browser.msie)
					return regInput(this, reg, event.dataTransfer.getData('Text'));
				else {
					var ok = reg.test(val);
					if (!ok) $(this).val('');
				}
			});
		});

		function regInput(obj, reg, inputStr) {
			var docSel = document.selection.createRange()
			if (docSel.parentElement().tagName != "INPUT") return false
			oSel = docSel.duplicate()
			oSel.text = ""
			var srcRange = obj.createTextRange()
			oSel.setEndPoint("StartToStart", srcRange)
			var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length)
			return reg.test(str)
		}
	};

	$.fn.uint_input = function () {
		$(this).reg_input(/^\d+$/);
	}

	$.fn.int_input = function () {
		$(this).reg_input(/^((-?)|(-?\d+))$/);
	}

	$.fn.float_input = function () {
		$(this).reg_input(/^((-?)|(-?\d+)|(-?\d+\.\d*))$/);
	}*/

	/*
	构建导航面板
	*/
	$.fn.navGroup = function(){
		function updateState(toggler){
			var panel=toggler.parentsUntil('dl').next(); //即获取到dd标签
			if(toggler.hasClass('opened')) {
				panel.show();
			} else {
				panel.hide();
			}
			
		};
		$(this).find('dl dt .toggler').each(function(i){
			var toggler = $(this);
			if(!toggler.hasClass('opened') && !toggler.hasClass('closed')) toggler.addClass('opened');
			updateState(toggler);
			toggler.click(function(){
				if($(this).hasClass('opened')) {
					$(this).removeClass('opened').addClass('closed');
				} else {
					$(this).removeClass('closed').addClass('opened');
				}
				updateState($(this));
			});
		});
	}

	$.fn.iframeOnLoad = function(options){
		var settings = {
			onloading : null,
			onloaded : null
		};

		jQuery.extend(settings, options); //扩展属性赋值

		function onloadedHandler($iframe){
			var state = $iframe.get(0).readyState;
			if (state == "loaded" || state == "interactive" || state == "complete") {
				if(settings.onloaded!=null) settings.onloaded($iframe);
			} else {
				if(settings.onloading!=null) settings.onloading($iframe);
				setTimeout(onloadedHandler($iframe),1000);
			}
		}

		if($.browser.msie) {
			$(this).load(function(){
				onloadedHandler($(this));
			});
		} else { //Firefox只触发一次
			$(this).load(function(){
				var $iframe = $(this);
				if(settings.onloaded!=null) settings.onloaded($iframe);
			});
		}
		return $(this);
	}

	/*
	设置菜单
	mode:'h' 表示水平菜单，'v'表示纵向菜单
	*/
	$.fn.jmenu = function (options) {
		var settings = {
			mode: 'h'  //水平菜单
		};
		jQuery.extend(settings, options); //扩展属性赋值
		if (mode = 'h') { //横向菜单
			$(this).find('li').mouseover(function () {
				$(this).find('.childs').show();
			}).mouseleave(function () {
				$(this).children('.childs').hide();
			});
			$(this).find('.childs').hide();
			/*
			$(this).find('li>a').mouseover(function() {
			$(this).next('.childs').show(); 
			}).parent().mouseleave(function(){
			$(this).children('.childs').hide();
			});
			$(this).find('.childs').hide();*/
		} else if (mode = 'v') { //纵向菜单
			alert("未实现该功能");
		}
	}

	//提示
	$.fn.tips = function () {
		//Select all anchor tag with rel set to tooltip
		$(this).mouseover(function (e) {
			//Grab the title attribute's value and assign it to a variable
			var tip = $(this).attr('title');
			if (tip == '') return;
			//Remove the title attribute's to avoid the native tooltip from the browser
			$(this).attr('title', '');
			//Append the tooltip template and its value
			$(this).append('<div id="tooltip"><div class="tipHeader"><!----></div><div class="tipBody">' + tip + '</div><div class="tipFooter"><!----></div></div>');
			//Show the tooltip with faceIn effect
			$('#tooltip').fadeIn('500');
			$('#tooltip').fadeTo('10', 0.9);
			//e.stopPropagation();
		}).mousemove(function (e) {
			//Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse
			$('#tooltip').css('top', e.pageY + 10);
			$('#tooltip').css('left', e.pageX + 10);
			//e.stopPropagation();
		}).mouseleave(function (e) {
			//Put back the title attribute's value
			$(this).attr('title', $('.tipBody').html());
			//Remove the appended tooltip template
			$(this).children('div#tooltip').remove();
			//e.stopPropagation();
		});
	}

	$.fn.backgroundPosition = function () {
		var bgPosition = $(this).css('background-position');

		if (typeof (bgPosition) == 'undefined') {
			return { x: $(this).css('background-positionX'), y: $(this).css('background-positionY') };
		} else {
			var tmp = bgPosition.split(' ');
			return { x: tmp[0], y: tmp[1] };
		}
	}
})(jQuery);
