

jQuery(document).ready(function() {

	//pngfix("#map-container img.region", "#popup-container h4, #popup-container ul");
	
	jQuery.fn.getBox = function() {
		return {
			left: jQuery(this).offset().left - jQuery(this).parent().offset().left,
			right: jQuery(this).parent().outerWidth() - (jQuery(this).offset().left - jQuery(this).parent().offset().left),
			top: jQuery(this).offset().top - jQuery(this).parent().offset().top,
			bottom: jQuery(this).parent().outerHeight() - (jQuery(this).offset().top - jQuery(this).parent().offset().top),
			width: jQuery(this).outerWidth(),
			height: jQuery(this).outerHeight()
		};
	}

	// Preload images
	var cache = [];
	jQuery("#map-container img").each(function() {
		var cacheImage = document.createElement('img');
		cacheImage.src = jQuery(this).attr("src");
		cache.push(cacheImage);
	});
	
	// Add close buttons
	jQuery(".popup-content").each(function() {
		var cb = jQuery("<a class='closeButton' title='Lukk' href='#'>Lukk</a>");
		cb.click(function() {
			jQuery('#map-container .region').removeClass('selected').hide();
			jQuery(this).parent().removeClass('selected').hide();
			return false;
		});
		jQuery(this).append(cb);
	});
	
	jQuery("#map-container area").mouseover(function(){
		var regionMap = jQuery('.'+jQuery(this).attr('id')+'-map').show();
	}).mouseout(function(){
		var regionMap = jQuery('.'+jQuery(this).attr('id')+'-map');
		var regionBox = jQuery('.'+jQuery(this).attr('id')+'-content');

		// Check if a click event has occured and only change the Region hover state accodringly
		if (!regionMap.hasClass('selected')) {
			regionMap.hide();
		}

		// Check if a click event has occured and only change the Region hover state accodringly
		if (!jQuery('#popup-container .popup-content').hasClass('selected')) {
			regionBox.hide();
		}
	});

	jQuery("#map-container").click(function(){
		jQuery('#map-container .region').removeClass('selected').hide();
		jQuery('#popup-container .popup-content').removeClass('selected').hide();
	});
	
	jQuery("#map-container area").click(function(){
		jQuery('#map-container .region').removeClass('selected').hide();
		jQuery('#popup-container .popup-content').removeClass('selected').hide();
		
		var regionMap = jQuery('.'+jQuery(this).attr('id')+'-map');
		var regionBox = jQuery('.'+jQuery(this).attr('id')+'-content');
		regionMap.addClass('selected').show();
		
		var regionMapPos = regionMap.getBox();
		
		var boxRight = Math.floor(regionMapPos.right - regionMapPos.width/2)-50;
		var boxBottom = Math.floor(regionMapPos.bottom - regionMapPos.height/2)+10;
		
		if (boxRight > 390) {
			var newPos = 37 + (boxRight - 390);
			regionBox.find('.popup-bottom').css('right', newPos+'px');
			boxRight = 390;
		}
		
		regionBox.addClass('selected').css({right:boxRight + "px", bottom:boxBottom + "px"}).show();
		
		return false;
	});

});

/* PNGfix */

function pngfix(imgselector, backgroundselector) {
    var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
    var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
    if (jQuery.browser.msie && (ie55 || ie6)) {
	
        jQuery(imgselector).load(function() {
            pngfiximage(this);
        });
		jQuery(document).ready(function() {
			pngfixbackground(backgroundselector);
		});
	
    }
}

function pngfixbackground(selector) {
	jQuery(selector).each(function(){
		var bgIMG = jQuery(this).css('background-image');
		if(bgIMG.indexOf(".png")!=-1){
			var iebg = bgIMG.split('url("')[1].split('")')[0];
			jQuery(this).css('background-image', 'none');
			jQuery(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
		}
	});
}

function pngfiximage(img) {
    jQuery(img).attr('width', jQuery(img).width());
    jQuery(img).attr('height', jQuery(img).height());

    var prevStyle = '';
    var strNewHTML = '';
    var imgId = (jQuery(img).attr('id')) ? 'id="' + jQuery(img).attr('id') + '" ' : '';
    var imgClass = (jQuery(img).attr('class')) ? 'class="' + jQuery(img).attr('class') + '" ' : '';
    var imgTitle = (jQuery(img).attr('title')) ? 'title="' + jQuery(img).attr('title') + '" ' : '';
    var imgAlt = (jQuery(img).attr('alt')) ? 'alt="' + jQuery(img).attr('alt') + '" ' : '';
    var imgAlign = (jQuery(img).attr('align')) ? 'float:' + jQuery(img).attr('align') + ';' : '';
    var imgHand = (jQuery(img).parent().attr('href')) ? 'cursor:hand;' : '';
    if (img.style.border) {
        prevStyle += 'border:' + img.style.border + ';';
        img.style.border = '';
    }
    if (img.style.padding) {
        prevStyle += 'padding:' + img.style.padding + ';';
        img.style.padding = '';
    }
    if (img.style.margin) {
        prevStyle += 'margin:' + img.style.margin + ';';
        img.style.margin = '';
    }
    var imgStyle = (img.style.cssText);

    strNewHTML += '<span ' + imgId + imgClass + imgTitle + imgAlt;
    strNewHTML += 'style="white-space:pre-line;background:transparent;' + imgAlign + imgHand;
    strNewHTML += 'width:' + jQuery(img).width() + 'px;' + 'height:' + jQuery(img).height() + 'px;';
    strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + jQuery(img).attr('src') + '\', sizingMethod=\'scale\');';
    strNewHTML += imgStyle + '"></span>';
    if (prevStyle != '') {
        strNewHTML = '<span style="' + prevStyle + imgHand + 'width:' + jQuery(img).width() + 'px;' + 'height:' + jQuery(img).height() + 'px;' + '">' + strNewHTML + '</span>';
    }

    jQuery(img).after(strNewHTML);
    jQuery(img).remove();
}

