$(document).ready(function(){
	
	emailListCheck();
	rowClick();
	setJqueryDatePickers();
	expandClick();
	subscribeClick();
	
});

/**
 * This is checks the checkbox for a row when selected.
 */
function rowClick() {
	$('tr').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');
		updateTotal();
	});
}

/**
 * cancel the check event, in a way..
 * @return
 */
function emailListCheck(){
	$('input:checkbox').click(function () {
		if($(this).parent().get(0).tagName == "TD"){
			$(this).attr('checked', !$(this).attr('checked'));
		}
	});
}

function subscribeClick(){
	$('input[name="weeklyreport"]').click(function(){
		var textCtrl = $('span[id="subscriptionText"]');
		textCtrl.html("Updating...");
		$.ajax({
		    type: "GET",
		    url: "http://www.trap25.com/ajax.php",
		    data: "m=weeklyreport&value=" + (($(this).attr('checked')) ? "1" : "0" ),
			success: function(msg){
				displayData = msg;
				textCtrl.html(displayData);
		 	},
			error: function(msg){
				displayData = "Unable to retreive data, please try again...";
				textCtrl.html(displayData);
			}
		 });
	});
}

function expandClick(){
	$('a[id^="link"]').click(function(event) {
		var id = getIdFromLink($(this).attr('id'));
		$('tr[id="row'+id+'"]').toggle();
		event.stopImmediatePropagation();
	});
}

function getIdFromLink(val){
	return val.replace("link", "");
}

function updateTotal(){
	$('span#breakdown').html($('input[name="release[]"]:checked').size());
}

function setJqueryDatePickers(){
	$('.datePicker').datepicker({
		dateFormat: 'dd-mm-yy',
		currentText: 'Now'
	});
}

$.fn.pause = function(duration) {
    $(this).animate({ dummy: 1 }, duration);
    return this;
};


//function rowSelect(){
//	$('tbody tr').click(function (){
//		alert("hi");
//	});
//}