var Emv = Emv || {};
Emv.Mediabizde = Emv.Mediabizde || {};
Emv.Videode = Emv.Videode || {};
Emv.Kinode = Emv.Kinode || {};

Emv.Config = {

    flashVersion: '9.0.124',
    fpDomain: {
        videoDe: 'http://flashplayer.video.de',
        kinoDe: 'http://flashplayer.kino.de',
        mediabizDe: 'http://flashplayer.mediabiz.de'
    }
};

Emv.Window = {

    /**
     * Open popup window.
     * 
     * @param string url
     * @param integer width
     * @param integer height
     * @param string target
     * @return boolean
     */    
    open: function(url, width, height, target) {

        if ('undefined' == typeof width) {

            width = 200;
        }
        
        if ('undefined' == typeof height) {
            
            height = 200;
        }
        
        if ('undefined' == typeof target) {
            
            target = 'popup';
        }
        
        topPositon  = (screen.height / 2) - (height / 2);
        leftPositon = (screen.width / 2) - (width / 2);

        oWindow = window.open(url, target, 'fullscreen=no, resizable=yes, status=yes, toolbar=no, menubar=no, location=no, width=' + width + ', height=' + height +', top=' + topPositon + ', left=' + leftPositon);

        if (!oWindow) {

            alert('Bitte lassen Sie Popup-Fenster zu.');
        } else {

            oWindow.focus();
        }

        return false;
    },
    
    /**
     * Return window-object for custom use.
     * 
     * @param string url
     * @param integer width
     * @param integer height
     * @param string target
     * @param string scrollbars
     * @return object|boolean
     */
    returnPopup: function(url, width, height, target, scrollbars) {

        if ('undefined' == typeof width) {

            width = 200;
        }
        
        if ('undefined' == typeof height) {
            
            height = 200;
        }
        
        if ('undefined' == typeof target) {
            
            target = 'popup';
        }
        
        if ('undefined' == typeof scrollbars) {
            
            scrollbars = '';
        }

        topPositon  = (screen.height / 2) - (height / 2);
        leftPositon = (screen.width / 2) - (width / 2);

        oWindow = window.open(url, target, scrollbars + ', fullscreen=no, resizable=yes, status=yes, toolbar=no, menubar=no, location=no, width=' + width + ', height=' + height +', top=' + topPositon + ', left=' + leftPositon);

        if (!oWindow) {

            alert('Bitte lassen Sie Popup-Fenster zu.');
        } else {

            return oWindow;
        }

        return false;
    }
};

Emv.Dom = {
        
    isChildOf: function(parent, child) {
        
        if (null !== child) {
            
            while (child.parentNode) {
                
                if ((child = child.parentNode) == parent) {
                    
                    return true;
                }
            }
        }
        
        return false;
    },

    /**
     * Checks if mouseout is really on this element and not a mouseout with mouseover on nested elements.
     * 
     * @param current element
     * @param event
     * @return boolean
     */
    isMouseOut: function(element, event) {
        
        currentMouseTarget = null;
        
        if (event.toElement) {
            
            currentMouseTarget = event.toElement;
        } else {
            
            if (event.relatedTarget) {
                
                currentMouseTarget = event.relatedTarget;
            }
        }
        
        return !this.isChildOf(element, currentMouseTarget) && element != currentMouseTarget ? true : false;
    }
};

/**
 * Registry for storing objects
 * 
 * (all stored objects has to have getId implemented)
 */
Emv.Registry = {
        
    // Stored objects
    objects: [],

    // Stored key-value pairs
    params: [],

    /**
     * Storing javascript objects
     * all objects has to implement function getId otherwise an exception is thrown
     * 
     * @params object obj
     * @throws exception
     */
    setObject: function(obj) {
        
        //console.log('+++ Emv.Registry.setObject: ' + obj.getId());
        if ('function' !== typeof obj.getId) {
            
            throw (typeof(obj) + ' hat keine Funktion "getId()" definiert - bitte noch implementieren');
        } else {
            
            if (null === Emv.Registry.getObject(obj.getId())) {
                //console.log('+++ Emv.Registry.setObject: ' + obj.getId());
                this.objects[this.objects.length] = obj;
            }
        }
    },

    /**
     * Returns object for id
     * 
     * @param string id
     * @return object|null
     */
    getObject: function(id) {
        
        //console.log("<<< Emv.Registry.getObject: " + id);
        for (j = 0, l = this.objects.length; j < l; j++) {
            
            var o = this.objects[j];
            if (o.getId() == id) {
                
                return o;
            }
        }
        
        return null;
    },

    /**
     * storing of key value pairs
     * @param string key
     * @param mixed  value
     */
    setParam: function(key, value) {
        
        if (Emv.Registry.getParam(key) !== null) {

            for(j = 0, l = this.params.length; j < l; j++) {
                
                var p = this.params[j];
                if (p.getKey() == key) {
                    
                    this.params[j] = new Emv.Registry.Param(key, value);
                }
            }
        } else {
            
            this.params[this.params.length] = new Emv.Registry.Param(key, value);
        }
    },

    /**
     * Returns value for key
     * 
     * @param string key
     * @return mixed
     */
    getParam: function(key) {
        
        for(j = 0, l = this.params.length; j < l; j++) {
            
            var p = this.params[j];
            if (p.getKey() == key) {
                
                return p.getValue();
            }
        }
        
        return null;
    }
};

/**
 * object to store key-value pairs in registry
 * dataholder for Emv.Registry params
 * 
 * @param string key
 * @param mixed val
 */
Emv.Registry.Param = function(key, val) {
    
    this.key = key;
    this.value = val;

    /**
     * @return string
     */
    this.getKey = function() {
        
        return this.key;
    };

    /**
     * @param mixed
     */
    this.getValue = function() {
        
        return this.value;
    };
};

// Adding 'trim'-function to String object.
if ('undefined' == typeof String.prototype.trim) {
    
    String.prototype.ltrim = function() {

        return this.replace(/^\s+/, '');
    };

    String.prototype.rtrim = function() {

        return this.replace(/\s+$/, '');
    };

    String.prototype.trim = function() {

        return this.ltrim().rtrim();
    };
}
