/**
 * scroll.js 
 *
 * COPYRIGHT: All  title   and  proprietary  rights,  including  trade
 * secrets,   in   the   Software   and   any   copies thereof and the
 * accompanying  written   materials,   are  owned  by   schukai  GmbH
 * and  are  protected  by  German  copyright  laws,  other applicable
 * copyright   laws  and  international  treaty  provisions.
 *
 * @package    alvine
 * @author     schukai GmbH <info@schukai.de>
 * @copyright  Copyright (C) 2002, 2003, 2004, 2005, 2006 schukai GmbH
 * @license    http://www.alvine.de/license/
 * @version    20061114
 * @link       http://www.alvine.de/
 */

if(typeof alvine.js.plugin.scroll == 'undefined') alvine.js.plugin.scroll = new Object();
if(typeof alvine.js.plugin.scroll.action == 'undefined') alvine.js.plugin.scroll.action = new Object();


alvine.js.plugin.scroll.init  = function (){

    //Pagestepperclasse ausblenden
    $('.alvineJSPluginScrollPagestepper').hide();

    alvine_js_plugin_scroll_Counter = new Array();

    $('.alvineJSPluginScroll').each(function() {
        if($(this).data('isInit')==true) return;
        $(this).data('isInit', true);
        
        var target = $(this);

        alvine.js.plugin.scroll.OPP = $(this).data('opp');// Anzahl der Objekte pro Container
        alvine.js.plugin.scroll.Total = $(this).data('total');// Anzahl der Objekte insgesammt 
        alvine.js.plugin.scroll.Boxfilter = $(this).data('boxfilter');
        alvine.js.plugin.scroll.PagestepperBoxId = $(this).data('pagestepperboxid');
        alvine.js.plugin.scroll.AlvinePage = $(this).data('alvinepage');
        alvine.js.plugin.scroll.SID = $(this).data('sid');
        alvine.js.plugin.scroll.scrollaction = $(this).data('action');
        alvine.js.plugin.scroll.Maincontainerid = $(this).data('maincontainerid');// Content Container der Liste 
        alvine.js.plugin.scroll.scrollcontentwidth = $(this).data('scrollcontentwidth');
        alvine.js.plugin.scroll.scrollcontentheight = $(this).data('scrollcontentheight');
        alvine.js.plugin.scroll.shownumberdisplay = $(this).data('shownumberdisplay');

        if(typeof alvine.js.plugin.scroll.action[alvine.js.plugin.scroll.scrollaction] == "function"){
          if(alvine.js.plugin.scroll.Total>alvine.js.plugin.scroll.OPP) alvine.js.plugin.scroll.action[alvine.js.plugin.scroll.scrollaction](target);
        }

    });
  
};



alvine.js.plugin.scroll.inViewHorizontal = function(){
  
  var jspContaineroffset = $('.jspContainer').offset();
  var jspContainerwidth = $('.jspContainer').width();
  
  $('.scroll-content-item').each(function(){

    var scrollcontentoffset = $(this).offset();
    var scrollcontentpage = $(this).data('page');
    
    var scrollcontentposition = scrollcontentoffset.left-jspContaineroffset.left;
 
     if(scrollcontentposition<jspContainerwidth){
       if($(this).data('isload')!=true){
        alvine.js.plugin.scroll.alvineScrollHorizontal.load(scrollcontentpage);
        $(this).data('isload', true);
      }
     }
     
  })
  
}

alvine.js.plugin.scroll.inViewDown = function (elem) {
      
    var docViewTop = $(window).scrollTop(),
    docViewBottom = docViewTop + $(window).height(),
    elemTop = $(elem).offset().top,
    elemBottom = elemTop + $(elem).height();
    //Is more than half of the element visible
    var pgMainContent = $('#'+alvine.js.plugin.scroll.Maincontainerid).height();
    docViewTop = docViewTop + 1000;
    
    if(docViewTop>=pgMainContent){
        return  true
    }
    
    //return ((elemTop + ((elemBottom - elemTop)/2)) >= docViewTop && ((elemTop + ((elemBottom - elemTop)/2)) <= docViewBottom));
    
  }  
  
alvine.js.plugin.scroll.alvineScrollDown =  function (ev) {
  
    $(".arrowSidebarBottom").remove();

    if($('.alvineJSPluginScroll').data('onajaxloadFlag')==true) return;
    $('.alvineJSPluginScroll').data('onajaxloadFlag', true);
    
    
    if ( typeof alvine.js.plugin.scroll.alvineScrollDown.idCounter == 'undefined' ) {
        alvine.js.plugin.scroll.alvineScrollDown.idCounter = 1;
    }    

    $('.alvineJSPluginScroll').parent().append('<div id="galleryAjaxVerticalContent'+(alvine.js.plugin.scroll.alvineScrollDown.idCounter)+'"><div class="loaderImageBox"><div class="loaderImage">&nbsp;</div></div></div>');
   
    if(jQuery.data('body','loaderstate')!='on') $('.loaderImage').show();
    
    jQuery.data('body','loaderstate','on');
    r = Math.round(1 + 1000*(Math.random()));
    
    $.ajax({
        cache: false,
        url: "/index.php?sxx_encoding=utf8&sxx_page="+alvine.js.plugin.scroll.AlvinePage+"&sxx_boxfilter="+alvine.js.plugin.scroll.Boxfilter+"&sxx_call["+alvine.js.plugin.scroll.PagestepperBoxId+"][page]="+(alvine.js.plugin.scroll.alvineScrollDown.idCounter)+"&rsid="+alvine.js.plugin.scroll.SID+"&r="+r+"",
        success: function(data) {

            $('.alvineJSPluginScroll').data('onajaxloadFlag', false);

            response = $(data);
            alvineJSPluginScrollContent = response.find('.alvineJSPluginScroll').html();
        
            $('#galleryAjaxVerticalContent'+alvine.js.plugin.scroll.alvineScrollDown.idCounter).html(alvineJSPluginScrollContent);
      
            var t = $('#galleryAjaxVerticalContent'+alvine.js.plugin.scroll.alvineScrollDown.idCounter).offset().top;
      
            $('.alvineJSPluginScroll').parent().show();
            alvine.js.plugin.scroll.alvineScrollDown.idCounter ++;
            if(alvine.js.plugin.scroll.Total>alvine.js.plugin.scroll.OPP*(alvine.js.plugin.scroll.alvineScrollDown.idCounter)) {
                $('.alvineJSPluginScroll').parent().append('<div class="arrowSidebarBottom arrowSidebarBottomTog">&nbsp;</div>');
                $(".arrowSidebarBottom").show().bind('click',alvine.js.plugin.scroll.alvineScrollDown);
            }
            
            
            alvine.js.init();
 
        }
    });
    
        
};

alvine.js.plugin.scroll.shiftPanelRight = function() {
    alvine.js.plugin.scroll.scrollPannelApi.scrollBy(alvine.js.plugin.scroll.scrollcontentwidth, 0, true);
    alvine.js.plugin.scroll.inViewHorizontal();
  }

alvine.js.plugin.scroll.shiftPanelLeft = function() {
    alvine.js.plugin.scroll.scrollPannelApi.scrollBy(alvine.js.plugin.scroll.scrollcontentwidth*-1, 0, true); 
    alvine.js.plugin.scroll.inViewHorizontal();
  }

alvine.js.plugin.scroll.alvineScrollHorizontal = function() {
    
    var i = alvine.js.plugin.scroll.pageActCounter;
    alvine.js.plugin.scroll.pageActCounter ++;
    w = alvine.js.plugin.scroll.scrollcontentwidth;
    h = alvine.js.plugin.scroll.scrollcontentheight;
    r = Math.round(1 + 1000*(Math.random()));
    galleryAjaxContentwidth = w+(w*i);
    
    $(".galleryAjaxContent").css('width',galleryAjaxContentwidth+'px');
    $(".galleryAjaxContent").css('height',h+'px');
    alvine.js.plugin.scroll.scrollPannelApi.reinitialise();
    
    $('.galleryAjaxContent').append('<div class="scroll-content-item" style="position:relative;overflow:hidden;float:left;height:'+(h)+'px;width:'+w+'px" id="innerItems'+i+'" data-page="'+i+'"><div style="position:absolute;top:'+(h/2-90)+'px;left:'+(w/2-50)+'px;"><div class="loaderImageBox"><div class="loaderImage">&nbsp;</div></div></div></div>');

}
  
  
  
  alvine.js.plugin.scroll.alvineScrollHorizontal.load = function(page){
    
    $.ajax({
        cache: false,
        url: "/index.php?sxx_encoding=utf8&sxx_page="+alvine.js.plugin.scroll.AlvinePage+"&sxx_boxfilter="+alvine.js.plugin.scroll.Boxfilter+"&sxx_call["+alvine.js.plugin.scroll.PagestepperBoxId+"][page]="+page+"&rsid="+alvine.js.plugin.scroll.SID+"&r="+r,
        success: function(data) {
            var response = $(data);
            var Content = response.find(".alvineJSPluginScroll").html();
            $("#innerItems"+page).html(Content); 
            alvine.js.init();
        }
      });
      
  }
  
    alvine.js.plugin.scroll.action.scrolldown = function(obj){
      
       $(obj).parent().append('<div class="arrowSidebarBottom arrowSidebarBottomTog">&nbsp;</div>');

            if(alvine.js.plugin.scroll.Total>alvine.js.plugin.scroll.OPP) {
                $(".arrowSidebarBottom").show().bind('click',alvine.js.plugin.scroll.alvineScrollDown);
                // @TODO: PRÜFEN ob schon sichtbar.
                $(window).scroll(function () {

                    if(!$('.arrowSidebarBottom').is(':visible')) { 
                        return;
                    }

                    if( alvine.js.plugin.scroll.inViewDown('.arrowSidebarBottom')) {       
                        $('.arrowSidebarBottom').click();
                        $('.arrowSidebarBottom').hide();
                    };

                });

                if($('.arrowSidebarBottom').is(':visible')) { 
                    if( alvine.js.plugin.scroll.inViewDown('.arrowSidebarBottom')) {       
                        $('.arrowSidebarBottom').click();
                        $('.arrowSidebarBottom').hide();
                    };      
                };      


            //$('.alvineJSPluginScroll').append('<div style="clear:both;">&nbsp;</div>');
            }
      
    }
  
  alvine.js.plugin.scroll.action.scrollhorizontal = function(obj){
    
          $(obj).parent().append('<div class="arrowSidebar arrowSidebarLeft"><div class="arrowImg arrowImgLeft" >&nbsp;</div></div>');
          $(obj).parent().append('<div class="arrowSidebar arrowSidebarRight"><div class="arrowImg arrowImgRight" >&nbsp;</div></div>');
          $(obj).wrap('<div class="scrollGalleryPane"></div>');
          
          if(alvine.js.plugin.scroll.shownumberdisplay == 'yes'){
            var numberDisplay = '  <div id="numberDisplay">&nbsp;</div>';
          }
                    
           var settings = {
            showArrows: true,
            arrowScrollOnHover: true,
            animateScroll: true,
            horizontalDragMinWidth: 110,
            horizontalDragMaxWidth: 110,
            trackClickSpeed:100,
            animateEase:'linear',
            animateDuration:800
          };
          
          alvine.js.plugin.scroll.pageActCounter = 0;
          alvine.js.plugin.scroll.maxPageCount = alvine.js.plugin.scroll.Total/alvine.js.plugin.scroll.OPP;
          alvine.js.plugin.scroll.maxPageCount = Math.ceil(alvine.js.plugin.scroll.maxPageCount);
             
          var containerPane = $('#pgContentContainer');
          containerPane.css('position','relative');
          var pane = $('.scrollGalleryPane');
          pane.css('height',alvine.js.plugin.scroll.scrollcontentheight+'px');
          var api = pane.jScrollPane(settings).data('jsp');
          
          alvine.js.plugin.scroll.scrollPannelApi = api;
          var contentPane = pane.data('jsp').getContentPane();

          contentPane.html('<div style="height:1px;" class="galleryAjaxContent"></div>');
          
          //prepare container
          for(i=0;i<alvine.js.plugin.scroll.maxPageCount;i++){
            alvine.js.plugin.scroll.alvineScrollHorizontal();
          }
          
              setInterval(function() {
                  alvine.js.plugin.scroll.inViewHorizontal();
              }, 2000);
         
          
         // if(api.getIsScrollableH()) {

            $('.arrowSidebarRight').click(alvine.js.plugin.scroll.shiftPanelRight);
            $('.arrowSidebarLeft').click(alvine.js.plugin.scroll.shiftPanelLeft);

            if(_ALVINE_IE) {
              $('.arrowSidebarRight').show();
              $('.arrowSidebarLeft').hide();
            } else {
              $('.arrowSidebarLeft').hide();
              if($('.arrowSidebarRight').css("display")=='none') $('.arrowSidebarRight').css({"display":"block","opacity":"0"});
              $('.arrowSidebarRight').animate({opacity: 0.8}, 1000, function() {});
            };
            if(alvine.js.plugin.scroll.shownumberdisplay == 'yes'){
              containerPane.append(numberDisplay);
              $('#numberDisplay').html("1 / "+(alvine.js.plugin.scroll.Total));
            }
         // } else {
         //   if(alvine.js.plugin.scroll.shownumberdisplay == 'yes'){
         //     containerPane.append(numberDisplay);
         //     $('#numberDisplay').html((alvine.js.plugin.scroll.Total)+" / "+(alvine.js.plugin.scroll.Total));
         //   }
         // };

          $('.scrollGalleryPane').bind(
          'jsp-scroll-x',
          function(event, scrollPositionX, isAtLeft, isAtRight){
                  var x = alvine.js.plugin.scroll.OPP;
                  itemCount =  Math.round(scrollPositionX/w*x)+1;
                  if(isAtRight) itemCount = alvine.js.plugin.scroll.Total;
                  c = itemCount+" / "+(alvine.js.plugin.scroll.Total);
                  
                  if(alvine.js.plugin.scroll.shownumberdisplay == 'yes'){
                    $('#numberDisplay').html(c);
                  }
          }
        ).bind(
          'jsp-arrow-change',
          function(event, isAtTop, isAtBottom, isAtLeft, isAtRight) {

            if(_ALVINE_IE) {

              if(isAtLeft) {
                $('.arrowSidebarLeft').hide();
              } else {
                $('.arrowSidebarLeft').show();
              }

              if(isAtRight) {
                $('.arrowSidebarRight').hide();
              } else {
                $('.arrowSidebarRight').show();
              }

            } else {

              if(isAtLeft) {
                 
                $('.arrowSidebarLeft').animate({opacity: 0}, 1000, function() {});
              } else {
                if($('.arrowSidebarLeft').css("display")=='none') $('.arrowSidebarLeft').css({"display":"block","opacity":"0"});
                $('.arrowSidebarLeft').animate({opacity: 0.8}, 1000, function() {});
              }

              if(isAtRight) {
                $('.arrowSidebarRight').animate({opacity: 0}, 1000, function() {});
              } else {
                $('.arrowSidebarRight').animate({opacity: 0.8}, 1000, function() {});
              }
            }

            if(isAtRight) {
              //     loadHorNext();
            }

          }
          ).bind(
          'jsp-scroll-x',
          function(event, scrollPositionY, isAtBottom, isAtTop) {
            //scrollPositionY
          });

 
  }


