window.addEvent('load', function() {
	/* Setup slideshow/accordion */
	$$('.slideshow').each(function(e) {
		new ExtenedSlidshow({slideTarget:e, hasButtons:true, slideDuration:10000});		
	});
});
window.addEvent('domready', function() {
	//Customised versions of the above functions for eVision (Events)
	if(typeof($$('.change.into.customtabs')) == 'function'){
		$$('.change.into.customtabs').customChangeIntoTabs();
		$$('.make.customExpandable').customMakeExpandable();
	}
	
	if(document.id('events_conference_programme_search')) {
		$$('.make.expandable').makeExpandable();
	}

	//For "EventsExhibitorSearch.aspx" page. Activate correct tab depending on "?type=" in querystring
	if(document.id('events_exhibitor_search')) {
		if ($defined(Browser.getQueryString('type'))) {
			var searchTab = Browser.getQueryString('type').toUpperCase();
			$$('.tab_block li').removeClass('current');
			switch(searchTab) {
				case 'K':
					$$('.tab_block li h2.search_keyword').getParent().addClass('current');
					break;
				case 'C':
					$$('.tab_block li h2.search_category').getParent().addClass('current');
					break;
				case 'Z':
					$$('.tab_block li h2.search_az').getParent().addClass('current');
					break;
				case 'R':
					$$('.tab_block li h2.search_location').getParent().addClass('current');
					break;
			}
		}
	}

	Element.implement({
	/*
	---------------------------------------------------------------------
	MAKE EQUAL HEIGHT COLS - E.G. LI'S
	---------------------------------------------------------------------
	Adds equal height columns
	---------------------------------------------------------------------
	*/
		equalHeightColumns: function(col) {
			var counter = 0;
			var element = this.getElements('li');
			element.each(function(e) {
				// Get height for all elements in multiples of columns
				if (counter % col == 0 && counter !=0) {
					// Check previous heights within multiple
					this.checkPreviousHeights(col, element , counter, 1, 0);
				}
				counter++; // Increment Counter
				e.addClass(counter);
			}.bind(this)); // Bind so we can create other functions
			var overall = (counter-1) % col;
			if(overall !=0){
				this.checkPreviousHeights(col, element , (counter-1)-overall, 0, counter-1);
			}
		},
		checkPreviousHeights: function(cols, data, count, flag, odd) {
			var icount=(count - cols); // Multiple(3,6,9) - column = start of multiple
			var storeiCount = icount; // Store icount for later use
			var storeCount = count; // Store count for later use
			var height = 0; // Reset height
			// Loop to get the tallest from multiple compare with next
			if(odd ==0){
				while (count > icount) {
					if (data[icount].offsetHeight > height) {
						height = data[icount].offsetHeight; // Get tallest height of all elements in row
					}
					icount++;
				}
				this.changeHeights(height, data, storeiCount, storeCount);
			}else{
				 while (odd >= count) {
					if (data[count].offsetHeight > height) {
						height = data[count].offsetHeight; // Get tallest height of all elements in row
					}
					count++;
				}
				this.changeHeights(height, data, odd-1 , count);
			}
		},
		changeHeights: function(tallest, heightArray, icount, count) {
			// Loop through changing each items height to the tallest within the given multiple
			while (count > icount && count !=0) {
				heightArray[icount].setStyle('height', tallest + 'px'); // Set height to the tallest
				if (heightArray[icount].offsetHeight > tallest) {
					heightArray[icount].setStyle('height', (tallest - (heightArray[icount].offsetHeight - tallest)) + 'px');
				}
				icount++;
			}
		}
	});
	$$('.gradient_box').equalHeightColumns(2);
});

(function($) {

	$(function(){
		mainInit();

		$('div.active').makeTabs();

		//==Advanced search==
		//=== show more or less ===
		var showMoreList = $('#rightcolumn .advancedSearch ul.filter');
		var showMoreList2 = $('#rightcolumn .advancedSearch ul.filter_date');
		var showMoreText = $(showMoreList).siblings('p').children('a');
		var showMoreText2 = $(showMoreList2).siblings('p').children('a');

		//works out whether selected filter is inside the dropdown. If it is then open the dropdown on page load.
		$(showMoreList).siblings('p.opener').addClass('showmore').children('a').text('More sections');
		$(showMoreList).children('li').each(function(count){
			if($(this).hasClass('selected')){
				$(this).parent().siblings('p.opener');
				if(count>5)
					$(this).parent().siblings('p.opener').removeClass('showmore').addClass('showless').children('a').text('Fewer sections');
			}
		});
		$(showMoreList2).siblings('p.opener').addClass('showmore').children('a').text('More dates');
		$(showMoreList2).children('li').each(function(count){
			if($(this).hasClass('selected')){
				$(this).parent().siblings('p.opener');
				if(count>5)
					$(this).parent().siblings('p.opener').removeClass('showmore').addClass('showless').children('a').text('Fewer dates');
			}
		});

		//hides more than 6 results
		$('#rightcolumn .advancedSearch ul.filter_date li:gt(5)').wrapAll('<div class="hidden"></div>');
		$('#rightcolumn .advancedSearch ul.filter li:gt(5)').wrapAll('<div class="hidden"></div>');
		if ($(showMoreText).parent().hasClass('showmore')) { $(showMoreList).children('div.hidden').hide(); }
		if ($(showMoreText2).parent().hasClass('showmore')) { $(showMoreList2).children('div.hidden').hide(); }

		$(showMoreText).click(function(){
		//if there's a showmore class, hide the more than 6 results in a new div
			if ($(this).text('Fewer sections').parent().hasClass('showmore')) {$(this).parent().removeClass('showmore').addClass('showless').siblings('ul.filter').children('div.hidden').slideDown('fast');}
		//else remove class and allow hiding of extra content
			else if ($(this).text('More sections').parent().hasClass('showless')) {$(this).parent().removeClass('showless').addClass('showmore').siblings('ul.filter').children('div:visible').slideUp('fast');}
			return false;
		});
		$(showMoreText2).click(function(){
		//if there's a showmore class, hide the more than 6 results in a new div
			if ($(this).text('Fewer dates').parent().hasClass('showmore')) {$(this).parent().removeClass('showmore').addClass('showless').siblings('ul.filter_date').children('div.hidden').slideDown('fast');}
		//else remove class and allow hiding of extra content
			else if ($(this).text('More dates').parent().hasClass('showless')) {$(this).parent().removeClass('showless').addClass('showmore').siblings('ul.filter_date').children('div:visible').slideUp('fast');}
			return false;
		});

		// Apply PNG fix to the following selectors
		if ($.browser.msie && $.browser.version<7) {
			$.ifixpng.pixel = '/magazine/graphics/pixel.gif';
			$('#logo img[@src$=.png]').ifixpng();
		}

		// setup focus states on form fields
		$('#addlinks, #addtags, #mp_direcory_home #content input[type=text], #content div.formbox input[type=text], #content div.formbox input[type=password], #content div.formbox input[type=file], #content div.formbox textarea, #rightcolumn .formbox input.text, #inform fieldset.informcontent input[type=text], #inform fieldset.informcontent input[type=file], #inform fieldset.informcontent textarea, #emailsignup input[type=text], .informcontent table input')
			.css({backgroundColor:'#FFF', color:'#8C8C8C'})
				.focus(function() {
					$(this)
						.css({backgroundColor:'#FFFFB7', color:'#000', borderColor: '#454545'});
				})
				.blur(function() {
					$(this)
						.css({backgroundColor:'#FFF', color:'#8C8C8C', borderColor: '#fff'});
				});

		//inform text input style
		$('.informcontent table input[type="text"]').css('border','1px solid #999');

	// Page Tools 
	// Write out print button for users with JS enabled
	$('.page_options ul, .job_options ul, #getListedAmend .options ul').prepend('<li class="first"><a href="javascript:window.print()" class="po_print">Print</a></li>').children('li:eq(1)').removeClass('first');
		$('.paging_results').append('<span class="print"><a href="javascript:window.print()">Print</a></span>');
		$('.events_paging_results').append('<span class="print"><a href="javascript:window.print()">Print</a></span>');

		$('.refine_search a.refine').click(function() {
			$(this).toggleClass('up');
			$('.refinesearch').toggleClass('hide');
			return false;
		});

		/* Clear on click function */
		//$('.clearme').one('focus', function() { $(this).val(''); });

		//stops inheriting the height for the auto height
		$('#rightcolumn .active.searches .active_block ul, .searches .filter ul').css('height','auto');

		// Initially disable buttons
		$('#register .termsandconditions input.button, .myNewsletters .button, #story #comments_form .button, #blogPost #comments_form .button').attr('disabled','disabled').css({cursor:'default', opacity:.5});

		// Toggle button style depending on checkbox state
		$('.termsandconditions .formOptions :checkbox, .newsletterformSleeve .formOptions :checkbox, #story #comments_form .note :checkbox, .uploadAgreement :checkbox, #blogPost #comments_form :checkbox').click(function(){
			if(this.checked) {$('#register .termsandconditions input.button, .myNewsletters .button, #story #comments_form .button, .uploadAgreement .button, #blogPost #comments_form .button').attr('disabled','').css({cursor:'pointer', opacity:'1'});}
			else {$('#register .termsandconditions input.button, .myNewsletters .button, #story #comments_form .button, .uploadAgreement .button, #blogPost #comments_form .button').attr('disabled','disabled').css({cursor:'default', opacity:.5});}
		});

		//feedback button
		$('<div class="feedback_close"><a href="#">Close form overlay</a></div>').appendTo('.feedback_box .inner_sleeve');
		$('.feedback_box .feedback_close').click(function(){ $(this).parents('.feedback_box').remove(); return false;});

		// browser sniff for IE
		if ($.browser.msie){
			$('.section_column2 .colour2 .sectionhead .sectionhead_sleeve:first-child, .section_column2 .colour3 .sectionhead .sectionhead_sleeve:first-child').css('paddingTop','0');
		}

	// for my library page
	$('#myLibrary').load(function(){
		var currentQueryString = new Querystring();
		var tab = currentQueryString.get("type","S")
		//Setup everything properly.
		if (tab == 'T')
			$('#MyLibrarySavedDiscussions').click();
		if (tab == 'B')
			$('#MyLibrarySavedBlogs').click();
	});

		$('#story .relatedimages_block a.thickbox_image').each(function(){
			href = $(this).attr('href');
			// *************
			if(href.match('js=yes'))	alert('js=yes still hardcoded');
			// *************
			$(this).attr('href', href.replace(/\.aspx$/g, ".aspx?js=yes&"));
		});

	});

	function buttonOver() {
		if ($(this).is(':not(.over)')) {
			if ($.browser.msie && $.browser.version<7) $(this).iunfixpng();
			this.src.match(/_over\..../) ? null : this.src = this.src.replace(/\.(...)$/,'_over.$1');

			if ($.browser.msie && $.browser.version<7) $(this).ifixpng();
			$(this).addClass('over');
		}
	}

	function buttonOut() {
		if ($(this).is('.over')) {
			if ($.browser.msie && $.browser.version<7) $(this).iunfixpng();
			this.src = this.src.replace(/_over\.(...)$/,'.$1')

			if ($.browser.msie && $.browser.version<7) $(this).ifixpng();
			$(this).removeClass('over');
		}
	}


	function mainInit() {
		$('#mainnav').SetupMenu();
	}

})(jQuery);
