/* GLOBAL VARIABLES
----------------------------------------------------*/
var slideTimer = new Array();


/* EVENTHANDLER FOR DOMONLOAD
//## ADD FUNCTIONS THAT SHOULD RUN ON DOM LOAD
----------------------------------------------------*/
var onPageLoad = function() {
	//initWorldMap();
	//initContinentMap();

	/*Prevent IE 6 background flickering*/
	try { document.execCommand('BackgroundImageCache', false, true) } catch (err) { };
};


var getElementsByClassName = function(a, b, c) { var d = (b == '*' && a.all) ? a.all : a.getElementsByTagName(b), arrReturnElements = new Array(); c = c.replace(/\-/g, '\\-'); var e = new RegExp('(^|\\s)' + c + '(\\s|$)'), oElement = null; for (var i = 0, n = d.length; i < n; i++) { oElement = d[i]; if (e.test(oElement.className)) arrReturnElements.push(oElement) }; return (arrReturnElements) };
var addEvent = function(obj, type, fn) { if (obj.addEventListener) { obj.addEventListener(type, fn, false); EventCache.add(obj, type, fn) } else if (obj.attachEvent) { obj['e' + type + fn] = fn; obj[type + fn] = function() { obj['e' + type + fn](window.event) }; obj.attachEvent('on' + type, obj[type + fn]); EventCache.add(obj, type, fn); } else { obj['on' + type] = obj['e' + type + fn] } }
var EventCache = function() { var listEvents = []; return { listEvents: listEvents, add: function(node, sEventName, fHandler) { listEvents.push(arguments) }, flush: function() { var i, item; for (i = listEvents.length - 1; i >= 0; i = i - 1) { item = listEvents[i]; if (item[0].removeEventListener) { item[0].removeEventListener(item[1], item[2], item[3]) } if (item[1].substring(0, 2) != 'on') { item[1] = 'on' + item[1] }; if (item[0].detachEvent) { item[0].detachEvent(item[1], item[2]) }; item[0][item[1]] = null } } } } ();
var removeEvent = function(obj, type, fn) { if (obj.removeEventListener) { obj.removeEventListener(type, fn, false) } else if (obj.detachEvent) { obj.detachEvent('on' + type, obj[type + fn]); obj[type + fn] = null; obj['e' + type + fn] = null } }
var CancelEvent = function(e) { if (e.preventDefault) { e.preventDefault() } else { e.returnResult = false }; if (e.stopPropagation) { e.stopPropagation() } else { e.cancelBubble = true; event.returnValue = false; } };
var getElementsByAttribute = function(a, b, c, d) { var e = (b == '*' && a.all) ? a.all : a.getElementsByTagName(b); var f = new Array(), g = (typeof d != 'undefined') ? new RegExp("(^|\\s)" + d + "(\\s|$)") : null, h, j; for (var i = 0, n = e.length; i < n; i++) { h = e[i]; j = h.getAttribute && h.getAttribute(c); if (typeof j == 'string' && j.length > 0) { if (typeof d == 'undefined' || (g && g.test(j))) { f.push(h) } } } return f }
var wm$ = function(elm) { return document.getElementById(elm); }

/* OPACITY FADE
----------------------------------------------------*/
var Fade = {
	In: function(uid, opacity) { if (typeof uid == 'object') { if (uid.id) { uid = uid.id; } else { var newUid = Guid(); DOM.SetAttribute(uid, 'id', newUid); uid = newUid; } } var obj = wm$(uid); if (!obj) return; obj.style.display = 'block'; if (opacity <= 100) { Fade.setOpacity(obj, opacity); opacity += 20; fadeTimer = window.setTimeout('Fade.In("' + uid + '",' + opacity + ')', 40) } else { Fade.setOpacity(obj, 100); obj.style.filter = ''; fadeTimer = null } },
	Out: function(uid, opacity) { if (typeof uid == 'object') { if (uid.id) { uid = uid.id; } else { var newUid = Guid(); DOM.SetAttribute(uid, 'id', newUid); uid = newUid; } } var obj = wm$(uid); if (!obj) return; if (opacity > 0) { Fade.setOpacity(obj, opacity); opacity -= 5; fadeTimer = window.setTimeout('Fade.Out("' + uid + '",' + opacity + ')', 30) } else { obj.style.display = 'none'; Fade.setOpacity(obj, 0); fadeTimer = null; } },
	setOpacity: function(obj, opacity) { if (!obj) return; obj.style.filter = 'alpha(style=0,opacity:' + opacity + ')'; obj.style.KHTMLOpacity = (opacity / 100); obj.style.MozOpacity = (opacity / 100); obj.style.opacity = (opacity / 100) }
};
/* CREATE GUID
----------------------------------------------------*/
var Guid = function() { var fakeGuid = '' + new Date().getTime() * Math.ceil(Math.random() * 100); fakeGuid = fakeGuid.substring(0, 12); return fakeGuid };

var getElmPosition = {
	GetX: function(obj) { var xy = 0; if (obj.offsetParent) { while (1) { xy += obj.offsetLeft; if (!obj.offsetParent) break; obj = obj.offsetParent } } else if (obj.x) { xy += obj.x } return xy },
	GetY: function(obj) { var xy = 0; if (obj.offsetParent) { while (1) { xy += obj.offsetTop; if (!obj.offsetParent) break; obj = obj.offsetParent } } else if (obj.y) { xy += obj.y } return xy }
};



var initWorldMap = function() {
	var worldMap = wm$('mapworld'); if (!worldMap) return; //Get  element with id mapworld

	// Get all the areas of the imagemap
	var continentList = worldMap.getElementsByTagName('AREA');

	// Loop trough all areas
	for (var i = 0, n = continentList.length, elm = null; i < n; i++) {
		elm = continentList[i];
		elm.style.display = 'none';

		// Add on mouse over on area
		addEvent(elm, 'mouseover', hoverArea);
		addEvent(elm, 'mouseout', unHoverArea);
		addEvent(elm, 'click', clickArea);
	}
};
var initContinentMap = function() {
	// Get all continents
	var continent = getElementsByClassName(document, 'div', 'map');
	for (var i = 0, n = continent.length, elm = null; i < n; i++) {
		elm = continent[i];

		// Get all countries in continents
		var countryList = elm.getElementsByTagName('AREA');
		for (var j = 0, m = countryList.length, elm = null; j < m; j++) {
			elm = countryList[j];
			elm.style.display = 'none';

			// Add onmouseover on area
			addEvent(elm, 'mouseover', hoverArea);
			addEvent(elm, 'mouseout', unHoverArea);
			addEvent(elm, 'click', clickCountry);
		}
	}
};

var hoverArea = function() {

	var areaId = this.id;
	// Extract the country-part of the id of the list-item
	areaId = areaId.substring(areaId.indexOf('_') + 1, areaId.length);
	liElm = wm$(areaId); if (!liElm) return;
	if (liElm == null) return;
	
	liElm.style.display = 'block';
	var ulElm = liElm.parentNode;
	ulElm.style.display = 'block';
};
var unHoverArea = function() {
	var modal = wm$('modal'); if (!modal) return;
	if (modal == null) return;
	if (modal.style.display != 'block') {
		var areaId = this.id;
		areaId = areaId.substring(areaId.indexOf('_') + 1, areaId.length);
		var liElm = wm$(areaId); if (!liElm) return; if (liElm == null) return;
		liElm.style.display = 'none';
		var ulElm = liElm.parentNode;
		ulElm.style.display = 'none';
	}
};
var clickArea = function(e) {
	CancelEvent(e)

	// Get the clicked map
	var clickedMap = this.parentNode.parentNode;

	// Get the clicked map image
	var clickedMapImg = clickedMap.getElementsByTagName('img');
	Fade.Out(clickedMap, 70);

	// Get clicked continent
	var clickedContinent = 'map_' + this.id.substring(this.id.indexOf('_') + 1, this.id.length);
	var continent = wm$(clickedContinent); if (!continent) return;
	Fade.In(continent, 30);

	// Get back button to get back to the world map
	var backButton = getElementsByClassName(wm$('mapwrapper'), 'a', 'backtotheworld')[0];
	backButton.style.visibility = 'visible';
	addEvent(backButton, 'click', goBackToWorldMap);
};

var clickCountry = function(e) {
    CancelEvent(e)

    // Get map wrapper element
    var mapWrapper = wm$('mapwrapper'); if (!mapWrapper) return;
    var mapWrapperWidth = mapWrapper.offsetWidth;
    var mapWrapperHeight = mapWrapper.offsetHeight;


    /********************************************************
    From...
    var modal = wm$('modal'); if (!modal) return; (the row below '// Get modal popup')
    to...
    var modal = wm$('modal'); if (!modal) return; (the row below '// Get modal wrapper element')
    ...are used to make the continent map (for example Europe)
    unavailable when a country is clicked. 
    ********************************************************/

//    // Get modal popup
//    var modal = wm$('modal'); if (!modal) return;
//    modal.style.display = 'block';
//    modal.style.width = mapWrapperWidth + 'px';
//    modal.style.height = mapWrapperHeight + 'px';
//    Fade.setOpacity(modal, 10);
//    // Get modal wrapper element
//    var modal = wm$('modal'); if (!modal) return;


    // Extract the country code to know from which country to genetere infrmation
    var areaId = this.id;
    var coutryCode = areaId.substring(areaId.indexOf('_') + 1, areaId.length);

    // When clickng a country, get the pup up and display the coutry information
    var popUpWrapper = wm$('popup'); if (!popUpWrapper) return;
    popUpWrapper.className = 'popup_' + coutryCode;

    var lang = "en";
    var language = wm$('language'); if (language) lang = language.value;
//    var t = Halvarsson.Sapa.Services.MapService;
    var t = Sapa_Public_Web_v6.WebReferences.MapService;
    t.CountryPopupMarkup(coutryCode, lang, FillPopupCallback, null, null);
};

var goBackToWorldMap = function(e) {
	CancelEvent(e)
	//window.clearTimeout(fadeTimer);
	//fadeTimer = null;
	this.style.visibility = 'hidden';

	// Get all continents and if they are visible, hide them
	var continents = getElementsByClassName(wm$('mapwrapper'), 'div', 'map');
	for (var i = 0, n = continents.length, elm = null; i < n; i++) {
		elm = continents[i];
		if (elm.style.display == 'block') Fade.Out(elm, 20);
	}
	// Get the world map and display it
	var worldMap = wm$('mapworld'); if (!worldMap) return;
	if (worldMap.style.display == 'none') Fade.In(worldMap, 20);

	// Get popup and if it is visible, close it
	var popup = wm$('popup'); if (!popup) return;
	if (popup.style.display == 'block') popup.style.display = 'none';

	// Get modal and if it is visible, close it
	var modal = wm$('modal'); if (!modal) return;
	if (modal.style.display == 'block') modal.style.display = 'none';

	// Reset map - Get selected country
	var popUpWrapper = wm$('popup'); if (!popUpWrapper) return;
	var activeCountry = popUpWrapper.className.substring(popUpWrapper.className.indexOf('_') + 1, popUpWrapper.className.length);
	var activeCountryLi = wm$(activeCountry); if (!activeCountryLi) return;
	activeCountryLi.style.display = 'none';
};

var closeModalPopup = function() {
	// Close popup and modal back plate
	//var popUpWrapper = this.parentNode.parentNode;
	var popUpWrapper = wm$('popup'); if (!popUpWrapper) return;
	popUpWrapper.style.display = 'none';
	var modal = DOM.NextSibling(popUpWrapper.nextSibling);
	modal.style.display = 'none';

	// Reset map - Get selected country
	var activeCountry = popUpWrapper.className.substring(popUpWrapper.className.indexOf('_') + 1, popUpWrapper.className.length);
	var activeCountryLi = wm$(activeCountry); if (!activeCountryLi) return;
	activeCountryLi.style.display = 'none';
};



/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/


var Console = function() {
	return {
		Log: function(msg, args) { if (typeof console == 'undefined') return false; if (!args) args = ''; console.log(msg, args) },
		Info: function(msg, args) { if (typeof console == 'undefined') return false; if (!args) args = ''; console.info(msg, args) },
		Warn: function(msg, args) { if (typeof console == 'undefined') return false; if (!args) args = ''; console.warn(msg, args) },
		Error: function(msg, args) { if (typeof console == 'undefined') return false; if (!args) args = ''; console.error(msg, args) }
	}
} ();

var DOM = {
	Get: function(el) { if (typeof el === 'string') { return document.getElementById(el); } else { return el; } },
	Add: function(el, dest) { var el = this.Get(el); var dest = this.Get(dest); dest.appendChild(el); },
	Remove: function(el) { var el = this.Get(el); el.parentNode.removeChild(el) },
	Create: function(tag, cssClass, id) { var tmp = document.createElement(tag); if (cssClass) { tmp.className = cssClass; } if (id) { tmp.id = id; } return tmp; },
	SetAttribute: function(parent, type, value) { var t = document.createAttribute(type); t.nodeValue = value; parent.setAttributeNode(t); },
	NextSibling: function(node) { while (node.nodeType == 3) { node = node.nextSibling; }; return node; },
	ChildNodes: function(obj) { var children = obj.childNodes; for (var ii = 0, nn = children.length, node = null, tmp = []; ii < nn; ii++) { node = children[ii]; if (node.nodeType == 1) tmp.push(node); }; return tmp; }
};

/* CANCEL EVENT
----------------------------------------------------*/
var CancelEvent = function(e) { if (e.preventDefault) { e.preventDefault() } else { e.returnResult = false }; if (e.stopPropagation) { e.stopPropagation() } else { e.cancelBubble = true; event.returnValue = false; } };

var onDOMload = function() { if (arguments.callee.done) return; arguments.callee.done = true; if (_timer) clearInterval(_timer); onPageLoad() };
/*@cc_on
@if(@_win32 || @_win64)if (document.getElementById) { document.write("<script id=\"ieScriptLoad\" defer src=\"//:\"><\/script>"); document.getElementById("ieScriptLoad").onreadystatechange = function() { if (this.readyState == "complete") { onDOMload() } } } @end@*/
if (document.addEventListener) { document.addEventListener('DOMContentLoaded', onDOMload, false) }; if (/KHTML|WebKit|iCab/i.test(navigator.userAgent)) { var _timer = setInterval(function() { if (/loaded|complete/.test(document.readyState)) { onDOMload() } }, 10) } window.onload = onDOMload;



function FillPopupCallback(result) {
	var popup = wm$('popup'); if (!popup) return;
	popup.innerHTML = result;
	popup.style.display = 'block';

	var closeModal = wm$('closemodal'); if (!closeModal) return;
	var closeClick = wm$('modal'); if (!closeClick) return;
	//addEvent(closeModal, 'click', closeModalPopup);
	addEvent(closeClick, 'click', closeModalPopup);
}

