
// OVERLAYS HANDLING //////////////////////////////////

function removeSiteOverlays() {
	//make sure there ain't nuttin overlayin'
	$(".siteOverlay").animate({"opacity":"0"}, 100, "linear", function(){
		$(this).remove();
		MoveInvite = false;
	});
}
function killTimers() {
	window.clearTimeout(timer);
	window.clearTimeout(timerAutoOpen);
}

//message d'invite drag n dropper la map
var MoveInvite = false;
function handleMoveInvite() {
    //get the body scroll position and height
    var bodyScrollTop = $(window).scrollTop(); //only works if body has overflow
    var bodyScrollHeight = $(window).height();

	if (!MoveInvite) {
		$("body")
			.append('<img src="./buttons/moveinvite.png" class="siteOverlay" id="moveInvite" style="position:absolute; z-index:50; opacity:0; filter:alpha(opacity=0);" />')
			.click(function(){$("#moveInvite").remove(); MoveInvite=false;});
		MoveInvite = true;
		if (IE)
			$("#moveInvite").attr("background-image","./buttons/moveinvite.gif");
		$("#moveInvite")
			.css({
	            "border-width":0 + "px",
	            "top":         bodyScrollTop + "px",
	            "left":        "50%",				
	            "margin-top":  bodyScrollHeight/2 - 65 + "px",
	            "margin-left": - 65 + "px" //to position it in the middle
	        })
	        .animate({"opacity":"1"}, 200, "linear", function() {});
	}
}


// MAP EVENTS & GLOBAL CONTROL //////////////////////////////////

jQuery.easing.def = 'linear';
var WinSize = windowSize();
var MapSize = {"width":6390, "height":4922};
var MapOn = false;
var MapPos = {"x":0, "y":0};
var PrevX = new Array(0,0), PrevY = new Array(0,0);
var timer = new Object;
var timerAutoOpen = new Object;

function mapMouseDown(o) {
	//make sure there ain't nuttin overlayin'
	removeSiteOverlays();

	//preparation for dragging
    MapOn = true;
    WinSize = windowSize();
    PrevX[1] = MouseX;
    PrevY[1] = MouseY;
    PrevX[0] = MouseX;
    PrevY[0] = MouseY;
    mapMouseMove();
    return false;
}

function mapMouseMove() {
    if (MapOn) {
        moveX = MouseX - PrevX[0];
        moveY = MouseY - PrevY[0];
        if (MapPos.x+moveX>=WinSize.width-MapSize.width && MapPos.x+moveX<=0) {
            MapPos.x += moveX;
        }
        if (MapPos.y+moveY>=WinSize.height-MapSize.height && MapPos.y+moveY<=0) {
            MapPos.y += moveY;
        }
        
        $("#map").css({"left":MapPos.x+"px", "top":MapPos.y+"px"});
        
        PrevX[1] = PrevX[0];
        PrevY[1] = PrevY[0];
        PrevX[0] = MouseX;
        PrevY[0] = MouseY;
        timer = window.setTimeout("mapMouseMove();",20);
    }
}

function mapMouseUp(o) {
    if (MapOn) {
        MapOn = false;
        window.clearTimeout(timer);
        var speed = getSpeed(PrevY[0],PrevY[1],PrevX[0],PrevX[1]);
        var distance = getDistance(PrevY[0],PrevY[1],PrevX[0],PrevX[1]);
        var rawTarget = getTarget(PrevY[0],PrevY[1],PrevX[0],PrevX[1], distance);
        var targets = calculateBounce(rawTarget);
        var bounce = (targets['bounce']!=undefined ? true : false);
        mapEase(targets, bounce, 600, speed);
        lineMapEase(bounce, 600);
    }
}

function moveBy(dx, dy, time) {
    window.clearTimeout(timer);
    var rawTarget = {"dx":dx, "dy":dy};
    var targets = calculateBounce(rawTarget);
    var bounce = false;
    mapEase(targets, bounce, time, undefined);
	lineMapEase(bounce, time);
}

function moveItemToCenterAndOpen(id) {
    //get the window's width and height and item's position
    WinSize = windowSize();
    var itemPos = {'x':$("#item_"+id).position().left, 'y':$("#item_"+id).position().top};
    
	//make sure there ain't nuttin overlayin'
	removeSiteOverlays();
	
	//show item infobox
	$("#item_"+id).mouseover();
	
	//move the map top center that position in the window and set timer to open item's related lightbox
	window.clearTimeout(timerAutoOpen);
	moveBy(-itemPos.x-MapPos.x+WinSize.width/2-27, -itemPos.y-MapPos.y+WinSize.height/2, 2500); //-27 to comply with item's width/2
	timerAutoOpen = window.setTimeout("$('#item_"+id+"').click();", 3000);
}
$(document).ready(function(){
    // make string for optional container id and add a click event
	$(".lineMapItem").click(function() {
		id = $(this).attr("rel");
		window.moveItemToCenterAndOpen(id);
		return false;
	});
});


// LARGE IMG DRAGGING & EASING ////////////////////////////////////////

function mapEase(targets, bounce, time, speed) {
    MapPos.x += targets['primary'].dx;
    MapPos.y += targets['primary'].dy;
    if (bounce)
        $("#map").animate({"left":MapPos.x+"px", "top":MapPos.y+"px"}, 300, "linear", function(){mapBounceBack(targets['bounce'], speed);});
    else
        $("#map").animate({"left":MapPos.x+"px", "top":MapPos.y+"px"}, time, "easeOutQuart", function(){});
}

function mapBounceBack(target, speed) {
    if (target.dx || target.dy) {
        MapPos.x += target.dx/(speed*.05);
        MapPos.y += target.dy/(speed*.05);
        $("#map").animate({"left":MapPos.x+"px", "top":MapPos.y+"px"}, 900, "easeOutQuad", function(){});
    }
}


// LINE MAP DRAGGING & EASING ////////////////////////////////////////

function lineMapEase(bounce, time) {
    if (bounce)
        $("#lineMap").animate({"left":MapPos.x+"px"}, 300, "linear", function(){lineMapBounceBack();});
    else
        $("#lineMap").animate({"left":MapPos.x+"px"}, time, "easeOutQuart", function(){});
}

function lineMapBounceBack() {
    $("#lineMap").animate({"left":MapPos.x+"px"}, 900, "easeOutQuad", function(){});
}

function lineMapScroll(direction) {
    //get the window's width and height
    WinSize = windowSize();
	//make the move    
	moveBy(direction*WinSize.width, 0, 600);
}

var ToggleLineMap = true;
function toggleLineMap() {
	ToggleLineMap = !ToggleLineMap;
	$("#lineMapParent").animate({"bottom":(ToggleLineMap ? 0 : -101)+"px"}, 200, "linear", function(){});
}


// TRAJECTORY CALCULATIONS ///////////////////////////////////////

function getSpeed(yB, yA, xB, xA) {
    return Math.abs(yB-yA+xB-xA);
}

function getDistance(yB, yA, xB, xA) {
    return Math.pow(getSpeed(yB, yA, xB, xA), 2)/200.0;
}

function getTarget(yB, yA, xB, xA, d) {
    if ((yB-yA)!=0 && (xB-xA)!=0) {
        alpha = Math.atan((yB-yA)/(xB-xA));
        dx = ((xB-xA)/Math.abs(xB-xA))*d*Math.abs(Math.cos(alpha));
        dy = ((yB-yA)/Math.abs(yB-yA))*d*Math.abs(Math.sin(alpha));
    } else {
        dx = 0;
        dy = 0;
    }    
    return {"dx":dx, "dy":dy};
}

function calculateBounce(target) {
    //get the window's width and height
    WinSize = windowSize();
    
    //prepare return vars
    var newTarget = {"dx":0, "dy":0};
    var bounce = false;
    
    if (MapPos.x+target.dx < WinSize.width-MapSize.width) {
        newTarget.dx = WinSize.width-MapSize.width-MapPos.x;
        bounce = true;
    } else if (MapPos.x+target.dx > 0) {
        newTarget.dx = -MapPos.x;
        bounce = true;
    } else
        newTarget.dx = target.dx;
    
    if (MapPos.y+target.dy < WinSize.height-MapSize.height) {
        newTarget.dy = WinSize.height-MapSize.height-MapPos.y;
        bounce = true;
    } else if (MapPos.y+target.dy > 0) {
        newTarget.dy = -MapPos.y;
        bounce = true;
    } else
        newTarget.dy = target.dy;
    
    if (bounce)
        var bounceTarget = {"dx":newTarget.dx-target.dx, "dy":newTarget.dy-target.dy};
    else
        var bounceTarget = undefined;
    
    return {"primary":newTarget, "bounce":bounceTarget};
}

