$(window).load(function() {


var hrefAfterLoad   = "/home.php"; // Ziel der Weiterleitung.
var redirect        = false; // true = Seite leitet nach dem Preload weiter | false = Seite leitet nicht nach dem Preload weiter
var preloaderLength = 250;
var loadBarNameID   = "loading";


//[BEGIN -->
var Pictures        = new Array
(
	'files/popmap/popup.png',
	'files/popmap/popup_header.png',
	'files/popmap/popup_footer.png',
    'files/popmap/button.png',
    'files/popmap/hamburg.png',
    'files/popmap/vechta.png',
    'files/popmap/berlin.png',
    'files/popmap/osnabrueck.png',
    'files/popmap/muenster.png',
    'files/popmap/alfeld.png',
    'files/popmap/magdeburg.png',
    'files/popmap/halle.png',
    'files/popmap/dresden.png',
    'files/popmap/kassel.png',
    'files/popmap/winterberg.png',
    'files/popmap/koeln.png',
    'files/popmap/aachen.png',
    'files/popmap/pruem.png',
    'files/popmap/limburg.png',
    'files/popmap/mainz.png',
    'files/popmap/wuerzburg.png',
    'files/popmap/bamberg.png',
    'files/popmap/nuernberg.png',
    'files/popmap/speyer.png',
    'files/popmap/eichstaett.png',
    'files/popmap/stuttgart.png',
    'files/popmap/neuulm.png',
    'files/popmap/muenchen.png',
    'files/popmap/freiburg.png',
    'files/popmap/passau.png'
);
// <-- END]


function preloader(){
    document.getElementById(loadBarNameID).width=1;
    if (!document.preImages) {
        preImages = new Array(); addition=Math.floor(preloaderLength/Pictures.length);
    	}
    	loadpics();
	}
	
function loadpics(){
  for (i = 0; i < Pictures.length; i++) {
      preImages[i] = new Image();preImages[i].src = Pictures[i];
  		}
  		check(0);
	}
	
function check(done){
 for (i = 0; i < preImages.length; i++) {
   if (preImages[i].complete) {
     if (done<preImages.length) {
        document.getElementById(loadBarNameID).width =      parseFloat(document.getElementById(loadBarNameID).width) + addition + "px";
        done++;
    	 } 
     	else 
     	{
         document.getElementById(loadBarNameID).width=preloaderLength;
         if (redirect) {
            location.replace(hrefAfterLoad); }
         return;
    	 }
  	 }
 	}setTimeout("check("+done+")",10);     
	}

preloader();



    function initializeMap(){
            $('#map').empty().css({
                width: '478px',
                height: '638px',
                backgroundImage: 'url(files/popmap/map.png)',
                backgroundRepeat: 'no-repeat',
                position: 'relative',
                backgroundPosition: '6px 0px'
            });
            $('#map').fadeIn();
            loadBullets('files/popmap/eplkekdata', false);
    }

    function addZoomable(id, left, top){
        $('<img class="zoomable" src="files/popmap/button.png" id="' + id + '" />').css({
            border: 'none',
            position: 'absolute',
            width: '30px',
            height: '27px',
            top: top + 'px',
            left: left + 'px',
            cursor: 'pointer'
        }).appendTo('#map').click(function() {
            $(this).siblings().fadeOut();  
            showPopup($(this).attr('id'));
            $(this).hide()
                   .attr('src', 'files/popmap/' + id + '.png')
                   .fadeIn('slow')
                   .animate({
                        width: '478px',
                        height: '638px',
                        top: '0px',
                        left: '0px'
                    }, 100, '', function(){
                        $('#mapover').css({backgroundImage: 'url(files/popmap/' + id + '.png)'});
                        showPopup(id);
                addZoomable('hamburg',200,111);
                addZoomable('vechta',124,161);
                addZoomable('berlin',378,184);
                addZoomable('osnabrueck',108,201);
                addZoomable('muenster',80,231);
                addZoomable('alfeld',192,221);
                addZoomable('magdeburg',288,223);
                addZoomable('halle',286,262);
                addZoomable('dresden',397,313);
                addZoomable('kassel',177,277);
                addZoomable('winterberg',128,291);
                addZoomable('koeln',37,322);
                addZoomable('aachen',0,330);
                addZoomable('pruem',29,378);
                addZoomable('limburg',112,365);
                addZoomable('mainz',104,406);
                addZoomable('wuerzburg',199,409);
                addZoomable('bamberg',245,405);
                addZoomable('nuernberg',257,444);
                addZoomable('speyer',119,450);
                addZoomable('eichstaett',256,488);
                addZoomable('stuttgart',158,496);
                addZoomable('neuulm',210,532);
                addZoomable('muenchen',284,545);
                addZoomable('freiburg',87,556);
                addZoomable('passau',394,515);

                    });
        });
    }
    
    function loadBullets(id, back){
        $('#map').load('files/popmap/eplkekdata?count=25', {}, function(){
            //add back button
            if(back){
                $('<a id="mapback" href="javascript:void(0)"><span>zurueck</span></a>')
                    .appendTo(this)
                    .click(function(){initializeMap()});
            }
            else{
                addZoomable('hamburg',200,111);
                addZoomable('vechta',124,161);
                addZoomable('berlin',378,184);
                addZoomable('osnabrueck',108,201);
                addZoomable('muenster',80,231);
                addZoomable('alfeld',192,221);
                addZoomable('magdeburg',288,223);
                addZoomable('halle',286,262);
                addZoomable('dresden',397,313);
                addZoomable('kassel',177,277);
                addZoomable('winterberg',128,291);
                addZoomable('koeln',37,322);
                addZoomable('aachen',0,330);
                addZoomable('pruem',29,378);
                addZoomable('limburg',112,365);
                addZoomable('mainz',104,406);
                addZoomable('wuerzburg',199,409);
                addZoomable('bamberg',245,405);
                addZoomable('nuernberg',257,444);
                addZoomable('speyer',119,450);
                addZoomable('eichstaett',256,488);
                addZoomable('stuttgart',158,496);
                addZoomable('neuulm',210,532);
                addZoomable('muenchen',284,545);
                addZoomable('freiburg',87,556);
                addZoomable('passau',394,515);

            }
            //place bullets
            $(this).children('a.bullet').each(function(){
                var coords = $(this).attr('rel').split('-');
                $(this).css({left: coords[0] + 'px', top: coords[1] + 'px'})
                       .hide()
                       .fadeIn()
                       .click(function(){showPopup($(this).attr('id'));});
            });
        });
    }
    
    function showPopup(id){ 
        var boxid = '#' + id + '-box';
        $(boxid).fadeIn();
        $('a.closelink').click(function(){
            $(boxid).fadeOut();
            loadBullets('files/popmap/eplkekdata', false);
        });
    }

    
    //initialize map
    initializeMap();
    

});
