(function(jQuery) {
    jQuery.fn.extend({
        panorama: function(options) {

            var defaults = {
                defaultSize: 75,
                stepsUp: 5,
                stepsDown: 0,
                stepValue:100,
                cookieName: 'jquery_fontsize_plugin'
            };

            var options = jQuery.extend(defaults, options);


            return this.each(function() {
                var stepValue = 4;
                var timeoutValue = 20;
                var obj = jQuery(this);
                var timeoutLeft = null;
                var timeoutUp = null;
                var timeoutDown = null;
                var isLeftMouseDown = false;
                var isUpMouseDown = false;
                var isDownMouseDown = false;
                var timeoutRight = null;
                var isRightMouseDown = false;
                var panoramaImage = null;
                var isPlusMouseDown = null;
                var timeoutPlus = null;
                var isMinusMouseDown = null;
                var timeoutMinus = null;
                var lastHeight = null;
                var lastWidth = null;
                var lastLeft = null;
                var isEnabled = false;
                var lastTop = null;
                var counter=0;
                //Create references to the options
                var defaultSize = options.defaultSize;

                jQuery(document).keydown(function(event){
                     log3(event.keyCode);
                     switch (event.keyCode) {
                     case 107: //oben

                    log1("plus");
                            stopAll();
                             startPlus();
                        break;
                      case 109: //oben

                    log1("minus");
                               stopAll();
                             startMinus();
                        break;
                     case 38: //oben

                    log1("oben");
                              stopAll();
                             startDown();
                        break;
                      case 39: //rechts
                    log1("rechts");
                               stopAll();
                              startLeft();
                      break;
                      case 40: //unten
                    log1("unten");
                               stopAll();
                               startUp();
                     break;
                      case 37: //links
                    log1("links");
                               stopAll();
                    startRight();
                     break;
                     }
                });
                 jQuery(document).keyup(function(event){
                    switch (event.keyCode) {
                    case 107: //oben

                    log1("plus");
                             stopPlus();
                        break;
                      case 109: //oben

                    log1("minus");
                             stopMinus();
                        break;
                     case 38: //oben

                    log2("oben");
                             stopDown();
                    break;
                      case 39: //rechts
                    log2("rechts");
                              stopLeft();
                    break;
                      case 40: //unten
                    log2("unten");
                              stopUp();
                    break;
                      case 37: //links
                    log2("links");
                                stopRight();
                    break;
                     }
                });

                // minus Button start*********************************************************************************

                jQuery('#minusButton').mouseover(function(event) {
                    counter+=1;
                    log3('minus: '+counter);

                    event.stopPropagation();
                    event.preventDefault()
                   event.cancelBubble = true;
                      event.returnValue = false;
                    stopAll();
                    startMinus();


   return false;
                });
                function stopAll(){
                    stopMinus();
                    stopPlus();
                    stopLeft();
                    stopRight();

                }
                function startMinus() {
                      stopAll();
                    isMinusMouseDown = true;

                    if (panoramaImage == null) {
                        panoramaImage = obj.contents().find('#panoramaImage');

                           }
                        var offsets = panoramaImage.offset();
                        lastTop = offsets.top;
                        lastLeft = offsets.left;
                        lastHeight = panoramaImage.height();
                        lastWidth = panoramaImage.width();

                    if (isMinusMouseDown) {

                        timeoutMinus = setTimeout(animateMinus, timeoutValue); // adjust your time here
                    }


                }
                function animateMinus() {
                    if(isMinusMouseDown) {                                            
                          var factor= lastHeight / lastWidth;
                    var myValue = lastHeight - (stepValue * 2);
                    var myValueWidth= myValue/factor;
                        var myValueTop=(lastTop + stepValue);
                   if(myValue < obj.height()){
                      myValue =obj.height();
                      myValueWidth=myValue/factor;
                   }
                         var mytopF=panoramaImage.height()+myValueTop
                        if(mytopF < obj.height()){
                           myValueTop=(obj.height()-panoramaImage.height())/2;
                        }
                   if(myValueTop>=0) {
                      myValueTop=0;
                       
                   }


                    panoramaImage.attr('height', myValue);

                    panoramaImage.attr('width', myValueWidth);


                    panoramaImage.css('top', myValueTop + 'px');

                        var objWidth=obj.width()/2;

                                         var lengthFactor=100/lastWidth*((lastLeft-objWidth)*-1);


                                          var newValue=panoramaImage.width()/100*lengthFactor;
                                      var newLeftV=((newValue-objWidth)*-1);
                        if(newLeftV>0){
                            newLeftV=0;
                        } else{

                        if((newLeftV - obj.width()) * -1 >= panoramaImage.width()){
                           newLeftV=obj.width()-panoramaImage.width();
                    }
                        }
                                      panoramaImage.css('left',newLeftV + 'px');

                    lastLeft = panoramaImage.offset().left;
                    lastTop = panoramaImage.offset().top;
                    lastHeight = panoramaImage.height();
                    lastWidth = panoramaImage.width();

                     //console.log("lastTop: "+lastTop +"; lastHeight: "+lastHeight+"; obj.height(): "+obj.height());
                      setBorders();
                              timeoutMinus = setTimeout(animateMinus, timeoutValue);



                    }
                }

                jQuery('#minusButton').mouseout(function(event) {
                    event.stopPropagation();
                    event.preventDefault()
                    event.cancelBubble = true;
                      event.returnValue = false;
                    stopMinus();
                   return false;
                });
                function stopMinus() {
                    isMinusMouseDown = false;
                    clearTimeout(timeoutMinus);
                }


                // plus Button start*********************************************************************************

                jQuery('#plusButton').mouseover(function(event) {
                    event.stopPropagation();
                     event.preventDefault() ;
                      event.cancelBubble = true;
                      event.returnValue = false;
                     stopAll();
                    startPlus();
                   return false;
                });
                function startPlus() {
                    stopAll();
                    isPlusMouseDown = true;
                    if (panoramaImage == null) {
                        panoramaImage = obj.contents().find('#panoramaImage');
                      }
                        var offsets = panoramaImage.offset();
                        lastTop = offsets.top;
                        lastLeft = offsets.left;
                        lastHeight = panoramaImage.height();
                        lastWidth = panoramaImage.width();             

                    if (isPlusMouseDown) {

                        timeoutPlus = setTimeout(animatePlus, timeoutValue); // adjust your time here
                    }



                }

                function setBorders(){
                   //  console.log("lastTop: "+lastTop +"; lastHeight: "+lastHeight+"; obj.height(): "+obj.height());
                   //  console.log("lastWidth: "+lastWidth +"; lastLeft: "+lastLeft+"; obj.width(): "+obj.width());
                      var mytopF=panoramaImage.height()+lastTop;
                        if(mytopF <=obj.height()){

                            obj.css('border-bottom','1px solid red');
                        }
                    else{
                           obj.css('border-bottom','1px solid white');
                        }
                     if(lastTop==0){
                                   obj.css('border-top','1px solid red');
                               }
                               else{
                                   obj.css('border-top','1px solid white');

                               }
                           if(lastHeight>=obj.height()){
                             /*  if(lastTop==0){
                                   obj.css('border-top','1px solid red');
                               }
                               else{
                                   obj.css('border-top','1px solid white');

                               }
*/
                        } else{

                         obj.css('border-top','1px solid red');
                         //obj.css('border-bottom','1px solid red');
                         }
                           if(lastLeft==0){
                         obj.css('border-left','1px solid red');
                    }
                    else{
                       obj.css('border-left','1px solid white');
                }
                    if(lastLeft==obj.width()-panoramaImage.width()){
                           obj.css('border-right','1px solid red');
                    }
                    else{
                          obj.css('border-right','1px solid white'); 
                    }


                }

                function animatePlus() {
                    if (isPlusMouseDown) {

                    var factor= lastHeight / lastWidth;

                    var myValue = lastHeight + (stepValue * 2);
                         var myValueWidth= myValue/factor;
                    panoramaImage.attr('height', myValue);
                     panoramaImage.attr('width', myValueWidth);
                   
                    panoramaImage.css('top', (lastTop - stepValue) + 'px');
                        var objWidth=obj.width()/2;
                        
                     var lengthFactor=100/lastWidth*((lastLeft-objWidth)*-1);


                      var newValue=panoramaImage.width()/100*lengthFactor;
                  panoramaImage.css('left',((newValue-objWidth)*-1) + 'px');
              lastLeft = panoramaImage.offset().left;
                    lastTop = panoramaImage.offset().top;
                    lastHeight = panoramaImage.height();
                    lastWidth = panoramaImage.width();
                    setBorders();
                    if(lastWidth<6000){
                              timeoutPlus = setTimeout(animatePlus, timeoutValue);
                            }
                      


                    }
                }

                jQuery('#plusButton').mouseout(function(event) {
                    event.stopPropagation();
                     event.preventDefault();
                      event.cancelBubble = true;
                      event.returnValue = false;
                    stopPlus();
                    return false;
                });
                function stopPlus() {
                    isPlusMouseDown = false;
                    clearTimeout(timeoutPlus);
                }


                // centerButton start **********************************************************************************
                jQuery('#centerButton').mouseover(function(event) {
                    event.stopPropagation();
                    isEnabled = true;
                });
                jQuery('#panoramaPanel').mouseover(function(event) {
                    event.stopPropagation();
                    if (isEnabled)
                        isEnabled = false;

                });


                // leftUp start **********************************************************************************
                jQuery('#leftUpButton').mouseover(function(event) {
                    event.stopPropagation();
                     event.preventDefault();
                      event.cancelBubble = true;
                      event.returnValue = false;
                    if (isEnabled) {
                        startLeft();
                        startUp();
                    }
                    return false;
                });

                jQuery('#leftUpButton').mouseout(function(event) {
                    event.stopPropagation();
                      event.preventDefault();
                      event.cancelBubble = true;
                      event.returnValue = false;
                    stopUp();
                    stopLeft();
                   return false;
                });


                // leftDown start **********************************************************************************
                jQuery('#leftDownButton').mouseover(function(event) {
                    event.stopPropagation();
                      event.preventDefault();
                      event.cancelBubble = true;
                      event.returnValue = false;
                    if (isEnabled) {
                        startLeft();
                        startDown();
                    }
                    return false;
                });

                jQuery('#leftDownButton').mouseout(function(event) {
                    event.stopPropagation();
                      event.preventDefault();
                      event.cancelBubble = true;
                      event.returnValue = false;
                    stopDown();
                    stopLeft();
                     return false;
                });


                // rightUp start **********************************************************************************
                jQuery('#rightUpButton').mouseover(function(event) {
                    event.stopPropagation();
                      event.preventDefault();
                      event.cancelBubble = true;
                      event.returnValue = false;
                    if (isEnabled) {
                        startRight();
                        startUp();
                    }
                    return false;
                });

                jQuery('#rightUpButton').mouseout(function(event) {
                    event.stopPropagation();
                      event.preventDefault();
                      event.cancelBubble = true;
                      event.returnValue = false;
                    stopUp();
                    stopRight();
                    return false;
                });


                // rightDown start **********************************************************************************
                jQuery('#rightDownButton').mouseover(function(event) {
                    event.stopPropagation();
                      event.preventDefault();
                      event.cancelBubble = true;
                      event.returnValue = false;
                    if (isEnabled) {
                        startRight();
                        startDown();
                    }
                    return false;
                });

                jQuery('#rightDownButton').mouseout(function(event) {
                    event.stopPropagation();
                      event.preventDefault();
                      event.cancelBubble = true;
                      event.returnValue = false;
                    stopDown();
                    stopRight();
                    return false;
                });

                // down Button start*********************************************************************************
                jQuery('#downButton').mouseover(function(event) {
                    event.stopPropagation();
                      event.preventDefault();
                      event.cancelBubble = true;
                      event.returnValue = false;
                    if (isEnabled) {
                        startDown();
                    }
                    return false;
                });

                function startDown() {
                    isDownMouseDown = true;
                    if (panoramaImage == null) {
                        panoramaImage = obj.contents().find('#panoramaImage');
                           }
                        var offsets = panoramaImage.offset();
                        lastTop = offsets.top;
                        lastLeft = offsets.left;

                    lastHeight = panoramaImage.height();
                    lastWidth = panoramaImage.width();


                    if (isDownMouseDown) {

                        timeoutDown = setTimeout(animateDown, timeoutValue); // adjust your time here
                    }


                }
                jQuery('#downButton').mouseout(function(event) {
                    event.stopPropagation();
                      event.preventDefault();
                      event.cancelBubble = true;
                      event.returnValue = false;
                    stopDown();
                    return false;

                });
                function stopDown() {
                    isDownMouseDown = false;
                    clearTimeout(timeoutDown);
                }
                function animateDown() {

                    if (isDownMouseDown) {
                        var myValue = lastTop + stepValue;
                            //console.log(myValue);
                        if (myValue < 0) {
                            panoramaImage.css('top', myValue + 'px');
                            lastTop = panoramaImage.offset().top;
                            setBorders();
                            timeoutDown = setTimeout(animateDown, timeoutValue);
                        } else {
                            myValue = 0;
                             panoramaImage.css('top', myValue + 'px');
                             lastTop = panoramaImage.offset().top;

                             setBorders();

                        }

                    }
                }
                // up Button start*********************************************************************************

                jQuery('#upButton').mouseover(function(event) {
                    event.stopPropagation();
                      event.preventDefault();
                      event.cancelBubble = true;
                      event.returnValue = false;
                    if (isEnabled) {
                        startUp();
                    }
                    return false;
                });

                function startUp() {
                    isUpMouseDown = true;
                    if (panoramaImage == null) {
                        panoramaImage = obj.contents().find('#panoramaImage');
                         }
                        var offsets = panoramaImage.offset();
                        lastTop = offsets.top;
                        lastLeft = offsets.left;
                      lastHeight = panoramaImage.height();
                    lastWidth = panoramaImage.width();

                    if (isUpMouseDown) {

                        timeoutUp = setTimeout(animateUp, timeoutValue); // adjust your time here
                    }


                }

                jQuery('#upButton').mouseout(function(event) {
                    event.stopPropagation();
                      event.preventDefault();
                      event.cancelBubble = true;
                      event.returnValue = false;
                    stopUp();
                    return false;


                });
                function stopUp() {
                    isUpMouseDown = false;
                    clearTimeout(timeoutUp);

                }
                function animateUp() {
                    if (isUpMouseDown) {

                        var myValue = lastTop - stepValue;
                      //   console.log("Up: "+myValue);
                       // console.log("Up: "+((myValue - obj.height())*-1)+" < "+panoramaImage.height());
                        if(myValue<0){
                        if ((myValue - obj.height()) * -1 < panoramaImage.height()) {
                           
                            panoramaImage.css('top', myValue + 'px');
                            lastTop = panoramaImage.offset().top;
                             setBorders();
                            timeoutUp = setTimeout(animateUp, timeoutValue);
                        } else {
                            myValue =obj.height()- panoramaImage.height();
                            

                            panoramaImage.css('top', myValue + 'px');
                             lastTop = panoramaImage.offset().top;
                             setBorders();
                        }
                        }
                    }
                }
                // left Button start*********************************************************************************

                jQuery('#leftButton').mouseover(function(event) {
                    event.stopPropagation();
                      event.preventDefault();
                      event.cancelBubble = true;
                      event.returnValue = false;
                    if (isEnabled) {
                        startLeft();
                    }
                    return false;
                });
                function startLeft() {
                    isLeftMouseDown = true;
                    if (panoramaImage == null) {
                        panoramaImage = obj.contents().find('#panoramaImage');
                       }
                        var offsets = panoramaImage.offset();
                        lastLeft = offsets.left;
                        lastTop = offsets.top;

                    if (isLeftMouseDown) {

                        timeoutLeft = setTimeout(animateLeft, timeoutValue); // adjust your time here
                    }


                }
                jQuery('#leftButton').mouseout(function(event) {
                    event.stopPropagation();
                      event.preventDefault();
                      event.cancelBubble = true;
                      event.returnValue = false;
                    stopLeft();
                    return false;
                });
                function stopLeft() {
                    isLeftMouseDown = false;
                    clearTimeout(timeoutLeft);

                }
                function animateLeft() {

                    if (isLeftMouseDown) {
                        var myValue = lastLeft - stepValue;
                        if ((myValue - obj.width()) * -1 < panoramaImage.width()) {
                            panoramaImage.css('left', myValue + 'px');
                            lastLeft = panoramaImage.offset().left;
                            setBorders();
                            timeoutLeft = setTimeout(animateLeft, timeoutValue);
                        } else {

                            myValue =obj.width()-panoramaImage.width();
                            panoramaImage.css('left', myValue + 'px');
                             lastLeft = panoramaImage.offset().left;
                            setBorders();
                        }
                    }
                }
                // right Button start *********************************************************************************
                jQuery('#rightButton').mouseover(function(event) {
                    event.stopPropagation();
                      event.preventDefault();
                      event.cancelBubble = true;
                      event.returnValue = false;
                    if (isEnabled) {
                        startRight();
                    }
                    return false;
                });
                function startRight() {
                    isRightMouseDown = true;

                    if (panoramaImage == null) {
                        panoramaImage = obj.contents().find('#panoramaImage');
                         }
                        var offsets = panoramaImage.offset();
                        lastLeft = offsets.left;
                        lastTop = offsets.top;


                    if (isRightMouseDown) {

                        timeoutRight = setTimeout(animateRight, timeoutValue); // adjust your time here
                    }
                }
                jQuery('#rightButton').mouseout(function(event) {
                      event.preventDefault();
                      event.cancelBubble = true;
                      event.returnValue = false;
                    event.stopPropagation();
                    stopRight();
                    return false
                });
                function stopRight() {
                    isRightMouseDown = false;
                    clearTimeout(timeoutRight);

                }
                function log1(val) {
                 //  jQuery('#//console1').val(val);
                }
                function log2(val) {
                   // jQuery('#//console2').val(val);
                }
                function log3(val) {
                   //  jQuery('#//console3').val(val);
                }
                function animateRight() {

                    if (isRightMouseDown) {
                        var myValue = lastLeft + stepValue;
                        if (myValue < 0) {
                            panoramaImage.css('left', myValue + 'px');
                            lastLeft = panoramaImage.offset().left;
                            setBorders();
                            timeoutRight = setTimeout(animateRight, timeoutValue);
                        } else {
                           
                            myValue = 0;
                            panoramaImage.css('left', myValue + 'px');
                              lastLeft = panoramaImage.offset().left;
                            setBorders();
                        }

                    }
                }
                 
       // jQuery('#panoramaContainer').hide() ;
            });
        }
    });

})(jQuery);
