var FrameManager = function(namespace) {
	this.namespace = namespace;
}

FrameManager.prototype = {
	namespace: '',
    currentFrameId : '',
    currentFrameHeight : 0,
    lastFrameId : '',
    lastFrameHeight : 0,
    resizeTimerId : null,
	init: function (){
		if (this.resizeTimerId == null){
			this.resizeTimerId = window.setInterval(this.namespace+"FrameManager.resizeFrames("+this.namespace+"FrameManager)", 500);
		}	
	},
    resizeFrames : function(instance) {
        instance.retrieveFrameIdAndHeight(instance);

        if ((instance.currentFrameId != instance.lastFrameId) ||
            (instance.currentFrameHeight != instance.lastFrameHeight))
        {
            var iframe = document.getElementById(instance.currentFrameId.toString());
            if (iframe == null) return;
            iframe.style.height = instance.currentFrameHeight.toString() + "px";
            instance.lastFrameId = instance.currentFrameId;
            instance.lastFrameHeight = instance.currentFrameHeight;
            window.location.hash = '#';
        }
    },
    retrieveFrameIdAndHeight : function(instance) {
        if (window.location.hash.length == 0) return;
        var hashValue = window.location.hash.substring(1);
        if ((hashValue == null) || (hashValue.length == 0)) return;
        var pairs = hashValue.split('&');
        if ((pairs != null) && (pairs.length > 0)){
            for(var i = 0; i < pairs.length; i++){
                var pair = pairs[i].split('=');
                if ((pair != null) && (pair.length > 0)){
                    if (pair[0] == 'frameId'){
                        if ((pair[1] != null) && (pair[1].length > 0)){
                            instance.currentFrameId = pair[1];
                        }
                    } else if (pair[0] == 'height') {
                        var height = parseInt(pair[1]);
                        if (!isNaN(height)) {
                            instance.currentFrameHeight = height;
                            instance.currentFrameHeight += 15;
                        }
                    }
                }
            }
        }
    },
    registerFrame : function(frame) {
        var currentLocation = location.href;
        var hashIndex = currentLocation.indexOf('#');
        if (hashIndex > -1){
            currentLocation = currentLocation.substring(0, hashIndex);
        }
        frame.contentWindow.location = frame.src + '?frameId=' + frame.id + '#' + currentLocation;
    }
};