(function( $ ){

  $.fn.staticcarousel = function() {

      var nav     = (this.find('div.str_nav'));
      var content = (this.find('div.str_content'));
      var first   = null;
      var actual  = 1;
      var cycle   = true;
      var ajaxcontent = new Array();

      var n = nav.children('.str_arrow').length;

      var activate = function(object){

          var act_url = $(object).find('a').attr('href');

          nav.children('.str_arrow').children().removeClass('box_hov');
          nav.children('.str_arrow').children().removeClass('arr_hov');
          nav.children('.str_arrow').children('.box').addClass('box_norm');

          var arr = $(object).children('.arr');
          var me = $(object).children('.box');

          $(me).addClass("box_hov");
          $(arr).addClass("arr_hov");
          $(me).removeClass("box_norm");

          var ind = $.data(object, 'index');

          var response = $(content).children('.str_'+ind).html();
          if (response!=='') loadcontent(response);
          if (act_url!==undefined  && act_url!=='') $(content).clickUrl(act_url);
      };


      var loadcontent = function(mycontent) {
          $(content).children('.str_display').html(mycontent);
      }

      
      var deactivate = function(object){
          var arr = $(object).children('.arr');
          var me = $(object).children('.box');
          $(me).addClass("box_norm");
          $(arr).removeClass("arr_hov");
          $(me).removeClass("box_hov");
      };


      if (n!==undefined && n>0) {

            // Add clicks
            nav.children('.str_arrow').each(function(index) {
              var ind=index+1;
              $(this).addClass('index'+ind);
              $.data(this,'index',ind);
              if (first==null) first = $(this);
              var url = $(this).children('div').children('p').children('a').attr('href');
              if (url!==''&&url!==null&&url!==undefined) $(this).clickUrl(url);
            });

            // Add hover
            nav.children('.str_arrow').hover(
              function () {
                cycle=false;
                activate(this);
              },
              function () {
                cycle=true;
                //deactivate(this);
                var ind = $.data(this, 'index');
                actual=ind;
              }
            );

            // Add hover
            content.hover(
              function () {
                cycle=false;
              },
              function () {
                cycle=true;
              }
            );

            activate(first);

            window.setInterval(function() {

                if (cycle==true) {
                    var max = n-1;
                    if (actual>max) actual = 0;
                    nav.children('.str_arrow').each(function(index) {
                        //console.log('Index '+index+' '+actual);
                        if (actual==index) {
                            activate(this);
                        }
                    });
                    actual++;
                }
            }, 5000);
      }
  };

  $.fn.carousel = function() {

      var nav     = (this.find('div.str_nav'));
      var content = (this.find('div.str_content'));
      var first   = null;
      var actual  = 1;
      var cycle   = true;
      var ajaxcontent = new Array();

      var n = nav.children('.str_arrow').length;

      var activate = function(object){

          var act_url = $(object).find('a').attr('href');

          nav.children('.str_arrow').children().removeClass('box_hov');
          nav.children('.str_arrow').children().removeClass('arr_hov');
          nav.children('.str_arrow').children('.box').addClass('box_norm');

          var arr = $(object).children('.arr');
          var me = $(object).children('.box');
          var link = $(object).children('.load').attr('class');
          $(me).addClass("box_hov");
          $(arr).addClass("arr_hov");
          $(me).removeClass("box_norm");
          if (link.substr(0,4)=='load') {
              var arr = link.split(' ');
              if (arr[1]!==undefined) {
                  var rand=Math.random();
                  var url = arr[1]+'?rand='+rand;
                  var ind = $.data(object, 'index');
                  //var url = arr[1]+'#!'+ind;
                  if (ajaxcontent[ind]==undefined || ajaxcontent[ind]=='') {
                    $.ajax({
                      url: url,
                      context: document.body,
                      success: function(response){
                          ajaxcontent[ind] = response;
                          loadcontent(response);
                      }
                    });
                  } else {
                      loadcontent(ajaxcontent[ind]);
                  }
                  if (act_url!==undefined  && act_url!=='') $(content).clickUrl(act_url);
              }
          }
      };

      var loadcontent = function(mycontent) {
          $(content).html(mycontent);
      }


      var deactivate = function(object){
          var arr = $(object).children('.arr');
          var me = $(object).children('.box');
          $(me).addClass("box_norm");
          $(arr).removeClass("arr_hov");
          $(me).removeClass("box_hov");
      };


      if (n!==undefined && n>0) {

            // Add clicks
            nav.children('.str_arrow').each(function(index) {
              var ind=index+1;
              $(this).addClass('index'+ind);
              $.data(this,'index',ind);
              if (first==null) first = $(this);
              var url = $(this).children('div').children('p').children('a').attr('href');
              if (url!==''&&url!==null&&url!==undefined) $(this).clickUrl(url);
            });

            // Add hover
            nav.children('.str_arrow').hover(
              function () {
                cycle=false;
                activate(this);
              },
              function () {
                cycle=true;
                //deactivate(this);
                var ind = $.data(this, 'index');
                actual=ind;
              }
            );

            // Add hover
            content.hover(
              function () {
                cycle=false;
              },
              function () {
                cycle=true;
              }
            );

            activate(first);

            window.setInterval(function() {

                if (cycle==true) {
                    var max = n-1;
                    if (actual>max) actual = 0;
                    nav.children('.str_arrow').each(function(index) {
                        //console.log('Index '+index+' '+actual);
                        if (actual==index) {
                            activate(this);
                        }
                    });
                    actual++;
                }
            }, 5000);
      }
  };

   /**
     * Moves background image by changing background position
     * @param {Integer} Change of left property (in pixels)
     * @param {Integer} Change of top property in (pixels)
     *
     */

  $.fn.bgposMove = function(moveLeft, moveTop) {

    if (moveLeft==undefined) moveLeft=0;
    if (moveTop==undefined) moveTop=0;

    moveLeft = parseInt(moveLeft);
    moveTop = parseInt(moveTop);

    if (moveLeft!==0 || moveTop!==0){
        var pos = this.css('backgroundPosition');
        var x = $(this).css("background-position-x");
        var y = $(this).css("background-position-y");
        //F'ing IE
        if (pos==undefined && x!== undefined && x!==null && x!=='' && y!== undefined && y!==null && y!=='') {
        pos = x+' '+y;
        }
        //alert(this.attr('class')+' '+pos+' '+moveLeft+' '+moveTop+' X '+x+' Y  '+y);
        if (pos!==undefined && pos!==null && pos!=='') {
            var arr = pos.split(' ');
            if (arr[0]!==undefined && arr[1]!==undefined) {
                //alert('Los gehts');
                var pos1 = arr[0].search('px');
                var left = arr[0].slice(0,pos1);
                var pos2 = arr[1].search('px');
                var top = arr[1].slice(0,pos2);
                left = parseInt(left);
                top = parseInt(top);
                var l=left;
                var t=top;
                left = (left+moveLeft);
                top = (top+moveTop);
                //alert('Vorher: '+l+'px'+' '+t+'px / Nachher: '+left+'px'+' '+top+'px');
                var newValue = left+'px'+' '+top+'px';
                this.css('backgroundPosition', newValue);
            }
        }
    } else {
        this.css('backgroundPosition', '');
    }
  };

   /**
     * Moves background image by changing background position
     * @param {Integer} Change of left property (in pixels)
     * @param {Integer} Change of top property in (pixels)
     *
     */

  $.fn.clickUrl = function(url, title, target) {

    if (target==undefined) target=false;
    if (title==undefined) title=Math.random();
      
    this.click(function() {
        if (url!==''&&url!==null&&url!==undefined) {
            if (target==true) {
                var mywin=window.open(url,title);
                mywin.focus();
            } else {
                window.location.href=url;
            }
        }
    });

  };



  $.fn.letmefadein = function() {

    // there's no need to do $(this) because
    // "this" is already a jquery object

    // $(this) would be the same as $($('#element'));

    this.fadeIn('normal', function(){

      // the this keyword is a DOM element

    });

  };

})( jQuery );
