//this object requires 'php.js' library
//it can't be called by requireScript, so 'php.js' have to be called manualy

var geck = {
	//variables
	_private : {},
	
	//public variables
	vars : {},
	
	addVars : function(data)
	{
		if (data !== '' && 
		    data !== [] &&
		    data !== {}) {
			
			//var vars = json.decode(data);
			
			if (this.vars == {}) {
				this.vars = data;
			} else {
				$.extend(this.vars, data);
			}
		}
	},
	
	//Loads required script file (if is not already loaded)
	requireScript : function (scriptFile, callback) 
	{ 
		var i, notExists = true, scripts = document.getElementsByTagName('script');

        //if is not absolute path, create relative
        if ('http' !== scriptFile.substr(0, 4))
            scriptFile = baseUrl + '/public/' + php.ltrim(scriptFile,'/');

	    //check whether or not file already exists in document
	    for (i=0; i<scripts.length; i++){
	    	if (scripts[i].src.match( scriptFile )){
	    		notExists = false;break;
	    	}
	    }
		//if file not - add it to document
	    if (notExists) {
	    	document.write('<script type="text/javascript" src="' + scriptFile + '"></script>');
	    }
    	//if is set callback function, do callback on script file load complete
	    if (typeof(callback) == 'function') {
	    	var done = false, script, scripts = document.getElementsByTagName('script');
		    //findout loaded script 
	    	for (i=0; i<scripts.length; i++){
		    	if (scripts[i].src.match( scriptFile )){
		    		script = scripts[i];break;
		    	}
		    }
	    	//set onload callback
		    script.onload = script.onreadystatechange = function() {
				if ( !done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
					done = true;
					callback();
				}
			};
	    }
	},
	
	//Loads required style file (if is not already loaded)
	requireStyle : function (styleFile, media) 
	{ 
		//prepare variables
		styleFile = baseUrl + '/public/' + php.ltrim(styleFile,'/');
		if (media == undefined) {media = 'screen';}
		//create link element
		var link = document.createElement("link");
		link.setAttribute('rel', 'stylesheet');
		link.setAttribute('type', 'text/css');
		link.setAttribute('media', media);
		link.setAttribute('href', styleFile);
		//append link element to header
		document.getElementsByTagName('head')[0].appendChild(link);
	},
	
	//Loads required style file (if is not already loaded)
	removeStyle : function (styleFile) 
	{ 
		//prepare variables
		styleFile = baseUrl + '/public/' + php.ltrim(styleFile,'/');
		//find link with style
		$('link[href="'+styleFile+'"]').remove();
	},
	
	//generate guid
	guid : function ()
	{
        var guid = '';
        
        for(i=1;i<=32;i++) {
        	var numb = Math.round(Math.random()*35);
        	
        	if (numb > 9) {
        		guid += String.fromCharCode( numb + 55); //add letter
        	} else {
        		guid += String.fromCharCode( numb + 48); //add number
        	}
        }
        
        return guid;
	},
	
	//toggle site overlay
	toggleOverlay: function(args)
	{
		args = args || {};
		
		var defaults = {
			id:     'siteOverlay',
			zIndex:  1000,
			speed:   500,
			opacity: 0.5,
			color:  'black'};
		
		var args = jQuery.extend(defaults, args);
		var overlay = $('#' + args.id);
		
		if (overlay.length) {
			overlay.animate(
				{opacity:0}, 
				(args.speed / 2), 
				function() {$(this).remove();}
			);
		} else {
			overlay = $('<div>');
			overlay.attr('id', args.id);
			overlay.css({
				position: 'absolute',
				left: 	  '0px',
				top:      '0px',
				width:    $(document).width(),
				height:   $(document).height(),
				zIndex:   args.zIndex,
				opacity:  0});
			overlay.animate({left:0},1,function() {
				$(this).animate({opacity:0.5}, args.speed);
			});
			overlay.appendTo('body');
			
			return overlay;
		}
		
		return false;
	}
};
