$(function () {

    var offColor;
    var strokeColor;
    var mouseX = 0;
    var mouseY = 0;
    var current = null;

    // Detect if the browser is IE.
    var IE = $.browser.msie ? true : false;

    function createMap() {

        //start map
        var r = new ScaleRaphael('Cmap', 450, 490),
            attributes = {
                fill: '#d9d9d9',
                cursor: 'pointer',
                stroke: mapstroke,
                'stroke-width': 2,
                'stroke-linejoin': 'round'
            },
            arr = new Array();

        for (var state in paths) {

            //Create obj
            var obj = r.path(paths[state].path);
            obj.attr(attributes);
            arr[obj.id] = state;

                obj.attr({
                    //fill: stateColors[obj.id]
					fill: paths[arr[obj.id]].coul
                });
				
                if(paths[arr[obj.id]].url != ''){
					
					obj.mouseover(function () {
						//Animate if not already the current state
							this.animate({
								fill: hover_fill
							}, 300);
						//bulle info texte
						var point = this.getBBox(0);
						$('#Cmap').next('.point').remove();
						$('#Cmap').after($('<div />').addClass('point'));
						$('.point').html(paths[arr[this.id]].title).css({
							left: mouseX - 50,
							top: mouseY - 70
						}).fadeIn();
	
					});
	
	
					obj.mouseout(function () {
						
							this.animate({
								fill: paths[arr[this.id]].coul
							}, 300);
					
						$('#Cmap').next('.point').remove();
	
					});
	
					
					obj.click(function(){
						if(paths[arr[this.id]].target=="_self"){
						location.href = paths[arr[this.id]].url;
						}else{
						window.open(paths[arr[this.id]].url, '_blank');
						}
						 
					})
				
				}
				else{
				   obj.attr({
                    cursor: 'default',
                    //stroke: '#000000',
					fill: mapoff
                   });	
				}//end if 

        }

        resizeMap(r);
    }


    // Set up for mouse capture
    if (document.captureEvents && Event.MOUSEMOVE) {
        document.captureEvents(Event.MOUSEMOVE);
    }

    // Main function to retrieve mouse x-y pos.s

    function getMouseXY(e) {
		
        var scrollTop = $(window).scrollTop();

        if (e && e.pageX) {
            mouseX = e.pageX;
            mouseY = e.pageY-scrollTop;
        } else {
            mouseX = event.clientX + document.body.scrollLeft;
            mouseY = event.clientY + document.body.scrollTop;
        }
        // catch possible negative values
        if (mouseX < 0) {
            mouseX = 0;
        }
        if (mouseY < 0) {
            mouseY = 0;
        }

        $('#map').next('.point').css({
            left: mouseX - 50,
            top: mouseY - 70
        })
    }

    // Set-up to use getMouseXY function onMouseMove
    document.body.onmousemove = getMouseXY;


    function resizeMap(paper) {

        paper.changeSize(mapWidth, mapHeight, true, false);

       
            $(".mapWrapper").css({
                'width': mapWidth + 'px',
                'height': mapHeight + 'px'
            });
        

    }

createMap();

});