jQuery(window).load(function() {
	var $ = jQuery;

	$('.modal-box header a').click(function() {
		$(this).parents('.modal-box').hide();

		return false;
	});
	

	$('#footer a.contact-us').click(function() {
		$('.modal-box').hide();

		if ($.browser.msie === true && $.browser.version.substr(0,1) < 7) {
			$(document).scrollTop(0);
		}

		$('#modal-contact-us').show();

		return false;
	});

	if ($.browser.msie === true && $.browser.version.substr(0,1) < 9) {
		if ($('body').hasClass('page-template-events-php')) {
			$('.calendar-body a:nth-child(4n)').css('margin', '3px 0 0');
		}

		$('#footer .col-1 ul li:nth-child(3n-1)').css('width', '100px');
		$('#footer .col-1 ul li:nth-child(3n)').css('width', '93px');
	
		if ($.browser.version.substr(0,1) < 8) {
			$('#main-nav ul.menu > li').hover(function() {
				$('#main-nav ul.menu > li').removeClass('hover');
				$(this).addClass('hover');
			}, function() {
				$(this).removeClass('hover');
			});
		}
	}
	
	if ($('body').hasClass('home')) {
		function postScrollable(s) {
			var page = 0;
			var t;
			var max;

			function init() {
				page = 1;
				max = Math.ceil($(s + ' .articles article').length / 2);

				$(s + ' .articles article:gt(1)').hide();

				if (max > 1) {
					var htmlStr = '<div><div class="page-link"><span>Page</span>';
					for (var i = 0; i < max; i++) {
						htmlStr += '<a href="#" class="page-numbers">' + (i + 1) + '</a> ';
					}
					

					htmlStr += '</div></div>';

					$(s + ' .articles').after(htmlStr);
					$(s + ' .page-link a:first').addClass('current');
					
					$(s + ' .page-link a').click(function() {
						clearTimeout(t);

						page = parseInt($(this).html(), 10) - 1;
						update();

					
						return false;
					});

					scrollable();
				}
			}
				
			function update() {
				$(s + ' .articles article').hide();
				$(s + ' .page-link a').removeClass('current');
				$(s + ' .articles article:eq(' + (page * 2) + ')').show();
				$(s + ' .articles article:eq(' + ((page * 2) + 1) + ')').show();
				$(s + ' .page-link a:eq(' + page + ')').addClass('current');
			}

			function scrollable() {
				 t = setTimeout(function() {
					update();

					page++;
						
					if (page === max) {
						page = 0;
					}

					scrollable();
			
				}, 10000);

			}

			return {
				init: init
			}
		}

		var article_min_height = 0;
		$('.recent-posts article').each(function() {
			var height = $(this).height() -	$(this).children('.entry-summary').children('p').height();

			height = height + (8 * 16);

			if (height > article_min_height) {
				article_min_height = height;
			}
		});
		
		$('.recent-posts article').each(function() {
			var summary = $(this).children('.entry-summary').children('p');

			var height = $(this).height() - $(this).children('.entry-summary').children('p').height();
			
			var lines = 8;

			height = height + (lines * 16);

			while (height < article_min_height) {
				lines++;

				height = height + 16;
			}

			var text = summary.text().split(' ');

			while (summary.height() > (lines * 16)) {
				text.pop();

				summary.text(text.join(' ') + ' [...]');
			}
		});
		
		var new_min_height = 0;
		$('.recent-posts article').each(function() {
			var height = $(this).height();

			if (height > new_min_height) {
				new_min_height = height;
			}
		});
		
		$('.recent-posts article').each(function() {
			var min_height, summary, height = $(this).height();

			if (height < new_min_height) {
				summary = $(this).children('.entry-summary').children('p');
				min_height = summary.height() + (new_min_height - height);
					
				if ($.browser.msie === true && $.browser.version.substr(0,1) < 7) {
					summary.css('height', min_height + 'px');
				} else {
					summary.css('min-height', min_height + 'px');
				}
			}
		});

		var newsScrollable = postScrollable('.recent-posts .news');
		newsScrollable.init();

		var eventsScrollable = postScrollable('.recent-posts .events');
		eventsScrollable.init();
	}
});

