/*global jQuery */
(function($) {
 
    function mergeProperties(pl, propertyList) {
        $.each(propertyList, function() {
            var name = this.property;
            if (!pl[name]) {
                pl[name] = $.extend(true, {}, $.freemix.property.prototype, {config: this});
            } 
        });
    }
 
    $.freemix.property = {
        prototype: {
            config: {property: '', enabled: true, tags: [], width: undefined},
            listTypes: function() {
                var result = [];
                var types =  $.machineTagSearchRecordTags("property:type", [this.config]);

                $.each(types, function() {
                    result.push($.machineTag(this).value);
                });
                return result;
            },
            name: function() {
                return this.config.property;
            },
            enabled: function(enabled) {
                if (enabled !== undefined) {
                    this.config.enabled = enabled;
                } else {
                    return this.config.enabled;
                }
            },
            label: function(label) {
                if (label !== undefined) {
                    this.config.label = label;
                } else {
                    return this.config.label || this.config.property;
                }
            },            
            hasType: function(type) {
                return $.machineTagSearch("property:type=" + type, {records: [this.config], appendHash: false}).length > 0;   
            },
            expression: function() {
                return "." + this.config.property;
            },
            addType: function(type) {
                var prop = this;
                if (!prop.hasType(type) && $.freemix.property.type[type]) {
                    property.tags.push("property:type=" + type);
                    $.each($.freemix.property.type[type].dependsOn, function() {
                        prop.addType(this);
                    });
                    return true;
                } else {
                    return false;
                }
            },
            removeType: function(type) {
                var tag = "property:type=" + type;
                var tags = [];
                var result = false;
                $.each(this.config.tags, function() {
                    if (this != tag) {
                        tags.push(this);
                    } else {
                        result = true;
                    }
                });
                this.config.tags = tags;
                return result;
            },
            setTypes: function(types) {
                var tags = [];
                $.each(types, function() {
                    tags.push("property:type=" + this);
                });
                this.config.tags = tags;
            },
            defaultType: function(type) {
                if (!type) {
                    if (this.config.defaultType) {
                        return this.config.defaultType;
                    } 
                    var types = this.listTypes();
                    if (types.length > 0) {
                        return types[0];
                    }
                    return "text";
                } else {
                    this.config.defaultType = type;
                }
                
            },
            getExhibitHtml: function(type) {
                return $.freemix.property.type[type || this.defaultType()].getExhibitHtml(this);
            },
            getValueHtml: function(type, value) {
                return $.freemix.property.type[type || this.defaultType()].getValueHtml(this, value);
            }
        },
        initializeDataProfile: function() {
            var pl = {};
            mergeProperties(pl, $.freemix.profile.properties);
            
            var props = [];
            $.each(pl, function() {
                props.push(this.config);
            });
            $.freemix.profile.properties = props;
            this.propertyList = pl;            
        },
        initializeFreemix: function() {
            var pl = {};
            
            var localProperties = $.freemix.profile.properties;
            
            mergeProperties(pl, $.freemix.profile.properties);
            $.each($.freemix.profile.dataProfiles, function(key, profile) {
                mergeProperties(pl, profile.properties);
            });
            
            var props = [];
            $.freemix.profile.localProperties = {};
            $.each(pl, function() {
                var p = this.config;
                props.push(p);
                $.each(localProperties, function() {
                    if (p.property === this.property) {
                        $.freemix.profile.localProperties[p.property] = p;
                    }
                });
            });            
            $.freemix.profile.properties = props;          
            this.propertyList = pl;
        },
        getPropertiesWithType: function(type) {
            var results = [];
            $.each(this.propertyList, function(name, prop) {
                if (prop.hasType(type) && prop.enabled()) {
                    results.push(prop);
                }
            });
            return results;
        },
        getPropertiesWithTypes: function(types) {
            var results = [];
            $.each(this.propertyList, function(name, prop) {
                if (prop.enabled()) {
                    var found = false;
                    $.each(types, function() {
                        if (prop.hasType(this)) {
                            found = true;
                        }
                    });
                    results.push(prop);
                }
            });
            return results;            
        },
        enabledProperties: function() {
            var results = {};
            $.each(this.propertyList, function(name, prop) {
                if (prop.enabled()) {
                    results[name] = prop;
                }
            });
            return results;
        },
        type: {}        
    };
    
    $.freemix.property.type.text = {
        getValueHtml: function(metadata, value) {
            return "<span>" + value + "</span>";
        },
        getExhibitHtml: function(metadata) {
           return "<span ex:content='" + metadata.expression() + "'/>";
        },
        dependsOn: []
    };
    
    $.freemix.property.type.url = {
        getValueHtml: function(metadata, value) {
            return "<a href='" + value + "' target='_blank'>" + value + "</a>";
        },
        getExhibitHtml: function(metadata) {
            return "<a ex:href-content='" +  metadata.expression() + "' target='_blank'><span ex:content='" +  metadata.expression() + "'></span></a>";
        },
        dependsOn: []
    };
    
    $.freemix.property.type.image = {
        getValueHtml: function(metadata, value) {
            return "<a class='thickbox' href='" + value + "' onclick=\"tb_show(null, this.href, false);this.blur();return false\"><img src='" + value + "'/></a>";
        },
        getExhibitHtml: function(metadata) {
            return "<a class='thickbox' ex:href-content='" + metadata.expression() + "' onclick=\"tb_show(null, this.href, false);this.blur();return false\"><img ex:src-content='" + metadata.expression() + "'/></a>";
        },
        dependsOn: ["url"]
    } ;  


    $.freemix.property.type.datetime = $.extend({}, $.freemix.property.type.text,{
        // dependsOn: ["date", "time"]
    });
    // $.freemix.property.type["time"] = $.extend({}, $.freemix.property.type.text, {
    //     dependsOn: []
    // });
    // $.freemix.property.type.date = $.extend({}, $.freemix.property.type.text, {
    //     dependsOn: []
    // });
    $.freemix.property.type.location = $.extend({}, $.freemix.property.type.text, {
        dependsOn: []
    });    

})(jQuery);
