
  //var $j = jQuery.noConflict();
  
  $(document).ready(function() {
		//alert('Funktioniert!');
		var erschein_speed = 1000;
        var erschein_warten = erschein_speed;
        var erschein_bg = 300;
        var erschein_bg_warten = erschein_speed - erschein_bg;
        var animation_start = 1000;

        $('#icons_liste img').parent().hover(
            function(){
                $(this).addClass('icons_hover');
            },
            function(){
                $(this).removeClass('icons_hover');
            }
        );

            // tooltip vars
            var erschein_speed = 500;
            var centering_speed = 1000;
            var moving_speed = 200;
            var _pointer_status_default = 'default';
            var _pointer_status_aktive = 'pointer';
            var _pointer_status_jetzt = _pointer_status_default;

            // split var
            var _my_split = '_msplit_';

            // test debug vars
            var output_html;
            var maus_status_html;
            var my_counter = 0;

            $("#viewport").mapbox({
                zoom: true,//is the map zoomable?
                pan: true,//is the map draggable?
                defaultLayer: 0,//this is an array type of number (zero being the first layer), which indicates which layer is shown by default
                layerSplit: 2,//this is how many zoom levels each layer has.  The higher this number is, the smoother the transition will be between each layer
                mapContent: ".mapcontent",//this is the class that goes on the layer which holds any content you will manually place onto the map
                defaultX: null,//if not set, the map will be centered on the x-axis.  If set, the left side of the map will be here
                defaultY: null,//defaultY is the same as defaultX, but for the y-axis
                callBefore: function(layer, xcoord, ycoord, viewport) {},//callback which happens when the left mouse button is held down, and gives access to the layer node, the x and y coordinates, and the viewport node.
                callAfter: function(layer, xcoord, ycoord, viewport) {},//callback which happens when the left mouse button is released
                beforeZoom: function(layer, xcoord, ycoord, viewport) {},//callback which occurs before the map zooms in either direction.
                afterZoom: function(layer, xcoord, ycoord, viewport) {
                },//callback which occurs after the map zooms in either direction.
                mousewheel: false, //false by default only because it requires mousewheel event plugin: http://plugins.jquery.com/project/mousewheel
                max_width: 2495,
                max_height: 1691,
                centering_speed: centering_speed,
                moving_speed: moving_speed
            });

            // move map Initially
            $("#viewport").mapbox('right', 43);
            $("#viewport").mapbox('down', 70);


            $(".map-control a").click(function() {//control panel
                var viewport = $("#viewport");
                //this.className is same as method to be called
                close_aktive_qtip();

                if(this.className == "zoom") {
                    viewport.mapbox('zoom');     //step once
                }
                else if (this.className == "back") {
                    viewport.mapbox('back');
                }
                else {
                    viewport.mapbox(this.className, 150);
                }

                _draw_tooltips();
                // writing to the debug
                return false;
            });

            // initial drawing
            _draw_tooltips();

            /* Aktionen wann mann auf der Firmenliste klickt */
            $('#icons_liste img').each(function(i, val_i){

                var _list_id = $(this).attr('id');
                var _pre_call_element = _list_id.split(_my_split);

                // get corresponding element name
                var _call_element = _pre_call_element[0];


                $(val_i).hover(
                    function(){
                    // showing big icon
                    /*$(this).hide();
                    $(this).parent().css({'z-index' : '10'});
                    //alert($(this).find('img').attr())
                    $(this).attr('src','logo_bilder/logo_gross/bremen.png');
                    $(this).fadeIn(300);*/

                    // moving icons on the map
                        var _old_top = parseInt($('#viewport div.current-map-layer  div.mapcontent').children('#'+_call_element).css('marginTop'));
                        //alert(_old_top);
                        var _new_top = _old_top - 10;
                        if(_old_top>0) {
                            $('#viewport div.current-map-layer  div.mapcontent').children('#'+_call_element).animate({marginTop: _new_top+'px'}, 100);
                            $('#viewport div.current-map-layer  div.mapcontent').children('#'+_call_element).animate({marginTop: _old_top+'px'}, 500);
                        }
                    },
                    function(){

                    }
                );

                $(val_i).click(function(){

                    $("#viewport").mapbox('zoomTo', 2);
                    _draw_tooltips();
                    $('#viewport div.current-map-layer div.mapcontent').children('div.untere_obj').css('background-image', 'url(fileadmin/logo_bilder/reddot.png)');

                    $('#viewport div.current-map-layer div.mapcontent').children('#'+_call_element).each(function(m, val_m){

                      var _current_classname = new String($('#viewport div.current-map-layer  div.mapcontent').children('#'+_call_element).attr('class').replace(/^\s+|\s+$/g,""));
                      //alert($('#viewport div.current-map-layer  div.mapcontent').children('#'+_call_element).html());
                      if(_current_classname.length != 'untere_obj_no_bg'.length) {
                        $('#viewport div.current-map-layer  div.mapcontent').children('#'+_call_element).css('background-image', 'url(fileadmin/logo_bilder/orangedot.png)');
                        //alert('CHECK IT? '+_current_classname+'='+_current_classname.length+'=?'+'untere_obj_no_bg'.length);
                      }

                      // get position of this element in aktive map
                      var _center_x = parseInt($(val_m).css('margin-left'));
                      var _center_y = parseInt($(val_m).css('margin-top'));

                      // center map around it
                      $("#viewport").mapbox('center', {x: _center_x+50, y: _center_y});

                      // imitate element's click
                      // closing aktive qtip before moving to the next item
                      close_aktive_qtip();
                      setTimeout(function() {
                          $(val_m).click();
                          //$(val_m+' .qtip').qtip("show");
                      }, centering_speed);
                    });
                });


            });

            // Hover die Markierung auf die Mappe
            //$('.untere_obj').each(function(uo, val_uo){ div[class^='untere_obj']
            $("div[class^='untere_obj']").each(function(uo, val_uo){
                var _sub_selector = '#'+$(val_uo).attr('id')+_my_split+'list';
                var _check_class = 'untere_obj_no_bg';
                $(val_uo).click(function(){
                    var _current_classname = new String($('#viewport div.current-map-layer div.mapcontent').children('#'+$(val_uo).attr('id')).attr('class').replace(/^\s+|\s+$/g,""));

                    $('#viewport div.current-map-layer div.mapcontent').children('div.untere_obj').css('background-image', 'url(fileadmin/logo_bilder/reddot.png)');
                    if(_current_classname.length!=_check_class.length) {
                        $('#viewport div.current-map-layer div.mapcontent').children('#'+$(val_uo).attr('id')).css('background-image', 'url(fileadmin/logo_bilder/orangedot.png)');
                        //alert($('#viewport div.current-map-layer div.mapcontent').children('#'+$(val_uo).attr('id')).attr('class'));
                        //$('#viewport div.current-map-layer div.mapcontent').children('#'+$(val_uo).attr('id')).css('background-image', 'url(logo_bilder/orangedot.png)');
                    }
                });

                $(val_uo).hover(
                    function(){
                        //console.log(_sub_selector);
                        $(_sub_selector).parent('td').addClass('icons_hover');
                    },
                    function(){
                        $(_sub_selector).parent('td').removeClass('icons_hover');
                    }
                );
            });

            // Remove Tooltip when dragging
            $("#viewport").click(function(){
                close_aktive_qtip();
            });

            // Closing active Tooltip
            function close_aktive_qtip() {
                if($('div.qtip-active').attr('qtip')!='')$('div.qtip-active').hide();
                $('.qtip').qtip('remove');
                //$('.qtip').remove();
            }

            // Top Menu
            $('#top_menu_platz a:not([class$=_active])').each(function(m, var_m) {
                main_navi_geschwindigkeit = 500;

        		$(this).parent().hover(
        			function() {
                        //alert('.'+$(var_m).attr('class')+'_active');
                        $(this).find('.'+$(var_m).attr('class')).animate({ opacity: 0 }, main_navi_geschwindigkeit);
                        $(this).find('.'+$(var_m).attr('class')+'_active').fadeIn(main_navi_geschwindigkeit);
        			},
        			function() {
                        $(this).find('.'+$(var_m).attr('class')).animate({ opacity: 1 }, main_navi_geschwindigkeit);
                        $(this).find('.'+$(var_m).attr('class')+'_active').fadeOut(main_navi_geschwindigkeit);
        			});

    		});

            // Footer Menu
            $('#footer_links a:not([class$=_active])').each(function(m, var_m) {
                main_navi_geschwindigkeit = 500;

        		$(this).parent().hover(
        			function() {
                        //alert('.'+$(var_m).attr('class')+'_active');
                        $(this).find('.'+$(var_m).attr('class')).animate({ opacity: 0 }, main_navi_geschwindigkeit);
                        $(this).find('.'+$(var_m).attr('class')+'_active').fadeIn(main_navi_geschwindigkeit);
        			},
        			function() {
                        $(this).find('.'+$(var_m).attr('class')).animate({ opacity: 1 }, main_navi_geschwindigkeit);
                        $(this).find('.'+$(var_m).attr('class')+'_active').fadeOut(main_navi_geschwindigkeit);
        			});

    		});

            /* Kreation von Tooltips auf der Mappe */
            function _draw_tooltips() {
                $("div.current-map-layer div[class^='untere_obj']").each(function(){
                    //console.log($(this).attr('rel'));

			
		/*
		var _my_domain_www = document.domain;
		if(_my_domain_www.search('www')!=-1) console.log(document.domain);
		else console.log('www not found');
		
		*/
		
		
                    $(this).qtip({
                       content: {
                            url: $(this).attr('href'),
                            title: {
                                text: "Info",
                                button: "close"
                             }
                       },
                       style: {
                            width: 250
                       },
                       position: {
                          corner: {
                             target: 'bottomRight'
                          }
                       },
                       show: {
                          when: { event: 'click' }
                       },
                       /*hide: {
                          when: { event: 'mouseout' }
                       },*/
                       hide: false,
                       api: {
                          onHide: function() {
                             close_aktive_qtip();
                          }
                       }
                    })
                    //if($(this).qtip!=false) alert($(this).qtip('getPosition')+'already exists');
                });
            };

            /* TEILNEHMER SEITE */
            /*$('#teilnehmer_liste img').parent().hover(
                function(){
                    $(this).addClass('icons_hover');
                },
                function(){
                    $(this).removeClass('icons_hover');
                }
            );*/

            $.easing.elasout = function(x, t, b, c, d) {
        		var s=1.70158;var p=0;var a=c;
        		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
        		if (a < Math.abs(c)) { a=c; var s=p/4; }
        		else var s = p/(2*Math.PI) * Math.asin (c/a);
        		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
        	};

            /* Aktionen wann mann auf der Firmenliste klickt */
            $('#teilnehmer_liste a').each(function(i, val_i){

                var _list_id = $(this).attr('id');
                var _pre_call_element = _list_id.split(_my_split);

                // get corresponding element name
                var _url = $(this).attr('href');
                
                //alert(document.domain+'/'+_url);
                //console.log(document.domain);
                

                $(val_i).click(function(e){
                    e.preventDefault();
                    var $c = $('.container');
                    
                    $('#teilnehmer_liste .teilnehmer_active_link').removeClass('teilnehmer_active_link').addClass('teilnehmer_passive_link');
                    $(val_i).addClass('teilnehmer_active_link');
                                        
                    $('#teilnehmer_info').hide();
                    $('#teilnehmer_info').html( '<p class="loading">Loading...</p>' );
                    $.ajax({
                        url: 'http://'+document.domain+'/'+_url,
                        cache: false,
                        //data: $(this).serialize(),
                        success: function(data) {
                            $('#teilnehmer_info').html(data);
                        }
                    });
                    $('#teilnehmer_info').fadeIn(1000);

            	    return false;
                });

                // Scroll-to

                /*$(val_i).click(function(){
                    var $c = $('.container');
            		$c.stop().scrollTo( 'li:eq('+_call_element+')', {
            		    speed:      1000,

                        axis:       $c.attr('id'),
                        queue:      true
                    });
            		return false;
                });*/


            });

            $('#teilnehmer_liste a:eq(0)').click();



  });
