/*
 * Lazy 0.2 - jQuery based framework
 *
 * @requires jQuery v 1.3 or  higher
 * 
 * http://emotionjs.sourceforge.net
 * http://sourceforge.net/projects/emotionjs
 * 
 * Copyright (c) 2009 Stefano Curtoni (emotionjs.sourceforge.net)
 * Licensed under GPL (GPL-LICENSE.txt) license.
 *
 * $Date: 2009-08-28
 */

(function($) {
	
$.extend({
	//control if obj contains a element 	
	contains : function(a, obj){
		for(var i = 0; i < a.length; i++)
		{
			if(a[i] === obj)
			{
		    	return true;
		    }
		}
		return false;
	},

	//lazy style loading method
	addStyle : function (element,styleLabel,completeFn){
	
		
		var styleUrl = SETTINGS.rootPath+element+"/css/" + styleLabel +".css";
		var exist = $.contains(SETTINGS.cssList,styleLabel);
		
	    if(!exist)
	    { 
	    	
	    	//don't set href attribute
			SETTINGS.styleMarker.before("<link rel='stylesheet' type='text/css'  id='"+styleLabel+"'/>");
			
			$("#"+styleLabel).bind('load',function(){
				
				$("#"+styleLabel).unbind('load');
				
				completeFn();
			});	
					
			$("#"+styleLabel).attr("href",styleUrl);
			
			SETTINGS.cssList.push(styleLabel);
			
			$.checkStyle(styleLabel,218,0);
			
	    }
	    else
	    {
	    	completeFn();
	    }
	},
	//workaround to check if a style is loaded
	checkStyle : function (styleLabel,value,time) 
	{		
		if($('#'+styleLabel).css('z-index') == value){
			$('#'+styleLabel).trigger('load');
		}
		else{
			if(time < SETTINGS.timeout){
				setTimeout(function(){
					$.checkStyle(styleLabel,value,time+SETTINGS.range);	
					},SETTINGS.range);
			}
		}	
	},
	
	//lazy script loading method
	lazy : function () 
	{		
		var config = null;
			
		SETTINGS.cssList = new Array();
			
		SETTINGS.styleMarker = $("script").eq(0);
			
		// set relative rootpath to the page
		var rootString = $("script[src*='lazy.core']").attr('src');
		
		var rootIndex = rootString.indexOf("lazy.core");
			
		SETTINGS.rootPath =rootString.substring(0,rootIndex);
			
		$.ajax({
			type: "GET",
		   	url: SETTINGS.rootPath+"config.js",
		   	cache:true,
		   	async: false,
		   	success: function(json){
		        config= eval('(' + json + ')');
			}
		});

			
		//list of default methods/functions loaded in the initial page loading 
			
		var statics = config.statics;
		
		//list of jQuery methods loaded on demand and of corresponding file name 
		var onDemandComponents = config.components;
			
		var onDemandFunctions = config.functions;

		//setting of request manager for every methods/functions
		
		// static loading..cooming soon
		$.each(statics,function(index,element){
			var req = lazyRequest(element);
			if(!req){
				return false;	
			}
		});
		
			
		$.each(onDemandFunctions,function(index,element){		
			eval("$."+element.name+"=function(param){if(!lazyRequest(element)){return false;} return $."+element.name+"(param);};");
		});
		
		
		$.each(onDemandComponents,function(index,element){
			eval("$.fn."+element.name+"=function(){"+
			"var obj=$(this);"+
			"if(!lazyRequest(element)){"+
			"return false;}"+
			"return obj."+element.name+"(arguments[0]);"+
			"};");
			
		});
		
		//private script request function 
		function lazyRequest(element){
			var result = false;
			valore = $.ajax({
				type: "GET",
				cache:true,
				url: SETTINGS.rootPath+element.js,
				dataType: "script",
				async:false,
				success: function(obj){ 
		        	result = true;
				},
				error: function(xhr){
					$("body").html(xhr.responseText)
					result = false;
				}
			});	
			return result;	
		};
	}
});

var SETTINGS = $.lazy.settings = {
		cssList : null,
		version: "0.2",
		rootPath: "",
		range:100,
		timeout: 4000
	};

$.lazy();
	
})(jQuery);
