// Copyright Jonathan Poland Online LLC

var gg;
if (!gg) {
    gg = {};
}

if (!gg.Site) {
    gg.Site = {};
}
gg.Site.currentSubsite = {};

// Prevents a function from executing again if it has already executed within a threshold
// Use 'id' to keep functions which should be throttled from stepping on each other.
gg._functionThrottle = {};
gg.throttleFunction = function(id, func, threshold) {
    return function(event) {
        var now = new Date().getTime();
        var last = gg._functionThrottle[id];
        if (!last || now > last + (threshold || 100)) {
            gg._functionThrottle[id] = now;
            return (func)(event);
        }
        if (event) {
            event.preventDefault();
        }
        return false;
    };
};

gg._formThrottle = {};
gg.throttleFormSubmit = function() {
    for (var i = 0, len = document.forms.length; i < len; ++i) {
        var form = document.forms[i];
        var id = form.id || ('form_' + Math.floor(Math.random()*1000000));
        if (gg._formThrottle[id]) {
            // don't install it twice on the same form -- might happen with ajax updates.
            return;
        }
        gg._formThrottle[id] = 1;
        jQuery(form).bind('submit', function(event) {
            var now = new Date().getTime();
            var last = gg._formThrottle[id] || 1;
            if (now > last + 1000) {
                gg._formThrottle[id] = now;
                // let event go ahead.
            }
            else {
                event.preventDefault();
                return false;
            }
            return true;
        });
    }
};


if (!gg.FixIE) {
    gg.FixIE = {};
}

gg.FixIE.PngToGif = function() { // Deprecated
    var trans_png_exp = /\-trans.png/i;
    $('.trans').each(function(_,e) {
        if (e.src && trans_png_exp.test(e.src)) {
            e.src = e.src.replace(/\.png/,'.gif');
        }
    });
};

gg.FixIE.Sprites = function() {
  $("a.sprite, button.sprite").each(function() {
    var sizingClone = $(this).clone();
    sizingClone.appendTo("body");
    $(this).css({ width: sizingClone.width() });
    sizingClone.remove();
  }).filter("a.sprite-scalable")
      .hover(function(){
        if ($(this).hasClass("sprite-scalable-nodownstate")) {
          $(this).addClass("sprite-scalable-nodownstate-hover");
        } else {
          $(this).addClass("sprite-scalable-hover");
        }
      }, function() {
        $(this)
          .removeClass("sprite-scalable-nodownstate-hover")
          .removeClass("sprite-scalable-hover");
      })
      .mousedown(function() {
        if ($(this).hasClass("sprite-scalable-nodownstate")) {
          $(this).addClass("sprite-scalable-nodownstate-active");
        } else {
          $(this).addClass("sprite-scalable-active");
        }
      })
      .mouseup(function() {
        $(this)
          .removeClass("sprite-scalable-nodownstate-active")
          .removeClass("sprite-scalable-active");
      });
};

if (!gg.FlashMessage) {
    gg.FlashMessage = {};
}

gg.FlashMessage.init = function () {
    log.setup("startup", "gg")("Initializing Flash Message");
    var flash_cookie = fl.Util.readCookie('flash');
    log("Flash Cookie:", flash_cookie);
    if (flash_cookie) {
        var flash_message = gg.parse_json(flash_cookie);
        fl.Util.expireCookie('flash');
        if (flash_message.notice) {
            var flash_message_div = $('#flash_message');
            if (flash_message_div) {
                flash_message_div.append(flash_message.notice);
                flash_message_div.show();
            }
        }
    }
};





