
(function($){

// don't run this if it has been imported twice
if (window.Core) return; 

var Core = {
    init: function() {
       Core.translations();
    },

    options: {
       site: 'unspecifiedsite',
       plugins: ''
    },

    settings: function(opt) {
      for (var v in opt) {
        Core.options[v] = opt[v];
      }
    },
    
    translations: function() {
    
        Core.translations_status = 'closed';
        Core.translations_interval = null;
        function check_status() {
             if (Core.translations_status == 'closed') {
                $("#translations").removeClass("open");
                
             }
             if (Core.translations_interval) window.clearInterval(Core.translations_interval);
        }
        function ql_open() {
             Core.translations_status = 'open';
             $("#translations").addClass("open");
             if (Core.translations_interval) window.clearInterval(Core.translations_interval);
             return false;
        }
        function ql_close(force) {
             Core.translations_status = 'closed';
             Core.translations_interval=self.setInterval(check_status,1000);
             return false;
        }
        
        function ql_toggle() {
             $("#translations").toggleClass("open");
             return false;
        }
        
        //$("#translations").click(check_status);

        $("#toggleQL").click(ql_toggle);
        $("#translist").hover(ql_open,ql_close);
        $("#toggleQL").hover(function() {
                               Core.translations_status = 'open';
                             },
                             ql_close);
        $("body").click(function(){Core.translations_status='closed';check_status() });
    },

    admin: function() {
       // test that this script isn't being called from a webstage server
       
       $("script").each(function(){
         if (document.location.host.indexOf('webstage') > -1 && this.src.indexOf('webdev') > -1) {
            alert("error: Can't call webdev file from webstage\n"+this.src);
         }
       });
       
       //load any webstage admin tools
        if (document.getElementById('edit_button')) {
           $.ajax({
              type: 'GET',
              url: '/admin/shared/js/wps.js',
              cache: true,
              success: function() {
                  Admin.init();
              },
              dataType: 'script',
              data: null
              });
        }
    },
    
    validated: function() {
        if (!Core.options.plugins) { return }
        var $forms = $("form.validated");
        if ($forms.size() > 0) {
           $.ajax({
              type: 'GET',
              url: Core.options.plugins+'/jquery.validate.js',
              cache: true,
              success: function() {
                 $(document).trigger('validator.loaded');
                 $forms.each(function(i,form) {
                    $(form).validate();
                 });
              },
              dataType: 'script',
              data: null
              });
        }
    },

    new_window: function(link) {

        // prevent doubleclicking
        if ($(link).hasClass("clicking")) return;
        $(this).addClass("clicking");
        window.setTimeout(function(){$(this).removeClass("clicking")},500);

        var url = link.href || link;
        var nw = window.open(url, "newwin", 'width=800,height=550,directories=yes,toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes');
        if (nw) {nw.focus();}
    },

    email_obfuscation: function() {
       function reverse(text) {
          return text.split("").reverse().join("");
       }
    
       $("a.to").each(function(i,a) {
          // replace the last '+' with an '@'
          // <a class="to" href="#">jgriffi+hbs.edu</a>
          // <a class="to" href="mailto:jgriffi+hbs.edu">Jeff Griffith</a>
          var email;
          if (a.href.indexOf("mailto:") > -1) { email = a.href.replace("mailto:",'') }
          else {email = a.innerHTML};
          email = reverse(email);
          email = email.replace(/\+/,"@");
          email = reverse(email);
          if (a.href.indexOf("mailto") == -1) { a.innerHTML = email };
          a.href = "mailto:"+email;
       });
    
    },

    // Utility Functions

    currurl: function() {
         var thisurl = document.location.href;
         if (thisurl.indexOf('#') > -1) {
            thisurl = thisurl.split(/#/)[0];
         }
         if (thisurl.indexOf('list_editables') > -1 ) {
            thisurl = document.referrer;
         }
         return thisurl;
    },

    normalize: function(url) {
        url = url.replace(/index.html/,'')
        return url;
    },

    sameurl: function(url,link) {
        if (!link || !link.href){return 0}
        if (Core.normalize(url) == Core.normalize(link.href)) {return 1};
        return 0;
    }
    
}

// jquery thisurl extension
jQuery.extend(jQuery.expr[':'],{thisurl:"Core.sameurl(Core.currurl(),a)"});

$(document).ready(function(){Core.init()});

/*
 *  Finds all img.rollover and enables mouseover rollovers
 */


jQuery.cookie = Core.cookie;
// an uncached js load
jQuery.loadJS = function(url,fn) {
   $.ajax({
        type: 'GET',
        url: url,
        cache: true,
        success: fn,
        dataType: 'script',
        data: null
   });
}

// window.Core = Core;
// window.onerror = Core.log.error;

// support an old API
// window.Core.SiteIndex = {};
// window.Core.SiteIndex.setheight = function(){};
// window.Core.SiteIndex.setfocus = function(){};

})(jQuery);



