/*

JavaScript library by Shelton Development Services

*/

$(document).ready(function() {
	$(document).scrollTop(0)
	EnableNavigationSliding()
	AddTrainingCourseFixtures()
	EnableSearchBoxPlaceholder()
	EnableWindowSizeDisplay()
	MakeLinksPopUpInstead()
});

function EnableWindowSizeDisplay() {
	var timer = false
	var $logo = $('<span/>').addClass('ScreenSize')
	$(document).dblclick(function() {
		if (timer) {
			clearInterval(timer)
			timer = false
			$logo.remove()
		} else {
			$('#logo').append($logo)
			timer = setInterval(function() {
				$logo.text($(document).width() + 'x' + $(document).height())
			}, 250);
		}
	});
}

function EnableSearchBoxPlaceholder() {
	var search$ = $('input.search')
	search$.focus(function() {
		var text = $(this).val();
		if (text == "Search here...") {
			$(this).css({ color: '#000' });
			$(this).val('');
		}
	})
	search$.blur(function() {
		var text = $(this).val().trim();
		if (!text) {
			$(this).css({ color: '#888' });
			$(this).val('Search here...');
		}
	})
}
function EnableNavigationSliding() {
	var nav = $('#navagation div:first');
	var scroll = nav[0];
	nav.bind('mousemove', function(e) {
		var cw = scroll.clientWidth;
		var sw = scroll.scrollWidth - cw;
		//document.title = cw + '-' + sw + ':' + scroll.scrollOffset;
		if (sw <= 0) return;
		cw -= 20;
		var ex = e.pageX - nav.offset().left - 10;
		nav.scrollLeft(Math.round(ex / cw * sw));
	});
}

function AddTrainingCourseFixtures() {
	var courseFixtures$ = $('.CourseFixtures');
	if (courseFixtures$.length) {
		var courseName = document.title.split('-').pop().trim();
		$.ajax({
			url: '/Training/courses.aspx?course=' + courseName,
			success: function(data) {
				eval(data);
			}
		});
	}
}

function Course(name, year, month, day, slot/*1=AM|2=PM|3=All day*/, location, href) {
	this.name = name;
	this.year = year;
	this.month = month;
	this.day = day;
	this.slot = slot;
	this.location = location;
	this.href = href;
	this.date = day + '/' + month + '/' + year;
	switch (slot) {
		case 1:
			this.AMPM = 'AM';
			this.AM = true;
			this.SlotName = 'Morning';
			break;
		case 2:
			this.AMPM = 'PM';
			this.PM = true;
			this.SlotName = 'Afternoon';
			break;
		case 3:
			this.AMPM = 'Day';
			this.Allday = true;
			this.SlotName = 'All day';
			break;
	}
}

function LoadCourses(list) {
	var fixtures$ = $('.CourseFixtures');
	var ul$ = $('.CourseFixtures ul');
	if (list && list.length) {
		ul$.html(''); // clear existing courses
		for (var i in list) {
			var course = list[i];
			var li$ = $('<li/>');
			li$.append($('<span/>').addClass('Date').text(course.date));
			li$.append($('<span/>').addClass('Session').text(' ' + course.SlotName + ' session'));
			li$.append($('<span/>').addClass('Location').text(' at ' + course.location));
			li$.append($('<a/>')
				.attr('href', '/Contact-Us?Enquiry=Please book me on the \'' + course.name + '\' course on ' + course.date + ' at ' + course.location)
				.text(' Book now'));
			ul$.append(li$);
		}
		fixtures$.css({ display: 'block' });
	} else {
		fixtures$.css({ display: '' });
	}
}

function NavTo() {
	var nav = $("#ExtraNavigator");
	var url = nav.val();
	location = url;
}

function MakeLinksPopUpInstead(){

	var aTags = $('a[href]').filter(function(index){
		var href = (''+this.href).toLowerCase()
		return href.indexOf('.png') > 0 || href.indexOf('.jpg') > 0 || href.indexOf('.gif') > 0
	}).click(function(e){
		if ($(window).width() <= 480) return true;

		e.preventDefault()
		var img = $('<img/>')
			.attr('src', this.href)
			.attr('alt', this.title)
		var container = $('<div/>')
			.addClass('PopUp')
			.append($('<div/>').addClass('Background'))
			.append(img)
			.append($('<div/>').addClass('Close').text('X'))
			.click(function(){
				container.animate({opacity: 0},
				{
					duration: 250,
					complete: function() {
						container.remove()
					}
				})
			})
		$('body').append(container)
		container.animate({opacity: 1}, 250);
		return false;
	})
}

