$(document).ready(function() {
	
	$('#asia_inset').mouseenter(function() {
		$('#coming_soon').show();
	}).mouseover(function() {
		$('#coming_soon').show();
	}).mouseout(function() {
		$('#coming_soon').hide();
	});
	
	// $('input.ajax_submit').live('click',function() {
	//         var post_url = $(this).parents('form').attr('action');
	//         var method = $(this).parents('form').attr('method');
	//         var data = $(this).parents('form').serialize();
	//         //alert('url='+post_url+',method='+method);
	//         ajaxSubmit(post_url,method,data);
	//         return false;
	//     });
    
	$('#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(1);
		return false;
	});
	
	$("#register").click(function() {
	    $.historyLoad($('#root_url').html()+"registrations/add");
	    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;
		});
		
	$("div#bottomleft_links").hover(function(){
	    $(this).addClass('hover')
	},function(){
	    $(this).removeClass("hover")
	});
		
	$("input.submit_btn").click(function() {
	   return false; 
	});
	
	$("#footer_boxes h1").click(function() {
	    if ($(this).hasClass('open')) {
	        $(this).removeClass('open');
	        close_boxes($("div.footer_box")[$(this).index()]);
	    } else {
	        $(this).addClass('open');
	        open_boxes($("div.footer_box")[$(this).index()]);
	    };
	});
	
	$("div.footer_boxes_close").click(function() {
	    close_boxes($(this).parent());
	});
	
	$("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) {
    close_boxes();
	$('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 ,
			//cache:false,
			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 ajaxSubmit(url,method,data) {
	if ( typeof(method) == 'undefined' ){
		method = 'GET';
	}	
	if ( typeof(data) == 'undefined' ){
		data = null;
	}
	$.ajax({
		type: method,
		url: url,
		data: data,
		success: function(data) {
			render(data); // rerenders the current page
		}
	});
}

function render(data) {
	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();
}

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() {
	
}

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').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);
		}
	});
}

function toggle(source,box_name) {
    checkboxes = document.getElementsByName(box_name);
    for(x in checkboxes) {
      checkboxes[x].checked = source.checked;
    }
}

function render_footer_boxes(sector_name_id,country_id) {
    //alert(sector_name_id+","+country_id);
    $('#latest_jobs_box_content').load(
        $("#root_url").html()+"jobs/index/sector_name_id:"+sector_name_id+"/country_id:"+country_id+"/limit:3"
    );
    // $('#editorial_box_content').load(
    //         $("#root_url").html()+"articles/box_index/sector_name_id:"+sector_name_id+"/country_id:"+country_id+"/limit:3"
    //     );
    $("div#employer_box_contents").load(
        $("div#root_url").html()+"companies/featured/country_id:"+country_id
    );
}

function open_boxes(element) {
    $(element).slideDown(800,"easeOutBounce").addClass('open');
    return false;
}

function close_boxes(element) {
    if (typeof(element)=="undefined") {
        $("div.footer_box.open").slideUp(400,"easeInOutBack").removeClass('open');
    };
    $(element).slideUp(400,"easeInOutBack").removeClass('open');
    return false;
}

var ajax_request = null;
var current_page = null;
