(function($){
	$.preLoader = {
		preLoader:"",
		option : {
			moduleList:[],
			moduleAjaxIndex:0,
			moduleAjaxStatus:0,
			moduleDoneStatus:0,
			moduleDoneNow:0,
			allDown:0,
			overlay: "",
			loadBar: "",
			preloader: "",
			loadmask:'#loadmask',
			items: [],
			doneStatus: 0,
			doneNow: 0,
			selectorPreload: "body",
			ieLoadFixTime: 4000,
			ieTimeout: null,
			moduleCall:false,
			doneCall:false
		},
		module:{
			name:"",
			doneStatus: 0,//所有
			doneNow: 0,
			isRunning:false,
			isCompleted:false,
			url:"",
			scripts:[],
			items:[],
			itemIndex:0,
			ieLoadFixTime: 3000,
			ieTimeout: null
		},
		createModule:function(module){
			return $.extend({},$.preLoader.module,module);
		},
		insertModule:function(module){
			$.preLoader.option.moduleList.push($.preLoader.createModule(module));
		},
		initOption:function(option){
			$.extend($.preLoader.option,option);
		},
		init: function() {
			if (!-[1,]&&!window.XMLHttpRequest){	
				return false;
			}
			if ($.preLoader.option.selectorPreload == "body") {
				$.preLoader.spawnLoader();
				$.preLoader.getImages($.preLoader.option.selectorPreload);
				$.preLoader.createPreloading();
			} else {
				$(document).ready(function() {
					$.preLoader.spawnLoader();
					$.preLoader.getImages($.preLoader.option.selectorPreload);
					$.preLoader.createPreloading();
				});
			}
			//help IE drown if it is trying to die :)
			$.preLoader.option.ieTimeout = setTimeout("$.preLoader.ieLoadFix()", $.preLoader.option.ieLoadFixTime);
		},
		initModule:function(){
			if($.preLoader.option.moduleList.length == 0 ){
				//alert("return false");
				return false ;
			}
			$.preLoader.ajaxModule();
			
		},
		ajaxModule:function(){
			$("#debug").append($.preLoader.option.moduleAjaxIndex+"<br>");

			if( $.preLoader.option.moduleAjaxIndex < $.preLoader.option.moduleList.length){
				$.ajax({
				  url:$.preLoader.option.moduleList[$.preLoader.option.moduleAjaxIndex].url,
				  dataType: 'json',
				  //data:{},
				  success:function(data){
					  $.extend($.preLoader.option.moduleList[$.preLoader.option.moduleAjaxIndex],data);
					  $.preLoader.option.moduleList[$.preLoader.option.moduleAjaxIndex].doneStatus = $.preLoader.option.moduleList[$.preLoader.option.moduleAjaxIndex].items.length ;
//alert(data.name + ":doneStatus :"+$.preLoader.option.moduleList[$.preLoader.option.moduleAjaxIndex].doneStatus);
					  //alert(data.scripts);
					  $.preLoader.option.moduleAjaxIndex ++ ;
					  $.preLoader.ajaxModule();
				  }
				});
			}else{
				$.preLoader.option.moduleAjaxStatus = 1;
//				 $.preLoader.preLoadModule();
				$.preLoader.init();
			}
		},
		preLoadModule:function(){
			if($.preLoader.option.allDown == 1){
				return false ;	
			}
			$.preLoader.option.doneStatus = $.preLoader.option.moduleList.length ;
			//alert("$.preLoader.option.doneStatus :"+$.preLoader.option.doneStatus);
			$.preLoader.loadingModule(0);
			//$("#debug").append("preload<br>");
		},
		loadingModule:function(moduleIndex){
			$("#debug").append("moduleIndex:"+moduleIndex+"load<br>");
			$.preLoader.option.moduleList[moduleIndex].isRunning = true ;
			var items = $.preLoader.option.moduleList[moduleIndex].items;
			$.preLoader.option.moduleList[moduleIndex].preloader = $("<div></div>").appendTo($.preLoader.option.selectorPreload);
			$($.preLoader.option.moduleList[moduleIndex].preloader).css({
				height: 	"0px",
				width:		"0px",
				overflow:	"hidden"
			});
			//timeout
			$("#debug").append("moduleIndex Timeout:"+moduleIndex+"<br>");
			$.preLoader.option.moduleList[moduleIndex].ieTimeout = setTimeout(function(){
				$.preLoader.ieModuleLoadFix(moduleIndex);
			}, $.preLoader.option.moduleList[moduleIndex].ieLoadFixTime);
			
			//module image items
			var length = items.length;
			//$.preLoader.option.moduleDoneNow = moduleIndex ; // process
			for (var i = 0; i < length; i++) {
				/*var imgLoad = $("<img></img>");
				$(imgLoad).attr("src", items[i]);
				var src = $(imgLoad).attr("src") ;
				$.preLoader.option.moduleList[moduleIndex].itemIndex++;
				$(imgLoad).appendTo($.preLoader.option.moduleList[moduleIndex].preloader);
				//$("#debug").append("itemIndex :" + k+" ,moduleIndex :"+moduleIndex+"<br> ");
				$(imgLoad).unbind("load").bind("load", function(){
					$.preLoader.loadingModuleControl(moduleIndex,$(this).parent().children("img").index(this),true);
				});
				$(imgLoad).bind("error",function(){
					var img = new Image();
					img.src = $(imgLoad).attr("src");
					$.preLoader.loadingModuleControl(moduleIndex,$(this).parent().children("img").index(this),false);
				});
				//$(imgLoad).appendTo($.preLoader.option.moduleList[moduleIndex].preloader);*/
				var image = new Image();
				image.src = items[i];
				if(image.complete){
					$.preLoader.loadingModuleControl(moduleIndex,i,true);
				}else{
					(function(i,image){
						image.onload = function(e){
							//$("#debug").append("down src = "+image.src+",index:"+i+"<br>");
							$.preLoader.loadingModuleControl(moduleIndex,i,true);
						}
					})(i,image);
				}
			}
			
		},
		loadingModuleByName:function(moduleName){
			var moduleIndex = -1 ;
			for(var k = 0 ; k < $.preLoader.option.moduleList.length ; k++ ){
				if($.preLoader.option.moduleList[k].name == moduleName){
					moduleIndex = k ;
					break ;	
				}
			}
			if(moduleIndex == -1){
				return false ;	
			}else{
				$.preLoader.loadingModule(moduleIndex);
				$("#debug").append(moduleName + " try load ...<br>");
			}
		},
		loadingModuleControl:function(moduleIndex,itemIndex,flag){
			//$("#debug").append("$.preLoader.option.moduleList["+moduleIndex+"].ieTimeout is "+($.preLoader.option.moduleList[moduleIndex].ieTimeout!= null)+"<br>");
			if($.preLoader.option.moduleList[moduleIndex].ieTimeout!=null){
				clearTimeout($.preLoader.option.moduleList[moduleIndex].ieTimeout);
				$("#debug").append("moduleIndex:"+moduleIndex+" timeout clear<br>");
				$.preLoader.option.moduleList[moduleIndex].ieTimeout = null ;	
			}
			$.preLoader.option.moduleList[moduleIndex].doneNow ++ ;
			if(!flag){
				$("#debug").append("flag : "+flag+" - "+itemIndex+" : "+$.preLoader.option.moduleList[moduleIndex].scripts[itemIndex]+"<br>");
			}
			/*if(moduleIndex == 4){
				$("#debug").append("flag : "+flag+" - "+itemIndex+" : "+$.preLoader.option.moduleList[moduleIndex].scripts[itemIndex]+"<br>");
			}*/
			
			if($.preLoader.option.moduleList[moduleIndex].scripts[itemIndex] != undefined && $.preLoader.option.moduleList[moduleIndex].scripts[itemIndex] != '' ){
				//$("#debug").append($.preLoader.option.moduleList[moduleIndex].scripts[itemIndex]+"<br>")
				if(!-[1,]){
					eval($.preLoader.option.moduleList[moduleIndex].scripts[itemIndex]);
				}else{
					$("<script></script>").append($.preLoader.option.moduleList[moduleIndex].scripts[itemIndex]).appendTo($($.preLoader.option.preloader));
				}
			}
			//alert(imageIndex);
			if($.preLoader.option.moduleList[moduleIndex].doneNow==$.preLoader.option.moduleList[moduleIndex].doneStatus){
				$.preLoader.option.moduleList[moduleIndex].isCompleted = true ;
				var isLoadWorking = false ;
				var isAllDone = true ;
				$("#debug").append($.preLoader.option.moduleList[moduleIndex].name + " is done <br>");
				for(var k = 0 ; k < $.preLoader.option.moduleList.length ; k++ ){
						//$("#debug").append($.preLoader.option.moduleList[k].name + "running :"+$.preLoader.option.moduleList[k].isRunning+" ,Completed :" +$.preLoader.option.moduleList[k].isCompleted+"<br>");
						if($.preLoader.option.moduleList[k].isRunning&&!$.preLoader.option.moduleList[k].isCompleted){
							isLoadWorking = true ;
							$("#debug").append($.preLoader.option.moduleList[k].name+" is working ...<br>");
							//break ;
						}
						if(!$.preLoader.option.moduleList[k].isCompleted){
							isAllDone = false ;
							//break ;	
						}
				}
				if(!isLoadWorking){
					for(var i = 0 ; i < $.preLoader.option.moduleList.length ; i++ ){
						if(!$.preLoader.option.moduleList[i].isRunning&&!$.preLoader.option.moduleList[i].isCompleted){
							$.preLoader.loadingModule(i);
							$("#debug").append($.preLoader.option.moduleList[moduleIndex].name+" call"+ $.preLoader.option.moduleList[i].name +"working ...<br>");
							break ;
						}
					}
				}
				if(isAllDone){
					$("#debug").append("All module is done <br>");
					$.preLoader.option.allDown = 1 ;
				}
			}
		},
		
		
		ieLoadFix: function() {
			
			if (!-[1,]) {
				while ($.preLoader.option.doneNow*(100 / $.preLoader.option.doneStatus)< 100) {
					$.preLoader.imgCallback(true);
				}
			}
		},

		ieModuleLoadFix:function(moduleIndex){
			var length = $.preLoader.option.moduleList[moduleIndex].scripts.length ;
			$("#debug").append("timeout module:"+moduleIndex+",length:"+length+"<br>");
			for (var i = 0; i < length; i++) {
				if(!-[1,]){
					eval($.preLoader.option.moduleList[moduleIndex].scripts[i]);
				}else{
					$("<script></script>").append($.preLoader.option.moduleList[moduleIndex].scripts[i]).appendTo($($.preLoader.option.preloader));
				}
			}
			$.preLoader.option.moduleList[moduleIndex].isCompleted = true ;
			var isLoadWorking = false ;
			var isAllDone = true ;
			$("#debug").append($.preLoader.option.moduleList[moduleIndex].name + " is done <br>");
			for(var k = 0 ; k < $.preLoader.option.moduleList.length ; k++ ){
					//$("#debug").append($.preLoader.option.moduleList[k].name + "running :"+$.preLoader.option.moduleList[k].isRunning+" ,Completed :" +$.preLoader.option.moduleList[k].isCompleted+"<br>");
					if($.preLoader.option.moduleList[k].isRunning&&!$.preLoader.option.moduleList[k].isCompleted){
						isLoadWorking = true ;
						$("#debug").append($.preLoader.option.moduleList[k].name+" is working ...<br>");
						//break ;
					}
					if(!$.preLoader.option.moduleList[k].isCompleted){
						isAllDone = false ;
						//break ;	
					}
			}
			if(!isLoadWorking){
				for(var i = 0 ; i < $.preLoader.option.moduleList.length ; i++ ){
					if(!$.preLoader.option.moduleList[i].isRunning&&!$.preLoader.option.moduleList[i].isCompleted){
						$.preLoader.loadingModule(i);
						$("#debug").append($.preLoader.option.moduleList[moduleIndex].name+" call"+ $.preLoader.option.moduleList[i].name +"working ...<br>");
						break ;
					}
				}
			}
			if(isAllDone){
				$("#debug").append("All module is done <br>");
				$.preLoader.option.allDown = 1 ;
			}
			clearTimeout($.preLoader.option.moduleList[moduleIndex].ieTimeout);
			$.preLoader.option.moduleList[moduleIndex].ieTimeout = null ;
		},
		imgCallback: function(isAnimate) {
			$.preLoader.option.doneNow ++;
			$.preLoader.animateLoader(isAnimate);
		},
		
		getImages: function(selector) {
			var everything = $(selector).find("*:not(script)").each(function() {
				var url = "";
				
				if ($(this).css("background-image") != "none") {
					var url = $(this).css("background-image");
				} else if (typeof($(this).attr("src")) != "undefined" && $(this).attr("tagName").toLowerCase() == "img") {
					var url = $(this).attr("src");
				}
				
				url = url.replace("url(\"", "");
				url = url.replace("url(", "");
				url = url.replace("\")", "");
				url = url.replace(")", "");
				
				if (url.length > 0 && url.indexOf('tishi.png')==-1  && url.indexOf('left.png')==-1  && url.indexOf('middle.png')==-1  && url.indexOf('right.png')==-1 && url.indexOf('loading.gif')==-1 ) {
					$("#debug").append(url+"<br>");
					$.preLoader.option.items.push(url);
				}
			});
		},
		createModuleLoading:function(){
			
		},
		createPreloading: function() {
			$.preLoader.option.preloader = $("<div></div>").appendTo($.preLoader.option.selectorPreload);
			$($.preLoader.option.preloader).css({
				height: 	"0px",
				width:		"0px",
				overflow:	"hidden"
			});
			
			var length = $.preLoader.option.items.length; 
			$.preLoader.option.doneStatus = length;
			
			for (var i = 0; i < length; i++) {
				/*var imgLoad = $("<img></img>");
				$(imgLoad).attr("src", $.preLoader.option.items[i]);
				$(imgLoad).unbind("load").bind("load", function() {
					$.preLoader.imgCallback(true);
				});
				$(imgLoad).bind("error",function(){
					var img = new Image();
					img.src = $(imgLoad).attr("src");
					$.preLoader.imgCallback(true);
				});
				if($.preLoader.option.items[i].indexOf("images/loading.gif")>-1){
					$("#debug").append($(imgLoad).attr("src")+"  <br>");
				}else{
					$(imgLoad).appendTo($($.preLoader.option.preloader));
				}*/
				var image = new Image();
				image.src =  $.preLoader.option.items[i];
				if(image.complete){
					$.preLoader.imgCallback(true);
				}else{
					image.onload = function(e){
						$.preLoader.imgCallback(true);
					};
				}
			}
		},
	
		spawnLoader: function() {
			if ($.preLoader.option.selectorPreload == "body") {
				var height = $(window).height();
				var width = $(window).width();
				var position = "fixed";
			} else {
				var height = $($.preLoader.option.selectorPreload).outerHeight();
				var width = $($.preLoader.option.selectorPreload).outerWidth();
				var position = "absolute";
			}
			var left = $($.preLoader.option.selectorPreload).offset()['left'];
			var top = $($.preLoader.option.selectorPreload).offset()['top'];
			
			$.preLoader.option.overlay = $("<div><img style='position:absolute;top:40%;margin-top:0px;left:50%;margin-left:-19px;width:39px;height:43px;' src='images/loading.gif' />"+(!-[1,]?"<div style='background-color:#151515;width:100%;height:35%;top:65%;position:relative;display:block;'><div style='position:relative;width:270px;height:130px;display:block;left:50%;margin-left:-135px;padding-top:30px;'><img src='images/tishi.png'/><ul style='list-style-type:none;margin-top:30px;margin-left:39px;'><li style='float:left;'><a target='_blank' href='http://www.google.cn/chrome/intl/zh-CN/landing_chrome.html'><img src='images/left.png' /></a></li><li style='float:left;margin-left:22px;margin-right:29px;'><img src='images/middle.png' /></li><li style='float:left;'><a target='_blank' href='http://www.firefox.com.cn/download/'><img src='images/right.png' /></a></li></ul></div></div>":"")+"</div>").appendTo($($.preLoader.option.selectorPreload));
			$($.preLoader.option.overlay).addClass("preload_bg");
			$($.preLoader.option.overlay).css({
				position: position,
				top: top,
				left: left,
				width: width + "px",
				height: height + "px"
			});
			
			$.preLoader.option.loadBar = $("<div></div>").appendTo($($.preLoader.option.overlay));
			$($.preLoader.option.loadBar).addClass("preload_bar");
			
			$($.preLoader.option.loadBar).css({
				position: "absolute",
				top: "50%",
				width: "0%"
			});
		},
		
		animateLoader: function(isAnimate) {
			var perc = $.preLoader.option.doneNow * (100 / $.preLoader.option.doneStatus);
			if(isAnimate){
				if (perc > 99) {
					$($.preLoader.option.loadBar).stop().animate({
						width: perc + "%"
					}, 500, "linear", function() { 
						$.preLoader.doneLoad();
					});
				} else {
					$($.preLoader.option.loadBar).stop().animate({
						width: perc + "%"
					}, 500, "linear", function() { });
				}
			}else{
				if (perc > 99) {$.preLoader.doneLoad();};
			}
		},
		
		doneLoad: function() {
			//prevent IE from calling the fix
			clearTimeout($.preLoader.option.ieTimeout);
			
			//determine the height of the preloader for the effect
			if ($.preLoader.option.selectorPreload == "body") {
				var height = $(window).height();
			} else {
				var height = $($.preLoader.option.selectorPreload).outerHeight();
			}
			//The end animation
			$($.preLoader.option.overlay).fadeOut(500);
			$($.preLoader.option.preloader).remove();
			if($($.preLoader.option.loadmask)){
				$($.preLoader.option.loadmask).remove();
			}
			
			$("#debug").append("doneLoad<br>");
			//$.preLoader.initModule();
			if($.preLoader.option.moduleCall){
				return false ;	
			}else{
				$.preLoader.option.moduleCall = true ;
				$.preLoader.preLoadModule();
			}
		}
	}
})(jQuery);
