ark.namespace('util.LazyContent');

(function() {
    var Event = YAHOO.util.Event
        CustomEvent = YAHOO.util.CustomEvent
        Connect = YAHOO.util.Connect
        Dom = YAHOO.util.Dom;
        
ark.util.LazyContent = function() {
    this.cache = {};
    this._on_render = false;

    this.flushCache = function() {
        this.cache = {};
    }

    this.clearCache = function(key) {
        this.cache[key] = null;
    }

    this.load = function(key,url,parameters) {
        if( this.cache[key] ) {
            if( !this.cache[key]['loading'] ) {
                obj.LoadedEvent.fire(this.cache[key]);
                return this.cache[key];
            } else {
                return null;
            }
        } else {
            if( url.indexOf('?') != -1 ) {
                var parts = url.split('?');
                url = parts[0];
                parameters = parts[1];
            }
            var callback = {
                success: this.loaded,
                failure: this.failed,
                timeout: 4000,
                argument: { obj: this, key: key, url: url }
            };
            Connect.asyncRequest('POST',url,callback,parameters);

            this.cache[key] = { loading: true, url: url };
            
            return null;
        }
    }

    this.loaded = function(oResponse) {
        try {
            var oR = eval("("+oResponse.responseText+")");
        } catch ( e ) {
            var oR = { };
        }
        obj = oResponse.argument['obj'];
        key = oResponse.argument['key'];
        obj.cache[key]['loading'] = false;
        obj.cache[key]['key'] = key;

        if( oR._html ) {
            obj.cache[key]['_html'] = oR._html;
        }
        if( oR._confirm ) {
            obj.cache[key]['_confirm'] = oR._confirm;
        }
        if( oR._error ) {
            obj.cache[key]['_error'] = oR._error;
        }

        if( obj._on_render && oR._html ) {
            var oNode = document.createElement('div');
            oNode.setAttribute('id','_ark_util_lazy_content_node_'+key);
            // TODO strip javascript out
            oNode.innerHTML = oR._html;
            obj.oCache.appendChild(oNode);
            obj.cache[key]['size'] = [];
            obj.cache[key]['counter'] = 0;
            var fn = function() {
                obj.check_rendered(key,oNode);
            }
            setTimeout(fn,100);
        } else {
            obj.LoadedEvent.fire(obj.cache[key]);
        }

		// redirecting page if needed
		if(oR._redirect){
			oR._redirect = oR._redirect.replace(/&amp;/,"&");
			self.location = oR._redirect;	
		}
    }

    this.check_rendered = function(key,oNode) {
        var i = this.cache[key]['counter'];
        var test = oNode.childNodes[0];
        this.cache[key]['size'][i] = { height: test.scrollHeight, width: test.scrollWidth };
        this.cache[key]['counter'] = this.cache[key]['counter'] + 1;
        if( i > 0 ) {
            var w1 = this.cache[key]['size'][i-1]['width'];
            var h1 = this.cache[key]['size'][i-1]['height'];

            var w2 = this.cache[key]['size'][i]['width'];
            var h2 = this.cache[key]['size'][i]['height'];

            //alert('1: '+w1+'x'+h1+' 2:'+w2+'x'+h2);
            if( h1 != h2 ) {
                var obj = this;
                var fn = function() {
                    obj.check_rendered(key,oNode);
                }
                setTimeout(fn,100);
            } else {
                this.LoadedEvent.fire(this.cache[key]);
            }
        } else {
            var obj = this;
            var fn = function() {
                obj.check_rendered(key,oNode);
            }
            setTimeout(fn,100);
        
        }
        
    }


    this.failed = function(oResponse) {
        obj = oResponse.argument['obj'];
        key = oResponse.argument['key'];
        obj.cache[key]['loading'] = false;
        if( oResponse.status == -1 ) {
            // Timeout
            obj.TimedOutEvent.fire();
        } else {
            // Regular Failure
            obj.FailedEvent.fire();
        }
    }

    this.in_cache = function(key) {
        if( this.cache[key] ) {
            return true;
        } else {
            return false;
        }
    }

    this.LoadedEvent = new CustomEvent('LoadedEvent',this);
    this.FailedEvent = new CustomEvent('FailedEvent',this);
    this.TimedOutEvent = new CustomEvent('TimedOutEvent',this);

    if( this._on_render ) {
        this.oCache = document.createElement('div');
        this.oCache.setAttribute('id','_ark_util_lazy_content_cache');
        Dom.setStyle(this.oCache,'position','absolute');
        Dom.setStyle(this.oCache,'display','block');
        Dom.setStyle(this.oCache,'visibility','hidden');
        Dom.setX(this.oCache,-5000);
        Dom.setY(this.oCache,-5000);
        document.body.appendChild(this.oCache);
    }

}

})();

