/**
*Author: LiangYunFei
*Email: 742149579@qq.com
*/

window.Base = window.Base || {};

//基础函数
(function(window, $){
	var _common = {};
	
	//判断是否为空函数
	_common.isEmptyFunc = function(fn){
		return /^function[^{]+\{\s*\}/m.test(fn.toString());
	};
	
	//当前时间
	_common.now = function(){
        return new Date().getTime();
    };
	
	//首字母大写
	_common.ucFirst = function(str){
		return str.substring(0, 1).toUpperCase() + str.substring(1);
	};
	
	//判断类型
	_common.typeOf = function(obj){
		return Object.prototype.toString.call(obj).replace(/^\[object\s(\w+)\]$/, "$1").toLowerCase();
	};
	
	window.Base.Common = _common;
	
})(window, $);

//公用函数
(function(window, $){
	var _public = {};

	//加载数据
	_public.loadData = function(opt){
		this.appendLoading(opt.appendField);
		var _opt = {
			'url': '',
			'params': ''
		};
		var options = $.extend(true, _opt, opt);
		var _url = options.url + '?callback=?' + options.params;
		var _self = this;
		
		$.getJSON(_url, function(data) {
			_self.removeLoading(opt.appendField);
			if(options.callback) options.callback(data);
		});
	};
	
	_public.appendLoading = function(obj){
		var loading = $('<img>').addClass('loading').attr('src', 'images/loading.png').css('display', 'block');
		$(obj).append(loading);
	};
	
	_public.removeLoading = function(obj){
		var loading = $(obj).find('.loading');
		loading.remove();
	};
	
	_public.compareVersion = function(a, b) {
		if (a === b) {
			return 0;
		}
		var a_components = a.split(".");
		var b_components = b.split(".");
		var len = Math.min(a_components.length, b_components.length);
		for (var i = 0; i < len; i++) {
			if (parseInt(a_components[i]) > parseInt(b_components[i])) {
				return 1;
			}
			if (parseInt(a_components[i]) < parseInt(b_components[i])) {
				return -1;
			}
		}
		if (a_components.length > b_components.length) {
			return 1;
		}
		if (a_components.length < b_components.length) {
			return -1;
		}
		return 0;
	};
		
	window.Base.Public = _public;
})(window, $);


//插件
(function(window, $){
	var _plugins = {};

	var Events = {
		ucFirst: function(str){
			return str.charAt(0).toUpperCase() + str.slice(1);
		},
		fireEvent: function(){
			var args = Array.prototype.slice.call(arguments),
				event = args.shift(),
				fn = 'on' + this.ucFirst(event);

			this[fn] && this[fn].apply(this, args);
		},
		addEvent: function(event, fn){
			event = 'on' + this.ucFirst(event);
			this[event] = fn;
		}
	};

	function Tab(options){
		this.initialize(options);
	}

	Tab.prototype = {
		initialize: function(options){
			this.setOptions(options);
			this.run();
		},
		setOptions: function(options){
			var _opt = {
				'tabs': null,
				'trigger': 'click'
			};
			this.setting = $.extend(true, _opt, options);
		},
		run: function(){
			var setting = this.setting,
				$tabs = setting.tabs,
				event = setting.trigger,
				_me = this;

			$tabs.on(event, function(){
				var _index = $(this).index();
				_me.fireEvent('select', _index);
			});
		},
	};

	$.extend(true, Tab.prototype, Events);

	_plugins.Tab = Tab;
	
	//LOCALSTORAGE缓存
	var Cache = {
		get: function(key){
		    var _now = new Date().getTime(),	
                _val = localStorage.getItem(key);
			
			if(_val){
				var _arr = _val.split('|'),
					_expire = parseInt(_arr[0]),
					_storageTime = parseInt(_arr[1]),
					_realVal = _arr[2];
				
				if(_now > _storageTime + _expire){					
					this.remove(key);
					_val = null;
				}else{
					_val = $.parseJSON(_realVal);
				}
			}
			return _val;
		},
		//过期时间秒为单位
		set: function(key, val, expire){
			expire = (expire || 300)*1000;
			val = expire + '|' + window.Base.Common.now() + '|' + JSON.stringify(val);
			localStorage.setItem(key, val);
		},
		remove: function(key){
		    localStorage.removeItem(key);
		}
	};
	
	_plugins.Cache = Cache;

	window.Base.Plugins = _plugins;
	
})(window, $);
