﻿$(document).ready(function () {

	// hover
	$('SPAN.toggle').hover(
		function () {
			$(this).addClass("hover");
		},
		function () {
			$(this).removeClass("hover");
		}
	);
	
	// showing and hiding locations
	$('.region .toggle').click(function () {
		ShowAreas($(this).parent());
	})
	$('.locationarea .toggle').click(function () {
		ShowLocations($(this).parent());
	})

	// ticking checkboxes
	$('.all_of_nz').click(function () {
		UpdateLocations(this, 'nz');
	})
	$('.region INPUT').click(function () {
		UpdateLocations(this, 'r');
	})
	$('.locationarea INPUT').click(function () {
		UpdateLocations(this, 'la');
	})
	$('.location INPUT').click(function () {
		UpdateLocations(this, 'l');
	})
});

function ShowAreas(ele) {
	// check if we already got the data
	if ($(ele).hasClass('fetched')) {
		$('.areas', ele).toggleClass('hidden');
	}
	else {
		var regionName = $('SPAN.toggle', ele).text();
		var isParentTicked = $('INPUT', ele).attr('checked');
		$.getJSON('/services/getLocations.asp', { a: 'la', r: regionName, rnd: Math.random }, function (data) {
			if (data.result == 'OK') {
				var row = $('.copyzone .locationarea');
				$.each(data.objects, function (i, item) {
					$('SPAN.toggle', row).text(item.name);
					$('.id', row).text(item.id);
					var isTicked = isParentTicked;
					isTicked = (item.selected == item.selectedmax && item.selected != 0) // all of the sub items are selected - tick this!
					$('INPUT', row).attr('checked', isTicked); //.attr('title', item.selected + ' out of ' + item.selectedmax + ' selected');
					var newlyAddedRow = row.clone(true).appendTo($('.areas', ele));
					// expand if it's a partial
					if (item.selected < item.selectedmax && item.selected != 0) {
						$('SPAN.toggle', newlyAddedRow).click();
					}
				})
			}
			else {
				// hide the expand arrow
				$('IMG.toggle', ele).addClass('invisible');
			}
			$(ele).addClass('fetched'); // mark it as fetched, so we don't go get the data if it's opened again
		});
	}
}

function ShowLocations(ele) {
	// check if we already got the data
	if ($(ele).hasClass('fetched')) {
		$('.locations', ele).toggleClass('hidden');
	}
	else {
		var locationAreaId = $('.id', ele).text();
		var isParentTicked = $('INPUT', ele).attr('checked');
		$.getJSON('/services/getLocations.asp', { a: 'l', i: locationAreaId, rnd: Math.random }, function (data) {
			if (data.result == 'OK') {
				var row = $('.copyzone .location');
				$.each(data.objects, function (i, item) {
					$('.name', row).text(item.name);
					$('.id', row).text(item.id);
					var isTicked = isParentTicked ? true : item.selected;
					$('INPUT', row).attr('checked', isTicked);
					row.clone(true).appendTo($('.locations', ele));
				})
			}
			else {
				// hide the expand arrow
				$('IMG.toggle', ele).addClass('invisible');
			}
			$(ele).addClass('fetched'); // mark it as fetched, so we don't go get the data if it's opened again
		});
	}
}

function UpdateLocations(ele, action) {
	// tick or untick all the sub tickboxes
	var isTicked = $(ele).attr('checked');
	var priority = ($(ele).is('.primary INPUT')) ? 1 : 2;
	$('INPUT', $(ele).parent()).attr('checked', isTicked);

	var reg = '';
	var id = '';
	switch (action) {
		case 'r':
			reg = $(ele).next().text();
			break;
		case 'la':
		case 'l':
			var firstItem = $('.id', $(ele).parent()).get(0); // we always want the first item - this returns DOM element
			id = $(firstItem).text();
			break;
	}
	$.getJSON('/services/setLocations.asp', { a: action, r: reg, i: id, t: isTicked, p: priority, rnd: Math.random }, function (data) {
		if (data.result != 'OK') alert(data.result);
	});
}

// -------------------------------------------------
// jobs section
// -------------------------------------------------

$(document).ready(function () {

	// showing and hiding jobs
	$('.jobcategory .toggle').click(function () {
		ShowSubCategories($(this).parent());
	})
	$('.subcategoryarea .toggle').click(function () {
		ShowJobs($(this).parent());
	})

	// ticking checkboxes
	$('.jobcategory INPUT').click(function () {
		UpdateJobs(this, 'c');
	})
	$('.subcategoryarea INPUT').click(function () {
		UpdateJobs(this, 'sc');
	})
	$('.job INPUT').click(function () {
		UpdateJobs(this, 'j');
	})

	// expand any partials
	$('.partial').click(); // run the click event
});

function ShowSubCategories(ele) {
	// check if we already got the data
	if ($(ele).hasClass('fetched')) {
		$('.subcategories', ele).toggleClass('hidden');
	}
	else {
		var catId = $('.id', ele).text();
		var isParentTicked = $('INPUT', ele).attr('checked'); // is the parent ticked?
		$.getJSON('/services/getJobs.asp', { a: 'sc', i: catId, rnd: Math.random }, function (data) {
			if (data.result == 'OK') {
				var row = $('.copyzone .subcategoryarea');
				$.each(data.objects, function (i, item) {
					$('SPAN.toggle', row).text(item.name);
					$('.id', row).text(item.id);
					var isTicked = isParentTicked;
					isTicked = (item.selected == item.selectedmax && item.selectedmax != 0) // all of the sub items are selected - tick this!
					$('INPUT', row).attr('checked', isTicked); //.attr('title', item.selected + ' out of ' + item.selectedmax + ' selected');
					var newlyAddedRow = row.clone(true).appendTo($('.subcategories', ele));
					// expand if it's a partial
					if (item.selected < item.selectedmax && item.selected != 0) {
						$('SPAN.toggle', newlyAddedRow).click();
					}
				})
			}
			else {
				// hide the expand arrow
				$('IMG.toggle', ele).addClass('invisible');
			}
			$(ele).addClass('fetched'); // mark it as fetched, so we don't go get the data if it's opened again
		});
	}
}


function ShowJobs(ele) {
	// check if we already got the data
	if ($(ele).hasClass('fetched')) {
		$('.jobs', ele).toggleClass('hidden');
	}
	else {
		var subCatId = $('.id', ele).text();
		var isParentTicked = $('INPUT', ele).attr('checked');
		$.getJSON('/services/getJobs.asp', { a: 'j', i: subCatId, rnd: Math.random }, function (data) {
			if (data.result == 'OK') {
				var row = $('.copyzone .job');
				$.each(data.objects, function (i, item) {
					$('.name', row).text(item.name);
					$('.id', row).text(item.id);
					var isTicked = isParentTicked ? true : item.selected;
					$('INPUT', row).removeAttr('class'); // this is a row we copy repeatedly - clean it's class attr first
					$('INPUT', row).attr('checked', isTicked).addClass('id_' + item.id);
					row.clone(true).appendTo($('.jobs', ele));
				})
			}
			else {
				// hide the expand arrow
				$('IMG.toggle', ele).addClass('invisible');
			}
			$(ele).addClass('fetched'); // mark it as fetched, so we don't go get the data if it's opened again
		});
	}
}

function UpdateJobs(ele, action) {
	// tick or untick all the sub tickboxes
	var isTicked = $(ele).attr('checked');
	// TODO: if unticked, we should untick parents and grandparents
	$('INPUT', $(ele).parent()).attr('checked', isTicked); // select all sub nodes

	var firstItem = $('.id', $(ele).parent()).get(0); // we always want the first item - this returns DOM element
	var id = $(firstItem).text();

	// a job can be in multiple sub categories - untick or tick them all
	$('INPUT.id_' + id).attr('checked', isTicked);

	$.getJSON('/services/setJobs.asp', { a: action, i: id, t: isTicked, rnd: Math.random }, function (data) {
		if (data.result != 'OK') alert(data.result);
	});
}

	