/*
 * SimpleLib with jQuery
 *
 * Version 1.0.3 on 2010.05.14
 *
 * Copyright (c) 2009 STARRYWORKS inc. Lisence under MIT lisence;
 * http://www.starryworks.co.jp/
 *
 */


if ( typeof("SimpleLib") == "undefined" ) var SimpleLib = {};
if ( typeof("SimpleLibSettings") == "undefined" ) var SimpleLibSettings = {};

SimpleLib = $.extend( true, {
	
	debug: false,
	loaded: false,
	numLoad : 0,
	jsDir: $("script[src*='simplelib.js']").attr('src').replace(/simplelib\.js.*?$/,""),
	
	//Check if the passed value exsits in the array
	containsInArray: function( i_value, i_array ) {
		for ( var i in i_array ) if ( i_array.hasOwnProperty(i) && i_array[i] === i_value) return true;
		return false;
	},
	
	//Convert url query to array
	queryToArray: function( i_url ) {
		var settings = [];
		var arr = String(i_url).split("?");
		if ( !arr || arr.length < 2 || !arr[1] ) return settings;
		var values = String(arr[1]).split(',');
		$.each(values, function(){ if ( this ) settings.push( String(this) ); });
		return settings;
	},
	
	//isIE
	isIE: function() { return (navigator.userAgent.indexOf("MSIE") != -1); },
	
	//isIE6
	isIE6: function() { return ( '\v'=='v' ); },
	
	//Init
	init: function( i_plugins, i_settings ){
		//
		if ( SimpleLib.jsDir == "" ) SimpleLib.jsDir = "./";
		
		//Load plugins
		var i;
		var l = i_plugins.length;
		if ( typeof(i_settings) == "undefined" ) i_settings = {};
		
		
		for ( i=0; i<l; i++ ) {
			var prefix = i_plugins[i].substr(0, 3);
			if( prefix == 'ie6' && !SimpleLib.isIE6() && !SimpleLib.debug ) continue;
			
			var settings = {};
			if ( typeof(i_settings[i_plugins[i]]) != "undefined" ) settings = i_settings[i_plugins[i]];
			if ( typeof(SimpleLib[i_plugins[i]]) == "undefined" ) SimpleLib[i_plugins[i]] = {};
			if ( SimpleLib[i_plugins[i]].settings ) {
				settings = $.extend( SimpleLib[i_plugins[i]].settings, settings );
			}
			SimpleLib[i_plugins[i]].settings = settings;
						
			
			if ( SimpleLib[i_plugins[i]] && SimpleLib[i_plugins[i]].init ) {
				SimpleLib[i_plugins[i]].init();
			} else {
				SimpleLib._load( i_plugins[i] );
			}
		}
	},
	
	//Load
	_load: function( i_plugin ) {
		var file = SimpleLib.jsDir + "plugins/"+i_plugin+".js";
		SimpleLib.numLoad++;
		$.ajax({
			type: "GET",
			url: file,
			dataType: "script",
			success:function(d) { 
				if ( SimpleLib[i_plugin].hasOwnProperty("init") ) SimpleLib[i_plugin].init(); 
				else {	//firefoxだと各ブライグインファイルの実行部が動かないまま、successのif文がよばれるため、とりあえず遅延実行。
					var tId = setInterval(function(){
						if ( SimpleLib[i_plugin].hasOwnProperty("init") ){
							SimpleLib[i_plugin].init(); 
							clearInterval(tId);
						} 
					}, 100);
				}
				SimpleLib.numLoad--;
				if ( SimpleLib.numLoad < 1 ) {
					SimpleLib.loaded = true;
					SimpleLib.onLoad();
				}
			},
			error: SimpleLib._onLoadError
		});
	},
	
	//
	load: function( i_function ) {
		SimpleLib.onLoad = i_function;
		if ( SimpleLib.loaded ) SimpleLib.onLoad();
	},
	
	//
	onLoad : function(){},
	
	//onLoadError
	_onLoadError: function( e ) {

	},
	
	//Setup
	setup: function() {
		var plugins = SimpleLib.queryToArray( $("script[src*='simplelib.js']").attr('src') );
		if ( plugins.length ) SimpleLib.init( plugins, SimpleLibSettings );
	},
	
	//extend
	extend: function( i_plugin, i_info ) {
		if ( typeof(SimpleLib[i_plugin]) == "undefined" ) SimpleLib[i_plugin] = {};
		SimpleLib[i_plugin] = $.extend( true, i_info, SimpleLib[i_plugin]);
	}
	
}, SimpleLib);

SimpleLib.setup();

