var site = new MD();

function MD() {
	this.init = function() {
		this.domFixes();
		this.domEnhancements();
		this.encryptEmails();
	}
	this.domFixes = function() {
		// Nav items padding fix
		if($('#navigation > li:last').hasClass('selected')) {
			$('#navigation > li:last').css('border-bottom', 'none');
			$('#navigation > li:last').css('padding-bottom', '50px');
			$('#navigation > li:last').css('margin-bottom', '0');
			$('#navigation').css('padding-bottom', '0');
		}
		else {
			$('#navigation > li:last').css('padding-bottom', '0');
		}
		if($('#navigation > li:first').hasClass('selected')) {
			$('#navigation > li:first').css('border-top', 'none');
			$('#navigation > li:first').css('padding-top', '0');
			$('#navigation > li:first').css('margin-top', '0');
			$('#navigation').css('padding-top', '0');
		}
		else {
			$('#navigation > li:first').css('padding-top', '0');
		}
		
		// Last item bottom border
		$('.item_2:last').css('border-bottom-color', '#0f0f0f');
		
		// Content min-height to line up with content left
		$('#content').css('min-height', $('#content-left').height() - 44);
	}
	this.encryptEmails = function() {
		// Emails
		$('.email').each(function() {
			$(this).html(Rot13.write($(this).html()));
		});
	}
	this.domEnhancements = function() {
		
		$('.item_1').each(function() {
			var href = $(this).find('h3 a').attr('href');
			$(this).click(function() {
				window.location = href;
			});
			$(this).hover(function() {
				$(this).addClass('item_1-over');
			}, function() {
				$(this).removeClass('item_1-over');
			});
		});
		
		$('.item_2').each(function() {
			var href = $(this).find('h3 a').attr('href');
			$(this).click(function() {
				window.location = href;
			});
			$(this).hover(function() {
				$(this).addClass('item_2-over');
			}, function() {
				$(this).removeClass('item_2-over');
			});
		});
		
		// Social media images hover
		$('#social-media a').hover(function() {
			var source = $(this).find('img').attr('src');
			var newImage = source.replace('_off', '_on');
			$(this).find('img').attr('src', newImage);
		}, function() {
			var source = $(this).find('img').attr('src');
			var newImage = source.replace('_on', '_off');
			$(this).find('img').attr('src', newImage);
		});
		
		// External link (open in new windows)
		$('.external, #social-media a').click(function() {
			window.open($(this).attr('href'), 'New window', '_blank, width=1024, height=768, status=0, scrollbars=1, toolbar=1');
			return false;
		});
	}
}

$(function() {
	site.init();
});

Rot13 = {
    map: null,
    convert: function(a) {
        Rot13.init();
        var s = "";
        for (i=0; i < a.length; i++) {
            var b = a.charAt(i);
            s += ((b>='A' && b<='Z') || (b>='a' && b<='z') ? Rot13.map[b] : b);
        }
        return s;
    },
    init: function() {
        if (Rot13.map != null) return;    
        var map = new Array();
        var s   = "abcdefghijklmnopqrstuvwxyz";
        for (i=0; i<s.length; i++)
            map[s.charAt(i)] = s.charAt((i+13)%26);
        for (i=0; i<s.length; i++)
            map[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase();
        Rot13.map = map;
    },
    write: function(a) {
        //document.write(Rot13.convert(a));
		return Rot13.convert(a);
    }
}
