$(function() {
  mappingMenu.init();
});

var mappingMenu = (function(){
var self = {},
$atom;

var hash_positions = [
{
"item_id": "unit001",
"name": "早稲田・高田馬場支部",
"left": 220,
"top": 185
},{
"item_id": "unit002",
"name": "川口支部",
"left": 210,
"top": 175
},{
"item_id": "unit003",
"name": "札幌支部",
"left": 265,
"top": 70
},{
"item_id": "unit004",
"name": "徳島支部",
"left": 140,
"top": 190
},{
"item_id": "unit005",
"name": "松山支部",
"left": 120,
"top": 190
},{
"item_id": "unit006",
"name": "仙台支部",
"left": 250,
"top": 140
},{
"item_id": "unit007",
"name": "新座支部",
"left": 220,
"top": 172
},{
"item_id": "unit008",
"name": "和光支部",
"left": 230,
"top": 175
},{
"item_id": "unit009",
"name": "八重山支部",
"left": 105,
"top": 129
},{
"item_id": "unit010",
"name": "春日井支部",
"left": 179,
"top": 175
},{
"item_id": "unit011",
"name": "新宿支部",
"left": 225,
"top": 184
},{
"item_id": "unit012",
"name": "安城支部",
"left": 184,
"top": 179
}
];

self.init = function () {
$atom = $("#mappingMenu .boxAtomImg");
initEventHandler();
initMap();
animateAtomWait(1);
};

function initEventHandler () {
$("#mappingMenu .linkList a").each(function () {
$(this).hover(
function () { onMouseOverMenuLink(this) },
function () { onMouseLeaveMenuLink(this) }
);
});
};

function initMap () {
$("#mappingMenu .linkList a").each(function(){
var data = getDataByName($(this).text(), hash_positions);
if (data !== null) {
$(this).addClass(data.item_id);
}
});
$(hash_positions).each(function(){
setMapLinkUnit(this);
});
$.Meca.pngfix.exec();
}

function setMapLinkUnit (args) {
var $unit = $('<span class="' + args.item_id + '" ></span>'),
iefix = 0;
if (navigator.userAgent.indexOf("MSIE 7.0") > -1 || navigator.userAgent.indexOf("MSIE 8.0") > -1) {
iefix -= 6;
}
$unit.css({
"left": args.left + "px",
"top": iefix + 6 + args.top + "px"
});
$unit.append('<img src="/img/mapmenu/img_pin.png" width="100%" height="100%" class="pngfix" />');
$("#mappingMenu .boxMap").append($unit);

}

var stateHolder = (function(){
var state = {};
state.isOnMenu = false
state.getFloatDiameter = function () {
return (state.isOnMenu) ? 7: 14;
}; 
return state;
}());

function onMouseOverMenuLink (obj) {
stateHolder.isOnMenu = true;
animateAtomMove($(obj).attr("class"));
var $obj = $("#mappingMenu .boxMap ." + $(obj).attr("class"));
$obj.find("img").attr("src", "/img/mapmenu/img_pin_o.png");
}

function onMouseLeaveMenuLink (obj) {
stateHolder.isOnMenu = false;
setTimeout(function () {
if (stateHolder.isOnMenu === false) {
$atom.animate({
"left": "-5px",
"top": "130px",
"width":"119px",
"height":"153px"
}, 300);
}
}, 400);

var $obj = $("#mappingMenu .boxMap ." + $(obj).attr("class"));
$obj.find("img").attr("src", "/img/mapmenu/img_pin.png");
}

function getDataByName (name, hashList) {
for (var i = 0, l = hashList.length; i < l; i++) {
if (name === hashList[i].name) {
return hashList[i];
}
}
return null;
}


function animateAtomWait (direction) {
var d = (direction < 0) ? "-": "+";

$atom.animate({
"margin-top": d + "" + stateHolder.getFloatDiameter() + "px"
}, {
"duration": 1000,
"easing": "jswing",
"queue": false,
"complete": function () {
setTimeout(function () {
animateAtomWait(direction*-1);
},0);
}
});
}

function animateAtomMove (unit_id) {
var $target = $("#mappingMenu .boxMap ." + unit_id)
$atom.animate({
"left": parseInt($target.css("left").replace("px", ""), 10) - 70 + "px",
"top": parseInt($target.css("top").replace("px", ""), 10) - 80 + "px",
"width":"73px",
"height":"102px"
}, {
"duration": 300,
"easing":"easeOutExpo",
"queue": false
});
}

return self;
}());

