// array for evenHeights

var contentPanelBoxes = [ 
	"#home #whereWeWork",
	"#home #keepInTouch",
	"#home #whyMesh",
	"#home #featuredCaseStudy"
];

var contentPanelBoxes2 = [ 
	"#home #ourCoreServices",
	"#home #testimonial",
	"#home #clients",
	"#home #hsTraining"
];

jQuery(document).ready(function() {

    $.localScroll();


	jQuery('#mycarousel').jcarousel({
        auto: 5,
		scroll: 1,
        wrap: 'both',
		buttonNextHTML: '<a class="buttonPrev" title="prev">Prev</a>',
		buttonPrevHTML: '<a class="buttonNext" title="next">Next</a>',
		animation: 'slow',
		easing: 'swing'		
    });

	$('#home .rotatorImages').cycle({
		fx: 'fade',
		timeout: 6000,
		speed: 500,
		pause: 1
	});

	coursePageHides();
	newsArchive();
	applyCufon();
	evenHeights(contentPanelBoxes);
	evenHeights(contentPanelBoxes2);
});

function test(){ alert("test"); }

function coursePageHides() {
	$('.aboutMore').hide();
	$('.howWeCanHelp').hide();
	
	if($('.aboutMore').length > 0 ) {
		$('#about').append("<p class='readMore'><a href='#' title='Click here to read more'><span>Read more</span></a></p>");
		$('#about p.readMore').bind("click", function(e){
			$('#about .aboutMore').slideToggle(600);
			if($(this).is(".open")) {
				$(this).removeClass("open");
				$(this).find("a").css("background-position","right 4px");
				$(this).find("span").text("Read more");
			} else {
				$(this).addClass("open");
				$(this).find("a").css("background-position","right -20px");
				$(this).find("span").text("Hide");
			}
			e.preventDefault();
		});
	}
	
	if($('.howWeCanHelp').length > 0 ) {
		$('#howWeCanHelp').append("<p class='readMore'><a href='#' title='Click here to read more'><span>Read more</span></a></p>");
		$('#howWeCanHelp p.readMore').bind("click", function(e){
			$('#howWeCanHelp .howWeCanHelp').slideToggle(600);
			if($(this).is(".open")) {
				$(this).removeClass("open");
				$(this).find("a").css("background-position","right 4px");
				$(this).find("span").text("Read more");
			} else {
				$(this).addClass("open");
				$(this).find("a").css("background-position","right -20px");
				$(this).find("span").text("Hide");
			}
			e.preventDefault();
		});
	}
}

function newsArchive() {
	$('.newsletterArchList').hide();
	
	$('#content .newsletterArchContainer:first-child .newsletterArchList').show();
	$('#content .newsletterArchContainer:first-child h2').addClass("open");
	$('#content .newsletterArchContainer:first-child h2 span.noStories span').text("hide")
	
	$('.newsletterArchContainer h2').bind("click", function(e){
		if($(this).is(".open")) {
			$(this).siblings(".newsletterArchList").slideUp(600);
			$(this).removeClass("open");
			$(this).find("span.noStories span").text("expand");
		} else {
			$(this).siblings(".newsletterArchList").slideDown(600);
			$(this).addClass("open");
			$(this).find("span.noStories span").text("hide");
		}
		e.preventDefault();
	});
	
	if($('.howWeCanHelp').length > 0 ) {
		$('#howWeCanHelp').append("<p class='readMore'><a href='#' title='Click here to read more'><span>Read more</span></a></p>");
		$('#howWeCanHelp p.readMore').bind("click", function(e){
			$('#howWeCanHelp .howWeCanHelp').slideToggle(600);
			if($(this).is(".open")) {
				$(this).removeClass("open");
				$(this).find("a").css("background-position","right 4px");
				$(this).find("span").text("Read more");
			} else {
				$(this).addClass("open");
				$(this).find("a").css("background-position","right -20px");
				$(this).find("span").text("Hide");
			}
			e.preventDefault();
		});
	}
}

function applyCufon() {
    Cufon.replace
		('.caseStudy h3')
		('.widget h2');
}


// FUNCTION TO FIND LARGEST ITEM AND APPLY TO ALL MATCHED ELEMENTS

function evenHeights(array) {
	//alert("called");
//set max height to 0
	var max = 0;
	var item = 0;
// iterate through each matched element and get its height
	$.each(array, 
		function(n,val) {
			var item = $(val).height();
			//alert(item);
// compare the current largest height to the current items height
// if the item is larger, reset the max height to the items height
			if ( max < item ) {
				max = item;
			}
		});

// create the style declaration
		var cssHeights = 'min-height: ' + max + 'px;height: auto !important; height: ' + max + 'px;';

// apply the style declaration to each matched element
	$.each(array, function(n,val) { $(val).attr('style',cssHeights); } );
}