//Passt ein Objekt dynamisch an das Scrollfenster an, Speed definiert, wie fies das Ding nachzieht
// Javascript Fehler unterdruecken
//onerror = stopError;
function stopError()
{
return true;
}
function sidebar(id)
{
    this.trigger = function()
    {
        if(!this.anim) this.helper();
    }
    this.helper = function()
    {
        this.anim = true;
        var _to = dojo.position(dojo.body());
        var _is = dojo.position(this.node, true);
        var set = - _to.y;
        var cur = _is.y;
        
        cur = cur + (((set - cur) * this.speed) > 0 ? Math.ceil((set - cur) * this.speed) : Math.floor((set - cur) * this.speed));
        if(cur + _is.h > _to.h)
        {
            cur = _to.h - _is.h;
            set = cur;  //Abbruchbed. schaffen
        }
        if(cur < this.paddingTop)
        {
            cur = this.paddingTop;
            set = cur; //Abbruchbed. schaffen
        }
        dojo.style(this.node, 'top', cur+'px');
        if(set != cur && this.speed != 1)
        {
            window.setTimeout(dojo.hitch(this, this.helper), 20);

        }else{
            this.anim = false;
        }
        
    }
    this.node = dojo.byId(id);
    this.anim = false;
    this.speed = 0.1;
    this.paddingTop = 0;
    this.paddingBottom = 0;
    dojo.connect(window, 'onscroll', dojo.hitch(this, this.trigger));
}


//Wechselt Bilder durhc, .start(sekunden) für automatischen Wechsel, im constructor node/id, .next .prev .set(nummer)
var myinterval = false;

function imagefade(id)
{
    var $ = jQuery;
    this._set = function(s)
    {
        if(this.anim) return;
        if(s == this.current) return;
        this.onSet(s);
        this.anim = true;
        $(this.node).empty();  

        var that = this;
        $(this.nodes[s]).clone().appendTo(this.node);
        $(this.nodes[this.current]).clone().appendTo(this.node).css('opacity', 1)
        .animate({opacity: 0}, function(){
            that.current = s;

            $(this).remove();


            that.anim = false;

        
        });
    };
    this._clear = function()
    {
        if(myinterval == false) return;
        window.clearInterval(myinterval);
        myinterval = false;
    };
    this._next = function()
    {
        var s = this.current;
        s++;
        if(s >= this.nodes.length) s = 0;
        this._set(s);
    };
    this._prev = function()
    {
        var s = this.current;
        s--;
        if(s <0 ) s = this.nodes.length - 1;
        this._set(s);
    };
    this.prev = function()
    {
        this._clear();
        this._prev();
    };
    this.next = function()
    {
        this._clear();
        this._next();
    };
    this.set = function(s)
    {
        this._clear();
        this._set(s);
    };
    this.start = function(t)
    {
        myinterval = this.nodes.length > 0 ? window.setInterval(dojo.hitch(this, this._next), t * 1000) : false;  //Zeigt auf den Interval, sonst false
    }
    
    //Events
    
    this.onSet = function(){};
    //Initialisierung
    this.current  = 0;  //Aktuelles Bild
    this.node     = dojo.byId(id);  //Node der die Bilder enthält
    this.nodes    = $('#' + id + ' > img').clone().css('display', 'block').toArray(); //Alle Bilder
    //console.log(this.nodes);
    this.anim     = false;          //Läuft gerade eine Animation?
    myinterval = false;
    dojo.empty(this.node);  //Aufräumen
    if(this.nodes.length) dojo.place(this.nodes[0], this.node);  //Startbild platzieren
}

//<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
//<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.5/dojo/dojo.xd.js" type="text/javascript"></script>
function navigation(div, des)
{
    this.directionsDisplay = new google.maps.DirectionsRenderer();
    this.directionsService = new google.maps.DirectionsService();
    this.map;
    this.des;
    this.marker;
    var _o = {
        zoom:15,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: new google.maps.LatLng(0, 0),
        disableDefaultUI: true,
        disableDoubleClickZoom: true,
        scrollwheel: false,
        navigationControl: true,
        keyboardShortcuts : false
      }
    this.map = new google.maps.Map(dojo.byId(div), _o);
    var coder = new google.maps.Geocoder();
    coder.geocode( { 'address': des }, 
        dojo.hitch(this, function(result, stat) {
            if (stat == google.maps.GeocoderStatus.OK) {
                this.des = new google.maps.LatLng(result[0].geometry.location.lat(), result[0].geometry.location.lng());
                this.map.setCenter(this.des);
                this.marker = new google.maps.Marker({
                    position: this.des
                });
                this.marker.setMap(this.map);
                google.maps.event.addListener(this.marker, 'click', dojo.hitch(this, function(){
                    this.route(window.prompt('Bitte geben Sie Ihre Adresse ein, um Ihre Anfahrtsroute berechnen zu lassen',''));
                }));
            }
        })
    );
    this.directionsDisplay.setMap(this.map);
  
    this.route = function(from) {
        var _r = {
            origin:from, 
            destination:this.des,
            travelMode: google.maps.DirectionsTravelMode.DRIVING,
            region: 'de'
        };
        this.directionsService.route(_r, dojo.hitch(this,function(result, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                this.marker.setMap(null);
                this.directionsDisplay.setDirections(result);
            }
        }));
    }   
}

function fixedbox (node)
{
    this.trigger = function()
    {
        var _to = dojo.position(dojo.body());
        var _is = dojo.position(this.node, true);
        var height = 0;
        if( - _to.y < this.paddingTop) height = this.paddingTop + _to.y;
        if(_is.h + this.paddingBottom - _to.y > _to.h) height = _to.h - (_is.h + this.paddingBottom - _to.y);
        dojo.style(this.node, 'top', height+'px');

    }
    this.paddingBottom = 0;
    this.node = dojo.byId(node);
    this.paddingTop = parseInt(dojo.toJson(dojo.style(this.node, 'top')));
    dojo.style(node, 'top', '0px');
    dojo.connect(window, 'onscroll', dojo.hitch(this, this.trigger));
    this.trigger();
    
}
function calcSidebarPosition() {
        var page_left = jQuery("#page").offset().left;
        jQuery("#sidebar").css("left",page_left);
}
jQuery(window).bind("resize",function(){
        calcSidebarPosition();
});


