Cufon.replace('#subHeader');
Cufon.replace('#nav li a:not(#nav li ul li a)',{color: '#ffffff', textShadow: '#014c38 1px 1px'});
Cufon.replace('h1');
Cufon.replace('h2');Cufon.replace('h3');
Cufon.replace('h4');
Cufon.replace('.greenTitle');
Cufon.replace('table th');Cufon.replace('.bigLink');Cufon.replace('.view-print a');
Cufon.replace('.largeCTA');Cufon.replace('.largeCTAHeader');Cufon.replace('.slider .toggler:not(.slider .toggler span)');
Cufon.replace('#courses td.bookTD button span');Cufon.replace('#courses td.soldTD span');Cufon.replace('#courses .bookNow');
Cufon.replace('#leftMenu ul li a');
Cufon.replace('#footer ul li a');Cufon.replace('#footer .logo-twitter');

$(document).ready(function(){
  $(".contact-link").colorbox();
  $('.text').hide();

  $('a.toggler').toggle(function(){
                $(this).parent().addClass('active').find('.text').stop		(true,true).slideDown('fast');
                return false;
            },function(){
                $(this).parent().removeClass('active').find('.text').stop(true,true).slideUp('fast');
                return false;
            });

  var cycleOpts = {
    speed : '2000',
    timeout : 6000,
    fx: 'scrollHorz',
    prev: 'li.prevBtn a',
    next: 'li.nextBtn a',
    slideExpr: 'img'
  }

  $('#banners').append('<ul class="cycleNav"><li class="prevBtn"><a href="#">Previous</a></li><li class="nextBtn"><a href="#">Next</a></li></ul>').cycle(cycleOpts);

  $('#nav li').hoverIntent(function () {
	$(this).addClass('active');
    	if($(this).find('.dropdown').length > 0){
		$(this).find('.dropdown').show();
	}
	},function(){
		$(this).removeClass('active');
		$(this).find('.dropdown').hide();
	});

});

// (c) 2010 Software Solution Shop

/***********************************************
* Bookmark site script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

function bookmarksite(title, url){
	url=location.href;
	if (document.all)
		window.external.AddFavorite(url, title);
	else if (window.sidebar)
		window.sidebar.addPanel(title, url, "")
}

function hidePlusSectionsTrainingPage()
{
	toggleShowHide('detailedContent', 'detailedContentplusminus');
	toggleShowHide('importantNotes', 'importantNotesplusminus');
}

function toggleShowHide(element, imageElement)
{
	if(document.getElementById(element).style.display == 'none')
	{
		window.document.getElementById(element).style.display = 'block';
		document.getElementById(imageElement).src = '/images/minus.jpg';
	}
	else if(document.getElementById(element).style.display == 'block' || document.getElementById(element).style.display == '')
	{
		document.getElementById(element).style.display = 'none';
		document.getElementById(imageElement).src = '/images/plus.jpg';
	}
}

function setOrderBy(newValue)
{
  document.forms[0].orderBy.value = newValue;
  document.forms[0].submit();
}

function toggleOrderByAscDesc()
{
  if (document.forms[0].orderByAscDesc.value == 'ASC')
  {
    document.forms[0].orderByAscDesc.value = 'DESC';
  }
  else
  {
    document.forms[0].orderByAscDesc.value = 'ASC';
  }
  document.forms[0].submit();
}

function setIncludeAllTrainingCentres()
{
  document.forms[0].includeAllTrainingCentres[0].checked = false;
  document.forms[0].includeAllTrainingCentres[1].checked = true;
}

/* popup */
var TINY={};

function T$(i){return document.getElementById(i)}

TINY.box=function(){
	var p,m,b,fn,ic,iu,iw,ih,ia,f=0;
	var timeoutVar;
	return{
		show:function(c,u,w,h,a,t){
			if(!f){
				p=document.createElement('div'); p.id='tinybox';
				m=document.createElement('div'); m.id='tinymask';
				b=document.createElement('div'); b.id='tinycontent';
				document.body.appendChild(m); document.body.appendChild(p); p.appendChild(b);
				m.onclick=TINY.box.hide; window.onresize=TINY.box.resize; f=1
			}
			if(!a&&!u){
				p.style.width=w?w+'px':'auto'; p.style.height=h?h+'px':'auto';
				p.style.backgroundImage='none'; b.innerHTML=c
			}else{
				b.style.display='none'; p.style.width=p.style.height='100px'
			}
			this.mask();
			ic=c; iu=u; iw=w; ih=h; ia=a; this.alpha(m,1,80,3);
			if (timeoutVar) {clearTimeout(timeoutVar)};
			if(t){timeoutVar=setTimeout(function(){TINY.box.hide()},1000*t)}
		},
		fill:function(c,u,w,h,a){
			if(u){
				p.style.backgroundImage='';
				var x=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
				x.onreadystatechange=function(){
					if(x.readyState==4&&x.status==200){TINY.box.psh(x.responseText,w,h,a)}
				};
				x.open('GET',c,1); x.send(null)
			}else{
				this.psh(c,w,h,a)
			}
		},
		psh:function(c,w,h,a){
			if(a){
				if(!w||!h){
					var x=p.style.width, y=p.style.height; b.innerHTML=c;
					p.style.width=w?w+'px':''; p.style.height=h?h+'px':'';
					b.style.display='';
					w=parseInt(b.offsetWidth); h=parseInt(b.offsetHeight);
					b.style.display='none'; p.style.width=x; p.style.height=y;
				}else{
					b.innerHTML=c
				}
				this.size(p,w,h)
			}else{
				p.style.backgroundImage='none'
			}
		},
		hide:function(){
			TINY.box.alpha(p,-1,0,3)
		},
		hideManual:function(){
			if (timeoutVar) {clearTimeout(timeoutVar);}
			TINY.box.hide();
		},
		resize:function(){
			TINY.box.pos(); TINY.box.mask()
		},
		mask:function(){
			m.style.height=TINY.page.total(1)+'px';
			m.style.width=''; m.style.width=TINY.page.total(0)+'px'
		},
		pos:function(){
			var t=(TINY.page.height()/2)-(p.offsetHeight/2); t=t<10?10:t;
			p.style.top=(t+TINY.page.top())+'px';
			p.style.left=(TINY.page.width()/2)-(p.offsetWidth/2)+'px'
		},
		alpha:function(e,d,a){
			clearInterval(e.ai);
			if(d==1){
				e.style.opacity=0; e.style.filter='alpha(opacity=0)';
				e.style.display='block'; this.pos()
			}
			e.ai=setInterval(function(){TINY.box.ta(e,a,d)},20)
		},
		ta:function(e,a,d){
			var o=Math.round(e.style.opacity*100);
			if(o==a){
				clearInterval(e.ai);
				if(d==-1){
					e.style.display='none';
					e==p?TINY.box.alpha(m,-1,0,2):b.innerHTML=p.style.backgroundImage=''
				}else{
					e==m?this.alpha(p,1,100):TINY.box.fill(ic,iu,iw,ih,ia)
				}
			}else{
				var n=Math.ceil((o+((a-o)*.5))); n=n==1?0:n;
				e.style.opacity=n/100; e.style.filter='alpha(opacity='+n+')'
			}
		},
		size:function(e,w,h){
			e=typeof e=='object'?e:T$(e); clearInterval(e.si);
			var ow=e.offsetWidth, oh=e.offsetHeight,
			wo=ow-parseInt(e.style.width), ho=oh-parseInt(e.style.height);
			var wd=ow-wo>w?0:1, hd=(oh-ho>h)?0:1;
			e.si=setInterval(function(){TINY.box.ts(e,w,wo,wd,h,ho,hd)},20)
		},
		ts:function(e,w,wo,wd,h,ho,hd){
			var ow=e.offsetWidth-wo, oh=e.offsetHeight-ho;
			if(ow==w&&oh==h){
				clearInterval(e.si); p.style.backgroundImage='none'; b.style.display='block'
			}else{
				if(ow!=w){var n=ow+((w-ow)*.5); e.style.width=wd?Math.ceil(n)+'px':Math.floor(n)+'px'}
				if(oh!=h){var n=oh+((h-oh)*.5); e.style.height=hd?Math.ceil(n)+'px':Math.floor(n)+'px'}
				this.pos()
			}
		}
	}
}();

TINY.page=function(){
	return{
		top:function(){return document.documentElement.scrollTop||document.body.scrollTop},
		width:function(){return self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth},
		height:function(){return self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight},
		total:function(d){
			var b=document.body, e=document.documentElement;
			return d?Math.max(Math.max(b.scrollHeight,e.scrollHeight),Math.max(b.clientHeight,e.clientHeight)):
			Math.max(Math.max(b.scrollWidth,e.scrollWidth),Math.max(b.clientWidth,e.clientWidth))
		}
	}
}();

/* end popup */

function refreshAll()
{
	// firstly, refresh the courses drop down based on the selection
	selectedCategoryId = getSelectedCategoryId();

	// always have the "include all courses" option
	courseList = "<select name=\"searchCourse\" id=\"searchCourse\" onchange=\"refreshCourses()\"><option value=\"0\" selected=\"selected\">Show all courses</option>";

	// the selectedCategory variable holds the relevant category id
	for (i = 1; i < courseCategories.length; i++)
	{
		if (selectedCategoryId == 0 || courseCategories[i].categoryId == selectedCategoryId)
		{
			// this course should be included
			courseList = courseList + "<option value=\"" + courseCategories[i].courseName + "\">" + courseCategories[i].courseName + "</option>";
		}
	}

	courseList = courseList + "</select>";

	// update the contents of the course list drop down
	document.getElementById('courseListSpan').innerHTML = courseList;

	// cascade down and refresh the courses list
	refreshCourses();

}

// get the selected category id
function getSelectedCategoryId()
{
	var x=document.getElementById("searchCategory");
	for (i=0;i<x.length;i++)
	{
		if (i == x.selectedIndex)
	  	{
	  		selectedCategory = x.options[i].value;
	  	}
	}

	return selectedCategory;
}

// get the selected course name
function getSelectedCourseName()
{
	var x=document.getElementById("searchCourse");

	// is it a hidden element or a drop down?
	if (x.tagName == "select")
	{
		for (i=0;i<x.length;i++)
		{
			if (i == x.selectedIndex)
			{
				searchCourse = x.options[i].value;
			}
		}
	}
	else
	{
		searchCourse = x.value;
	}

	return searchCourse;
}

// get all course names in the searchCourse dropdown
function getCoursesFromDropDown()
{
	selectedCourseNames = new Array;

	var x=document.getElementById("searchCourse");
	for (i=1;i<x.length;i++)
	{
		selectedCourseNames[i] = x.options[i].value;
	}

	return selectedCourseNames;
}

// refresh the course list result box
function refreshCourses()
{
	// get the selected course name
	selectedCourseName = getSelectedCourseName();

	// is it "include all courses" in a given category?
	if (selectedCourseName == "-1")
	{
		// we therefore need all the course names in the dropdown
		selectedCourseNames = new Array;
		selectedCourseNames[1] = "-1";
	}
	else if (selectedCourseName == "0")
	{
		// we therefore need all the course names in the dropdown
		selectedCourseNames = getCoursesFromDropDown();
	}
	else // otherwise, we just use the one selected course
	{
		selectedCourseNames = new Array;
		selectedCourseNames[1] = selectedCourseName;
	}

	// start the result box table off
	courseList = "<table width=\"100%\" id='courses' cellspacing='0'>";
	courseList = courseList + "<tr>";
	courseList = courseList + doHeading('Course', 'course');
	courseList = courseList + doHeading('Centre', 'centre');
	courseList = courseList + doHeading('Start date', 'startDate, course');
	courseList = courseList + doHeading('Duration', 'numDays');
	courseList = courseList + doHeading('Total places', 'totalPlaces');
	courseList = courseList + doHeading('Available places', 'freePlaces');
	courseList = courseList + "<th></th></tr>";

	// the selectedCategory variable holds the relevant category id
	rowNumber = 0;
	for (i = 1; i < courses.length; i++)
	{
		if (shouldIncludeCourse(courses[i].course, selectedCourseNames) && courseLocationIncluded(courses[i].centre))
		{
			// this course should be included
			courseList = courseList + includeCourse(courses[i], rowNumber);
			rowNumber = rowNumber + 1;
		}
	}

	// the end of the result box table
	courseList = courseList + "</table>";

	// update the contents of the course list drop down
	document.getElementById('resultBox').innerHTML = courseList;

}

// whether or not the given course name is included in the list of selected course names
function shouldIncludeCourse(courseName, selectedCourseNames)
{
	// search each selected course name and see if it matches the course name
	for (j=1; j<selectedCourseNames.length; j++)
	{
		// there is an override which includes ALL courses
		if (selectedCourseNames[j] == "-1")
		{
			return true;
		}
		
		// now try an exact match
		if (selectedCourseNames[j] == courseName)
		{
			return true;
		}
		
		// now try dropping AM or PM from the end of the payroll year end course
		if (courseName.slice(-3) == " AM" || courseName.slice(-3) == " PM")
		{
			// drop the AM/PM
			var courseNameToTest = courseName.substr(0, courseName.length - 3);
			if (selectedCourseNames[j] == courseNameToTest)
			{
				return true;
			}
		}
	}

	// we didn't find a match
	return false;
}

// whether or not the given location is included in the selected locations
function courseLocationIncluded(theCourseLocation)
{
	// check the location override first
	if (document.getElementById('locationOverride').value != "")
	{
		if (document.getElementById('locationOverride').value == theCourseLocation)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	// get the value of the radio button which says whether we are including all courses
	includeAllLocations = document.forms[0].includeAllTrainingCentres[0].checked;

	if (includeAllLocations)
	{
		return true;
	}

	//alert ("checking checkbox: " + 'location' + removeDots(theCourseLocation));

	//alert ("value is: " + document.getElementById('location' + removeDots(theCourseLocation)).checked);

	// otherwise, we need to check if the specific location is included
	return document.getElementById('location' + removeDots(theCourseLocation)).checked;
}

// remove dots from the name of the location
function removeDots(originalName)
{
	var newName = originalName.replace(".", "");
	return newName;
}

// reorder the list
function setOrderByJavaScript(columnName)
{
	// are we changing which column?
	if (document.getElementById('orderBy').value != columnName)
	{
		// set the order by to ascending
		newOrder = "ASC";
		document.getElementById('orderByAscDesc').value = newOrder;
	}
	else
	{
		// otherwise, toggle
		var currentOrder = document.getElementById('orderByAscDesc').value;
		var newOrder = "";
		if (currentOrder == "ASC")
		{
			newOrder = "DESC";
		}
		else
		{
			newOrder = "ASC";
		}
		// save the new order
		document.getElementById('orderByAscDesc').value = newOrder;
	}
	
	
	// pretend we have made a swap to get into the loop
	var swapMade = true;
	
	while (swapMade)
	{
		// we haven't made a swap yet
		swapMade = false;
		
		for (i = 1; i < courses.length - 1; i++)
		{
			for (j = i + 1; j < courses.length; j++)
			{
				// compare courses i and j for ascending
				var courseOrder = false;
				if (newOrder == "ASC")
				{
					courseOrder = doCoursesNeedSwapping(i, j, columnName);
				}
				else
				{
					// otherwise, compare courses j and i
					courseOrder = doCoursesNeedSwapping(j, i, columnName);
				}
				
				if (courseOrder)
				{
					var tempCourse = courses[i];
					courses[i] = courses[j];
					courses[j] = tempCourse;
					swapMade = true;
				}
			}
		}
	}
	
	// save the order by column
	document.getElementById('orderBy').value = columnName;
	
	// refresh the course list
	refreshCourses();
}

function doCoursesNeedSwapping(i, j, columnName)
{
	switch (columnName)
	{
		case "course":
			if (courses[i].course > courses[j].course)
				return true;
			else
				return false;
			break;
			
		case "centre":
			if (courses[i].centre > courses[j].centre)
				return true;
			else
				return false;
			break;
			
		case "startDate, course":
			if (courses[i].startDateForComparison > courses[j].startDateForComparison || (courses[i].startDateForComparison == courses[j].startDateForComparison && courses[i].course > courses[j].course))
				return true;
			else
				return false;
			break;
			
		case "numDays":
			if (courses[i].numDays > courses[j].numDays)
				return true;
			else
				return false;
			break;
			
		case "totalPlaces":
			if (courses[i].totalPlaces > courses[j].totalPlaces)
				return true;
			else
				return false;
			break;
			
		case "freePlaces":
			if (courses[i].freePlaces > courses[j].freePlaces)
				return true;
			else
				return false;
			break;
			
	}
	
	// we should never get here - don't make a swap
	return false;

}

// a course list result box heading
function doHeading(label, dbColumnName)
{
	orderBy = document.getElementById('orderBy').value;
	orderByAscDesc = document.getElementById('orderByAscDesc').value;

	headingString = "<th><a href=\"javascript:setOrderByJavaScript('" + dbColumnName + "');\" alt=\"Order by " + label + "\" title=\"Order by " + label + "\" class=\"body\">" + label + "</a> ";
	if (orderBy == dbColumnName)
	{
		//headingString = headingString + " <a href=\"javascript:toggleOrderByAscDescJavaScript();\">";
		if (orderByAscDesc == "ASC")
		{
			//headingString = headingString + "<img src=\"/images/up.gif\" border=\"0\" alt=\"Toggle search order\" title=\"Toggle search order\" />";
			headingString = headingString + "<img src=\"/images/up.gif\" border=\"0\" />";
		}
		else
		{
			//headingString = headingString + "<img src=\"/images/down.gif\" border=\"0\" alt=\"Toggle search order\" title=\"Toggle search order\" />";
			headingString = headingString + "<img src=\"/images/down.gif\" border=\"0\" />";
		}
		//headingString = headingString + "</a>";
	}
	headingString = headingString + "</th>\n";

	return headingString;
}

function includeCourse(theCourse, rowNumber)
{
	if (rowNumber % 2 == 0)
	{
		courseString = "<tr class=\"evenRow\">";
	}
	else
	{
		courseString = "<tr class=\"oddRow\">";
	}
	courseString = courseString + "<td>";
	courseString = courseString + theCourse.course;
	courseString = courseString + "</td>";
	courseString = courseString + "<td>";
	courseString = courseString + theCourse.centre;
	courseString = courseString + "</td>";
	courseString = courseString + "<td>";
	courseString = courseString + theCourse.startDate;
	courseString = courseString + "</td>";
	courseString = courseString + "<td>";
	courseString = courseString + theCourse.numDays;
	courseString = courseString + "</td>";
	courseString = courseString + "<td>";
	courseString = courseString + theCourse.totalPlaces;
	courseString = courseString + "</td>";
	courseString = courseString + "<td>";
	courseString = courseString + theCourse.freePlaces;
	courseString = courseString + "</td>";

	// add a "book" link if there is at least one place free
	if (theCourse.freePlaces > 0)
	{
		courseString = courseString + "<td class='bookTD'>";
		courseString = courseString + "<button onclick=\"javascript:document.location.href='http://www.accountingsystems.co.uk/shop/bookCourse.html?courseId=" + theCourse.id + "';return false;\"><span>Book now</span></button>\n";
		courseString = courseString + "</td>";
	}
	else{
		courseString = courseString + "<td class='soldTD'>";
		courseString = courseString + "<span>Sold out</span>";
		courseString = courseString + "</td>";
	}

	courseString = courseString + "</tr>\n";

	return courseString;
}

// course
function course(id, course, centre, startDate, startDateForComparison, numDays, totalPlaces, freePlaces)
{
  this.id = id;
  this.course = course;
  this.centre = centre;
  this.startDate = startDate;
  this.startDateForComparison = startDateForComparison;
  this.numDays = numDays;
  this.totalPlaces = totalPlaces;
  this.freePlaces = freePlaces;
}

// course category
function courseCategory(courseName, category, categoryId)
{
  this.courseName = courseName;
  this.category = category;
  this.categoryId = categoryId;
}

