﻿
// *********************************************************************
// MAP
// *********************************************************************

function Map(id, img)
{
	this._id = id;
	this._image = img;
}

Map.prototype.insertMap = function()
{
	var div = $("<div>");
	$(div).attr("id", this._id);
	$(div).attr("style", "position: absolute;");
	
    var img = $("<img>");
	$(img).attr("src", this._image);

	var map = $(div).append(img);
	
	$(map).appendTo("body");
}


// *********************************************************************
// BULLETS
// *********************************************************************

function Bullet()
{
}

Bullet.prototype.setLink = function(link, target) {
	this._link = link;
	if(target!=null) this._target = target;
}
Bullet.prototype.setImage = function(img) { this._image = img; }
Bullet.prototype.setDirection = function(direction) { this._direction = direction; }
Bullet.prototype.setClash = function(clash) { this._clash = clash; }
Bullet.prototype.setDescription = function(description) { this._description = description; }
Bullet.prototype.setPosition = function(x, y)
{
    this._x = x;
    this._y = y;
}

Bullet.prototype.insertBullet = function(destDiv) {
	// Make empty div
	var div = $("<div>");
	$(div).css("position", "absolute");

	// Create link
	var href = $("<a>");
	$(href).attr("href", this._link);
	if (this._target) $(href).attr("target", this._target);

	// Make image for bullet
	var img = $("<img>");
	$(img).attr("src", this._image);

	// Insert bullet (div with link and image)
	var bullet = $(div).append($(href).append(img));
	$(bullet).appendTo(destDiv);

	// Create description
	if (this._description != "") {
		var description = $("<div>");
		$(description).css("position", "absolute");
		$(description).addClass("description");
		$(description).append(this._description);

		$(description).css("left", $(destDiv).position().left + this._x + $(img).width());
		$(description).css("top", $(destDiv).position().top + this._y + $(img).height());

		// Insert description
		$(description).hide();
		$(description).appendTo(destDiv);

	}

	if (this._direction == 1) {
		// Set bullet at 0-position
		$(bullet).css("top", ($(destDiv).position().top + ($(destDiv).height() / 2)));
		$(bullet).css("left", ($(destDiv).position().left + ($(destDiv).width() * 0)));
		$(bullet).css("opacity", 0);

		// Animate bullet to correct position
		$(bullet).animate({
			top: ($(destDiv).position().top + this._y),
			left: ($(destDiv).position().left + this._x),
			opacity: 1
		}, 1200, "swing");
	}
	else if (this._direction == 0) {
		// just show it at the correct location
		$(bullet).css("top", ($(destDiv).position().top + this._y));
		$(bullet).css("left", ($(destDiv).position().left + this._x));
		$(bullet).css("opacity", 1);

	}
	else {
		// hide the bullet 
		$(bullet).css("top", ($(destDiv).position().top + this._y));
		$(bullet).css("left", ($(destDiv).position().left + this._x));
		$(bullet).css("opacity", 1);

		// Animate bullet to correct position
		$(bullet).animate({
		    top: ($(destDiv).position().top + ($(destDiv).height() / 2)),
		    left: ($(destDiv).position().left + ($(destDiv).width() * 0)),
		    opacity: 0
		}, 1200, "swing");
	}

	if (this._description != "" && this._clash == true) {
		// Add events on description for mouseover/mouseout
		// not perfect yet but it will do
		$(bullet).bind("mouseenter", function() { $(".description").fadeOut(); $(description).slideDown("fast") });
		//$(bullet).bind("mouseleave", function() { $(description).fadeOut() });
		$(description).bind("mouseleave", function() { $(description).fadeOut() });
	}
	else if (this._description != "") {
		$(bullet).bind("mouseenter", function() { $(".description").fadeOut(); $(description).slideDown("fast") });
		$(bullet).bind("mouseleave", function() { $(description).fadeOut() });
	}
}
