﻿

/* TODO:
 * - PRELOAD IMAGES(?)
 * - OTHER COUNTRIES' INFORMATION
 * - COUNTRY INFO BOX EFFECTS
 * - COUNTRY INFO RETREAVAL BY AJAX
 ******************************************************************************/


var IMAGE_PATH = "/images/customer/ajaxmap/";
var MAP_WIDTH = "326px";
var MAP_HEIGHT = "346px";

var company_hash = new Object();
var countries = new Array;
var ajaxmap_element;
var company_info_element;
var ajaxmap_cover;


function init_map() {
	company_info_element = $('ajaxmap_company');
	ajaxmap_element = $('ajaxmap');
	ajaxmap_cover = $('ajaxmap_cover');
	
	if(ajaxmap_element == null) return;

	countries.push(new Country('Egypt', 164, 284, 32, 40, 'egypt.gif'));
	countries.push(new Country('Morocco', 23, 257, 30, 32, 'morocco.gif'));
	countries.push(new Country('Spain', 24, 218, 28, 41, 'spain.gif'));
	countries.push(new Country('Portugal', 24, 228, 10, 28, 'portugal.gif'));
	countries.push(new Country('Germany', 87, 146, 30, 47, 'germany.gif'));
	countries.push(new Country('France', 44, 178, 45, 41, 'france.gif'));
	countries.push(new Country('Belgium', 73, 168, 14, 11, 'belgium.gif'));
	countries.push(new Country('The Netherlands', 76, 154, 13, 14, 'netherlands.gif'));
	countries.push(new Country('Poland', 120, 145, 36, 35, 'poland.gif'));
	countries.push(new Country('Czech Republic', 111, 171, 23, 16, 'czech.gif'));
	countries.push(new Country('Estonia', 149, 106, 21, 14, 'estonia.gif'));
	countries.push(new Country('Latvia', 145, 120, 26, 16, 'latvia.gif'));
	countries.push(new Country('Lithuania', 144, 132, 23, 15, 'lithuania.gif'));
	countries.push(new Country('Tunisia', 93, 253, 14, 33, 'tunisia.gif'));
	countries.push(new Country('Russia', 181, 19, 145, 150, 'russia.gif'));
	countries.push(new Country('Finland', 146, 18, 34, 82, 'finland.gif'));
	countries.push(new Country('Norway', 81, 19, 60, 99, 'norway.gif'));
	countries.push(new Country('Sweden', 111, 36, 35, 102, 'sweden.gif'));
	countries.push(new Country('United Kingdom', 31, 115, 35, 61, 'great_britain.gif'));
	countries.push(new Country('Indonesia', 232, 265, 80, 73, 'indonesia.gif'));
	countries.push(new Country('Denmark', 91, 120, 22, 26, 'denmark.gif'));
	
	countries.each(function(country, index) {
		var id = 'country_' + index;
		var background = country.background_image(id);
		var anchor = country.anchor(id);
		country.setID(id);
		background.style.zIndex = index;
		anchor.style.zIndex = 100 + index;
		ajaxmap_element.appendChild(anchor);
		ajaxmap_element.appendChild(background);
	});
}
/*
 * IE-fix
 * If the overlaymaps are hidden in the init function, IE6 will not respond to
 * the anchors' onmouseover events. Foobar.
 *
 * Solution: background map is duplicated on top of the original map and the
 * overlay maps. When mouse is moved over the map, overlaymaps are hidden and
 * the duplicate map follows, allowing normal function. Peace.
 ******************************************************************************/
function hideOverylayMaps() {
	countries.each(function(country, index) {
		$(country.map_id).hide();
	});
	ajaxmap_cover.onmouseover = '';
	ajaxmap_cover.hide();
}


/* Class: Country
 * 
 * name - full name
 * a_left, a_top, a_width, a_height - numbers (pixels without unit)
 * img_filename - filename without directory path
 ******************************************************************************/
function Country(name, a_left, a_top, a_width, a_height, img_filename) {
	this._name = name;
	this._filename = img_filename.toString();
	
	this._a_left = a_left;
	this._a_top = a_top;
	
	this._a_width = a_width;
	this._a_height = a_height;
	
	this.map_id = '';
	
	if(typeof Country._initialized == 'undefined') {
		Country.prototype.image = function() {
			return IMAGE_PATH + this._filename;
		}
		
		Country.prototype.setID = function(id) {
			this.map_id = id;
		}
		
		Country.prototype.background_image = function(id) {
			var background_node = document.createElement("div");
			background_node.style.background = "url('" + this.image() + "') no-repeat left top";
			
			background_node.id = id;
			
			// dimensiot
			background_node.style.display = 'block';
			background_node.style.height = MAP_HEIGHT;
			background_node.style.width = MAP_WIDTH;
			// positiot
			background_node.style.position = 'absolute';
			
			return background_node;
		}

		Country.prototype.anchor = function(id) {
			var anchor_node = document.createElement("a");
			// dimensiot
			anchor_node.style.display = 'block';
			anchor_node.style.height = this._a_height + 'px';
			anchor_node.style.width = this._a_width + 'px';
			// positiot
			anchor_node.style.position = 'absolute';
			anchor_node.style.marginTop = this._a_top + 'px';
			anchor_node.style.marginLeft = this._a_left + 'px';
			// event handlerit
			if(this._name != 'Algeria')
				anchor_node.href = '/en/Contacts/' + this._name + "/";
			attachCountryEventHandlers(anchor_node, id, this._name);
			
			return anchor_node;
		}
		
		Country._initialized = true;
	}
}

function attachCountryEventHandlers(node, id, name) {
	Event.observe(node, 'mouseover', function(event){
		$(id).show();
		
		// Set right side country/company info:
		var company_box_content = '<span class="country">'+name+'</span>\n';
		if(company_hash[name])
			company_box_content += company_hash[name];
		showCompanies(company_box_content)
		
		/* AJAX version
		var ajax = new Ajax.Updater('ajaxmap_company', '/scripts/consolis/ajaxmap_companies.aspx?country=' + name, 
		{
			onComplete: showCompaniesAjax,
			method: 'get'
		}
		);
		*/
	});
	Event.observe(node, 'mouseout', function(event){
		$(id).hide();
	});
}

// For AJAX version:
function showCompaniesAjax(req) {
	showCompanies(req.responseText);
}

function showCompanies(company_list) {
	company_info_element.innerHTML = company_list;
	
	var height = stripPx(MAP_HEIGHT)/2 - company_info_element.getHeight()/2 +'px'
	Element.setStyle(company_info_element, { top: height });
}


function stripPx(value) {
	if(value == '') return 0;
	return parseFloat(value.substring(0, value.length - 2));
}

/* TODO:
 * - PRELOAD IMAGES(?)
 * - OTHER COUNTRIES' INFORMATION
 * - COUNTRY INFO BOX EFFECTS
 * - COUNTRY INFO RETREAVAL BY AJAX
 ******************************************************************************/


var IMAGE_PATH = "/images/customer/ajaxmap/";
var MAP_WIDTH = "326px";
var MAP_HEIGHT = "346px";

var company_hash = new Object();
var countries = new Array;
var ajaxmap_element;
var company_info_element;
var ajaxmap_cover;


function init_map() {
	company_info_element = $('ajaxmap_company');
	ajaxmap_element = $('ajaxmap');
	ajaxmap_cover = $('ajaxmap_cover');
	
	if(ajaxmap_element == null) return;

	countries.push(new Country('Egypt', 164, 284, 32, 40, 'egypt.gif'));
	countries.push(new Country('Morocco', 23, 257, 30, 32, 'morocco.gif'));
	countries.push(new Country('Spain', 24, 218, 28, 41, 'spain.gif'));
	countries.push(new Country('Portugal', 24, 228, 10, 28, 'portugal.gif'));
	countries.push(new Country('Germany', 87, 146, 30, 47, 'germany.gif'));
	countries.push(new Country('France', 44, 178, 45, 41, 'france.gif'));
	countries.push(new Country('Belgium', 73, 168, 14, 11, 'belgium.gif'));
	countries.push(new Country('The Netherlands', 76, 154, 13, 14, 'netherlands.gif'));
	countries.push(new Country('Poland', 120, 145, 36, 35, 'poland.gif'));
	countries.push(new Country('Ukraine', 151, 160, 70, 52, 'ukraine.gif'));
	countries.push(new Country('Czech Republic', 111, 171, 23, 16, 'czech.gif'));
	countries.push(new Country('Romania', 141, 185, 40, 31, 'romania.gif'));
	countries.push(new Country('Serbia', 135, 199, 20, 27, 'serbia.gif'));
	countries.push(new Country('Hungary', 125, 183, 29, 20, 'hungary.gif'));
	countries.push(new Country('Estonia', 149, 106, 21, 14, 'estonia.gif'));
	countries.push(new Country('Latvia', 145, 120, 26, 16, 'latvia.gif'));
	countries.push(new Country('Lithuania', 144, 132, 23, 15, 'lithuania.gif'));
	countries.push(new Country('Tunisia', 93, 253, 14, 33, 'tunisia.gif'));
	countries.push(new Country('Russia', 181, 19, 145, 150, 'russia.gif'));
	countries.push(new Country('Finland', 146, 18, 34, 82, 'finland.gif'));
	countries.push(new Country('Norway', 81, 19, 60, 99, 'norway.gif'));
	countries.push(new Country('Sweden', 111, 36, 35, 102, 'sweden.gif'));
	countries.push(new Country('United Kingdom', 31, 115, 35, 61, 'great_britain.gif'));
	countries.push(new Country('Indonesia', 232, 265, 80, 73, 'indonesia.gif'));
	countries.push(new Country('Denmark', 91, 120, 22, 26, 'denmark.gif'));
	
	countries.each(function(country, index) {
		var id = 'country_' + index;
		var background = country.background_image(id);
		var anchor = country.anchor(id);
		country.setID(id);
		background.style.zIndex = index;
		anchor.style.zIndex = 100 + index;
		ajaxmap_element.appendChild(anchor);
		ajaxmap_element.appendChild(background);
	});
}
/*
 * IE-fix
 * If the overlaymaps are hidden in the init function, IE6 will not respond to
 * the anchors' onmouseover events. Foobar.
 *
 * Solution: background map is duplicated on top of the original map and the
 * overlay maps. When mouse is moved over the map, overlaymaps are hidden and
 * the duplicate map follows, allowing normal function. Peace.
 ******************************************************************************/
function hideOverylayMaps() {
	countries.each(function(country, index) {
		$(country.map_id).hide();
	});
	ajaxmap_cover.onmouseover = '';
	ajaxmap_cover.hide();
}


/* Class: Country
 * 
 * name - full name
 * a_left, a_top, a_width, a_height - numbers (pixels without unit)
 * img_filename - filename without directory path
 ******************************************************************************/
function Country(name, a_left, a_top, a_width, a_height, img_filename) {
	this._name = name;
	this._filename = img_filename.toString();
	
	this._a_left = a_left;
	this._a_top = a_top;
	
	this._a_width = a_width;
	this._a_height = a_height;
	
	this.map_id = '';
	
	if(typeof Country._initialized == 'undefined') {
		Country.prototype.image = function() {
			return IMAGE_PATH + this._filename;
		}
		
		Country.prototype.setID = function(id) {
			this.map_id = id;
		}
		
		Country.prototype.background_image = function(id) {
			var background_node = document.createElement("div");
			background_node.style.background = "url('" + this.image() + "') no-repeat left top";
			
			background_node.id = id;
			
			// dimensiot
			background_node.style.display = 'block';
			background_node.style.height = MAP_HEIGHT;
			background_node.style.width = MAP_WIDTH;
			// positiot
			background_node.style.position = 'absolute';
			
			return background_node;
		}

		Country.prototype.anchor = function(id) {
			var anchor_node = document.createElement("a");
			// dimensiot
			anchor_node.style.display = 'block';
			anchor_node.style.height = this._a_height + 'px';
			anchor_node.style.width = this._a_width + 'px';
			// positiot
			anchor_node.style.position = 'absolute';
			anchor_node.style.marginTop = this._a_top + 'px';
			anchor_node.style.marginLeft = this._a_left + 'px';
			// event handlerit
			if(this._name != 'Algeria')
				anchor_node.href = '/en/Contacts/' + this._name + "/";
			attachCountryEventHandlers(anchor_node, id, this._name);
			
			return anchor_node;
		}
		
		Country._initialized = true;
	}
}

function attachCountryEventHandlers(node, id, name) {
	Event.observe(node, 'mouseover', function(event){
		$(id).show();
		
		// Set right side country/company info:
		var company_box_content = '<span class="country">'+name+'</span>\n';
		if(company_hash[name])
			company_box_content += company_hash[name];
		showCompanies(company_box_content)
		
		/* AJAX version
		var ajax = new Ajax.Updater('ajaxmap_company', '/scripts/consolis/ajaxmap_companies.aspx?country=' + name, 
		{
			onComplete: showCompaniesAjax,
			method: 'get'
		}
		);
		*/
	});
	Event.observe(node, 'mouseout', function(event){
		$(id).hide();
	});
}

// For AJAX version:
function showCompaniesAjax(req) {
	showCompanies(req.responseText);
}

function showCompanies(company_list) {
	company_info_element.innerHTML = company_list;
	
	var height = stripPx(MAP_HEIGHT)/2 - company_info_element.getHeight()/2 +'px'
	Element.setStyle(company_info_element, { top: height });
}


function stripPx(value) {
	if(value == '') return 0;
	return parseFloat(value.substring(0, value.length - 2));
}
