﻿iFrame = function() { };

/* Private Properties */
iFrame.prototype.__validContext = true;
iFrame.prototype.__frameName = '__content_frame';
iFrame.prototype.__frameObject = null;
iFrame.prototype.__currentPage = 0;
iFrame.prototype.__inRequest = false;
iFrame.prototype.__scrollWindowT = null;
iFrame.prototype.__pagerLink = null;
iFrame.prototype.__pagerLoad = null;

/* Public Properties */
iFrame.prototype.ServiceName = null;
iFrame.prototype.PageSize = null;
iFrame.prototype.PageContainer = null;
iFrame.prototype.QueryParameters = null;

/* Initialize Request */
iFrame.prototype.initializeRequest = function() {

    if (this.PageContainer != null) {
        if (typeof (this.PageContainer) != Object) {
            this.PageContainer = YAHOO.util.Dom.get(this.PageContainer);

            if (this.PageContainer == null) {
                this.__validContext = false;
            }
        }
    } else {
        this.__validContext = false;
    }

    if (this.PageSize == null) {
        this.__validContext = false;
    }

    if (this.ServiceName == null) {
        this.__validContext = false;
    }

    if (this.__validContext == true) {
        this.__pagerLink = YAHOO.util.Dom.get('pagerLink');
        this.__pagerLoad = YAHOO.util.Dom.get('pageLoading');

        var url = this.buildUrl();
        url += '&currentPage=0';

        YAHOO.util.Event.onContentReady('mainContent', function() {
            iFrame.submitRequest(url);
        });
    }
};

/* Build URL */
iFrame.prototype.buildUrl = function() {
    var url = this.ServiceName + '.aspx?';

    if (this.QueryParameters != null) {
        url += this.QueryParameters + '&';
    }

    url += 'pageSize=' + this.PageSize;

    return url;
};

/* Submit Request */
iFrame.prototype.submitRequest = function(url) {
    if (this.__frameObject === null) {
        this.__frameObject = YAHOO.util.Dom.get(this.__frameName);
    }

    if (this.__inRequest == false) {
        this.__inRequest = true;
        this.__frameObject.src = url;
    }
};

/* Send Request */
iFrame.prototype.sendRequest = function(query) {
    this.__pagerLink = YAHOO.util.Dom.get('pagerLink');
    this.__pagerLoad = YAHOO.util.Dom.get('pagerLoading');

    this.__pagerLink.style.display = 'none';
    this.__pagerLoad.style.display = 'block';
    
    if (query === null) {
        return;
    }

    if (this.__inRequest === true) {
        return;
    } else {
        var url = this.buildUrl() + '&' + query;
        this.submitRequest(url);
    }

    return false;
};

/* Receive Request */
iFrame.prototype.receiveRequest = function(content) {
    if (content === null) {
        return false;
    } else {
        this.__inRequest = false;
        this.PageContainer.innerHTML = content;
        //this.PageContainer.innerHTML = 'tom';
    }
};

/* Send Response */
iFrame.prototype.sendResponse = function(contentId) {
    if (contentId === null) {
        return;
    } else {
        var __iFrame = document.getElementById(contentId);

        if (__iFrame == null) {
            return;
        } else {
            //alert(__iFrame.innerHTML);
            //this.scrollWindow();
            parent.iFrame.receiveRequest(__iFrame.innerHTML);
        }
    }
};

/* Scroll Window */
iFrame.prototype.scrollWindow = function() {
    if (parent.document.body.scrollTop != 0
        || parent.document.documentElement.scrollTop != 0) {
        parent.window.scrollBy(0, -10);
        this.__scrollWindowT = setTimeout('iFrame.scrollWindow()', 10);
    } else {
        window.clearTimeout(this.__scrollWindowT);
    }
};