    
    var proxy = "http://"+ document.domain + "/Proxy.aspx?url=";
    //var proxy = "http://www.coloradoultimate.org/Proxy.aspx?url=";
    //var proxy = "Proxy.aspx?url=";
       
   
    function Cookie(name,value,days,path) //set days = -1 to delete
    {
        if (!path) path = "/";
	    if (value)  //create/edit it  
	        {
	            if (days) 
	                {
		            var date = new Date();
		            date.setTime(date.getTime()+(days*24*60*60*1000));
		            var expires = "; expires="+date.toGMTString();
	                }
	            else var expires = "";
	            document.cookie = name+"="+value+expires+"; path=" +path;
            }
        else try  // read it
            {
	        var nameEQ = name + "=";
	        var ca = document.cookie.split(';');
	        for(var i=0;i < ca.length;i++) 
	            {
		            var c = ca[i];
		            while (c.charAt(0)==' ') c = c.substring(1,c.length);
		            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	            }
	        } catch (e) {}
	    return null;
    }

    
     function Process()
     {
        //fade in...    
        $(this).hide(); 
        
        //wait till ready
        var needs = $(this).attr('needs');
        if (needs)
            {
            if (! $(needs).hasClass('loaded'))
                {
                setTimeout(this.Process, 500);
                return;
                }
            }
        
        
        //enable editables
        if ($(this).hasClass('editable') 
            && (Cookie("edit")==="true" || window.bledit))
            {
            $(this).click(function()
                    {
                        var url = $(this).attr('url');
                        if (!url) throw "edit link with no url";
                        if (url.match("google")) url = url.replace("View?","Doc?");
                        window.open(url);
                    });
            }
        else $(this).removeClass('editable');        

        //fix google docs image links
        $(this).find('img').each(function(i,self) 
            {
                var href = $(this).attr('src');
                href = href.match(/File\?id.*/);
                if (href) 
                    {
                    href = "http://docs.google.com/" + href;
                    $(this).attr('src',href);
                    }
            });     
                
        
        $(this).find(":last('br')").remove();  //remove google crap
        $(this).find(":last('br')").remove();  //remove google crap
        
        // convert emails to mailto links
        $(this).find(":contains('@')").each(function(i,self)  
            {
                var txt = $(this).html();
                if (txt.match('href')) return true; //ignore existing links
                txt = txt.replace(/([\w\+\-\._]+@[\w\-\._]+\.\w{2,})/ig,'<a href="mailto:$1">$1</a>');
                $(this).html(txt);
            }); 
        
        
        //process [[ embedded ]] html
        var txt = $(this).html();
        if(txt.indexOf("[[") != -1)
        {
            var out = "" + txt.charAt(0) + txt.charAt(1);
            var list = [];     //find and copy inner text blocks [[...]]
            var str = false;

            for (var i=2; i<txt.length; i++)
            {
                var j=0;
                if (str || (txt.charAt(i-1) == "[" && txt.charAt(i-2) == "[")) 
                    {
                        if (!str) str = "";
                        str += txt.charAt(i);
                    }
                if (!str) out += txt.charAt(i);
                else if (txt.charAt(i+1) == "]" && txt.charAt(i+2) == "]") 
                    {
                    list[j++] = str;
                    str = false;
                    }
            }
            txt = out;
        
        
            if (!list[0]) $(this).html(txt);
            else
            {
                for (var i=0; i<list.length; i++) 
                {
                    var s= list[i];
                    s = s.replace(/<[^>]*>/g,"")     //strip out html
                        .replace(/&lt;/ig,"<")
                        .replace(/&gt;/ig,">")       //decode embedded html
                        .replace(/&nbsp;/ig," ")
                        .replace(/<title/ig,"<div class='title'").replace(/<\/title>/ig,"</div>")
                        .replace(/<style/ig,"<div class='style'><div").replace(/<\/style>/ig,"</div></div>")
                        .replace(/<script/ig,"<div class='script'><div").replace(/<\/script>/ig,"</div></div>")
                        .replace(/<meta/ig,"<div class='meta'><div").replace(/<\/meta>/ig,"</div></div>")
                        .replace(/<base/ig,"<div class='base'><div").replace(/<\/base>/ig,"</div></div>");
                        
                     //alert(s);
                      
                    txt = txt.replace("[[]]",s);  
                }
                $(this).html(txt);  
                
                $(this).find('.title').each(function(){
                        document.title = $(this).html();
                        $(this).remove();
                        });

                 $(this).find('.style').each(function(){
                        var txt = $(this).html();
                        txt = txt.replace(/<div/i,"<style").replace(/<\/div>/i,"</style>");
                        $('head').append( txt );
                        $(this).remove();
                        });

                 $(this).find('.script').each(function(){
                        var txt = $(this).html();
                        txt = txt.replace(/<div/i,"<scr"+"ipt").replace(/<\/div>/i,"</sc"+"ript>");
                        $('head').append( txt );
                        $(this).remove();
                        });

                 $(this).find('.meta').each(function(){
                        var txt = $(this).html();
                        txt = txt.replace(/<div/i,"<meta").replace(/<\/div>/i,"</meta>");
                        $('head').append( txt );
                        $(this).remove();
                        });

                 $(this).find('.base').each(function(){
                        var txt = $(this).html();
                        txt = txt.replace(/<div/i,"<base").replace(/<\/div>/i,"</base>");
                        $('head').append( txt );
                        $(this).remove();
                        });
            }

        }
        
        //process [* javascript *]
        txt = $(this).html();
        txt = txt.replace(/\[\*/g,"<span class='eval'>");
        txt = txt.replace(/\*\]/g,"</span>");
        $(this).html(txt);
        $(this).find(".eval").each(function(){
            var js = $(this).html();
            $(this).html( eval(js) );
            });
        
        
        
        window.status = "";      

        //fade in...
        var fade = $(this).attr('fade');    
        fade = parseInt(fade)?parseInt(fade):fade;
        if (!fade) fade = 600;
        $(this).fadeIn(fade);
        
        //recursive loading...
        $(this).find('.load').each(loader);  
        $(this).addClass('loaded');    
        
        //fix google docs links (must run late to ensure click events get linked up
        $(this).find('a').each(function(i,self)  
            {
                var href = $(this).attr('href');
                if (!href) return;
                
                href = href.match(/View\?docid.*/);
                if (href) // is a google doc
                    {
                    href = encodeURI("http://docs.google.com/" + href);
                    var docid = href.match(/docid=\S*/);
                    $(this).attr('href',"#" + docid);
                     if ($('#main'))  //open as a sub document
                        {
                        $(this).click(function(){
                                $('#main')
                                    .addClass('editable')                                
                                    .attr('url',href)
                                    .load(proxy +href+ ' #doc-contents',{},Process);
                                    
                                    myUrl = myUrl.replace(/#.*/,"?" + docid);
                                    window.parent.location.href = myUrl;
     
                                return false;
                                });
                        }
                    }
                  
            });   
            
     }


    function loader()
    {
            var url =  encodeURI($(this).attr('url'));
            if (!url) url = "";
            var docid = $(this).attr('docid');
            if (!docid) docid = url.match(/docid=\S*/i);
            if (!docid) docid = url.match(/id=\S*/i);
            if (url.indexOf("docs.google.com") < 0 && docid) url = "http://docs.google.com/View?docid=" + docid;
            var expr = $(this).attr('expr');
            if (!expr) expr = '#doc-contents';
            $(this).attr('url',url);
            window.status = "loading " + url;
            $(this).load(proxy+url+" "+expr,{}, Process);

    }

    $(document).ready( function(){
        //var pageTracker = _gat._getTracker("UA-7700574-1");
        //pageTracker._trackPageview();
    
        // are we editing?
        var href = location.href;
        
        if (href.match("edit=true")) { Cookie("edit","true"); window.bledit = true; }
        if (href.match("edit=false")){ Cookie("edit","false",-1); window.bledit = false; }
        
        //prepare to load main if url exists in querystring
        var url = encodeURI(href).match(/url=(\S*)/i);
        if (url && url[1]) url = url[1];

        //docid trumps url
        var docid = encodeURI(href).match(/docid=\S*/i);
        if (docid) url = "http://docs.google.com/View?" + docid;
        
        var expr = encodeURI(href).match(/expr=(\S*)/i);
        if (expr && expr[1]) expr = expr[1];
        if (!expr) expr = '#doc-contents';
        
        
        if (url) $('#main')
            .attr('url',url)
            .attr('expr',expr)
            .addClass('load');

        //load all docs
        $('.load').each(loader);
        
        $('.loadlink').click(function()
            {
                var docid = $(this).attr('docid');
                var url = "http://docs.google.com/View?docid=" + docid;
                $('#main').load(proxy+url+" #doc-contents",{}, Process);
                return false;
            });
    
     });   