var scrollInt; // for scrolling events

window.onload = init; 

function init() {
	if (document.getElementById('scrollEvents')) {
		scrollEvents_start(); 
	}

	// ADD EVENTS LISTENERS FOR SCROLLING EVENTS LINKS
	if (document.getElementById('scrollEvents')) {
		scollLinkArray = document.getElementById('scrollEvents').getElementsByTagName('a'); 
		for (var i = 0; i < scollLinkArray.length; i++) {
			if (document.all) {
				scollLinkArray[i].onmouseover = scrollEvents_stop; 
				scollLinkArray[i].onmouseout = scrollEvents_start; 
			} else {
				scollLinkArray[i].addEventListener('mouseover', scrollEvents_stop, true); 
				scollLinkArray[i].addEventListener('mouseout', scrollEvents_start, true); 
			}
		}
	}

	// ADD EVENT LISTENERS FOR EXTERNAL LINKS
	var aArray = document.getElementsByTagName('a'); 

	for (var i = 0; i < aArray.length; i++) {
		if (aArray[i].className == 'external_booking') {
			aArray[i].onclick = function() {
				window.open(this.getAttribute('href'), 'Book Event', "width=620,height=800,scrollbars=yes,resizable=yes"); 
				return false;
			}
		}
	}
}

function scrollEvents_stop() {
	clearInterval(scrollInt); 
}

function scrollEvents_start() {
	scrollInt = setInterval(function() {
		var scrollEvents = document.getElementById('scrollEvents'); 
		var scrollEventsTop = scrollEvents.offsetTop; 
		var scrollEventsHeight = scrollEvents.offsetHeight; 

		if (scrollEventsTop + scrollEventsHeight == 0) {
			scrollEvents.style.top = 160 + 'px';
		} else {
			scrollEvents.style.top = scrollEventsTop - 1 + 'px';
		}
	}, 100); 
}

function replaceImage(imageName, imageFile) {
	document.images[imageName].src = imageFile ; 
}

function openWindow(url, page) {
	if (page == 'bookEvent' || page == 'contactUs' || page == 'booking' || page == 'shop' || page == 'uploadPic') {
		window.open(url, page, "width=600,height=700,scrollbars=yes,resizable=yes"); 
	} else if (page == 'basket') {
		window.open(url, page, "width=950,height=600,scrollbars=yes,resizable=yes,directories=no"); 
	} else if (page == 'link_local' || page == 'delete_item') {
		window.open(url, page, "width=500,height=200,scrollbars=no,resizable=yes,directories=no"); 
	} else {
		window.open(url, page);
	} 
}

function enlargePic(id, wid, hig) {
	window.open('graphics/shop_' + id + '.jpg', 'Shop', 'width=' + wid + ',height=' + hig + ',scrollbars=no,resizeable=no'); 
}

function closeWindow(action) {
	window.close();
}

function refreshWindow(win, pic, align) {
	if (win == 'self') {
		window.location.reload(); 
	} else if (win == 'picUpload') {
		putPic(align, pic); 		
		window.close();
	} else if (win == 'bookEvent') {
		window.opener.location.reload();
	} else {
		window.opener.location.reload();
		window.close();
	}
}

function showDates(weekday, day, month, year, event) {
	var dateLine = document.getElementById('date'); 
	dateLine.innerHTML = 
		'<input type="hidden" name="date" + value="' + 
			weekday + ' ' + day + ' ' + month + ' ' + year + 
		'">' + 
			weekday + ' ' + day + ' ' + month + ' ' + year + 
		'. </input>' + 

		'<input type="hidden" name="day" + value="' + day + '"/>'; 

	if (event == 'ghost') {
		if (weekday == 'Thursday' || weekday == 'Friday' || weekday == 'Saturday') {
			dateLine.innerHTML += 
				'Please select a time: ' + 
				'<select id="time" name="time">' + 
					'<option></option>' + 
					'<option>18:00</option>' + 
					'<option>19:00</option>' + 
					'<option>20:00</option>' + 
					'<option>21:00</option>' + 
				'</select>'; 
		}
	}
}

function writeEmail() {
	var em1 = 'info';
	var em2 = 'falstaffsexperience'; 
	var em3 = 'co.uk'; 

	alert('email'); 

	document.write(em1 + '@' + em2 + '.' + em3); 
}

function updateBasket() {
		var loc = String(window.opener.location); 

		if (loc.indexOf('action=add') != -1) {		// ensure that new item not added to basket
			loc = loc.split('&action'); 
			window.opener.open(loc[0], '_self'); 
		} else {
			window.opener.location.reload(); 
		}
}

function warn(linkid, sublinkid, name, type) {
	var userInput = confirm('Do you really want to delete the ' + type +': ' + name + '?'); 

	if (userInput == true) {
		if (type == 'page') {
			window.location='content.php?action=delete&linkid=' + linkid; 
		} else {
			window.location='contentEdit.php?action=delete&linkid=' + linkid + '&sublinkid=' + sublinkid; 
		}
	}
}

function validate(event) {
	var date = document.getElementById('date').innerHTML;
	var address = document.getElementById('address').value;
	var email = document.getElementById('email').value;
	var phone = document.getElementById('phone').value;

	if (date == '') { 
		alert('Please enter a date from the calendar'); 
		return false;
	}

	if (document.getElementById('time')) {
		var time = document.getElementById('time').value;

		if (time == '') { 
			alert('Please select a time'); 
			return false;
		}
	}

	if (address == '' && email == '' && phone == '') { 
		alert('Please enter contact details'); 
		return false;
	}
}