// JavaScript Document
$(document).ready(function(){
	clearClick();
	setBrowserClass();
	adjustContentHeight();
	initCloseButton();
	initMenu();
});

function adjustContentHeight(){
	if($('#sidebar').length > 0){
		
		var sidebarTopMargin = parseInt($('#sidebar').css('margin-top').replace('px',''));
		var sidebarHeight = $('#sidebar').height() + sidebarTopMargin;
		//console.log(sidebarHeight);
		var contentHeight = $('#text').height()+80;
		//console.log(contentHeight);
		if(contentHeight < sidebarHeight){
				var newHeight = contentHeight - sidebarTopMargin;
				//$('#sidebar').height(newHeight);
				$('#content').height(sidebarHeight);
				if($('#sidebar').height()+sidebarTopMargin < contentHeight) {
					$('#sidebar').height(newHeight + 80);	
				}
				else if($('#sidebar').height()+sidebarTopMargin > contentHeight) {
					$('#content').height($('#sidebar').height()+sidebarTopMargin);	
				}
				else {
					$('#sidebar').height(newHeight);
					$('#content').height(newHeight + sidebarTopMargin);
					if($('#sidebar').height()+sidebarTopMargin < $('#content').height()) {
							$('#sidebar').height(newHeight+80 + sidebarTopMargin);
							$('#content').height($('#sidebar').height() + sidebarTopMargin);
					}
				}
			}
		else{
			var newHeight = contentHeight - sidebarTopMargin;
				$('#sidebar').height(newHeight);
				$('#content').height(contentHeight);
				if($('#sidebar').height()+sidebarTopMargin < contentHeight) {
					$('#sidebar').height(newHeight + 80);	
				}
		}
	}
}

/*-----------------
 * Ministry and Campus dropdowns
 *-----------------*/
var currentTab = "";

function initCloseButton(){
	$("#btnclose").click(function(){
		$("#dropdown_wrapper").slideUp(function(){
			$("#"+currentTab).css('display','none');
		});
	});
	return false;
}

function showDropdown(id){
	currentTab = id;
	$("#"+id).css('display','block');
	$('#dropdown_wrapper').slideDown();
	return false;
}


function initMenu() {
	
	// This is the function if any conntent needs to collapse
	$('#text .collapseContent').hide();
	//$('#nav ul:first').show();
	$('#text .collapseTitle').click(function(){
		var checkElement = $(this).next();
		if((checkElement.is('.collapseContent')) && (checkElement.is(':visible'))) {
			$('#text .collapseContent:visible').slideUp('fast', function() {
				adjustContentHeight();															 
			});
			return false;
		}
		if((checkElement.is('.collapseContent')) && (!checkElement.is(':visible'))) {
			$('#text .collapseContent:visible').slideUp('fast', function() {
				adjustContentHeight();															 
			});
			checkElement.slideDown('fast', function() {
				adjustContentHeight();										
			});
			return false;
		}
	});
}

/*-----------------
 * Gallery functions
 *-----------------*/
function initLightBox(){
	$(function(){
		$("a[rel='lightbox']").lightBox({
			imageLoading: '/images/lightbox-ico-loading.gif',
			imageBtnClose: '/images/lightbox-btn-close.gif',
			imageBtnPrev: '/images/lightbox-btn-prev.gif',
			imageBtnNext: '/images/lightbox-btn-next.gif',
			fixedNavigation: true
		});
	});
}

function gallery_prevPage(){
	var previous = $('.photo_list').attr('current_page');
	current = parseInt(previous) - 1;
	if(current == 1) $('.paging .prev a').hide();
	$('.paging .next a').show();
	$('.photo_list').attr('current_page', current);
	$('.paging .info').fadeOut('fast',function(){
		$('#current_page').text(current);
		$('.paging .info').fadeIn();
	});
	$('#gallery_group'+previous).fadeOut('fast',function(){ $('#gallery_group'+current).fadeIn(); });
}

function gallery_nextPage(){
	var previous = $('.photo_list').attr('current_page');
	current = parseInt(previous) + 1;
	var maxgroups = $('.gallery_group').length;
	if(current == maxgroups) $('.paging .next a').hide();
	$('.paging .prev a').show();
	$('.photo_list').attr('current_page', current);
	$('.paging .info').fadeOut('fast',function(){
		$('#current_page').text(current);
		$('.paging .info').fadeIn();
	});
	$('#gallery_group'+previous).fadeOut('fast',function(){ $('#gallery_group'+current).fadeIn(); });
}
