/* Carte */

var current_tooltip;

function set_zone(id, speed){
	var fond = $('div#carte_fond');
	var btn = $('div#carte_btn');
	var bulle = $('div#carte_bulle');
	
	bulle.stop().animate(
		{ opacity:0 },
		{
			duration:50,
			easing:'easeInCubic',
			complete: function(){
			}
		}	
	);
	
	btn.stop().animate(
		{ opacity:0 },
		{
			duration:50,
			easing:'easeInCubic',
			complete: function(){
			}
		}	
	);

	fond.stop().animate(
		{
			opacity:0
		},
		{
			duration:50,
			easing:'easeInCubic',
			complete: function(){
				fond.css('background-image', 'url('+base_path_carte+zones[id]['fond']+')');
				btn.html('');
				var html = '';
				if(zones[id]['btn']){
				$.each(zones[id]['btn'], function(i){
					html += '<div id="btn_'+i+'">&nbsp;</div>\n';
				});
				};
				if(zones[id]['flag']){
				$.each(zones[id]['flag'], function(i){
					html += '<div id="flag_'+i+'"><img src="'+base_path_carte+zones[id]['flag'][i]['img']+'" alt="" /></div>\n';
				});
				}
				btn.html(html);
				if(zones[id]['btn']){
				$.each(zones[id]['btn'], function(i){
					var this_btn = $('div#btn_'+i);
					this_btn.css('position', 'absolute');
					this_btn.css('top', zones[id]['btn'][i]['y']);
					this_btn.css('left',zones[id]['btn'][i]['x']);
					this_btn.width(70);
					this_btn.height(70);
					this_btn.css('cursor', 'pointer');
					this_btn.click(function(){
						set_zone(zones[id]['btn'][i]['zone'], speed);
					});
				});
				};
				if(zones[id]['flag']){
				$.each(zones[id]['flag'], function(i){
					$('img', this).css('border', 0);
					var this_flag = $('div#flag_'+i);
					this_flag.css('position', 'absolute');
					this_flag.css('top', zones[id]['flag'][i]['y']);
					this_flag.css('left',zones[id]['flag'][i]['x']);
					this_flag.width(30);
					this_flag.height(20);
					this_flag.css('cursor', 'pointer');
					
					if($.browser.msie){
						this_flag.css('background', 'none');
						this_flag.css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+$('img', this_flag).attr('src')+'", sizingMethod="crop")');
						this_flag.html('');
					}
					
					this_flag.click(function(){
						btn.unbind('click');
						var nb = 0;
						var tooltip_html = '<p>';
						$.each(zones[id]['flag'][i]['html'], function(j){ tooltip_html += zones[id]['flag'][i]['html'][j]; nb++; });
						tooltip_html += '</p>';	
						if(nb==1){
							var bulle = $('div#carte_bulle');
							var bulle_img = $('img#carte_tooltip_img');
							bulle_img.hide('fast');
							bulle.stop().animate(
								{ opacity:0	},
								{
									duration:50,
									easing:'easeInCubic',
									complete: function(){
										bulle.css('top', -1000);
										bulle.css('left', -1000);
									}
								}	
							);
							bulle.html(tooltip_html);
							top.location.href = $('.tooltip a').attr('href');
						}
					});
					this_flag.mouseout(function(){
						btn.click(function(){
							current_tooltip = i;
							var bulle = $('div#carte_bulle');
							var bulle_img = $('img#carte_tooltip_img');
							bulle_img.hide('fast');
							bulle.stop().animate(
								{ opacity:0 },
								{
									duration:50,
									easing:'easeInCubic',
									complete: function(){
										bulle.css('top', -1000);
										bulle.css('left', -1000);
									}
								}	
							);
						});
					});
					this_flag.mouseover(function(){
						btn.unbind('click');
						var bulle = $('div#carte_bulle');
						var bulle_img = $('img#carte_tooltip_img');
						bulle_img.css('display', 'none');
						if(current_tooltip!=i){
							bulle.stop().animate(
								{ opacity:0 },
								{
									duration:50,
									easing:'easeInCubic',
									complete: function(){
										bulle.css('top', -1000);
										bulle.css('left', -1000);
									}
								}	
							);
						}
						
						var nb = 0;
						var tooltip_html = '<p>';
						$.each(zones[id]['flag'][i]['html'], function(j){ tooltip_html += zones[id]['flag'][i]['html'][j]; nb++; });
						tooltip_html += '</p>';	
						
						if(nb!=1){
							bulle.html(tooltip_html);
							bulle.css('height', nb*25+22);
							var t_top = zones[id]['flag'][i]['y'];
							if((t_top + bulle.height())>= 468){ t_top = 448-bulle.height(); }
							bulle.css('top', t_top);
							var t_left = zones[id]['flag'][i]['x']-98;
							if((t_left-70) <= 0){ 
								t_left = zones[id]['flag'][i]['x']+34;
								bulle_img.css('left', zones[id]['flag'][i]['x']+27);
								bulle_img.attr('src',  tooltip_img_right);
							}
							else {
								bulle_img.css('left', zones[id]['flag'][i]['x']-8);
								bulle_img.attr('src',  tooltip_img_left);
							}
							bulle.css('left', t_left);
							bulle.css('display', 'block');
							bulle_img.css('top', zones[id]['flag'][i]['y']+10);
							
							bulle_img.show(20);
							bulle.stop().animate(
								{ opacity:1 },
								{
									duration:100,
									easing:'easeInCubic',
									complete: function(){
										btn.click(function(){
											current_tooltip = i;
											var bulle = $('div#carte_bulle');
											var bulle_img = $('img#carte_tooltip_img');
											bulle_img.hide('fast');
											bulle.stop().animate(
												{ opacity:0 },
												{
													duration:50,
													easing:'easeInCubic',
													complete: function(){
														bulle.css('top', -1000);
														bulle.css('left', -1000);
													}
												}	
											);
										});
									}
								}	
							);
						}
					});
				});
				}
				
				fond.animate(
					{ opacity:1 },
					{
						duration:speed,
						easing:'easeInCubic',
						complete: function(){
							btn.animate(
								{ opacity:1 },
								{
									duration:speed,
									easing:'easeInCubic',
									complete: function(){
										
									}
								}	
							);
						}
					}	
				);
			}
		}	
	);

}

function carte_init(){
	var fond = $('div#carte_fond');
	fond.css('z-index', 500);
	fond.css('position', 'absolute');
	fond.css('top', 0);
	fond.css('left', 0);
	fond.css('overflow', 'hidden');
	fond.width(650);
	fond.height(468);
	
	var btn = $('div#carte_btn');
	btn.css('z-index', 1000);
	btn.css('position', 'absolute');
	btn.css('top', 0);
	btn.css('left', 0);
	btn.css('overflow', 'hidden');
	btn.width(650);
	btn.height(468);
	
	var bulle = $('div#carte_bulle');

	btn.click(function(){
		var bulle = $('div#carte_bulle');
		var bulle_img = $('img#carte_tooltip_img');
		bulle_img.hide('fast');
		bulle.stop().animate(
			{ opacity:0 },
			{
				duration:50,
				easing:'easeInCubic',
				complete: function(){
					bulle.css('top', -1000);
					bulle.css('left', -1000);
				}
			}	
		);
	});
	
	fond.click(function(){
		var bulle = $('div#carte_bulle');
		var bulle_img = $('img#carte_tooltip_img');
		bulle_img.hide('fast');
		bulle.stop().animate(
			{ opacity:0 },
			{
				duration:50,
				easing:'easeInCubic',
				complete: function(){
					bulle.css('top', -1000);
					bulle.css('left', -1000);
					tooltip_status = 'closed';
				}
			}	
		);
	});
	
	set_zone(1, 100);
}


DD_roundies.addRule('div.tooltip', '11px');
$(document).ready(function() {
	carte_init();
});

