/* ----------------------------------------------------- */
// 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);
}

/* ----------------------------------------------------- */
// 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 == 'block') {
		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', 'block');
		$('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');
		}
	});
});