jQuery.noConflict();

function toggleList(element)
{
  var list = element.siblings("ul");
  var p = element.parent();
  if(list) 
  {
    if(p.hasClass('closed'))
    {
      p.removeClass('closed');
      p.addClass('opened');
      list.slideDown();
    }else{
      p.removeClass('opened');
      p.addClass('closed');
      list.slideUp();
    }
  }
}

/* Counts how many checkboxes are selected within a given element */
function GetSelectedCount(className) {
	var count = 0;
	jQuery('.' + className + ' input').each(function (box) {
		count += this.checked ? 1 : 0;		
	});
	return count;
}

//Commonwealth Fund namepsace
var CMWF = CMWF ? CMWF : {

	// detect if user has font size set in cookie, and adjust fontsize accordingly.
	loadUserFontSize: function () {
		// if user has fontsize stored in cookie, set font size
		var fontsize = jQuery.cookie("fontsize");
		if (fontsize !== null) {
			CMWF.setCustomFontSize(fontsize);
		}
	},

	// updated specific CSS classes with user-customized font-size.
	setCustomFontSize: function (fontSize) {
		var bd = jQuery('body');

		// remove other font size settings if they exist.
		bd.removeClass('small_text');
		bd.removeClass('normal_text');
		bd.removeClass('large_text');

		// add user-defined font settings
		bd.addClass(fontSize);
	},

	handleChangeFontSize: function (event) {

		// preven the anchor's default behavior.
		event.preventDefault();

		try {

			// get which font size was clicked
			var fontsize = event.target.id;

			// update fonts
			CMWF.setCustomFontSize(fontsize);

			// store font setting in cookie
			jQuery.cookie("fontsize", fontsize, { path: '/' });

		} catch (e) {
			//console.log(e.message);
		}
	},

	//Global page initializer
	init: function () {

		// setup user defined fontsize.
		CMWF.loadUserFontSize();

		// event handler wire-ups
		jQuery('a.resize').click(CMWF.handleChangeFontSize);

		// TODO: Below are the functions that can be converted into event handlers
		// and setup into CMWF namespace. If you get a chance to touch these, please
		// do that.
		// Also, the styles that get applied on page load may be better off setup in
		// the css files. The page will render faster if the styles live in CSS initially.

		//slide down menus on fellow sidebar module
		jQuery("ul.pastFellows li ul").slideUp();
		jQuery("ul.pastFellows li a.toggle").parent().addClass('closed');
		jQuery("ul.pastFellows li a.toggle").click(function () {
			toggleList(jQuery(this));
		});

		//toggle for slide open h3s
		jQuery("#categorizedNewsletters ul").hide();
		jQuery("#categorizedNewsletters h3 a").click(function () {
			jQuery(this).parent().siblings('ul').toggle();
			jQuery(this).parent().toggleClass("open");
		});

		//scorecard comparisons
		jQuery('.controls div.comparisons').hide();
		jQuery('.controls label.compare_list').click(function () {
			jQuery('.controls div.comparisons').slideToggle('normal');
			jQuery(this).toggleClass('opened');
		});

		//Lightbox functionality
		jQuery("a.lightbox").fancybox({
			'zoomSpeedIn': 300,
			'zoomSpeedOut': 300,
			'imageScale': true,
			'overlayShow': true,
			'centerOnScroll': true,
			'zoomOpacity': 300,
			'overlayOpacity': 0,
			'frameWidth': 600,
			'frameHeight': 800
		});

		//Lightbox functionality TWO for boxes that aren't quite as tall
		jQuery("a.lightboxShort").fancybox({
			'zoomSpeedIn': 300,
			'zoomSpeedOut': 300,
			'imageScale': true,
			'overlayShow': true,
			'centerOnScroll': true,
			'zoomOpacity': 300,
			'overlayOpacity': 0,
			'frameWidth': 600,
			'frameHeight': 462
		});

		//citation rollover
		jQuery(".citation_box").hide();
		jQuery("li.citation").hover(function () {
			jQuery(this).children('.citation_box').slideDown("fast");
		},
		    function () {
		    	jQuery(this).children('.citation_box').slideUp("fast");
		    }
	    );

		//this will show more on the click event
		jQuery(".sideBlogByAuthor_readMore").click(function (event) {
			jQuery(".sideBlogByAuthor_readLess").show();
			jQuery(".sideBlogByAuthor_readMore").hide();

			jQuery(".sideBlogByAuthor li").each(function (index, item) {
				//wire up click event for the li element
				jQuery(item).removeClass('hide');
			});
			event.preventDefault();
		});

		//this will show less on the click event
		jQuery(".sideBlogByAuthor_readLess").click(function (event) {
			var i = 0;
			jQuery(".sideBlogByAuthor_readLess").hide();
			jQuery(".sideBlogByAuthor_readMore").show();

			event.preventDefault();
			jQuery(".sideBlogByAuthor li").each(function (index, item) {
				//wire up click event for the li element
				if (i >= 20) {
					jQuery(item).addClass('hide');
				}
				i++;
			});
		});

		//this will show more on the click event
		jQuery(".sideBlogByTag_readMore").click(function (event) {
			jQuery(".sideBlogByTag_readLess").show();
			jQuery(".sideBlogByTag_readMore").hide();

			jQuery(".sideBlogByTag li").each(function (index, item) {
				//wire up click event for the li element
				jQuery(item).removeClass('hide');
			});
			event.preventDefault();
		});

		//this will show less on the click event
		jQuery(".sideBlogByTag_readLess").click(function (event) {
			var i = 0;
			jQuery(".sideBlogByTag_readLess").hide();
			jQuery(".sideBlogByTag_readMore").show();

			event.preventDefault();
			jQuery(".sideBlogByTag li").each(function (index, item) {
				//wire up click event for the li element
				if (i >= 20) {
					jQuery(item).addClass('hide');
				}
				i++;
			});
		});

	} // end init

};

// Kick off page load
jQuery(document).ready(CMWF.init);

