var newlist = '';

function hideall () {
		$('.data > ul > li').stop();
		$('.data > ul > li > ul').stop();
		$('.data > ul > li > ul').hide();
}

function parseXml(xml)
{
	$(xml).find("continent").each(function(i) { // seek to continent
		var continent_name = $(this).attr('name');
		var continent_codename = $(this).attr('codename');
		newlist += '<div class=\"data\" id=\"' + $(this).attr('codename').toLowerCase().replace(/ /g,'') + '\"><h2>' + continent_name + '</h2><ul class=\"' + $(this).attr('codename') + '">';

		$(this).find("country").each(function(i) { // seek to country
			if ( !(i % 12) ) newlist += '</ul><ul class=\"' + continent_codename + '">';
			newlist += '<li>' + $(this).attr('name') + '<ul style=\"z-index: 9999\">';

			$(this).find("method").each(function() { // seek to method
				newlist += '<li style=\"z-index: 99\" class=\"' + $(this).attr('class') + '\">' + $(this).text() + '</li>';
			});
			newlist += "</ul>";
		});
		newlist += "</ul></div>";
	});
		
	$('#world_data').html(newlist);

	$('.data > ul:nth-child(2)').hide();

	$('.data > ul > li').hover( function (event) {
			event.stopPropagation();
			if ( ($(this).parent().attr('class') == 'Europe') && ($.browser.msie == true) ) {
				$(this).css ('position','static');
				$(this).children("ul").css ( {'left': '450px', 'top':'10px' } );
			}
			else {
				$(this).css ('position','relative');
				$(this).children("ul").css ( 'left', ($(this).width() + 20) + 'px' );
				if ( ( $(this).offset().top - $(this).parent().offset().top ) > 90 )
					$(this).children("ul").css ( 'top', '-' + ( $(this).offset().top - $(this).parent().offset().top ) + 'px' );
			}
			$(this).children("ul").show();
		},
		function () {
			hideall();
		}
	);

	$('.data').hover( function () {
			hideall();
			return false;
		},
		function () {
			hideall();
			return false;
		}
	);

	$('.data > ul').hover( function () {	
			hideall();
			return false;
		},
		function () {
			hideall();
			return false;
		}
	);

	$('.data > ul > li > ul').hover( function () {
			hideall();
			return false;
		},
		function () {
			hideall();
			return false;
		}
	);

	$('.data > ul > li > ul > li').hover( function () {
			hideall();
			return false;
		},
		function () {
			hideall();
			return false;
		}
	);

}

$(document).ready(function()
{

	$('area').hover( function() {
		$('#map').stop();
		$('#map').fadeTo('fast',1);
		$('#mapdescription').show();
	
		$('#mapdescription').hover(
			function() { 
				$('#map').stop();
				$('#map').fadeTo('fast',1);
				$('#mapdescription').show();
		
				return false; 
			},
			function() { 
		
				$('#map').stop();
				$('#map').fadeTo('fast',0);
				$('#mapdescription').hide();
		
				return false; 
			}
		);
	
		return false; 
	},
	function() { 
		$('#map').stop();
		$('#map').fadeTo('fast',0);
	
		$('#mapdescription').hide();
	
		$('#mapdescription').hover(
			function() { 
		
				return false; 
			},
			function() { 
				
				return false; 
			}
		);
		
		return false; 
	}
	);
	
	$('area').click ( function (event) {
		
		event.stopPropagation();
		
		$('.data').hide();
	
	    $(('.data#' + $(this).attr('class'))).show();
	    $('.data > ul > li').css ('backgound-color','#fff');
		$('#world_data').show();
	});

	$('area').hover ( function () {
		document.getElementById('map').src = '/images/merchant-account/map/map-' + $(this).attr('class') + '.png'; 
		$('#mapdescription').html( $( ( '#' + $(this).attr('class') + ' h2' ) ).html() ); // the name of the continent tag
		
		$('#mapdescription').show();
		
		var description_top = 200;
		var description_left = 200;
		
		switch ( $(this).attr('class') ) {
			case 'africa': description_top = 300; description_left = 380; break;
			case 'southamerica' : description_top = 350; description_left = 220; break;
			case 'northamerica' : description_top = 230; description_left = 140; break;
			case 'europe' : description_top = 220; description_left = 400; break;
			case 'asia' : description_top = 240; description_left = 520; break;
			case 'oceania' : description_top = 380; description_left = 600; break;
			default: break;
		}
		
		$('#mapdescription').css ( { 'top' : description_top + 'px', 'left' : description_left + 'px' } );
		$('#mapdescription').attr('class', $(this).attr('class') );

		},
		function () {}
	);
		
	$('#map').click ( function() { $('#world_data').hide(); } );

	$('#mapdescription').click ( function() { 
		$( ('area.'+$(this).attr('class')) ).click(); 
	});
	
	$.ajax({
	type: "GET",
	url: "data.xml",
	dataType: "xml",
	success: parseXml
	});
 
});

