$(document).ready(function(){
	subscriptionSelector();
	updateCellStyles();
	tableCheckbox();
	subscriptionSelect();
	autoSelect();
	deleteGroups();
	updateSubscriptions();
});

function updateSubscriptions(){
	/*
$('#sleep input[type="checkbox"]').live('click', function(){
		var table = $(this).parent();
		while(!$(table).is('table')){
			table = $(table).parent();
		}
		if(checkBoxsEmpty(table, 'thead tr td input[type="checkbox"]') == false) $('#sleep button[name="submit"]').removeAttr('disabled');
		else $('#sleep button[name="submit"]').attr('disabled', 'disabled');
	});
*/
	$('#sleep button[name="submit"]').confirm({
		msg: 'Are you sure you want to sleep/delete these subscriptions?',
		dialogShow:'fadeIn',
		dialogSpeed:'slow',
		buttons:{
			wrapper: '<button></button>',
			separator:'  '
		}
	});
}

function deleteGroups(){
	$('#contacts table input[type="checkbox"], #groups table input[type="checkbox"]').live('click', function(){
		var table = $(this).parent().parent().parent().parent();
		if(checkBoxsEmpty(table, 'tbody tr td input[type="checkbox"]') == false) $(table).find('button[name="delete"]').removeAttr('disabled');
		else $(table).find('button[name="delete"]').attr('disabled', 'disabled');
	});
	
	$('#contacts button[name="delete"]').confirm({
		msg: 'Are you sure you want to delete these Contacts?',
		dialogShow:'fadeIn',
		dialogSpeed:'slow',
		buttons: {
			wrapper: '<button></button>',
			separator:'  '
		}
	});
	$('#groups button[name="delete"]').confirm({
		msg: 'Are you sure you want to delete these Groups?',
		dialogShow:'fadeIn',
		dialogSpeed:'slow',
		buttons: {
			wrapper: '<button></button>',
			separator:'  '
		}
	});
}

function subscriptionSelect(){
	
}

function autoSelect(){
	$('#searchbanner input[name="loginsubmit"], #subscriptions form input[type="submit"]').hide();
	$('#searchbanner select, #subscriptions select').change(function(){
		if ($(this).attr('name') == "sid") {
			window.location.href = "http://www.checkmybox.net/statistics/" + $(this).val();
		}
		else {
			$(this).parents('form:first').submit();
		}
	});
}

function subscriptionSelector(){
	$('#subscriptions tr.domain td input[type="checkbox"]').hide();
	$('#subscriptions tr.domain td').css('cursor', 'pointer');
	$('#subscriptions tr.domain td').hover(
		function(){
			$(this).addClass('hover');
		},
		function(){
			$(this).removeClass('hover');
		}
	);
	$('#subscriptions tr.domain td:not(:first-child)').live('click', function(){
		if($(this).find('input[type="checkbox"]').attr('checked') != true) $(this).find('input[type="checkbox"]').attr('checked', 'checked');
		else $(this).find('input[type="checkbox"]').removeAttr('checked');
		updateCellStyles();
	});
	
	checkboxTable();
	updateCellStyles();
}

function confirmModal(text, confirmlink){
	openModalWindow();
	$('#modalbox').append('<p>' + text + '</p><p><button name="something" class="yes">Yes</button> <button name="somethingelse" class="no">No</button></p>');
	$('#modalbox button').live('click', function(){
		closeModalWindow();
		return (($(this).hasClass('yes')) ? true : false);
	});
}

function checkboxTable(){
	$('table thead td:first-child input[type="checkbox"]').live('click', function(){
		var table = $(this).parent().parent().parent().parent();
		if($(this).attr('checked') == true){
			if(checkBoxsEmpty(table, 'tbody tr td input[type="checkbox"]') == true) $(table).find('tr td:first-child input[type="checkbox"]').attr('checked', 'checked'); //tick all
			else $(table).find('tr td:first-child input[type="checkbox"]').removeAttr('checked'); //empty ticks
		} else {
			$(table).find('tr td:first-child input[type="checkbox"]').removeAttr('checked'); //empty ticks
		}
	});
}

function checkBoxsEmpty(table, path){
	var rtn = true;
	$(table).find(path).each(function(){
		if($(this).attr('checked') == true) rtn = false;
	});
	return rtn;
}

function updateCellStyles(){
	$('#subscriptions tr.domain td').each(function(){
		if($(this).find('input[type="checkbox"]').attr('checked') == true) $(this).removeClass('hover').addClass('selected');
		else $(this).removeClass('hover').removeClass('selected');
	});
}

function tableCheckbox(){
	var row = $('table.checkbox tbody tr');
	$('table.checkbox tr td.checkbox').hide();
	$(row).css('cursor', 'pointer');
	$(row).hover(
		function(){
			$(this).addClass('hover');
		}, 
		function(){
			$(this).removeClass('hover');
		}
	);
	$(row).live('click', function(){
		if($(this).find('input[type="checkbox"]').attr('checked') == true) $(this).find('input[type="checkbox"]').removeAttr('checked').parent().parent().removeClass('selected');
		else $(this).find('input[type="checkbox"]').attr('checked', 'checked').parent().parent().addClass('selected');
	});
	$(row).each(function(){
		updateCheckboxRow($(this));
	});
}

function updateCheckboxRow(row){
	if($(row).find('input[type="checkbox"]').attr('checked') == true) $(row).addClass('selected');
	else $(row).removeClass('selected');
}