﻿
(function($)
{
    $.fn.extend({
        applySOFA: function(options)
        {
            var defaults = {
                preApplySofa: function() { },
                postApplySofa: function() { },
                useGoogleAnalytics: false
            };

            var options = $.extend(defaults, options);
            return this.each(function()
            {
                var input = $(this);
                ApplySOFA(this);
                function ApplySOFA(domnode)
                {
                    options.preApplySofa();
                    var href;
                    var $a;
                    $(domnode).find("a").each(function(index)
                    {
                        $a = $(this);

                        href = $a.attr("href");
                        if ($a.attr("dest") == null || $a.attr("src") == null)
                        {
                            var isDoc = $a.attr("href").match(/\.(?:doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)($|\&|\?)/);
                            if (isDoc)
                            {
                                $a.click(function()
                                {
                                    pageTracker._trackPageview($(this).attr("href"))
                                });
                            }
                            return;
                        }

                        $a.attr("href", "javascript:void(0)");
                        $a.attr("hrefbak", href);
                        $a.click(function()
                        {
                            var dest = $(this).attr("dest");
                            var src = $(this).attr("src");
                            var href = $(this).attr("hrefbak");
                            if (options.useGoogleAnalytics)
                                pageTracker._trackPageview(href);
                            if (src == null || dest == null)
                            {
                                window.location = href;
                            }
                            else
                            {
                                $.post(href, "",
                                function(data)
                                {
                                    SetHtml(data, dest, src);
                                },
                                "html"
                                );
                            }
                            options.postApplySofa(href);

                        });
                    });

                }
                function SetHtml(data, dest, src)
                {
                    var $dest = $(dest);
                    var $data = $(data);

                    //Apply SOFA to the src data
                    ApplySOFA($data);

                    $dest.html("");
                    $dest.append($(src, $data));
                }


            });
        }
    });
})(jQuery);




