jQuery(document).ready(function() {
  // add rollover class to left category menu
  jQuery('.left_menu img').addClass('rollover');

  doRollovers();
	indicateActiveLeftMenuItem();
});

// set up an image pre-loader as a jQuery plugin
jQuery.fn.preload = function() {
  this.each(function() {
    jQuery('<img/>')[0].src = this;
  });
}

function indicateActiveLeftMenuItem() {
	// shift menu item to the left
	var a = jQuery('.left_menu .active');
	if (a.position()) { // a.position() is null if no matches were found
		a.css('position', 'relative');
		a.css('left', (a.position().left - 21) + 'px'); // indicator image width plus margins

		// show indicator graphic to right of menu item
		var i = document.createElement('img');
		i.src = '/images/active_arrow.png';
		i.alt = 'active';
		i.style.width = '17px'; // dimensions of image file
		i.style.height = '67px'; // dimensions of image file
		i.style.margin = '0 2px';
		jQuery('.left_menu .active img').after(i);
	}
}

function doRollovers() {
  // add rollover effect
  jQuery("img.rollover").hover(
    function() {
      this.src = this.src.replace("_off","_on");
    },
    function() {
      this.src = this.src.replace("_on","_off");
    }
  );

  // preload rollover images
  jQuery("img.rollover").each(function(index) {
    var preloadUrl = jQuery(this).attr('src').replace('_off', '_on');
		var els = jQuery([preloadUrl]);

		if (els != undefined && els.preload != undefined) { els.preload(); }

//		if (jQuery) { jQuery([preloadUrl]).preload(); }
  });
}

function doPopupFlashWindow(base, flashfile, version, width, height) {
  return window.open('static_items/popup_flash_window.php'
          + '?w=' + width
          + '&h=' + height
          + '&base=' + base
          + '&swfobj=' + flashfile
          + '&swfver=' + version, 'quiz', 'width=' + width + ',height=' + height + ',resizable=yes,status=no');
}

function preloadSubscriberResourceButtons() {
  jQuery('div.subscriber_resources img').each(function(index) {
    var preloadUrl = jQuery(this).attr('src').replace('_off', '_on');

    jQuery([preloadUrl]);
  });
}

function preloadCategoryMenuRollovers() {
  jQuery('div.left_menu img').each(function(index) {
    var preloadUrl = jQuery(this).attr('src').replace('_off', '_on');

    jQuery([preloadUrl]).preload();
  });
}
