window.addEvent('domready', function() {	
	MMU.first();
	MMU.moveSearchButton();
	MMU.checkPromo();
	MMU.reformatEventDates();
	MMU.reformatGallery();
	MMU.switchLeftColumn();
	MMU.reformatCalendar();
	MMU.stripBlogPost();
	MMU.setupTabs();
	MMU.shop();
	MMU.checkShop();
	MMU.showSubNav();
	MMU.setupMoreNav();
	MMU.setupOrgFilter();
	MMU.makeOrgColumns();
	MMU.resizeEventImage();
});


Function.prototype.startsWith = function(str){
	return false;
    return (this.indexOf(str) === 0);
}


MMU = function() {

};

MMU.first = function() {

}

MMU.resizeEventImage = function() {
	var image = $(document).getElement('#event-logo img');
	if (image == null) {
		return;
	}
	image.set('src', image.get('src') + '?thumbnail_width=180');
	image.setStyle('display', 'block');
	
	var twitterLink = $('aTwitter');
	if (twitterLink != null) {
		twitterLink.set('href', twitterLink.get('href') + '?url=' + document.location);
	}
	
	window.fbAsyncInit = function() {
		FB.init({appId: '124037487649064', status: true, cookie: true,
			xfbml: true});

	};
	
	
	(function() {
	var e = document.createElement('script'); e.async = true;
	e.src = document.location.protocol +
	  '//connect.facebook.net/en_US/all.js';
	var fbroot = new Element('div', {id: 'fb-root'})
	fbroot.inject($(document.body), 'top');
	document.getElementById('fb-root').appendChild(e);
	}());
}

MMU.makeOrgColumns = function() {
	$$('.msl_organisation_list').each(function(item) {
		var items = item.getElements('li');
		var count = Math.floor(items.length / 3);
		var mod = items.length % 3;
		
		var firstList = new Element('ul');
		for(i = 0; i < count + (mod > 0 ? 1 : 0); i++) {
			items[i].inject(firstList);
		}
		
		items = item.getElements('li');
		var secondList = new Element('ul');
		for(i = 0; i < count + (mod > 1 ? 1 : 0); i++) {
			items[i].inject(secondList);
		}		
		
		items = item.getElements('li');
		var thirdList = new Element('ul');
		for(i = 0; i < count; i++) {
			items[i].inject(thirdList);
		}
		
		firstList.inject(item);
		secondList.inject(item);
		thirdList.inject(item);
	});
}

MMU.setupOrgFilter = function() {
	if($('aFilter') == null) {
		return;
	}
	$('aFilter').addEvent('click', function(event) {
		event.stop();
		var text = $('txtFilter').get('value');
		//get input from VISIBLE div
		var tabIndex = 0;
		$$('.tabs-content > div').each(function(item) {
			if (item.getStyle('display') == 'block') {
				item.getElement('input.textbox').set('value', text);
				item.getElement('input.button').click();
				Cookie.write('tab-selected', tabIndex);
			}
			tabIndex++;
		});
	});
}

MMU.homepage = function() {
	document.addEvent('domready', function() {
		//$$('.sidepanel').setStyle('display', 'none');
	});
}

MMU.shop = function() {
	$$('h1').forEach(function(item) {
		if (item.get('text').toLowerCase() == 'shop') {
			item.destroy();
		}
	});
}

MMU.setupTabs = function() {
	if($('tabs-selector') == null) {
		return;
	}
	$$('#tabs-selector > div').each(function(item, index) {
		item.addEvent('click', function() {
			$$('#tabs-selector > div').removeClass('over');	
			this.addClass('over');
			$$('.tabs-content > div').setStyle('display', 'none').addClass('scripted');
			var newTab = $$('.tabs-content').each(function(item) {
				var newTab = item.getChildren('div')[index];
				if(newTab != null) {
					newTab.setStyle('display', 'block');
				}
			});
		});
	});
	var tabIndex;
	tabIndex = Cookie.read('tab-selected');
	if(tabIndex == null || tabIndex == 'null') {
		tabIndex = 0;
	}
	Cookie.write('tab-selected', 'null');
	$$('#tabs-selector > div')[tabIndex].fireEvent('click');
}

MMU.stripBlogPost = function() {
	$$('.blog_post').each(function(blogPost) {
		var item = blogPost.getElements('ul')[0];
		
		var html = item.getElements('.rss_description')[0].get('html') + '';
		var html = html.stripTags().tidy();
		var charCount = 200;
		
		if (blogPost.get('title') != null) {
			charCount = blogPost.get('title');
		}
		
		if (html.length > charCount) {
			if(item.getElement('.rss_title a')!=null){
				var moreHref = item.getElement('.rss_title a').get('href')
				html = html.substr(0, charCount) + "... <a href='" + moreHref +"'>More</a>";
			}
		}
		item.getElements('.rss_description')[0].set('html', html);
		
		var posted = item.getElements('.rss_pubdate')[0].get('text');
		$$('.blog-signoff').set('text', 'Posted on ' + posted);
		
		item.setStyle('display', 'block');
		
		if (blogPost.hasClass('advice')) {
			if(item.getElement('.rss_title')!=null){
				item.getElement('.rss_title').destroy();
			}
		}
			
		
	});
}

//move div with id left-holder to placeholder in right hand column
MMU.switchLeftColumn = function() {
	if ($('left-holder') == null || $('placeholder') == null) {
		return;
	}
	$('left-holder').inject('placeholder');
	$('left-holder').setStyle('display', 'block').addClass('scripted');
}

MMU.checkShop = function() {
	if ($('shop-list') == null) {
		return;		
	}
	$('main').addClass('promo');
	if ($('shop-image') != null) {
		$('main').setStyle('background-image', 'url(' + $('shop-image').get('src')  + ')');
		$('shop-image').destroy();
	}
	var index = 1;
	$('shop-list').getElements('a').each(function(item) {
		item.addClass('item-' + index);
		index++;
	});
}

MMU.setupMoreNav = function() {
	if ($('more_nav') == null) {
		return;
	}
	var nav_list = new Element('div', {'id': 'nav-list'});
	var nav_list_ul = new Element('ul');
	
	nav_list_ul.inject(nav_list);

	$$('#more_nav > div > ul > li').inject(nav_list_ul);
	var lastChild = nav_list_ul.getElement('li:last-child')
	if(lastChild != null) {
		lastChild.addClass('last');
	}
	nav_list.inject('navigation');
	
	var open = false;
	
	$('more-btn').addEvent('click', function(event) {
		if(!open) {
			nav_list.setStyle('display', 'block');
			$('more-btn').addClass('selected');
			event.stop();
			$(document.body).addEvent('click', function() {
				nav_list.setStyle('display', 'none');
				$('more-btn').removeClass('selected');
				open = false;
			});
			open = true;
		}
		else {
			open = false;
			nav_list.setStyle('display', 'none');
			nav_list.setStyle('display', 'none');
			$('more-btn').removeClass('selected');
		}
	});
	
}

//show sub nav in main
MMU.showSubNav = function() {
	if ($('promo-list') != null || $('shop-list') != null || $('left-small') != null || $('full-left') != null) {
		return;		
	}	
	$('main').addClass('subnav');
	if ($('img-header') != null) {
		$('main').setStyle('background-image', 'url(' + $('img-header').get('src')  + ')');
		$('img-header').destroy();
	}
	var subNavDiv = new Element('ul', {'id': 'subnav-list'});
	$$('#hidden_nav ul > li > ul > li').each(function(item) {
		//remove any levels below
		item.getElements('ul').destroy();
		item.inject(subNavDiv);
	});
	subNavDiv.inject('left');

}


//check to see if we're doing a promo (normally homepage only)
MMU.checkPromo = function() {
	if ($('promo-list') == null) {
		return;		
	}	
	$('main').addClass('promo');
	$('promo-list').getElements('li').each(function(item) {
		item.addEvent('mouseover', MMU.promoOver.pass(item));
		item.addEvent('click', MMU.promoClick.pass(item));
	});
	MMU.promoOver($('promo-list').getElement('li'));
	MMU.periodID = MMU.autoChange.periodical(5000);
	
}

MMU.linkify = function(text) {
	if(text == null) {
		return;
	}
	var regexp = /http[^\s]+/i;
	var matches = text.match(regexp);
	if(matches != null && matches.length == 1) {
		text = text.replace(regexp, '<a href="' + matches[0] + '" target="_blank">' + matches[0] + '</a>');
	}
	
	return text;
}

MMU.autoChange = function() {
	if (MMU.currentIndex == null) {
		MMU.currentIndex = 0;
	}
	
	MMU.currentIndex++;
	if (MMU.currentIndex >= $('promo-list').getElements('li').length) {
		MMU.currentIndex = 0;
	}
	var next = $('promo-list').getElements('li')[MMU.currentIndex];
	MMU.promoOver(next, true);
}


MMU.promoClick = function(item) {
	var url = item.getElement('a').get('href');
	document.location = url;
}


MMU.promoOver = function(item, auto) {
	if (auto == null) {
		auto = false;
	}
	if (!auto) {
		//$clear(MMU.periodID);
		clearTimeout(MMU.periodID);
	}
	var arrow = null;
	if($('promo-selected') == null) {
		arrow = new Element('div', {id: 'promo-selected'});
		arrow.inject('content');
	}
	else {
		arrow = $('promo-selected')
	}
	arrow.setStyle('left', item.getPosition('main').x  + ((item.getSize().x / 2) - arrow.getSize().x / 2));
	var img = item.getElement('a').get('title');
	$('main').setStyle('background-image', 'url(' + img  + ')');
	$('promo-list').getElements('li').each(function(item) {
		item.removeClass('sel');
	});
	item.addClass('sel');
}

//reformats the event date to be more asthetic
MMU.reformatEventDates = function() {
	if (document.getElement('.basic-list .msl_eventlist') == null) {
		return;
	}
	document.getElements('.basic-list .msl_event_time').each(function(item) {
		var timeText = item.get('text');
		var timeArray = timeText.toString().trim().split(' ');
		if (timeArray.length < 2) {
			return;
		}
		var dateString = timeArray[0].toInt();
		if (dateString < 10) {
			dateString = "0" + dateString;
		}
		var monthString = timeArray[1].substr(0, 3);
		item.set('html', dateString + '<br/>' + monthString);
		
	});
}

//reformats the gallery on the homepage
MMU.reformatGallery = function() {
	$$('.photogallery').each(function(item) {
		var title = item.getElement('dt a').clone().addClass('title');
		var image = item.getElement('dd.photogallery_image a').clone();
		var uploaded = new Element('div', {'html' : item.getElement(':nth-child(4)').get('html')});
		
		item.empty();
		
		image.inject(item);
		title.inject(item);
		uploaded.inject(item);
		
		
	});
}

//reformats the calendar to aid styling
MMU.reformatCalendar = function() {
	$$('.msl_event_calendar > tbody > tr:first-child').setStyle('border-bottom', '1px solid #dedede');
	$$('.msl_event_calendar_title td:first-child a').set('text', '').addClass('cal-left');
	$$('.msl_event_calendar_title td:last-child a').set('text', '').addClass('cal-right');
}	

//moves the search button to aid styling
MMU.moveSearchButton = function() {
	if ($('ctl00_search_btnSubmit') != null) {
		$('ctl00_search_btnSubmit').inject($('search-main'), 'after');
	}
}
