//
// JavaScript used for CodeResort.com user interface
//
// (c) 2005-2006 ::: BV Network AS
// simon@bvnetwork.no ::: Odd Simon Simonsen
//

(function($){
    
    $(document).ready(function () {

        // Helper function to do some URL calculation for getting CSS style element
        window.getStyleElementURL = function(base_location, filename) {
            var base;
            var possible_login = base_location.slice(base_location.length-6-1, base_location.length);
            if (possible_login == '/login/') {
                base = base_location.slice(0, base_location.length-6);
            } else {
                base = base_location;
            }
            var css_location = 'chrome/codebvn/css/';
            return base + css_location + filename;
        };

        // Toggles display & content of topheader projectlist navigation ("Navigate Code")
        window.codenav = function(base_location) {
            $('#codenav').toggle();
            collapsed_url = 'url(' + getStyleElementURL(base_location, 'collapsed.png') + ')';
            expanded_url = 'url(' + getStyleElementURL(base_location, 'expanded.png') + ')';
            use_image = $('#codenav').css('display') == 'none' ? collapsed_url : expanded_url;
            $('#codenavlink').css('backgroundImage', use_image);
            if (($('#codenav').css('display') === 'block') && ($('#userprojects').html() === '')) {
                $('#loading').toggle();
                $.ajax({
                    url: base_location + "stubs/userprojects",
                    success: function (data, textStatus) {
                            $('#userprojects').html(data);
                        },
                    complete: function (XHR, textStatus) {
                            $('#loading').toggle();
                        }
                });
            }
        };

        // Part of patch for logout - http://www.trac-hacks.org/wiki/TrueHttpLogoutPatch
        // jQuery'ed by osimons
        // Dependencies: .../logout must not require authentication
        window.clearAuthenticationCache = function(page, new_location) {
          // Default to a non-existing page (give error 500).
          // An empty page is better, here.
          var do_clear = function (page) {  
              try {
                if (jQuery.browser.msie) {
                    // IE clear HTTP Authentication
                    document.execCommand("ClearAuthenticationCache");
                }
                else {
                    $.ajax({
                        url: page,
                        username: 'logout',
                        password: 'logout',
                        complete: function (XHR, textStatus) {
                                XHR.abort();
                            }
                    });
                }
              } catch(e) {
                // There was an error
                return;
              }
          }
          // Clear the current project
          do_clear(page);
          // Clear about project
          do_clear(new_location + '/logout');
          // Switch to front page
          window.location.replace(new_location);
          document.location.replace(new_location);
          window.location = new_location;
          var agt=navigator.userAgent.toLowerCase();
          if (! jQuery.browser.msie) {
              // Information to non-IE browsers. Some irregularities may exist.
              alert("You are now logged out, but some visual traces may remain. " +
                    "Close the web browser to clear all session information.")
          }
        }

        
    });

})(jQuery);
