 /*	End-user reservation	*/

// config
var SELECTION_SPEED = 'fast'
var bConfirmationNeeded = false;

function prepareGroupAnd(Dl, checked) {
	Dl.children('dd').each( function() {
		prepareGroup($(this).children('dl'), checked)
	})
}

function prepareGroupXor(Dl, checked) {
	Dl.children('dt').each( function() {
		var Dt = $(this)

		var Input = Dt.find('input')
		if (!checked)
			Input.removeAttr('checked')
		Input.change(radioChanged)
			
		var Dd = Dt.next()
		if (Dd.is('dd')) {
			var DdDl = Dd.children('dl')
			if (Input.is(':checked')) {
				prepareGroup(DdDl, true)
			} else {
				Dd.hide()
				prepareGroup(DdDl, false)
			}
		}
	})
}

function prepareGroupOr(Dl, checked) {
	Dl.children('dt').each( function() {
		var Dt = $(this)

		var Input = Dt.find('input')
		if (!checked)
			Input.removeAttr('checked')
		Input.change(checkboxChanged)

		var Dd = Dt.next()
		if (Dd.is('dd')) {
			var DdDl = Dd.children('dl')
			if (Input.is(':checked')) {
				prepareGroup(DdDl, true)
			} else {
				Dd.hide()
				prepareGroup(DdDl, false)
			}
		}
	})
}

var radioChanged = function() {
	var Dt = $(this).parents('dt')
	var Dds = Dt.siblings('dd')
	$(':checked', Dds).removeAttr('checked')
	Dds.slideUp(SELECTION_SPEED)
	Dt.next().slideDown(SELECTION_SPEED)
}

var checkboxChanged = function() {
	var Checkbox = $(this)
	var Dd = Checkbox.parents('dt').next()
	if (Dd.is('dd')) {
		if (Checkbox.is(':checked')) {
			Dd.slideDown(SELECTION_SPEED)
		} else {
			$(':checked', Dd).removeAttr('checked')
			Dd.slideUp(SELECTION_SPEED)
		}
	}
}

$(function() {
	$('.stepLink,.confirmationEditButton').click(function () {
		bConfirmationNeeded = false;
		return true;
	});
	
	$('#reservationProgressBar').progressbar({
		value: reservationProgressPercent
	});
	
	ServicesToEdit = $('#servicesToEdit');
	ServicesToEdit.parent().css('height', '0');
	
	// UI tweak to hide browser selection border from anchor element after a mouse click.
	$('#nav a').click(function() {
		$(this).blur();
	});

	if (ServicesToEdit.length) {
		var Dl = $('#servicesToEdit').children('dl:first')
		$('a.showDescription').click(showDescription)
		prepareGroup(Dl, true);
	}

	if (typeof(reservationSavedSuccesfully) != 'undefined') {
		if (reservationSavedSuccesfully == true) {
			$('#successfulDiv').css('display','block');
			$('#successfulDiv').dialog({
				closeOnEscape: false,
				buttons: {
					'Ok' : function() {
						$(this).dialog('destroy');
						$('#successfulDiv').css('display','none');
						if (MODE_LIMITED) { 
							window.location = "../yritys/index.php?ssw=reservationHistory";
						} else {
							window.location = "../index.php?ssw=start";
						}
					}
				},
				// Fix for jQuery UI dialog bug in version UI 1.5.3
				// Both definitions modal: true && closeOnEscape: false don`t work at the same time.
				open: function() { 
					  $(document).unbind('keydown.dialog-overlay'); 
				},
				title: 'Ilmoitus',
				modal: true,
				overlay: { 
					opacity: 0.5, 
					background: 'black' 
				},
				resizable: false,
				draggable: false
			})
		} else {
			$('#failureDiv').css('display','block');
			$('#failureDiv').dialog({
				buttons: {
					'Ok' : function() {
						$(this).dialog('destroy');
						$('#failureDiv').css('display','none');
					}
				},
				// Fix for jQuery UI dialog bug in version UI 1.5.3
				// Both definitions modal: true && closeOnEscape: false don`t work at the same time.
				open: function() { 
					  $(document).unbind('keydown.dialog-overlay'); 
				},
				title: 'Virhe palvelussa!',
				closeOnEscape: false,
				modal: true,
				overlay: { 
					opacity: 0.5, 
					background: 'black' 
				},
				resizable: false,
				draggable: false
			})
		}
	}
	
	$('#confirmSubmit').mouseover(function () {
		$(this).attr('src', '/images/confirmHover.jpg');
	}).mouseout(function () {
		$(this).attr('src', '/images/confirm.jpg');		
	});	
});

// // Old version
// function scheduleInit() {
// var Service = getFirstChildElementByNodeName(ServiceForm, 'dl');
//
// As = Service.getElementsByTagName('A')
// for (i in As)
// if (As[i].title)
// As[i].setAttribute('onclick', 'showDescription(this)')
//
// prepareGroup(Service)
// // showNextQuestion(Service)
// }
//
// function prepareGroupAnd(Dl) {
// var Dd = getFirstChildElementByNodeName(Dl, 'dd');
// while (Dd) {
// // if (getFirstChildElementByNodeName(Dd, 'input')) Dd.style.display =
// // 'none'
// prepareGroup(getFirstChildElementByNodeName(Dd, 'dl'))
// Dd = getNextSiblingElementByNodeName(Dd, 'dd')
// }
// }
// function prepareGroupXor(Dl) {
// var XorDt = getFirstChildElementByNodeName(Dl, 'dt');
// while (XorDt) {
// var Radio = getFirstChildElementByNodeName(XorDt, 'input')
// Radio.setAttribute('onchange', 'radioChanged(this)')
// XorDt = getNextSiblingElement(XorDt)
// if (XorDt && (XorDt.nodeName == 'DD')) {
// XorDt.style.display = 'none'
// prepareGroup(getFirstChildElementByNodeName(XorDt, 'dl'))
// XorDt = getNextSiblingElementByNodeName(XorDt, 'dt')
// }
// }
// }
// function prepareGroupOr(Dl) {
// A = create('a')
// A.setAttribute('onclick', 'nextClicked(this)')
// A.textContent = 'Seuraava'
// Dl.appendChild(A)
//
// var OrDd = getFirstChildElementByNodeName(Dl, 'dd');
// while (OrDd) {
// OrDd.style.display = 'none'
// prepareGroup(getFirstChildElementByNodeName(OrDd, 'dl'))
// OrDd = getNextSiblingElementByNodeName(OrDd, 'dd')
// }
// }
//
// function nextQuestionNotFound(From) {
// alert('no questions')
// }
//
// function radioChanged(Radio) {
// var Dt = Radio.parentNode.parentNode
//
// var PreviousDd = Dt
// while (PreviousDd = getPreviousSiblingElementByNodeName(PreviousDd, 'dd'))
// PreviousDd.style.display = 'none'
//
// var Dd = getNextSiblingElement(Dt)
// if (Dd && (Dd.nodeName == 'DD')) {
// Dd.style.display = ''
// Dt = Dd
// }
//
// var NextDd = Dt
// while (NextDd = getNextSiblingElementByNodeName(NextDd, 'dd'))
// NextDd.style.display = 'none'
//
// showNextQuestion(Xor)
// }
//
// function nextClicked(A) {
// var Or = A.parentNode.parentNode
// showNextQuestion(Or)
// }
//
// function showNextQuestion(Element) {
// if (Element)
// switch (Element.className) {
// case 'and':
// alert('and')
// var Dd = getFirstChildElementByNodeName(Element, 'dd')
// while (Dd) {
// if ('none' == Dd.style.display) {
// Dd.style.display = ''
// if (Input = getFirstChildElementByNodeName(Dd, 'input')) {
// Input.focus()
// return true
// }
// }
// Dd = getNextSiblingElementByNodeName(Sibling, 'dd')
// }
// showNextQuestion(Element.parentNode.parentNode)
// return
// case 'xor':
// alert('xor')
// return
// case 'or':
// alert('or')
// return
// case 'temp':
// var Next = Element
// while (Next = getNextSiblingElementByNodeName(Next, 'dl')) {
// if ('none' == Next.style.display) {
// Next.style.display = ''
// if (Input = getFirstChildElementByNodeName(Next, 'input')) {
// Input.focus()
// return true
// }
// }
// }
// showNextQuestion(Element.parentNode.parentNode)
// return
// case 'xorOption':
// case 'orOption':
// var ElementDd = getFirstChildElementByNodeName(Element, 'dd')
// if (Input = getFirstChildElementByNodeName(ElementDd, 'input')) {
// Input.focus()
// } else {
// var Xor = Element.parentNode.parentNode
// var Next = getNextSiblingElementByNodeName(Xor, 'dl')
// if (Next) {
// Next.style.display = ''
// } else {
// showNextQuestion(Xor.parentNode.parentNode)
// // alert('todo showNextQuestion:option1')
// }
// }
// return
// default:
// if (Element instanceof HTMLTableRowElement)
// nextQuestionNotFound(Element)
// // else alert('todo default case element:' + Element + ' class:'
// // + Element.className)
// }
// alert('question not found');
// }