var TotalNewsItems    = 0;
var PreviousNewsItem  = 0;
var AutoScrollTime    = 5000;
var Interval          = null;
$(document).ready(function(){
    // Serial scroll news
    /*$('#news-slider').serialScroll({
        items: '.item',     // selector to the items(relative to the scrolled element).
        duration: 1200,
        force: true,         // if true, an initial scroll will be forced on start.
        stop: true,         // if true, the plugin will stop any previous animations of the element, to avoid queuing.
        lock: true,          // disable queue
        cycle: true,         // do pull back once you reach the end
        interval:7500,      // If you specify a number, the plugin will add auto scrolling with that interval.
        constant:true 
    });   */
    
/*    $('#block-slide').serialScroll({
        items: '.item',     // selector to the items(relative to the scrolled element).
        duration: 1200,
        force: false,         // if true, an initial scroll will be forced on start.
        stop: true,         // if true, the plugin will stop any previous animations of the element, to avoid queuing.
        lock: true,          // disable queue
        cycle: false,         // do pull back once you reach the end
        onBefore: BeforeScroll 
    });  */


    
    var ItemsLength = $('#news-slider .item').length;
    var imgBulletFull  = "pics/newsticker-bullet-full.gif";
    var imgBulletEmpty = "pics/newsticker-bullet-empty.gif";
    
    $('#news-slider .item').each(function(i){
        var ControlContent = "";
        
        for(var a=0; a <= ItemsLength-1; a++) {
            if(i ==  a) {
                var imgSrc = imgBulletFull;
            } else {
                var imgSrc = imgBulletEmpty;        
            }
            
            ControlContent += '<a href="javascript:void(0);" onclick="ScrollNewsItem ('+ a +');"><img src=" '+ imgSrc + '" class="bullet" /></a>';           
        }
        
        TotalNewsItems++;
        
        $(this).find(".control").html(ControlContent);               
    });
    
    Interval = setInterval(function(){ ScrollNewsItem( PreviousNewsItem + 1 ) }, AutoScrollTime);
    
    BlockSlideCounter = -1;
    $('.block-slide').each(function(){
      ItemsBlockLength = $(this).find('.item').length;
      BlockSlideCounter++;

      $(this).find('.item.list').each(function(i){
        var _this = $(this);
        var ControlContent = "";
          for(var a=0; a <= ItemsBlockLength-1; a++) {
              if(i ==  a) {
                  var imgSrc = imgBulletFull;
              } else {
                  var imgSrc = imgBulletEmpty;        
              }
              ControlContent += '<a href="javascript:void(0);" onclick="ScrollToItem (\''+ a +'\',\'' + BlockSlideCounter + '\');"><img src="' + imgSrc + '" class="bullet" /></a>';           
          }
          
          $(this).find(".control").html(ControlContent);               
      });
    });

    
    /*
    $(".control-enabled").each(function(){
        setContentLiElements($(this));
        setControlLiElements($(this));    
    });
    */
    
    /*
    $(".animate").each(function(){
        var TopLayer   = $(this).find(".toplayer"); 
        var bAnimating = false;
        var _this = $(this); 
        $(this).hover(
            function() {
                //_this.dequeue();
                if(TopLayer.css("display") == "block" && !bAnimating) {
                    bAnimating = true;
                    TopLayer.fadeOut(200, function(){ bAnimating = false; });   
                } 
            },
            function () {
                //_this.dequeue();   
                if((TopLayer.css("display") == "none" || TopLayer.css("opacity") < 100)) {
                    bAnimating = true; 
                    TopLayer.fadeIn(200, function(){ 
                        bAnimating = false; 
                        
                        setContentLiElements(_this);                              
                        
                        //if(_this.hasClass("randomize")) {
                            //var oLiElement = _this.find("li.current");
                            //oLiElement.removeClass();
                            //moveFirstItemToLastPosition(oLiElement);
                            
                            //_this.find("li:first").attr("class","current");
                        //}
                    });
                }        
            }
        )  
     });  
     */           
    
     vCenterContent(".quote .txt", 100);  
     //vCenterContent("#content .row-home .wistudat li", 150);  
     //vCenterContent("#content .row-home .heeftuietstemelden div.vCenter", 150);  
     
     setTimeout(function(){
        $(".bg_image.fadeout").fadeOut(2000);     
     }, 1500);

     
     if($("#twitter-container_2").length > 0) {
        $("#twitter-container_2").html("<img src=\"" + WebUrl + "pics/ajax-loader.gif\" alt=\"Laden twitterfeeds...\" />");
        $.getJSON(WebUrl + 'twitter.php', function(data) {
            
            var Output = "";
            
            for(var i=0; i < data.length; i++) {
                var oDate        = new Date(data[i].created_at);
                var Date_Day     = oDate.getDate();
                var Date_Month   = (oDate.getMonth())+1;
                var Date_Year    = oDate.getFullYear();
                var Date_Hours   = oDate.getHours();
                var Date_Minutes = oDate.getMinutes();
                
                var Date_Display = Date_Day + "-" + Date_Month + "-" + Date_Year + " " + Date_Hours + ":" + Date_Minutes;
                
                Output += "<div class=\"twitter-item\">"
                       +    "<div class=\"twitter-date\">" + Date_Display + "</div>"
                       +    "<div class=\"twitter-content\">"
                       +        "<div class=\"twitter-image\"><img src=\"" + data[i].profile_image_url + "\" alt=\"" + data[i].from_user + "\" /></div>"
                       +        "<div class=\"twitter-txt\"><a href=\"http://twitter.com/" +  data[i].from_user + '/status/' + data[i].id_str + "\" target=\"_blank\" title=\"\">" + data[i].text + "</a></div>"
                       +    "</div>"
                       + "</div>";
            }
            
            $("#twitter-container_2").html(Output);

            $("#twitter-container_2").cycle({
                fx: 'fade',
                speed: 1000, 
                timeout: 5500,
                cleartype:  1 
             });
        });        
     }
});


$(window).load(function() {
    $("#content .row.middle .block").each(function() {
        var objBlock = $(this);
        
        var objBlockContentContainer = objBlock.find(".content_container");
        var objBlockContent          = objBlockContentContainer.find(".content");   
                     
        var ContentHeight_Over = "83px";
        var ContentHeight_Out  = "0px";
        
        var Top_Over = "80px";
        var Top_Out = "88px";
        
        var ContentPadding_Over = "10px";
        var ContentPadding_Out  = "0px";
        
        var SlideSpeed = 100;
        
        //check innerheight
        objBlockContent.css("height","auto");
       
        if(objBlock.find('.block-slide').length > 0){
          var ItemHeight = 0;
          $('.item', objBlock).each(function(){
            if($(this).innerHeight() > ItemHeight){
              ItemHeight = $(this).innerHeight() + 'px';
            }
          });
          ContentHeight_Over = ItemHeight;
        }
        objBlockContent.css("height","0px");         
                
        objBlock.hover(
            function() {                             
                objBlockContent.animate({ top: Top_Over, minHeight: ContentHeight_Over, height: "auto !important", height: ContentHeight_Over, paddingTop: ContentPadding_Over }, SlideSpeed, function() {
                  $(this).dequeue();
                });
                objBlockContent.find('.slide-container').animate({ minHeight: ContentHeight_Over, height: "auto !important", height: ContentHeight_Over }, SlideSpeed, function() {
                  $(this).dequeue();
                });
                
                if ($.browser.msie && ($.browser.version < 7)) {
                    objBlockContentContainer.animate({ minHeight: ContentHeight_Over, height: "auto !important", height: ContentHeight_Over }, SlideSpeed, function() {
                        $(this).dequeue();
                    }); 
                }           
            },
            function() {
                objBlockContent.animate({ top: Top_Out, minHeight: ContentHeight_Out, height: "auto !important", height: ContentHeight_Out, paddingTop: ContentPadding_Out  }, SlideSpeed, function() {
                    $(this).dequeue();
                    var SlideElement = $(this).find('.slide-container');
                    SlideElement.css({ left: '0px' });
                });
                
                objBlockContent.find('.slide-container').animate({ minHeight: ContentHeight_Out, height: "auto !important", height: ContentHeight_Out  }, SlideSpeed, function() {
                    $(this).dequeue();
                });
                  
                
                if ($.browser.msie && ($.browser.version < 7)) {
                    objBlockContentContainer.animate({ minHeight: ContentHeight_Out, height: "auto !important", height: ContentHeight_Out }, SlideSpeed, function() {
                        $(this).dequeue();
                    }); 
                } 
            }
        );      
    }); 
});

function ScrollToItem ( ItemIndex, ContainerDivIndex ){
  var ContainerDiv      = $('.block-slide').eq(ContainerDivIndex).find('.slide-container');  
  var ItemDiv           = ContainerDiv.find('.item').eq(ItemIndex);

  var ContainerWidth    = parseInt(ContainerDiv.outerWidth());
  var ItemWidth         = parseInt(ItemDiv.innerWidth());
  var ItemHeight        = parseInt(ItemDiv.innerHeight());
  var CurrentOffset     = parseInt(ContainerDiv.css('left'));
  
  var NewOffset         = ((ItemWidth * ItemIndex) * -1);
  
  ContainerDiv.parent().parent().css({ height : ItemHeight + 'px' });
  ContainerDiv.css({ height : ItemHeight + 'px' });
  ContainerDiv.animate({ left: NewOffset + 'px'}, function(){});
}

function ScrollNewsItem ( ItemIndex ){
  var ContainerDiv      = $('#news-slider').find('.slide-container');  
  var ItemDiv           = ContainerDiv.find('.item').eq(ItemIndex);

  var ContainerWidth    = parseInt(ContainerDiv.outerWidth());
  var ItemWidth         = parseInt(ItemDiv.innerWidth());
  var ItemHeight        = parseInt(ItemDiv.innerHeight());
  var CurrentOffset     = parseInt(ContainerDiv.css('left'));
  
  var NewOffset         = ((ItemWidth * ItemIndex) * -1);
  ContainerDiv.animate({ left: NewOffset + 'px'}, "slow", function(){
    if(parseInt(ItemIndex + 1) == TotalNewsItems){
      PreviousNewsItem = -1;
    } else {
      PreviousNewsItem = ItemIndex ;
    }
    clearInterval(Interval);
    Interval = setInterval(function(){ ScrollNewsItem( PreviousNewsItem + 1 ) }, AutoScrollTime);
  });
}

