$(document).ready(function() {
	
	$('#asia_inset').mouseenter(function() {
		$('#coming_soon').show();
	}).mouseover(function() {
		$('#coming_soon').show();
	}).mouseout(function() {
		$('#coming_soon').hide();
	});
	
	$('#map_instructions').slideDown('slow');
	
	$('a.helper').live('click',function() {
		return false;
	}).live('mouseover',function(e) {
		//alert($(this).attr('href'));
		$('#rating_helper').load($(this).attr('href')).show('fast').css('top',(e.pageY+10)+'px').css('left',(e.pageX)+'px');
	}).live('mouseout',function() {
		$('#rating_helper').hide('fast');
	});
	
	$('a.footer_helper').live('click',function() {
		return false;
	}).live('mouseover',function(e) {
		//alert($(this).attr('href'));
		$('#footer_helper').load($(this).attr('href')).show('fast').css('top',(e.pageY+10)+'px').css('left',(e.pageX)+'px');
	}).live('mouseout',function() {
		$('#footer_helper').hide('fast');
	});
	
	$.historyInit(ajaxRender, $('#root_url').html());
	
	$('#country_list').load($('#root_url').html()+'countries/index',null,function(responseText,textStatus,XMLHttpRequest) {
		//alert('responseText: '+responseText+',textStatus: '+textStatus+',XMLHttpRequest: '+XMLHttpRequest);
		$('div.spinner').hide();
	});
	
	$('#image_map').load($('#root_url').html()+'countries/areas_index',null,function(responseText,textStatus,XMLHttpRequest) {
		//alert('responseText: '+responseText+',textStatus: '+textStatus+',XMLHttpRequest: '+XMLHttpRequest);
	});
	
	$('#breadcrumb').click(function() {
		current_page = 'home';
		$(this).addClass('home');
		$.historyLoad('');
		render_ads(2);
		return false;
	});
	
	$('#header_left').click(function() {
		$.historyLoad('');
	});
	
	$('area').live('click',function(e) {
		var clicked = e.target;
		var country = $(clicked).attr('class');
		var element = $('ul#country_list li:contains(' + country + ')');
		show_countrysub(element);
		var post_url = $(this).attr('href');
		$.historyLoad(post_url);
		return false;
	}).live('mouseover',function(e) {
		var clicked = e.target;
		var country = $(clicked).attr('class');
		country_mouseover(country);
	}).live('mouseout',function(e) {
		var clicked = e.target;
		var country = $(clicked).attr('class');
		country_mouseout(country);
	});
	
	$('a.internal').live('click',function() {
		var post_url = $(this).attr('href');
		//alert(post_url);
		$.historyLoad(post_url);
		return false;
	});
	
	$('table.clickable tr')
		.live('mouseover', function() {
			//console.info('target:' + e.target);
			$(this).children().each(function() {$(this).addClass('hover');});
		})
		.live('mouseout', function() {
			$(this).children().each(function() {$(this).removeClass('hover');});
		});
	
	$('#image_map')
		.click(function(e) {
			var clicked = e.target;
			if ($(clicked).attr('class') != '') {
				var country = $(clicked).attr('class');
				change_nav_ad(country);
				var element = $('ul#country_list li:contains(' + country + ')');
				show_countrysub(element);
				var post_url = $(clicked).attr('href');
				$.historyLoad(post_url);
			};
			return false;
		});
	
	$("ul#country_list")
		.click(function(e) {
			var clicked = e.target;
			if ($(clicked).attr('class').search('selected') == '-1') {
				//console.info('here');
				if ($(clicked).attr('class').search('sub') != '-1') {
					
				} else {
					show_countrysub($(clicked).parent());
					var country = $(clicked).html();
					change_nav_ad(country);
				};		
			} else {
				hide_countrysub();
			};
		})
		.mouseover(function(e) {
			var clicked = e.target;
			$(clicked).addClass("cursor_hand");
			var country = $(clicked).html().replace(' ','_');
			country_mouseover(country);
			//console.info("here");
		})
		.mouseout(function(e) {
			var clicked = e.target;
			var country = $(clicked).html().replace(' ','_');
			country_mouseout(country);
		});
});

function ajaxRender(post_url) {
	$('body').css('cursor','wait'); // changes the cursor to a wait symbol
	if($.browser.msie) { // if the browser is Internet Explorer
		// jquery's $.load() function does't work when hash include special characters like åäö.
		//post_url = encodeURIComponent(post_url); // removes special characters
	}
	if (post_url) { // checks that a URL is present
		current_page = post_url;
		if (ajax_request) { // checks for an existing ajax request
			ajax_request.abort(); // aborts request
		}
		$('#globe_spinner').show('fast');
		$('#map_instructions').slideUp('slow');
		ajax_request = $.ajax({ // start the AJAX request
			type: "POST", // I'm sending it via POST, you can use GET if you're feeling dangerous
			url: post_url, // the URL we are retrieving
			data: null, // in this case we are not sending any data 
			success: function(data) { // execute the below if the AJAX goes according to plan
				//alert("success!");
				if (!($.browser.msie && $.browser.version.substr(0,1)<7)) {
					$('#map_container').animate({
						opacity:0.18
					},200);
				}					
				$('#breadcrumb').slideDown();		

					$(".page-body").slideUp('fast', function() {
						$('#globe_spinner').hide('fast');
						$('#grid_outer').css('z-index',3); // make the content jump forward and be clickable
						$(this).html(data).slideDown('slow', function() {
							$('#footer_pusher').hide();
							$('body').css('cursor','default'); // put the cursor back to normal							
							//console.info('finished');
							$('html, body').animate({scrollTop:0}, 'fast');
						});
					});
			
					afterRender();
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) { // execute this if it all goes tits up
					$('body').css('cursor','default');
					alert(textStatus + errorThrown);
					return false;
				}
		});
	} else { // if there is no post_url, i.e. it's the home page
		hide_countrysub();
		$('body').css('cursor','default'); // put the cursor back to normal
	}
};

function country_mouseover(country) {
	if (country=='Czech Republic') {
		country = 'Czech_Republic';
	}
	$('#'+country+'_hover').fadeIn('fast');
	country = country.replace('_',' ');
	//console.info(country);
	$('ul#country_list li:contains(' + country + ')').addClass('hover');
}

function country_mouseout(country) {
	if (country=='Czech Republic') {
		country = 'Czech_Republic';
	}
	$('#'+country+'_hover').fadeOut('slow');
	country = country.replace('_',' ');
	$('ul#country_list li:contains(' + country + ')').removeClass('hover');
}

function hide_countrysub() {	
	$('li.sub').slideUp('fast');
	$('li.selected').removeClass('selected');
	//$("#grid_contents").slideUp('slow');
	$("#grid_contents").html('');
	$('#footer_pusher').show();
	$('#map_container').animate({
		opacity:1
	},900);
	$('#grid_outer').css('z-index','');
	$('#breadcrumb').slideUp().removeClass('home');
};

function afterRender() {
	render_ads(2);
}

function change_nav_ad(country) {
	var img = 'img/ads/targetjobs.gif.png';
	var url = 'http://www.targetjobs.co.uk';
	if (country=='Ireland') {
		img='img/ads/gradireland.gif';
		url = 'http://www.gradireland.com';
	} else if(country=='Germany') {
		img='img/ads/staufenbiel-de.gif';
		url = 'http://www.staufenbiel.de';
	} else if(country=='Switzerland') {
		img='img/ads/staufenbiel-ch.gif';
		url='http://www.staufenbiel.ch';
	}
	img = $('#root_url').html()+img;
	$('#bottomleft_ad_link').attr('href',url);
	$('#bottomleft_ad_img').fadeOut('300',function() {
		$(this).attr('src',img).fadeIn();
	});
}

function show_countrysub(element) {
	$('li.sub').slideUp('fast');
	$('li.selected').removeClass('selected');
	$(element).addClass("selected");
	$(element).next().show('fast').next().show('fast');
}

function render_ads(count) {
	//alert('page='+current_page);
	if (current_page=='home') {
		var data='home=true';
	} else {
		var data = null;
	};
	$.ajax({
		type : "GET",
		data: data,
		url : $('#root_url').html()+'countries/render_ads/'+count,
		success : function(data) {
			$('#skyscraper_container').html(data);
		}
	});
}

var ajax_request = null;
var current_page = null;