$(document).ready(function(){
	
	// this is for the inbox message controls
	$('td.subject a').click(toggleMessage);
		
		function toggleMessage(){
			var $message = $(this).parents('.mailitem').children('.message');
			
			if($message.is(':hidden')){
				$(this).parents('#inbox').find('.message').slideUp();
				$message.slideDown();
			}else{
				$message.slideUp();
			}
			$(this).parents('.mailhead').children().children().children().children('.new').html("<img src='/assets/media/images/old.jpg' />");
		}
		
	// this is the click function to select all checkboxes
	$('a[name=select]').click(toggleSelect);
	
	function toggleSelect(){		
		if($('input[type=checkbox]')){
			var amt = $('input[type=checkbox]').length;		
			if(amt != $('input[checked]').length) {
				$('input[type=checkbox]').attr('checked', 'true');				
				$(this).text('Select none');
			} else {
				$('input[type=checkbox]').removeAttr('checked');							
				$(this).text('Select all');
			}
		}
	}
	
	// modal functions
	$('a[name=modal]').click(
		function modal(e){
		//cancel link behavior
		e.preventDefault();
		//get A tag
		var id = $(this).attr('href');
		
		//get screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		
		//set height and width to mask so it fills up the screen
		$('#mask').css({'width':maskWidth, 'height':maskHeight});
		
		//transition effect
		$('#mask').show(100);
		
		//get window height and width
		var winH = $(window).height();
		var winW = $(window).width();
		
		//setpopup to center
		$('#modal').css('top', getPageScroll()[1] + (getPageHeight() / 5));
		$('#modal').css('left', winW/2-$('#modal').width()/2);

		//transition
		$('#modal').fadeIn(200);
		
		$('#modal').load(id);
		
	});

	//close btn clicked
	$('a.close').click(
		function(){
			$(this).parents('#modal').fadeOut(200);
			$('#mask').hide();
		});
		
  //if mask is clicked
	$('#mask').click(
		function(){
			$(this).hide();
			$('#modal').fadeOut(200);
		}
	);

	
  // getPageScroll() by quirksmode.com
  function getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
      yScroll = self.pageYOffset;
      xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
      yScroll = document.documentElement.scrollTop;
      xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
      xScroll = document.body.scrollLeft;	
    }
    return new Array(xScroll,yScroll) 
  }

  // Adapted from getPageSize() by quirksmode.com
  function getPageHeight() {
    var windowHeight
    if (self.innerHeight) {	// all except Explorer
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowHeight = document.body.clientHeight;
    }	
    return windowHeight
  }

	//forum category collapse functions
	$('.category h3').click(toggleCat);

	function toggleCat(){
		var $table = $(this).parent().children('.table');
		
		if($table.is(':hidden')){
			$(this).parent().children('.table').slideDown();
			$(this).parent().css('background', '#f8f8f8');
			$(this).children('img.icon').attr('src', '/assets/media/images/minus.gif');
		}else{
			$(this).parent().children('.table').slideUp();
			$(this).parent().css('background', '#e9e9e9');
			$(this).children('img.icon').attr('src', '/assets/media/images/plus.gif');
		}
	}

	borderSize();
	
	function borderSize(){
		if($('#left').height() < $('#content').height()){
			var height = $('#content').height();
			$('#left').css('height', height);
		}
	}
	
});


function checkAll() {
	if($('input[type=checkbox]')){
		for (i = 0; i < $('input[type=checkbox]').length; i++){
			$('input[type=checkbox]')[i].checked = true ;	
		}	
		window.document.getElementById("chkselect").innerHTML = "<a href=\"#\" onclick=\"javascript: uncheckAll();\">Select none</a>";
	}	
}

function uncheckAll() {
	if($('input[type=checkbox]')){
		for (i = 0; i < $('input[type=checkbox]').length; i++){
			$('input[type=checkbox]')[i].checked = false;	
		}
		window.document.getElementById("chkselect").innerHTML = "<a href=\"#\" onclick=\"javascript: checkAll();\">Select all</a>";
	}
}

