
//-------------
// GLOBAL VARS
//-------------

	// global object
	if (!IOM) var IOM = {};

	IOM.textSizeCookieName = 'IOMTextSize';
	IOM.textSizeStyles = Array ('', 'Medium', 'Large');	
	IOM.slideshowColors = Array('#0C706B', '#995905', '#605677', '#8e301c');
	IOM.slideshowCurrColor = 0;
	
//-----------
// FUNCTIONS
//-----------

	IOM.closeLogin = function(){
		$(".memberlogin").removeClass("on");
		$("#memberlogin").fadeOut({speed:'fast'});
	};

	IOM.closeTopicsPanel = function(){
		$("#topics").slideUp("fast", function() {
			$("#explorebtn").removeClass("on").addClass("off");
		});
	};

	IOM.loadSavedTextSize = function() {
		/* called when page loads, set the text size from cookie */		
		IOM.setTextSize(IOM.getSavedTextSize());
	}

	IOM.toggleTextSize = function (_increase) {		
		/* increase/decrease text size. _increase:Bool */

		/*  text sizes:	
			default	= 0 | main.css
			Medium 	= 1 | text_size_m.css
			Large 	= 2 | text_size_l.css */

		var currTextSize = IOM.getSavedTextSize(); 

		if(_increase === true) {
			/* increase the text size*/
			if(currTextSize < IOM.textSizeStyles.length - 1) {
				IOM.setTextSize(currTextSize + 1);
			}
		}
		else {
			/* decrease the text size */
			if(currTextSize > 0) {
				IOM.setTextSize(currTextSize - 1);
			}		
		}
	}

	IOM.setTextSize = function (_size) {
		/* sets the text size, _size:Int */

		// set the cookie
		IOM.setCookie(IOM.textSizeCookieName, _size, 2);

		// load css file
		IOM.loadTextSizeStyle(_size);	
	}

	IOM.loadTextSizeStyle = function (_size) {
		/* switch stylesheets */

		// disable all 
		$('link[title]').each(function() {
			$(this).attr('disabled', true);
		});

		if(_size > 0) {
			//enable selected
			$('link[title=' + IOM.textSizeStyles[_size] + ']').attr('disabled', false);			
		}
	}

	
	IOM.getSavedTextSize = function () {
		/* retrieves the current text size from cookie */

		var textSize = IOM.getCookieVal(IOM.textSizeCookieName);
		return (textSize === null) ? 0 : Number(textSize);
	}
	
	IOM.setCookie = function(_name, _val, _days) {
		/* set cookie, expects _name:String, _val:String, _days:Int */
		
		var date = new Date();
		date.setTime(date.getTime() + (_days * 24 * 60 * 60 * 1000));
		
		var textSizeCookie = _name + '=' + 
							  escape(_val) + 
							'; expires='+ date.toGMTString();
							//'; path="/"'+
							//'; domain=""'+
							//'; secure=""';
	
		document.cookie = textSizeCookie;
	}

	IOM.getCookieVal = function(_name) {
		/* get the value from cookie, expects _name:String*/

		// IOMTextSize=1; IOMTextSize2=2		
		var cookie = document.cookie;
		var prefix = IOM.textSizeCookieName + '='
		var startAt = null;
		var endAt = null;
		
		if(cookie.indexOf(prefix) == -1) {
			// cookie doesn't exist
			return null;
		}
		else {
			startAt = cookie.indexOf(prefix) + prefix.length;
			endAt = cookie.indexOf(';', startAt);
						
			if(endAt == -1) {
				endAt = cookie.length;
			}
		
			return unescape(cookie.substring(startAt, endAt));
		}		
	}

	IOM.updatedSlideHeight = function(_slide) {
		if(_slide.is(':visible')) {			
			var height = 0;
			$('ul li', _slide).each(function(){
				if($(this).height() > height) {
					height = $(this).height();
				}
			});
			$('ul li', _slide).css('height', height);					
			_slide.parent().animate({height: _slide.height()}, 'fast');		
		}
	}
	
	IOM.updatedSlideNav1 = function(curr, next, opts) {

		var currSlide = opts.currSlide + 1;
		var totalSlides = opts.slideCount;

		if(currSlide == totalSlides) {$('#s1forward').removeClass('fon').addClass('foff');}
		else {$('#s1forward').removeClass('foff').addClass('fon');}

		if(currSlide == 1) {$('#s1back').removeClass('bon').addClass('boff');}
		else {$('#s1back').removeClass('boff').addClass('bon');}			

		IOM.updatedSlideHeight($(this));
	}
	
	IOM.updatedSlideNav2 = function(curr, next, opts) {

		var currSlide = opts.currSlide + 1;
		var totalSlides = opts.slideCount;

		if(currSlide == totalSlides) {$('#s2forward').removeClass('fon').addClass('foff');}
		else {$('#s2forward').removeClass('foff').addClass('fon');}
		if(currSlide == 1) {$('#s2back').removeClass('bon').addClass('boff');}
		else {$('#s2back').removeClass('boff').addClass('bon');}

		IOM.updatedSlideHeight($(this));
	}

	IOM.updateHint = function (event) {
		var hint = $(this).attr('title'); 
		if($(this).val() == hint) {
			$(this).addClass('hint');
			if(event.type != undefined) { // not page refresh
				$(this).val('').removeClass('hint');
			}
		}
		else if($(this).val() == '' || $(this).val() == hint) { 
				$(this).val(hint).addClass('hint');					
		}		
	}

//------
// INIT
//------

$(document).ready(function(){	
	
	// enable tabs, external links redirect 
	$('div.tabbed ul.tabs li').click(function(){		
		if($(this).parent().nextAll('div' + $('a', this).attr('href')).length > 0) {
			$(this).removeClass('off').addClass('on');
			$(this).siblings(':on').removeClass('on').addClass('off');
			$(this).parent().nextAll('div.tab').removeClass('on').addClass('off');
			$(this).parent().nextAll('div' + $('a', this).attr('href')).removeClass('off').addClass('on'); //
			// dispatch event
			$('a', this).blur();
			return false;
		}
	});

	// add first and last to li
	$('ul,ol').each(function(){
		$(this).children('li:first').addClass('first');
		$(this).children('li:last').addClass('last');
	});

	// add last to contact blocks
	$('div.module.contact > div.block2:last').addClass('last')

	// add last to content inside pop-up bubbles
	$('div.bubble div.content p:last').addClass('last');

	// feature slide show
	$('#featurecontent').cycle({
		fx:     'fade',
		speed:  'slow',
		timeout: 8000,
		pause: true,
		next:   '#image-next',
		prev:   '#image-previous',
    	pager:  '#featurenav',
		before: function() {
			
			do {var ranNum = Math.floor(Math.random() * IOM.slideshowColors.length);}
			while(ranNum == IOM.slideshowCurrColor);
			
			IOM.slideshowCurrColor = ranNum;
			$('div.header').css('background-color', '').css('background-color', IOM.slideshowColors[ranNum]);
		},
    	pagerAnchorBuilder: function(idx, slide) {
    	   return '<li><a href="#">&nbsp;</a></li>';
    	}

	});

	// regular slide show
	$('#s1').cycle({
		fx:     'scrollHorz',
		speed:  'slow',
		timeout: 0,
		nowrap: true,
		pause: true,
		next:   '#s1forward',
		prev:   '#s1back',
    	pager:  '#s1nav',
    	after: IOM.updatedSlideNav1,
    	pagerAnchorBuilder: function(idx, slide) {
    	   return '<li><a href="#">' + idx + '</a></li>';
    	}
	});

	// regular slide show
	$('#s2').cycle({
		fx:     'scrollHorz',
		speed:  'slow',
		timeout: 0,
		nowrap: true,
		pause: true,
		next:   '#s2forward',
		prev:   '#s2back',
    	pager:  '#s2nav',
    	after: IOM.updatedSlideNav2,
    	pagerAnchorBuilder: function(idx, slide) {
    	   return '<li><a href="#">' + idx + '</a></li>';
    	}
	});

	// accordions
	$("#accordion").accordion({
		header: "h3",
		autoHeight: false,
		clearStyle: true,
		alwaysOpen: false,
		active: false,
		cleartype: true
	});

	$("#accordion2").accordion({
		header: "h3",
		autoHeight: false,
		clearStyle: true,
		alwaysOpen: false,
		active: false,
		cleartype: true
	});

	$("#accordion3").accordion({
		header: "h3",
		autoHeight: false,
		clearStyle: true,
		alwaysOpen: false,
		active: false,
		cleartype: true
	});

	// enable titles to link externally
	$('ul.accordion > li a').hover(
	function(){
		$(this).parents('ul.accordion:firt').accordion('disable')
	}, 
	function(){
		$(this).parents('ul.accordion:firt').accordion('enable')
	});
	
	$('ul.accordion > li a').click(function() {
		window.location.href = $(this).attr('href');
	});

	$('ul.accordion > li.link h3').click(function() {
		if($('a', this).length > 0) {		
			window.location.href = $('a', this).attr('href');
		}
	});
	
	// open the accordion on selected node
	if($('.accordion > li.on').length > 0) {
		var activeItemIndex = $('.accordion > li.on').prevAll('li').size(); 
		$("#accordion").accordion('activate', activeItemIndex);
	}
	
	// mark accordion tab as selected
	$('.accordion').bind('accordionchangestart', function(event, ui) {
		ui.oldHeader.parent().removeClass('on');
		ui.newHeader.parent().addClass('on');	
	});

			
	// login button
	$("li.memberlogin,#memberloginbtn").click(function(e){
		if ($("li.memberlogin").hasClass("on")) {
			IOM.closeLogin();
		}
		else {
			$("li.memberlogin").addClass("on");
			$("#memberlogin").fadeIn({speed:'fast'}).find("#username").focus();
		}
		e.stopPropagation()
		e.preventDefault();
	});

	// round corners of pop up bubbles
	DD_roundies.addRule('div.bubble div.content', '6px', true);

	// append pointer and reposition
	$('div.bubble').append('<div class="pointer">&nbsp;</div>');
	$('div.bubble.right div.pointer').css('margin-top', '-9px');
	
	$('a.bubble').click(function() {
		
			var bubbleContent = $('#' + $(this).attr('id') + '-txt');
			var documentOffset = $(this).offset();			

			if(bubbleContent.hasClass('open')) {
				// close it
				IOM.closeBubble(bubbleContent);
			}
			else {			
				// open it
				if($.browser.msie) {
					$('div.bubble').removeClass('open').addClass('close');													
					bubbleContent.removeClass('close').addClass('open');					
				}
				else {
					$('div.bubble').fadeOut({speed:'fast'}).removeClass('open').addClass('close');					
					bubbleContent.fadeIn({speed:'fast'}).removeClass('close').addClass('open');					
				}			

				// hide select fields for ie6, conflicts with pop-up bubble
			    if($.browser.msie && ($.browser.version < 7)) {				
					$('select#keywordsearch').css('visibility','hidden');
				}
			}

			// reposition bubble next to '?' link
			if(bubbleContent.hasClass('right')) {
				var containerOffset = $(this).parents('ul.filter').offset();				
				bubbleContent.css('top', (((documentOffset.top - containerOffset.top) - (bubbleContent.height() / 2)) + ($(this).height() / 2)) + 'px');
				bubbleContent.css('left', (documentOffset.left - containerOffset.left) + 'px');
			}
			
			$(this).blur();
			return false;
	});

	// close bubble when clicking anywhere on the screen
	$(document).click(function(e){
		IOM.closeBubble($('div.bubble.open'));
	});
	
	IOM.closeBubble = function(_bubbleContent) {
		/* close popup bubble _bubbleContent:Object */
		
		if($.browser.msie) {
			_bubbleContent.removeClass('open').addClass('close');													
		}
		else {
			_bubbleContent.fadeOut({speed:'fast'}).removeClass('open').addClass('close');
		}

		// show select fields previously hidden for ie 6
	    if($.browser.msie && ($.browser.version < 7)) {				
			$('select#keywordsearch').css('visibility','visible');
		}		
	}

	//topics panel
	$("#explorebtn").click(function(e){
		if ($(this).hasClass("on")) {
			IOM.closeTopicsPanel();
		}
		else {
			$(this).removeClass("off").addClass("on");
			$("#topics").slideDown({speed:'fast'});
		}

		e.stopPropagation()
		e.preventDefault();
	});


	// enable date picker
    if($('#extraFilters').length == 0) {
	$('#datesearchfrom, #datesearchto').datepicker({
		changeMonth:true,
		changeYear:true,
		showOn: 'button', 
		buttonImage: '/_res/img/icon_calendar.gif', 
		buttonImageOnly: true
	});
    }

	// show filter options in chunks
	$('ul.options li.more').nextAll('li').hide();
	
	$('ul.options li.more').each(function(){
		$(this).data('index', $(this).prevAll('li').size());
		$(this).parent().append($(this));
	});
		
	$('ul.options li.more a').click(function(e) {
		$(this).parents('ul.options:first').find('li:hidden').slice(0,$(this).parent().data('index')).fadeIn();
		if($(this).parents('ul.options:first').find('li:hidden').size() == 0){
			$(this).parent().remove();
		}
		return false;		
	});

	// add 'last' to last link anchor
	$('span.links a:last-child').addClass('last');

	// clear login on document click
	$(document).click(function(e){
		var xxx = $(e.target);
		if (!xxx.is('input')) {IOM.closeLogin(); }

		//if panel is open close it..
		IOM.closeTopicsPanel();
	});

	// mark last item on column for images/logo pages
	$('ul.gallery li:nth(2)').addClass('last');

	// form hints
	$('textarea[title!=], :text[title!=]').each(IOM.updateHint);
	$('textarea[title!=], :text[title!=]').bind('focus', IOM.updateHint);
	$('textarea[title!=], :text[title!=]').bind('blur', IOM.updateHint);
	
	// find all slideshows, if no content turn pagers off
	$('.slideshow').each(function() {
		if($(this).children('div').length == 1) {
			$('#'+this.id+'forward').hide();
			$('#'+this.id+'back').hide();
		}
	});

	// directory search, more options
	$('.moresearchopts a').click(function(e) {

		var opts = $(this).siblings('div.searchopts')

		if(opts.is(':visible')) {
			opts.hide();
			$(this).removeClass('on').addClass('off');
			$(this).html('View More Search Options');
		}
		else {
			opts.show();
			$(this).removeClass('off').addClass('on');
			$(this).html('View Less Search Options');
		}

		e.preventDefault();
	});


	// activate lightbox for media pages
	if($('ul.gallery').length > 0) {
	   $('ul.gallery a.zoom').lightBox({
			overlayOpacity: 0.6,
			imageLoading: '/_res/img/lightbox/lightbox-ico-loading.gif',
			imageBtnClose: '/_res/img/lightbox/lightbox-btn-close.gif',
			imageBtnPrev: '/_res/img/lightbox/lightbox-btn-prev.gif',
			imageBtnNext: '/_res/img/lightbox/lightbox-btn-next.gif'
		});	
	}

	$('ul.gallery a.zoom').append('<span class="magnify"><\/span>');
	$('ul.gallery a.zoom span').hide();
	$('ul.gallery a').hover(
		function(){
			$('span',this).fadeIn('fast');
		}, 
		function(){
			$('span',this).fadeOut('fast');
		}
	);	

	// activate collapsible lists
	$('ul.collapsible > li h3').click(function(){
		if($(this).parent().hasClass('on')) {
			$(this).parent().removeClass('on').addClass('off');
		}
		else {
			$(this).parent().removeClass('off').addClass('on');			
		}
	});

	// enable increase text size widget
	$('a.textincrease').click(function(e) {
		IOM.toggleTextSize(true);
	 	e.preventDefault();
	});

	// enable decrease text size
	$('a.textdecrease').click(function(e) {
		IOM.toggleTextSize(false);			
	 	e.preventDefault(false);
	});

	// load saved text size
	IOM.loadSavedTextSize();
	

	
});



















