/* ----------------------------------------------------- */
// pop-ups

var _POPUP_FEATURES = 'location=0,statusbar=0,menubar=0,width=632,height=500,scrollbars=1,resizable=1';

function raw_popup(url, target, features) {
	if (features==undefined) {
		features = _POPUP_FEATURES;
	}
	if (target==undefined) {
		target = '_blank';
	}
	var theWindow = window.open(url, target, features);
	theWindow.focus();
	return theWindow;
}

function link_popup(src, features) {
	return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}

function toggle_bottom(link) {
	var el = document.getElementById('bottom_drawer');
	if (!el) return;
	if (el.style.display == 'inline') {
		el.style.display = 'none';
		if (link) link.innerHTML = '<span>More</span> +';
	} else {
		el.style.display = 'inline';
		if (link) link.innerHTML = '<span>Less</span>';
	}
}

/* ----------------------------------------------------- */
// plus/x animations

/*
function aniX(context) {
	var els = $ES('img.plusX', context);
	for(var i=0; i<els.length; i++) {
		$(els[i].parentNode.className=='skip'? els[i].parentNode.parentNode : els[i].parentNode).addEvents({
			mouseover: (function() {
				this.src = '/skin/images/PlusX.gif';
				}).bind(els[i]),
			mouseout: (function() {
				this.src = '/skin/images/PlusXstart.gif';
				}).bind(els[i])
		});
	}
}		
window.addEvent('load', aniX);
*/

/* ----------------------------------------------------- */
// maps

function tmapClearMarkers() {
	if (tmap_data) tmap_data.each(function(marker) {
		if (marker.pop) marker.pop.setStyle('display','none');
		if (marker.timer) window.clearTimeout(marker.timer);
	});
}
function tmapShowPop() {
	tmapClearMarkers();
	var coord = tmap.convertLatLonXY(new YGeoPoint(this.lat,this.lon));
	if (!this.pop) {
		this.pop = new Element('div', {
			'class': 'mappop',
			events: {
				mouseover: (function() { window.clearTimeout(this.timer); }).bind(this),
				mouseout: (function() {
					this.timer = window.setTimeout(
						(function(){ this.pop.setStyle('display', 'none'); }).bind(this),
						200);
				}).bind(this)
			}
		});
		this.pop.innerHTML = this.et;
		this.pop.injectInside('tmap_outer');
	}
	this.pop.setStyles({
		top: coord.y-4,
		left: coord.x+12,
		display: 'block'
	});
	this.timer = window.setTimeout(
		(function(){this.pop.setStyle('display', 'none')}).bind(this),
		2000);
}

var tmap,tmap_data,tmap_bounds,mylatlon;
function trainingMap() {
	// training map ------------------------
	tmap = new YMap(document.getElementById('trainingmap'));
	tmap.addZoomShort();
	tmap.removeZoomScale();

	// zoom and center
	if (tmap_bounds.radius) {
		tmap.drawZoomAndCenter(new YGeoPoint(tmap_bounds.lat, tmap_bounds.lon), 15);
		tmap.setZoomLevel(tmap.getZoomLevel(new YMapDistance(tmap_bounds.radius)) );
	} else {
		var tmp = tmap.getBestZoomAndCenter([
			new YGeoPoint(tmap_bounds.min.lat, tmap_bounds.min.lon),
			new YGeoPoint(tmap_bounds.max.lat, tmap_bounds.max.lon)
		]);
		tmap.drawZoomAndCenter(tmp.YGeoPoint, tmp.zoomLevel);
	}

	// place markers
	var markers = [];
	for(var i=0; i<tmap_data.length; i++) {
		markers[i] = new YMarker(new YGeoPoint(tmap_data[i].lat, tmap_data[i].lon), deficon);
		markers[i].addLabel('<div class="maplabel">'+tmap_data[i].c+'</div>');
		if (tmap_data[i].et) {
			YEvent.Capture(markers[i], EventsList.MouseOver, tmapShowPop.bind(tmap_data[i]));
		} else if (tmap_data[i].id) {
			YEvent.Capture(markers[i], EventsList.MouseClick, (function(){
				window.location.href = "/resources/training/"+this.id;
			}).bind(tmap_data[i]));
			YEvent.Capture(markers[i], EventsList.MouseOver, tmapClearMarkers);
		}
		tmap.addOverlay(markers[i]);
	}

	if (mylatlon) tmap.addMarker(mylatlon, homeicon);
}

var jmap_data,jmap_bounds,myradius=0;
function jobsMap() {	
	// jobs map ------------------------
	var jmap = new YMap(document.getElementById('jobsmap'));
	jmap.addZoomShort();
	jmap.removeZoomScale();

	// zoom and center
	var cur_zoom = 16;
	if (jmap_bounds) {
		var jtmp = jmap.getBestZoomAndCenter([
			new YGeoPoint(jmap_bounds.min.lat, jmap_bounds.min.lon),
			new YGeoPoint(jmap_bounds.max.lat, jmap_bounds.max.lon)
		]);
		jmap.drawZoomAndCenter(jtmp.YGeoPoint, jtmp.zoomLevel);
		cur_zoom = jtmp.zoomLevel;
	} else {
		jmap.drawZoomAndCenter(mylatlon, 8);
		cur_zoom = 8;
	}
	var rad_zoom = myradius? jmap.getZoomLevel(new YMapDistance(myradius)) : 14;
	if (rad_zoom>cur_zoom) jmap.setZoomLevel(rad_zoom);

	// place markers
	var markers = [];
	for(var i=0; i<jmap_data.length; i++) {
		markers[i] = new YMarker(new YGeoPoint(jmap_data[i].lat, jmap_data[i].lon), jmap_data[i].h? homeicon:deficon);
		if (jmap_data[i].c) {
			markers[i].addLabel('<div class="'+ (jmap_data[i].h? 'maplabelhome':'maplabel') + '">' + jmap_data[i].c + '</div>');
			if (jmap_data[i].url) YEvent.Capture(markers[i], EventsList.MouseClick, (function(){
					window.open(this.url);
					pageTracker._trackPageview('/outgoing/simplyhired');
			}).bind(jmap_data[i]));
		}
		jmap.addOverlay(markers[i]);	
	}
}

/* ----------------------------------------------------- */
// tool tips

function TT(prefix) {
	this.pre = prefix;
	this.cur = 0;
}
TT.prototype.next = function() {
	var el = $(this.pre+':'+(this.cur+1));
	if (el) {
		$(this.pre+':'+this.cur).setStyle('display', 'none');
		this.cur++;
		el.setStyle('display', 'block');
		$(this.pre+'_prev').removeClass('disabled');
		if (!$(this.pre+':'+(this.cur+1))) $(this.pre+'_next').addClass('disabled');
	}
};
TT.prototype.prev = function() {
	var el = $(this.pre+':'+(this.cur-1));
	if (el) {
		$(this.pre+':'+this.cur).setStyle('display', 'none');
		this.cur--;
		el.setStyle('display', 'block');
		$(this.pre+'_next').removeClass('disabled');
		if (!$(this.pre+':'+(this.cur-1))) $(this.pre+'_prev').addClass('disabled');
	}
};

function shoh(id) {
	try {
		var el = document.getElementById(id);
		el.style.visibility = el.style.visibility=='visible'?'hidden':'visible';
		if (id=='ttTraining') document.getElementById('ttJobs').style.visibility = 'hidden';
		if (id=='ttJobs') document.getElementById('ttTraining').style.visibility = 'hidden';
	} catch(e) {}
}

/* ----------------------------------------------------- */
// drawers

function show(id) {
	var sum = $('summary:'+id);
	var on = sum.getStyle('display');
	if (on == 'inline') {
		sum.setStyle('display', 'none');
		$('li:'+id).removeClass('current');
		try{$('a:'+id).setStyle('display', 'none');}catch(e){}
	} else {
		var items = $$('p.career_item');
		for(var i=0; i<items.length; i++) {
			var tid = items[i].id.replace(/summary:/, '');
			items[i].setStyle('display', 'none');
			$('li:'+tid).removeClass('current');
			try{$('a:'+tid).setStyle('display', 'none');}catch(e){}
		}
		sum.setStyle('display', 'inline');
		$('li:'+id).addClass('current');
		try{$('a:'+id).setStyle('display', 'block');}catch(e){}
	}
}

/* ----------------------------------------------------- */
// DITL embeds

window.addEvent('domready', function() {
	var container = $E('div.column_left');
	if (!container) return;
	var embeds = container.getElements('td.embed');
	for(var i=0; i<embeds.length; i++) embeds[i].addEvents({
		'mouseover': function() {
			this.addClass('hover');
		},
		'mouseout': function() {
			this.removeClass('hover');
		}
	});
});

/* ----------------------------------------------------- */
// Job Quiz on the home page

var Quiz = {
	sliders: [],
	query: {},
	last_query: {},
	opener: function() {
		Quiz.open(this);
	},
	start: function(starting_query) {
		var quiz_el = $E('#quiz');

		// IE6 fix:
		if (window.supersleight) quiz_el.getElements('.answer').setStyle('position', 'static');

		quiz_el.getElements('li').each(function(el) {
			el.addEvent('click', Quiz.opener);
		});
		quiz_el.getElementsBySelector('.question,a').each(function(el) {
			el.id = 'fx' + Quiz.sliders.length;
			Quiz.sliders[Quiz.sliders.length] = (new Fx.Slide(el, { duration: 250 })).hide();
		});

		quiz_el.removeClass('loading');

		this.open(quiz_el.getElement('.open'));
		quiz_el.getElements('.chosen').each(function(el) {
			Quiz.sliders[el.id.substring(2)].slideIn();
		});
		
		if (starting_query) {
			this.query = starting_query;
			this.fetch();
		}
	},
	answer: function(e, chosen, attribute, value) {
		(new Event(e)).stop();
		var li = $E('#q_'+attribute);
		if (li.hasClass('open')) {
			this.query[attribute] = value;
			this.fetch();

			li.getElements('a').each(function(el) {
				el.removeClass('chosen');
			});
			$(chosen).addClass('chosen');
			li.addClass('answered');

			this.close(li);
			var next = li.getNext();
			if (next && !next.hasClass('answered')) this.open(next);
		} else {
			this.query[attribute] = '';
			this.fetch();

			$(chosen).removeClass('chosen');
			li.removeClass('answered');

			this.open(li);
		}
	},
	close: function(li) {
		li.removeClass('open');
		li.addEvent('click', Quiz.opener);
		li.getElementsBySelector('.question,a').each(function(el) {
			if (!el.hasClass('chosen')) Quiz.sliders[el.id.substring(2)].slideOut();
		});
	},
	open: function(li) {
		if (!li) return;
		li.removeEvent('click', Quiz.opener);	
		li.getElementsBySelector('.question,a').each(function(el) {
			Quiz.sliders[el.id.substring(2)].slideIn();
		});
		$E('#quiz').getElements('li.open').each(function(el) {
			Quiz.close(el);
		});
		li.addClass('open');
	},
	fetch: function() {
		var url = '/careers/search/';
		for(var i in this.query) if (this.query[i]) url += i + '/' + encodeURIComponent(this.query[i]) + '/';
		if (this.last_query == url) return;

		(new Ajax(url, { update: 'quiz_careers' })).request();
		
		this.last_query = url;
	}
};

/* ----------------------------------------------------- */
// Main navigation

var Menu = {
	sliders: [],
	flyout_sliders: [],
	start: function() {
		this.menu_el = $E('#left_menu');

		// IE6 fix:
		if (window.supersleight) this.menu_el.getElements('a').setStyle('position', 'static');

		this.menu_el.getElements('ul.sub').each(function(el) {
			el.id = 'mfx' + Menu.sliders.length;
			Menu.sliders[Menu.sliders.length] = (new Fx.Slide(el, { duration: 250 })).hide();
		});

		this.menu_el.removeClass('loading');
		this.menu_el.getElementsBySelector('li.open,li.stuck_open').each(function(el) {
			Menu.open(el, true);
		});
	},
	toggle: function(el) {
		while (el && el.tagName != 'LI') el = el.parentNode;
		if (!el) return;
		var li = $(el);

		if (li.hasClass('open')) this.close(li);
		else {
			this.menu_el.getElements('li.open').each(function(li) {
				Menu.close(li);
			});
			this.open(li);
		}
	},
	open: function(li, force) {
		if (!li || (li.hasClass('open') && !force)) return;
		if (!li.hasClass('stuck_open')) li.addClass('open');
		var ul, div;
		if (ul = li.getElement('ul.sub')) {
			this.sliders[ul.id.substring(3)].slideIn();
		} else if (div = li.getElement('div.flyout')) {				
			var start = li.getSize().size;
			var goal = div.getSize().size;
			var wrap = li.getElement('.flyout_wrap');
			wrap.effect('height').start(start.y, goal.y);
			wrap.effect('width').start(start.x, goal.x);

			div.addEvent('click', this.flyout_stopper);
			li.addEvent('click', this.flyout_stopper);
			this.flyout_clicker = (function(e) {
				Menu.close(this.li);
			}).bind({li : li});
			$(document).addEvent('click', this.flyout_clicker);
		}
	},
	close: function(li) {
		if (!li || li.hasClass('stuck_open')) return;
		var ul, div;
		if (ul = li.getElement('ul.sub')) {
			this.sliders[ul.id.substring(3)].slideOut();
			li.removeClass('open');
		} else if (div = li.getElement('div.flyout')) {
			var start = li.getSize().size;
			var goal = div.getSize().size;
			var wrap = li.getElement('.flyout_wrap');
			wrap.effect('height').start(goal.y, start.y);
			wrap.effect('width', {
				onComplete: function() {
					li.removeClass('open');
				}
			}).start(goal.x, start.x);
			
			$(document).removeEvent('click', this.flyout_clicker);
			li.removeEvent('click', this.flyout_stopper);
			div.removeEvent('click', this.flyout_stopper);
		}
	},
	flyout_stopper: function(e) {
		var e = new Event(e);
		e.stopPropagation();
	}
};