/*
* 2008 copyright (c) kkito all right reserved
* http://kkito.cn
* kkito@sohu.com
* 
* some basic function for the blog using ajax
* base on jQuery.js(1.2.6)
* version 0.1.0
*/

/*
* ajax use html widget 
* normal used widget generated here
*
*/

//extend for utility function
(function($){
	$.kkbase = {};
	//js file path
	$.kkbase._dirprefix = "/includes/js/";
	
	//ajax 处理时候的显示
	//$.kkbase.
	
	//loader automatically load the plugin js
	$.kkbase.loader = function(js_name , is_plugin){
		//set a flag , if given true , just  get a full name script
		//alert(is_plugin);
		if(is_plugin == undefined)
			is_plugin = true;
		if(is_plugin){
			js_full_name = $.kkbase._dirprefix + "jquery.kkbase." + js_name + ".js";
		}else{
			js_full_name = js_name;
		}
		//alert(js_full_name);
		//alert(is_plugin);
		if(window["jQuery"] && window["jQuery"]["kkbase"]){
			if(window["jQuery"]["kkbase"][js_name]){
				//alert(window["jQuery"]["kkbase"][plugin_name]["aa"]);
				return window["jQuery"]["kkbase"][js_name];
			}else{
				//alert(js_full_name);
				//alert("/includes/js/jquery.kkbase.test.js");
				//js_full_name = "http://kkito.cn/includes/js/jquery.js";
				//$.getScript("");
				$.ajaxSettings.async = false;
				$.get(js_full_name , function(data){
					//alert(data);
					if(!!(window.attachEvent && !window.opera)){
					  //ie
					  execScript(data); 
					 }else{
					  //not ie
					  window.eval(data);
					 }
					//window.eval(data);
					//alert(window);
					//alert(window.dp);
					//alert($["dp"]);
					//alert($.kkbase["dp"]);
					//alert($.kkbase.loader["dp"]);
					//alert(dp);
					//alert("123123");
					//alert(window["jQuery"]["kkbase"][plugin_name]);
				});
				//alert("right now");
				$.ajaxSettings.async = true;
				//alert(window["jQuery"]["kkbase"][plugin_name]["aa"]);
				if(is_plugin){
					//alert(js_full_name);
					return window["jQuery"]["kkbase"][js_name];
					
				}else
					return undefined;
				//return "";
			}
		}
	}
	
	//the warehouse to contains the css had been loadered
	$.kkbase._csswarehouse = {};
	
	//loader a css dynamically synchronizely
	$.kkbase.cssloader = function(cssname , dirprefix){
		//dirprefix = dirprefix || $.kkbase._dirprefix;
		//css_full_name = dirprefix + cssname + ".css";
		css_full_name = cssname;
		//if such css in the warehouse ,we need not loader again
		if($.kkbase._csswarehouse[css_full_name]){
			return;
		}
		
		$.ajaxSettings.async = false;
		$.get(css_full_name , function(data){
			$("head").append("<style>" + data + "</style>");
		});
		$.ajaxSettings.async = true;
		//set the key to the warehouse
		$.kkbase._csswarehouse[css_full_name] = true;
	}
	
	$.kkbase.ajax_deal_prompt = function(){
	
	
		$("body").append("<div class='ajax_prompt_div'></div>");
		$(".ajax_prompt_div").ajaxStart(function(){
			//alert("start");
			$(this).fadeIn("slow");
		});
		
		$(".ajax_prompt_div").ajaxSuccess(function(){
			//alert("success");
			$(this).fadeOut("slow");
		});
		
		$(".ajax_prompt_div").ajaxError(function(request, settings){
			alert("fatal error!!!!");
			//alert(settings.url);
		});
		//alert("hahhaha");
		
	}
	//initialize
	return function(){
		$(document).ready(function(){
			//alert("inner document");
			$.kkbase.ajax_deal_prompt();
		});
		
		//alert("kkbase initi");

	} ();

})(jQuery);