(function($) {
    
    $.fn.anchorWarnings = function(options) {
        
        options = $.extend({
            
            enabled: true,
            message: "Are you sure you want to interrupt operations by navigating away from the page?"
            
        }, options);
        
    }
    
    
    // Note: This method is not safe to use yet =/
    $.fn.embedFlashSafely = function(options) {
        
        var safetyOptions = {};
        
        for(var i in options) {
            
            safetyOptions[i] = options[i];
                
        }
        
        var flash = $(this);
        var flashSWF;
        var flashSuccess = options.success;
        
        
        var safety = $('<div id="' + flash.attr('id') + '-safety"></div>');
        var safetySWF;
        
        safetyOptions.source = "/media/flash/safetynet.swf";
        safetyOptions.success = function(swf) {
            
            safetySWF = swf;
            
        }
        
        options.success = function(swf) {
            
            flashSWF = swf;
            
            if(flashSuccess) {
                
                flashSuccess(swf);
                
            }
            
        }
        
        flash.before(safety);
        //flash.detach();
        
        safety.embedFlash(safetyOptions);
        flash.addClass('hidden').embedFlash(options);
        
        var resultHandler = function(error) {
            
            if(error != undefined) {
                
                if(safetySWF) {
                    
                    safetySWF.get(0).showErrorMessage(error);
                    
                }
                
                if(flashSWF) {
                    
                    flashSWF.remove();
                    
                }
                
                flash.remove();
                
            } else {
                
                if(safetySWF) {
                    
                    safetySWF.replaceWith(flashSWF);
                    
                } else {
                    
                    safety.replaceWith(flashSWF);
                    
                }
                
            }
            
        };
        
        return resultHandler;
        
    }
    
    $.fn.embedFlash = function(options) {
        
        options = $.extend({
            
            container: this,
            source: "",
            width: "640",
            height: "480",
            cssClass: "",
            flashvars: {},
            params: {},
            attributes: {},
            version: "9.0.0",
            expressInstall: "flash/expressInstall.swf",
            success: function() {},
            error: function() {}
            
        }, options);
        
        if(options.source != "") {
            
            options.flashvars = $.extend(
                {}, options.flashvars
            );
            
            options.params = $.extend(
                {
                    
                    "allowscriptaccess"	: "always",
                    "allowfullscreen"	: "true"
                    
                }, options.params
            );
            
            options.attributes = $.extend (
                {}, options.attributes
            );
            
            options.attributes["class"] = options.cssClass;
            
            var flashID = this.first().attr('id');
            
            swfobject.embedSWF(
                options.source, 
                flashID, 
                options.width, 
                options.height, 
                options.version, 
                options.expressInstall, 
                options.flashvars, 
                options.params, 
                options.attributes, 
                function(result) {
                    
                    if(result.success) {
                        
                        options.success($('#' + flashID));
                        
                    } else {
                        
                        options.error();
                        
                    }
                }
            );
        }
        
        return this;
        
    }
    
    $.fn.embedClippy = function(options) {
        
        options = $.extend({
            
            container: this,
            textToCopy: "",
            remove: false
            
        }, options);
        
        options.container.each(function(i, e) {
            
            $(e).embedFlash(
                {
                    source: "/flash/clippy.swf",
                    width: "110px",
                    height: "14px",
                    cssClass: "flash-clippy",
                    version: "9.0.0",
                    flashvars: {
                        text: options.textToCopy
                    },
                    params: {
                        bgcolor: "#FFFDE5"
                    }
                }
            );
        }); 
        
        return this;
        
    }
    
    $.expr[':'].external = function(e) {
        
        return (e.host && e.host !== location.host) === true;
        
    };

    $.fn.trackEvent = function(options) {
        
        if(_gaq) {
            
            options = $.extend(
                {
                    events: 'click',
                    bind: 'bind',
                    category: this.nodeName,
                    action: null,
                    label: null,
                    value: 1
                }, 
                options
            );
            
            var targets = this;
            
            var resolveLabel = function(element) {
                
                if(element.is('a') && element.attr('href') != "javascript:void(0);") {
                    return element.attr('href');
                }
                else if(element.is('input')) {
                    return element.val();
                }
                else if(element.attr('id')) {
                    return element.attr('id');
                }
                else{
                    return element.text();
                }
                
            }
            
            options.events = $.isArray(options.events) ? options.events : [options.events];
            
            $.each(
                options.events,
                function(i, e) {
                    
                    var events = e;
                    var targetOptions = $.extend({}, options);
                    
                    targets[targetOptions.bind](
                        
                        events,
                        function(event) {
                            
                            targetOptions.action = targetOptions.action ? targetOptions.action : event;
                            targetOptions.label = targetOptions.label ? targetOptions.label : resolveLabel(targets);
                            
                            _gaq.push(
                                [
                                    '_trackEvent', 
                                    targetOptions.category,
                                    targetOptions.action,
                                    targetOptions.label,
                                    targetOptions.value
                                ]
                            );
                        }
                    );
                }
            );
            
        } else {
            
            // TODO: Report missing analytics...
            
        }
    };
    
    
})(jQuery);