/*
	jquery scriptInclude 插件 by 混合理论 QQ:371490254
	version:1.0.0
	扩展自：jquery.include.pack-1.0.js
	参考：	script.aculo.us
	
	主要功能?
		1.自动加载js文件或css文件，省去在head里引用一大堆的js、css,自动加载本省所依赖的文?页面加载时自动运?
			example:	
				<script language="javascript" src="inc/scriptInclude.js?load=d1.js,d2.js,http://xxx/xx.js" type="text/javascript"></script>
		2.异步加载js或css后执行方?加载后事?调用include方法)
			example:
				$("#tb1").click(function(){$.include("inc/d3.js",function(){d3show('加载成功')});});
				$("#tb1").click(function(){$.include(["inc/d3.js","inc/d3.css"],function(){d3show('加载成功')});});
				
	调用说明?
		$.Dependence?				依赖关系数组，[0]	依赖于[1]
		$.include(arg[,callback]):		arg:字符串或数组,文件路径    callback:回调方法
		$.afterLoadScript?			异步加载完毕后执行：
			$(document).ready(
				function(){
					$.afterLoadScript(function(){		
											   alert("全部加载完毕")
									})
				})
*/

(function($){
		  $.extend(
				   {
					Dependence:[
						["ui/ui.draggable.js","ui/ui.core.js,themes/base/ui.all.css"],
						["ui/ui.dialog.js","ui/ui.core.js,ui/ui.draggable.js,ui/ui.resizable.js"],
						["plugin/jdMenu/jquery.jdMenu.js","plugin/jdMenu/jquery.jdMenu.css,util/jquery.bgiframe.js,util/jquery.dimensions.js,util/jquery.positionBy.js"],
						["plugin/facebox/facebox.js","plugin/facebox/facebox.css"],
						["plugin/ddsmoothmenu/ddsmoothmenu.js","plugin/ddsmoothmenu/ddsmoothmenu.css"],
						["plugin/msdropdown/dd.js","plugin/msdropdown/dd.css"],
						["plugin/cluetip/jquery.cluetip.js","plugin/cluetip/jquery.cluetip.css,util/jquery.bgiframe.js,util/jquery.hoverIntent.js"],
						["plugin/float.js","util/jquery.bgiframe.js"]
					],
					
					getDepend:function(file,list){
						if(!file||(file.length&&file.length<1)){return list};
						file=(typeof file=="string"?[file]:file)
						var files=$.grep($.Dependence,function(value){return $.grep(file,function(n){return value[0]==n}).length>0});
						$.each(files,function(index,value){
									var t=value[1].split(",");									   
									$.each(t,function(i,n){$.getDepend(n,list)});									  
						});

						list.reverse();
						$.each(file,function(index,value){list.push(value)});
						
						var tmp=[];
						for(var i=0;i<list.length;i++){
							var flag=true;
							for(var j=0;j<tmp.length;j++){
								if(tmp[j]==list[i])flag=false;
							}
							if(flag)tmp.push(list[i]);
						}
						list.length=0;
						tmp.reverse();
						$.each(tmp,function(index,value){list.push(value)});
						
					},
					
					StartIncludeSciptNum:0,
					AllStartIncludeSciptLoaded:function(){return $.StartIncludeSciptNum==0},
					
					afterLoadScript:function(fn){
						var scriptCollection=[];
						if(arguments.length>1){
							 for (var i = 0; i < arguments.length; i++){
								scriptCollection[scriptCollection.length]=arguments[i];
							}
						}else{
							scriptCollection[scriptCollection.length]=fn;
						}
						
						function runFn(){
									if($.AllStartIncludeSciptLoaded()){
										for(var i=0;i<scriptCollection.length;i++){
											if(typeof scriptCollection[i]=='function'){scriptCollection[i]()}
										}
										window.clearInterval(checkLoadScript)
									}
						}
						var checkLoadScript=window.setInterval(runFn,200)
					},
					
				    getImportPath:function(path){
						return 	 path;			
					},
					
				   	fileinfo:function(data){
					   data=data.replace(/^\s|\s$/g,"");
					   var m;
					   if(/\.\w+$/.test(data)){
						   m=data.match(/([^\/\\]+)\.(\w+)$/);
						   if(m){
							   if(m[2]=='js'){
								   return{filename:m[1],ext:m[2],tag:'script'}
								}else if(m[2]=='css'){
									return{filename:m[1],ext:m[2],tag:'link'}
								}
							}else{
								return{filename:null,ext:null}
							}
						}else{
							m=data.match(/([^\/\\]+)$/);
							if(m){
								return{filename:m[1],ext:null,tag:null}
							}else{
								return{filename:data,ext:"",tag:'script'}
							}
						}
					},
					
					fileExist:function(filename,filetype,attrCheck){
						var elementsArray=document.getElementsByTagName(filetype);
						for(var i=0;i<elementsArray.length;i++){
							if(elementsArray[i].getAttribute(attrCheck)==$.getImportPath(filename)){return true}
						}return false
					},
					
					createElement:function(filename,filetype){
						switch(filetype){
							
							case'link':
								if(!$.fileExist(filename,filetype,'href')){
									var styleTag=document.createElement(filetype);
									styleTag.setAttribute('type','text/css');
									styleTag.setAttribute('rel','stylesheet');
									styleTag.setAttribute('href',$.getImportPath(filename));
									return styleTag
								}else{
									return false
								}break;
							case'script':
								if(!$.fileExist(filename,filetype,'src')){
									var scriptTag=document.createElement(filetype);
									scriptTag.setAttribute('language','javascript');
									scriptTag.setAttribute('type','text/javascript');
									scriptTag.setAttribute('src',$.getImportPath(filename));
									return scriptTag
								}else{
									return false
								}break;
							default:
									var scriptTag=document.createElement(filetype);
									scriptTag.setAttribute('language','javascript');
									scriptTag.setAttribute('type','text/javascript');
									scriptTag.setAttribute('src',$.getImportPath(filename));
									return scriptTag
								break;
							//default:return false;break
						}
					},
					
					cssReady:function(index,callback){
						function check(){
							if(document.styleSheets[index]){
								if(typeof callback=='function'){callback()}
								window.clearInterval(checkInterval)
							}
						}
						var checkInterval=window.setInterval(check,200)
					},
					
					include:function(file,callback){
						var headerTag=document.getElementsByTagName('head')[0];
						var fileArray=[];
						typeof file=='string'?fileArray[0]=file:fileArray=file;
						for(var i=0;i<fileArray.length;i++){
							var elementTag=$.fileinfo(fileArray[i]).tag;
							var el=[];
							if(elementTag!==null){
								el[i]=$.createElement(fileArray[i],elementTag);
								if(el[i]){
									headerTag.appendChild(el[i]);
									if($.browser.msie){
										el[i].onreadystatechange=function(){
											if(this.readyState==='loaded'||this.readyState==='complete'){
												if(typeof callback=='function'){callback()}
											}
										}
									}else{
										if(elementTag=='link'){
											$.cssReady(i,callback)
										}else{
											el[i].onload=function(){
												if(typeof callback=='function'){
													callback()
												}
											}
										}
									}
								}
							}else{
								return false
							}
						}
					}
				});

})(jQuery);

(function($) {
    var js = /scriptInclude\.js(\?.*)?$/;
	var jsCollection=$("head script[src*='scriptInclude.js']");
	var path="";
	var TmpArr=[];
	jsCollection.each(function() {
		  	path = this.src.replace(js, '');
			$.pluginPath=path;
		  	//var includes = this.src.match(/\?.*load=([a-zA-Z,\.\:\/\?_\-]*)/);
			var includes = this.src.match(/\?.*load=(.*)/);
			
		  	$.getDepend((includes ? includes[1].split(',').concat(["base.js"]) : ["base.js"]),TmpArr);
		});
	$.StartIncludeSciptNum=TmpArr.length;
	$.each(TmpArr.reverse(),
		  function(i,n) {$.include((new RegExp("://")).test(n)?n:path+n,function(){$.StartIncludeSciptNum--;}) 
		  });
  })(jQuery);