﻿/*
 * Written by Cadu de Castro Alves, http://www.cadudecastroalves.com
 * Based on Emanuele Feronato technique, http://www.emanueleferonato.com/2007/08/22/create-a-lightbox-effect-only-with-css-no-javascript-needed/
 * Edited heavily with other additional functionality by Geoff Ford
 */

var scrollpos = [0, 0];
var short_jobs = '';
var allJobs = new Array();
var short_cans = '';
var allCans = new Array();

function popupsetc() {
	
	var anchor		= getElementsByClassName(document, 'a', 'popupbox');
	var external	= getElementsByClassName(document, 'a', 'external');
	var closeButton	= getElementsByClassName(document, 'a', 'close-button');
	var jobbox		= getElementsByClassName(document, 'a', 'popupjob');
	var clbox		= getElementsByClassName(document, 'a', 'popupcl');
	var cvbox		= getElementsByClassName(document, 'a', 'popupcv');
	var appbox		= getElementsByClassName(document, 'div', 'appcards');
	var colbox		= getElementsByClassName(document, 'input', 'color');
	var help		= getElementsByClassName(document, 'a', 'help');
	var helpbox		= getElementsByClassName(document, 'li', 'helpbox');



	var black	= document.getElementById('fade');
		black.style.display = 'none';
	var popup	= document.getElementById('popup');
		popup.style.display = 'none';
	var allhelp	= document.getElementById('allhelp');


	if(anchor) {
		for(var i = 0; i < anchor.length; i++) {
			anchor[i].href = '#';
			anchor[i].onclick = function() {
				getPopUpContents(this);
				if(black.style.display == 'none' && popup.style.display == 'none') {
					scrollpos = getScrollingPosition();
					var screensize = getViewportSize();
					var popupofftop = scrollpos[1] + ( screensize[1] * 0.1);
					var popupoffleft = (screensize[0]-600)/2;
					black.style.display = 'block';
					black.style.width = screensize[0];
					popup.style.background = '#aad3d3';
					popup.style.top = popupofftop + 'px';
					popup.style.left = popupoffleft + 'px';
					popup.style.display = 'block';
					document.getElementById('toprightnav').style.display = 'none';
					document.getElementById('bottom').style.display = 'none';
					hideDrops();
				}
				return false;
			};
		}
	}

	if(allhelp) {
		allhelp.href = '#';
		allhelp.title = 'Show All Help';
		allhelp.onclick = function() {
	        this.title = this.title=='Show All Help' ? 'Hide All Help' : 'Show All Help';
	        this.innerHTML = this.title=='Hide All Help' ? 'Hide All Help' : 'Show All Help';
			for(var i = 0; i < helpbox.length; i++) {
				helpbox[i].style.display = this.title=='Hide All Help' ? 'block' : 'none';
			}
			return false;
		};
	}

	if(help) {
		for(var i = 0; i < help.length; i++) {
			help[i].href = '#';
			help[i].title = 'Show Help';
			var bfhlp = help[i].id + 'box';
			bfhlpbox = document.getElementById(bfhlp);
			bfhlpbox.style.display = 'none';
			help[i].onclick = function() {
				var hlp = this.id + 'box';
				hlpbox = document.getElementById(hlp);
				hlpbox.style.display = hlpbox.style.display=='none' ? 'block' : 'none';
		        this.title = this.title=='Show Help' ? 'Hide Help' : 'Show Help';
				return false;
			};
		}
	}

	if(colbox) {
		for(var i = 0; i < colbox.length; i++) {
			if(colbox[i].id=='textcolour') {
				colbox[i].onchange = function() {
					document.getElementById("wysiwygIframe").contentWindow.document.body.style.color = this.value;
				};
			}
			if(colbox[i].id=='backcolour') {
				colbox[i].onchange = function() {
					document.getElementById("wysiwygIframe").contentWindow.document.body.style.backgroundColor = this.value;
				};
			}
		}
	}

	if(external) {
		for(var i = 0; i < external.length; i++) {
			var extlink = external[i];
			extlink.target = "_blank";
		}
	}

	if(jobbox) {
		short_jobs = getCookie('jd_shortlist');
		shortlistEvents();

		for(var i = 0; i < jobbox.length; i++) {
			jobbox[i].href = '#';
			jobbox[i].onclick = function() {
				getPopUpJobContents(this);
				if(black.style.display == 'none' && popup.style.display == 'none') {
					scrollpos = getScrollingPosition();
					var screensize = getViewportSize();
					var popupofftop = scrollpos[1] + ( screensize[1] * 0.1);
					var popupoffleft = (screensize[0]-600)/2;
					black.style.display = 'block';
					black.style.width = screensize[0];
					popup.style.top = popupofftop + 'px';
					popup.style.left = popupoffleft + 'px';
					popup.style.display = 'block';
					document.getElementById('toprightnav').style.display = 'none';
					document.getElementById('bottom').style.display = 'none';
					hideDrops();
				}
				return false;
			};
			if(!in_array(jobbox[i].id, allJobs)) {
				allJobs.push(jobbox[i].id);
			}
		}
		if(short_jobs){
			for(var jIndex in allJobs) {
				checkShortlist( short_jobs, allJobs[jIndex] );
			}
		}
	}

	if(clbox) {
		for(var i = 0; i < clbox.length; i++) {
			clbox[i].href = '#';
			clbox[i].onclick = function() {
				getPopUpClContents(this);
				if(black.style.display == 'none' && popup.style.display == 'none') {
					scrollpos = getScrollingPosition();
					var screensize = getViewportSize();
					var popupofftop = scrollpos[1] + ( screensize[1] * 0.1);
					var popupoffleft = (screensize[0]-600)/2;
					black.style.display = 'block';
					black.style.width = screensize[0];
					popup.style.top = popupofftop + 'px';
					popup.style.left = popupoffleft + 'px';
					popup.style.display = 'block';
					document.getElementById('toprightnav').style.display = 'none';
					document.getElementById('bottom').style.display = 'none';
					hideDrops();
				}
				return false;
			};
		}
	}

	if(cvbox) {
		for(var i = 0; i < cvbox.length; i++) {
			cvbox[i].href = '#';
			cvbox[i].onclick = function() {
				getPopUpCvContents(this);
				if(black.style.display == 'none' && popup.style.display == 'none') {
					scrollpos = getScrollingPosition();
					var screensize = getViewportSize();
					var popupofftop = scrollpos[1] + ( screensize[1] * 0.1);
					var popupoffleft = (screensize[0]-600)/2;
					black.style.display = 'block';
					black.style.width = screensize[0];
					popup.style.top = popupofftop + 'px';
					popup.style.left = popupoffleft + 'px';
					popup.style.display = 'block';
					document.getElementById('toprightnav').style.display = 'none';
					document.getElementById('bottom').style.display = 'none';
					hideDrops();
				}
				return false;
			};
		}
	}
	
	if (appbox) {
		short_cans = getCookie('jd_advshortlist');
		advshortlistEvents();
		for(var i = 0; i < appbox.length; i++) {
			id = appbox[i].id.substring(3);
			if(! in_array(id, allCans)) {
				allCans.push(id);
			}
		}
		if(short_cans){
			for(var cIndex in allCans) {
				advcheckShortlist( short_cans, allCans[cIndex] );
			}
		}
	}

	if(closeButton) {
		for(var j = 0; j < closeButton.length; j++) {
			closeButton[j].onclick = function() {
				setScrollingPosition();
				black.style.display = 'none';
				popup.style.display = 'none';
				clearPreviousResults();
				document.getElementById('toprightnav').style.display = 'block';
					document.getElementById('bottom').style.display = 'block';
				showDrops();
				return false;
			};
		}
	}
}

/*
 * Written by Geoff Ford
 */

function shortlistEvents() {
	var greenlight		= getElementsByClassName(document, 'a', 'grlt');
	var amberlight		= getElementsByClassName(document, 'a', 'amlt');
	var redlight		= getElementsByClassName(document, 'a', 'rdlt');

	var clearBox		= document.getElementById('clearbox');
	
	if(greenlight) {
		for(var i = 0; i < greenlight.length; i++) {
			greenlight[i].onclick = function() {
				changeBox( this.id, 'gr');
				shortlist( this.id, 'gr');
				return false;
			};
		}
	}
	if(amberlight) {
		for(var i = 0; i < amberlight.length; i++) {
			amberlight[i].onclick = function() {
				changeBox( this.id, 'am');
				shortlist( this.id, 'am');
				return false;
			};
		}
	}
	if(redlight) {
		for(var i = 0; i < redlight.length; i++) {
			redlight[i].onclick = function() {
				changeBox( this.id, '');
				shortlist( this.id, '');
				return false;
			};
		}
	}
	
	if(clearBox) {
		clearBox.onclick = function() {
			delCookie('jd_shortlist');
			for(var sjIndex in allJobs) {
				changeBox(allJobs[sjIndex], '');
			}
			short_jobs = '';
		};
	}

}

function popupShortlist( id, colour ) {
	changeBox( id, colour );
	shortlist( id, colour );
	setScrollingPosition();
	document.getElementById('fade').style.display = 'none';
	document.getElementById('popup').style.display = 'none';
	clearPreviousResults();
	document.getElementById('toprightnav').style.display = 'block';
	document.getElementById('bottom').style.display = 'block';
	showDrops();
	return false;
}

function popupClose() {
	setScrollingPosition();
	document.getElementById('fade').style.display = 'none';
	document.getElementById('popup').style.display = 'none';
	clearPreviousResults();
	document.getElementById('toprightnav').style.display = 'block';
	document.getElementById('bottom').style.display = 'block';
	showDrops();
	return false;
}

function changeBox( id, colour ) {
	cleanWhitespace( document.getElementById('box'+id) );
	var box = document.getElementById('box'+id).childNodes[0];
	box.className = colour+'box';
	box.childNodes[0].setAttribute('id',colour+'lm');
	box.childNodes[0].childNodes[0].setAttribute('id',colour+'rm');
	box.childNodes[0].childNodes[0].childNodes[0].setAttribute('id',colour+'bm');
	box.childNodes[0].childNodes[0].childNodes[0].childNodes[0].setAttribute('id',colour+'bl');
	box.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].setAttribute('id',colour+'br');
	box.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].setAttribute('id',colour+'tm');
	box.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].setAttribute('id',colour+'tl');
	box.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].setAttribute('id',colour+'tr');
}

function shortlist( jobid, type ) {
	var pattern = new RegExp(',' + jobid + ':am|,' + jobid + ':gr');
	if ( type == '' ) {
		short_jobs = short_jobs.replace( pattern, '' );
	} else if ( short_jobs.match( pattern ) ) {
		var newJob = ',' + jobid + ':' + type;
		short_jobs = short_jobs.replace( pattern, newJob );
	} else {
		short_jobs = short_jobs + ',' + jobid + ':' + type;
	}
	var today = new Date();
	today.setTime( today.getTime() );
	var expires_date = new Date( today.getTime() + 2592000000 );
	document.cookie = 'jd_shortlist=' + escape(short_jobs) + ';expires=' + expires_date.toGMTString() + ';path=/;';
}

function checkShortlist( short_jobs, jobid ) {
	var pattern = new RegExp(',' + jobid + ':am|,' + jobid + ':gr');
	if (jobmatch = short_jobs.match(pattern)) {
		joblen = jobmatch[0].length -2;
		colour = jobmatch[0].substr(joblen);
		changeBox( jobid, colour );
	}
}

/********************** Adv Shortlist ***************************/

function advshortlistEvents() {
	var advgreenlight		= getElementsByClassName(document, 'a', 'advgrlt');
	var advamberlight		= getElementsByClassName(document, 'a', 'advamlt');
	var advredlight			= getElementsByClassName(document, 'a', 'advrdlt');

	var advclearBox			= document.getElementById('advclearbox');
	var advupdate			= document.getElementById('advupdate');
	
	if(advgreenlight) {
		for(var i = 0; i < advgreenlight.length; i++) {
			advgreenlight[i].onclick = function() {
				changeBox( this.id, 'gr');
				advshortlist( this.id, 'gr');
				return false;
			};
		}
	}
	if(advamberlight) {
		for(var i = 0; i < advamberlight.length; i++) {
			advamberlight[i].onclick = function() {
				changeBox( this.id, 'am');
				advshortlist( this.id, 'am');
				return false;
			};
		}
	}
	if(advredlight) {
		for(var i = 0; i < advredlight.length; i++) {
			advredlight[i].onclick = function() {
				changeBox( this.id, 'fb');
				advshortlist( this.id, 'fb');
				return false;
			};
		}
	}
	
	if(advclearBox) {
		advclearBox.onclick = function() {
			delCookie('jd_advshortlist');
			short_cans = '';
		};
	}

	if(advupdate) {
		advupdate.onclick = function() {
			short_cans = '';
		};
	}
}

function advpopupShortlist( id, colour ) {
	changeBox( id, colour );
	advshortlist( id, colour );
	setScrollingPosition();
	document.getElementById('fade').style.display = 'none';
	document.getElementById('popup').style.display = 'none';
	clearPreviousResults();
	document.getElementById('toprightnav').style.display = 'block';
	document.getElementById('bottom').style.display = 'block';
	showDrops();
	return false;
}

function advshortlist( canid, type ) {
	var pattern = new RegExp(',' + canid + ':fb|,' + canid + ':am|,' + canid + ':gr');
	if ( short_cans.match( pattern ) ) {
		var newCan = ',' + canid + ':' + type;
		short_cans = short_cans.replace( pattern, newCan );
	} else {
		short_cans = short_cans + ',' + canid + ':' + type;
	}
	document.cookie = 'jd_advshortlist=' + escape(short_cans) + ';path=/;';
}

function advcheckShortlist( short_cans, canid ) {
	var advpattern = new RegExp(',' + canid + ':fb|,' + canid + ':am|,' + canid + ':gr');
	if (canmatch = short_cans.match(advpattern)) {
		canlen = canmatch[0].length -2;
		colour = canmatch[0].substr(canlen);
		changeBox( canid, colour );
	}
}

/*****************************************************/

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + '=');
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(';',c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return '';
}

function delCookie(c_name) {
	document.cookie = c_name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;path=/;';
}

function hideDrops() {
	var drops = document.getElementsByTagName('select');
	for (i = 0; i < drops.length; i++) {
		drops[i].style.display = 'none';
	// or
	// drops[i].style.visibility = 'hidden'
	}
}

function showDrops() {
	var drops = document.getElementsByTagName('select');
	for (i = 0; i < drops.length; i++) {
		drops[i].style.display = 'inline';
	// or
	// drops[i].style.visibility = 'visible'
	}
}

/*************** Get Pop Contents and AutoComplete by Ajax :: Written by Geoff Ford *************/

var xmlHttp;

function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}

function getPopUpContents( element ) {
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open("GET", "/getPopUp/" + element.id + "/Ajax", true);
	xmlHttp.send(null);
}

function getPopUpJobContents( element ) {
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	var url = "/getPopUpJob/" + element.id + "/Ajax";
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function getPopUpClContents( element ) {
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	var url = "/adv/getPopUpCL/" + element.id + "/Ajax";
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function getPopUpCvContents( element ) {
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	var url = "/adv/getPopUpCV/" + element.id + "/Ajax";
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function getAutoCompleteTags() {
	createXMLHttpRequest();
	xmlHttp.open("GET", "/acfiles/autocompletetags.txt", false);
	xmlHttp.send(null);
	var tags = xmlHttp.responseText.split(",");
	return tags;
}

function getAutoCompleteLocs() {
	createXMLHttpRequest();
	xmlHttp.open("GET", "/acfiles/autocompletelocs.txt", false);
	xmlHttp.send(null);
	var locs = xmlHttp.responseText.split(",");
	return locs;
}

function getAutoCompleteLocations() {
	createXMLHttpRequest();
	xmlHttp.open("GET", "/acfiles/autocompletecitiesetc.txt", false);
	xmlHttp.send(null);
	var locations = xmlHttp.responseText.split(",");
	return locations;
}

function handleStateChange() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			document.getElementById("popup").innerHTML = xmlHttp.responseText;
		}
	}
}

function clearPreviousResults() {
	document.getElementById("popup").innerHTML = "";
}

/* onload functions */

function startup(type) {
	popupsetc();
	initWYSIWYG(type);
}
