
function setOpacity() {
    $(".opacity").css('opacity', 0.15);
}

function fixHeight() {
	var main = $("#main").height();
	var aside = $("#aside").height();
	var popup = $(".popup").height();		
	var fixedAside;
	if (popup != null) {
		fixedAside = aside - popup;
	} else {
		var marginTop = parseInt($("#aside").css("margin-top"));
		fixedAside = aside + marginTop;
	}
	
    if (main < fixedAside) {
    	$("#main").height(fixedAside);
    }  
    	
    fixedFooterAtBottom();
    $(window).resize(fixedFooterAtBottom);
}

function fixedFooterAtBottom() {	
	// don't do anything for IE7
	if (!$.browser.msie && $.browser.version != '7.0') {
		var documentHeight = $(document).height();
		var footerHeight = $("#footer").height();	
		var header = $('#header').height();
		var navigation = $('#navigation').height();
		var main = $('#main').height();			
		var wrapperHeight = (header + navigation + main + footerHeight);
		
		var result;
		if (wrapperHeight + footerHeight == documentHeight) {
			result = wrapperHeight + footerHeight;
		} else if (wrapperHeight + footerHeight < documentHeight) {
	    	result = documentHeight - footerHeight;
	    }    		
		$('#wrapper').height(result);
	}
}

function alignStartpage() {
    if ($("#main").hasClass("startpage")) {
    	var offset = alignStartpageOffset();
   	
    	var firstHeight = $(".module:first").outerHeight(true);
    	var profileHeight = $("#profile").height();
    	// it will be null when looking at collection pages where the 
    	// profile/flash has been removed which is every page but the first, 
    	// setting it to default 320px, we can't do much better than that
    	if (profileHeight == null) {
    		profileHeight = 320;
    	}
        $(".module:eq(1)").css("height", profileHeight - firstHeight - offset);
    
    }    
}

function alignStartpageOffset() {	
	var result = 21;
	
	// jquery chrome detection doesn't seem to work all the time, at least 
	// not the version we got
	if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {		
		if ($.client.os == 'Mac') {
			result = 15;
		} else {
			result = 16;
		}		
	} else if ($.browser.safari) {
		if ($.client.os == 'Mac') {
			result = 15;
		} else {
			result = 16;
		}
	} else if ($.browser.msie) {
		// ie 8 == 6
		if ($.browser.version == '7.0') {
			result = 0;
		} else {		
			result = 20;
		}
	} else if ($.browser.mozilla) {		
		if (parseFloat($.browser.version) >= 1.9) {
			result = 16;
		} else if ($.client.os == 'Windows') {
			result = 20;
		} 
	}	
	
	return result;
}

function initPopup(){
    // Return a specific popup, or all if no id is specified
    function getPopup(popup_id) {
        var elements = $(".popup .popup_content");
        if (popup_id != null) {
            elements = $(".popup:eq(" + popup_id + ") .popup_content");
        }
        return elements;
    }

    // Add extra wrappers if javascript is enabled
    $(".popup").addClass("popup_loaded");
    $(".popup .module_inner").wrap('<div class="popup_content" id="popup_content"></div>');
    $(".popup .popup_content").prepend("<h2>" + $(".popup h2").text() + "</h2>");
    $(".popup .popup_content").prepend('<div class="close"></div>');
    $(".popup .popup_content").wrapInner('<div class="popup_outer"></div>');
    $(".popup").find("h2:first").addClass("fakeLink");
    getPopup().hide();

    // Setup behvior for popups
    var headers = $(".popup").find("h2:first");
    
    // IE6 does not support :hover on things other than links
    headers.hover(
        function(){$(this).addClass("popup_mouseover");},
        function(){$(this).removeClass("popup_mouseover");}
    );

    // Hide all popups, and show one popup when clicking on its link
    headers.each(function(i) {
        $(this).click(function(){ 
            getPopup().hide();
            getPopup(i).show();
        });
    });

    // Hide one popup when clicking on its close icon
    $(".popup .close").each(function(i) {
        $(this).click(function(){ 
            getPopup(i).hide();
        });
    });

    // Hide all popups when clicking outside popup...
    $("body").click(function(){ 
        getPopup().hide();
    });
    
    // But not when clicking inside it
    $(".popup").click(function(event){
        event.stopPropagation();
    });
}

function pause(millis) {
	var date = new Date();
	var curDate = null;
	
	do { 
		curDate = new Date(); 
	}
	while(curDate-date < millis);
} 

$(document).ready(function() {	
    setOpacity();
    fixHeight();
    initPopup();
    alignStartpage();
});
