/*

           ::::::::  :::::::::: ::::::::   ::::::::  ::::    ::: :::::::::  
          :+:    :+: :+:       :+:    :+: :+:    :+: :+:+:   :+: :+:    :+: 
          +:+        +:+       +:+        +:+    +:+ :+:+:+  +:+ +:+    +:+ 
          +#++:++#++ +#++:++#  +#+        +#+    +:+ +#+ +:+ +#+ +#+    +:+ 
                 +#+ +#+       +#+        +#+    +#+ +#+  +#+#+# +#+    +#+ 
          #+#    #+# #+#       #+#    #+# #+#    #+# #+#   #+#+# #+#    #+# 
           ########  ########## ########   ########  ###    #### #########  

     ::::::::   ::::::::  :::    :::  :::::::: ::::::::::: ::::    :::  ::::::::  
    :+:    :+: :+:    :+: :+:    :+: :+:    :+:    :+:     :+:+:   :+: :+:    :+: 
    +:+        +:+    +:+ +:+    +:+ +:+           +:+     :+:+:+  +:+ +:+        
    +#+        +#+    +:+ +#+    +:+ +#++:++#++    +#+     +#+ +:+ +#+ +#++:++#++ 
    +#+        +#+    +#+ +#+    +#+        +#+    +#+     +#+  +#+#+#        +#+ 
    #+#    #+# #+#    #+# #+#    #+# #+#    #+#    #+#     #+#   #+#+# #+#    #+# 
     ########   ########   ########   ######## ########### ###    ####  ########  


                /           /       / /  /      /     /  /       / /
                    / /      /    /   /     ===================  
                         /    /   /___  /    secondcousins.org   /
                  /   /       /  ,;(  /-,   ===================
                    /      /    /(((\   (    /   /  /    /   /
                /      /      / \\\\(.__,'           /       /   /
                   /  /   /     /\)\\:._`.__/   /      /   /
                / /     /   /    ,)'        `._   /     /    /
                     /         ,'           \  `-..___)\__,,     /
                 /       /  ,-'    ,         `._     (__.-'   /
                 ,.-'`,_..-'    .-' \         \ ``---''   /
                /  ``-/      _.-'   / \         `. /    /       /
                 /   ``---''    /     \       _,(   /      /
                      /     /           \ _.-''   \ /   /     /
                  /   /      /    /  /  (     .   \      /  
                        /        /       \     \   \ /    /    /
                /  /    /     /      /   / )     )   )    /    
                          /     /     _/_/     /   /  /    /    /
                /  _/__...---/--'''/''  /     /   /```--/-..._/__
                  < >   _.-.    -   _  /    _(  _(_   - _   < >  
                      .   _-  <  >   -'..-''  `'   .-     _   .
                   `--...__     .-.  __.   <  >      <  >
                           ``---..__        ..-._  .   _.-.   <  >
                                    ```----....-._________________   
                                                      
*/
$(document).ready(function() {
	var parent_height = $(window).height();
	var container_height = $('#window-container').height();
	var top_margin = (parent_height - container_height) / 2;
	var parent_width = $(window).width();
	var container_width = $('#window-container').width();
	var left_margin = (parent_width - container_width) / 2;
	$('#window-container').css('left', left_margin + "px");
	$('#window-container').css('top', top_margin + "px");
	$('#new-mail-container').css('left', left_margin + 200 + "px");
	$('#new-mail-container').css('top', top_margin - 25 + "px");
	$('#background-info').css('left', left_margin - 40 + "px");
	$('#background-info').css('top', top_margin - 35 + "px");
	$('#cam-container').css('left', left_margin + 710 + "px");
	$('#cam-container').css('top', top_margin + 195 + "px");
	if ($.browser.mozilla) {
		$('#top-row ul').css({
			'position': 'relative',
			'top': '-1px'
		});
		$('#top-row li').css({
			'padding-top': '2px'
		});
		$('.col-2 input').css({
			'margin-bottom': '3px'
		});
	}
	if ($.browser.msie) {
		//$('#window-container, #cam-container').hide();
		//$('#background-info').css('left', left_margin + 275 + "px");
		//$('#background-info').css('top', top_margin + 100 + "px");
		$('#background-info ul').append('<br><li>&mdash; By the way, this site looks much prettier in newer browsers</li>');
	}
	if (jQuery.browser.mozilla && jQuery.browser.version < '1.9.2') {
		//$('#window-container, #cam-container').hide();
		//$('#background-info').css('left', left_margin + 275 + "px");
		//$('#background-info').css('top', top_margin + 100 + "px");
		$('#background-info ul').append('<br><li>&mdash; By the way, this site looks much prettier in newer browsers</li>');
	}
	$('#background-info').resizable();
	$('.column-2').jScrollPane();
	$('.column-3').jScrollPane();
	$('#inbox li:first-child').addClass('first-unread');
	$('.first-unread').prepend('<span class="unread-circle"></span>');
	$('img').removeAttr('alt').removeAttr('title');
	$('.checking .checking-icon').fadeIn(200);
	setTimeout(function() {
		$('.checking .checking-icon').fadeOut(200);
		$('li.first-unread').toggleClass('first-unread');
		$('.unread-count').removeClass('hidden');
		$('.column-2').jScrollPane();
	}, 3000);
	$('#tab-icon').click(function() {
		$('#activity').slideToggle(200);
		$('#tab-icon img').toggleClass('rotate');
	});
	$('ul.top-buttons li, .circle-button:first-child').bind({
		mousedown: function() {
			$(this).addClass('mousedown');
		},
		blur: function() {
			$(this).removeClass('mousedown');
		},
		mouseup: function() {
			$(this).removeClass('mousedown');
		}
	});
	$('#check').click(function() {
		$('#activity').slideDown(200);
		$('#tab-icon img').removeClass('rotate');
		$('.checking .checking-icon').fadeIn(200);
		setTimeout(function() {
			$("#activity").slideUp(200);
			$('#tab-icon img').addClass('rotate');
			$('.checking .checking-icon').fadeOut(200);
		}, 2000);
	});
	$('.first-unread').click(function() {
		$('.unread-count').addClass('hidden');
		$('.unread-circle').addClass('hidden');
	});
	$('.mailboxes li.clickable, #top-row li, .top-buttons li#notes').click(function() {
		var currentlinkId = $(this).attr('id');
		$('.column-2 li').removeClass('active');
		$('#msg').empty();
		
		$('#msg').prepend('<h1 class="no-message">No message selected</h1>');
		$('.mailboxes li').removeClass('active');
		$('#top-row li').removeClass('active');
		$('li#' + currentlinkId).addClass('active');
		$(this).addClass('active');
		$('.column-2').removeClass('visible');
		$('.column-2').addClass('hidden');
		$('div#' + currentlinkId).addClass('visible');
		$('.column-2').jScrollPane();
	});
	$('.column-2 li').click(function() {
		var currentId = $(this).attr('id');
		$('.column-2 li').removeClass('active');
		$(this).toggleClass('active');
		$('#msg').empty();
		$('.column-3').jScrollPane();
		$.ajax({
			url: 'http://dow-smith.com/?p=' + currentId,
			cache: false,
			success: function(html) {
				$('#msg').append(html);
				$('.column-3').jScrollPane();
			}
		});
	});
	$('li#send').click(function() {
		$('#new-mail-container').addClass('hidden');
		$('#window-container').removeClass('background');
		$('#activity').slideDown(200);
		$('#tab-icon img').removeClass('rotate');
		$('.checking .checking-icon').addClass('hidden');
		$('.checking .sending-icon').fadeIn(200);
		setTimeout(function() {
			$("#activity").slideUp(200);
			$('#tab-icon img').addClass('rotate');
			$('.checking .sending-icon').fadeOut(200);
		}, 2000);
	});
	$('#write-mail').click(function() {
		$('#new-mail-container').removeClass('hidden');
		$('#new-mail-container').removeClass('background');
		$('#window-container').addClass('background');
		$('#background-info').addClass('background');
		$('#cam-container').addClass('background');
		$('#new-mail-container').css({
			zIndex: 10000
		});
		$('.mail-content, .new-header').empty();
		$('#to, #subject, #cc,').val('');
		$('.new-header').prepend('(No Subject)');
		$('.mail-content').append('<br><p>&mdash;<br>dow-smith.com<br>020 7193 5616</p>');
		$('.mail').jScrollPane();
	});
	$('#reply').click(function() {
		if ($('.column-2 li').hasClass('active')) {
			$('#new-mail-container').removeClass('hidden');
			$('#new-mail-container').removeClass('background');
			$('#window-container').addClass('background');
			$('#background-info').addClass('background');
			$('#cam-container').addClass('background');
			$('#new-mail-container').css({
				zIndex: 10000
			});
			$('.mail-content, #to, #subject, #cc, .new-header').empty();
			$('.wp-content').clone().appendTo('.mail-content');
			$('input#to').val($('.wp-from-address').text());
			$('input#subject').val('Re: ' + $('.wp-subject').text());
			$('.wp-subject').clone().appendTo('.new-header').prepend('Re: ');
			$('.mail-content').prepend('<br><p>&mdash;<br>dow-smith.com<br>020 7193 5616</p><br>');
			$('.mail').jScrollPane();
		}
	});
	$('#forward').click(function() {
		if ($('.column-2 li').hasClass('active')) {
			$('#new-mail-container').removeClass('hidden');
			$('#new-mail-container').removeClass('background');
			$('#window-container').addClass('background');
			$('#background-info').addClass('background');
			$('#cam-container').addClass('background');
			$('#new-mail-container').css({
				zIndex: 10000
			});
			$('.mail-content, #to, #subject, #cc, .new-header').empty();
			$('.wp-content').clone().appendTo('.mail-content');
			$('input#subject').val('Fwd: ' + $('.wp-subject').text());
			$('.wp-subject').clone().appendTo('.new-header').prepend('Fwd: ');
			$('.wp-date').clone().prependTo('.mail-content').prepend('On ').append(', ').append($('.wp-from').clone()).append(' said:<br>');
			$('.mail-content').prepend('<br><p>&mdash;<br>dow-smith.com<br>020 7193 5616</p><br>');
			$('.mail').jScrollPane();
		}
	});
	$('#new-mail-container .circle-button:first-child').click(function() {
		$('#new-mail-container').addClass('hidden');
	});
	$('#window-container .circle-button:first-child').click(function() {
		$('#window-container').addClass('hidden');
	});
	$('#background-info .circle-button:first-child').click(function() {
		$('#background-info').addClass('hidden');
	});
	$('#cam-container .circle-button:first-child').click(function() {
		$('#cam-container').addClass('hidden');
	});
	$('#new-mail-container').mousedown(function() {
		$('#new-mail-container').removeClass('background');
		$('#window-container').addClass('background');
		$('#background-info').addClass('background');
		$('#cam-container').addClass('background');
	});
	$('#window-container').mousedown(function() {
		$('#new-mail-container').addClass('background');
		$('#window-container').removeClass('background');
		$('#background-info').addClass('background');
		$('#cam-container').addClass('background');
	});
	$('#background-info').mousedown(function() {
		$('#new-mail-container').addClass('background');
		$('#window-container').addClass('background');
		$('#background-info').removeClass('background');
		$('#cam-container').addClass('background');
	});
	$('#cam-container').mousedown(function() {
		$('#new-mail-container').addClass('background');
		$('#window-container').addClass('background');
		$('#background-info').addClass('background');
		$('#cam-container').removeClass('background');
	});
	var sqs = $('div.drag').draggable({
		handle: '.header'
	}).mousedown(function() {
		sqs.css({
			zIndex: function() {
				return Math.max(--this.style.zIndex, 0);
			}
		});
		$(this).css({
			zIndex: 10000
		});
		$(this).removeClass('background');
	});
});

$(window).load(function(){

	if ($.browser.msie) {
			}
	if (jQuery.browser.mozilla && jQuery.browser.version < '1.9.2') {
		
	}
	else {
	$('#window-container').fadeIn(300, function(){
		$('#cam-container, #background-info').fadeIn(300);
	});
	}
});

