$(window).load(function() {

	function initializeMap(){
		$('#searchlocations').hide();
		$('#map').fadeOut(900, function(){
			$('#map').empty().css({
				width: '430px',
				height: '506px',
				backgroundImage: 'url(../jqmap/images/map/bao_base.png)',
				position: 'relative'
			});
			$('#map').fadeIn(900);
			loadBullets('bao', false);
			$("div.over").css('display','none');
			$("div.popupcontent").css('z-index:1001','none');

		});
	}

	function addZoomable(id, width, height, top, left){
		//-> reglage coord
		$('<img class="zoomable" src="../jqmap/images/map/blankLocalisation.gif" id="' + id + '" />').css({

			border: 'none',
			position: 'absolute',
			width: width + 'px',
			height: height + 'px',
			top: top + 'px',
			left: left + 'px',
			cursor: 'pointer'
		}).appendTo('#map').click(function() {
			$(this).siblings().fadeOut();
			$(this).hide()
				   .attr('src', '../jqmap/images/map/' + id + '_base.jpg')
				   .fadeIn('0')
				   .animate({
						width: '577px',
						height: '418px',
						top: '0px',
						left: '0px'
					}, 1000, '', function(){
						$('#map').css({backgroundImage: 'url(../jqmap/images/map/' + id + '_base.jpg)'}).empty();
						loadBullets(id, true);
					});
		});
	}

	function loadBullets(id, back){

		$('#map').load('/jqmap/section/pprod.'+ id + '.php', {}, function(){
//->add back button
			if(back){
				$('<a id="mapback" href="javascript:void(0)"><span>return to NC Coast &gt;</span></a>')
					.appendTo(this)
					.click(function(){initializeMap()});
			}
//->place bullets
			$(this).children('a.bullet').each(function(){
				var ecommerce = $(this).attr('class');
				var coords = $(this).attr('rel').split('-');
				$(this).css({left: coords[0] + 'px', top: coords[1] + 'px'})
					   .hide()
					   .fadeIn()
					   .click(function(){
					   	showPopup($(this).attr('id'));
						// initialization de la carte
					    $(".init").css({position:'absolute', width: '149px',height: '128px',top :'174px',left:'231px', zIndex:'2002',cursor:'default' });
//->TODO: GOTO URL

//->double click
			 $(this).dblclick( function(){
			 	//alert('TODO: goto url');
//->a.bullet attr	id mag
				var id_url =  $(this).attr('id').split('-');

				id_url_splitted =id_url[0];
					 // alert(id_url_splitted);
//->a.bullet ajax xml attr	http
								$.ajax({
								url: 'jqmap/data/'+id_url_splitted+'.xml',
								dataType: "xml",
								success: function(xml) {
								//$('.result').xml(data);
													$(xml).find('url').each(function(){
													 id_text_http = $(this).attr('http');

													//alert(id_text_http);
//->a.bullet ajax xml attr	http !empty
													if (id_text_http!='')  window.location.href=id_text_http;

													}); //close each
								} //close $.ajax
			});
    });
});
				var divId = $(this).attr('id');
				//init over
				$('div.'+divId).css('display','none')
				$(this).mouseover( function(){$('div.'+divId).css({display:'block'});});
				$(this).mouseout( function(){$('div.'+divId).css('display','none');});
				$('div.'+divId).parent().mouseover( function(){$('div.'+divId).parent().css('z-index','1001');});
				$('div.'+divId).parent().mouseout( function(){$('div.'+divId).parent().css('z-index','1');});


			});

		// div content
		$('a.bullet').each(function(){
          var data=$(this).attr('id');
          var htmls=$(this).attr('id').replace("-banks","");
        //
       // $('#'+data+'-box .popupcontent').load('../jqmap/data/service.php?mag='+htmls, function(){appendTo(this);} );
		});
		});
		//
	}

	function showPopup(id){
		//alert(id);
		$('#map div.popup').fadeOut();
		var boxid = '#' + id + '-box';
        $(boxid).corner();
		$(boxid).show();
		$(boxid).css('z-index','1001');
		$("#colonneDroite").css('z-index','1');
		$('a.close').click(function(){
			$(this).parent().fadeOut();

		});

	}

	//initialize map
	initializeMap();


});


function noaccent(chaine) {

  temp = temp.replace(/[" "]/gi,"_");
  return temp
}
