(function($) {

  $.sjf = {
    functions: {
      pageInit: function() {
        if($.sjf.functions.isIE6()) {
          $.sjf.functions.pngFix();
  	      try { document.execCommand("BackgroundImageCache", false, true); } catch(e) {}
        }
        $("#startpage-flash").exists(function() {
          swfobject.embedSWF("http://www.sjf.se/sj/gui/swf/dummy.swf", "startpage-flash", "280", "320", "9.0.0","http://www.sjf.se/sj/gui/swf/express-install.swf");          
        });
        $.sjf.functions.preloadImages("http://www.sjf.se/sj/gui//img/header-menu-button-hover-bg.gif","http://www.sjf.se/sj/gui/img/button-red-big-left-hover.gif","http://www.sjf.se/sj/gui/img/button-red-big-right-hover.gif","http://www.sjf.se/sj/gui/img/button-red-small-left-hover.gif","http://www.sjf.se/sj/gui/img/button-red-small-right-hover.gif");
      },
      isIE6: function() {
        return ($.browser.msie && $.browser.version == "6.0") ? true : false;
      },
      preloadImages: function() {
        for(var i=0;i<arguments.length;i++) {
          $("<img>").attr("src",arguments[i]);
        }
      },
     	pngFix: function() {
				var pings = new Array();
			  $(".png").each(function() {
					pings.push(this);
				});
			  var ip = /\.png/i;
			  var i = pings.length;
			  while(i-- > 0) {
			   var el = pings[i];
			   var es = el.style;
			   if(el.src && el.src.match(ip) && !es.filter) {
			      es.height = el.height;
			      es.width = el.width;
			      es.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + el.src + "', sizingMethod='crop')";
			      el.src = $.sjf.constants.ieblank;
			    } else {
			      var elb = el.currentStyle.backgroundImage;
			      if(elb.match(ip)) {
			        var path = elb.split('"');
			        var rep = (el.currentStyle.backgroundRepeat == "no-repeat") ? "crop" : "scale";
			        es.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + path[1] + "', sizingMethod='" + rep + "')";
			        es.height = el.clientHeight + "px";
			        es.backgroundImage = "none";
			        var elkids = el.getElementsByTagName("*");
			        if(elkids) {
			          var j = elkids.length;
			          if(el.currentStyle.position != "absolute") {
			            es.position = "static";
			          }
			          while(j-- > 0) {
			            if(!elkids[j].style.position) {
			              elkids[j].style.position = "relative";
			            }
			          }
			        }
			      }
			    }
			  }
			},
      inputMemory: function(obj) {
        var $defaultValue = obj.val();
        $(obj).focus(function() {
          if($(obj).val() == $defaultValue) {
           $(obj).val("");
          }       
        }).blur(function() {
          if(!$(obj).val().length) {
            $(obj).val($defaultValue)
          }
        });
      }
    },
    constants: {
      ieblank: "http://www.sjf.se/sj/gui/img/spacer.gif"
    }
  };

  $.extend($.fn, {
    exists: function(fn) {
      var l = this.length;
      if($.isFunction(fn)) {
        if(l > 0) fn.apply(this);
        return this;
      } else {
        return (l > 0) ? true : false;
      }
    },
    inputMemory: function() {
      return this.each(function() {
        $.sjf.functions.inputMemory($(this));        
      });
    }
  });

  $(function() {

    $.sjf.functions.pageInit();
  
    $(".mainmenu li:not('.right'):not('.empty')").hover(function() {
      $(this).addClass("left-hover");
    }, function() {
      $(this).removeClass("left-hover");
    });
    
    $(".button-small-red").hover(function() {
      $(this).addClass("button-small-red-hover");
    }, function() {
      $(this).removeClass("button-small-red-hover");      
    });
    
    $(".button-big-red").hover(function() {
      $(this).addClass("button-big-red-hover");
    }, function() {
      $(this).removeClass("button-big-red-hover");      
    });
    
    $(".submit-button").click(function() {
      $(this).parents("form:first").submit();
    });
    
    $("input[type='text']:not('.input-text-keep')").inputMemory();

  });

})(jQuery);