<!--
/**********************************************************************************************/
/* Copyright Visual Reality. All Rights Reserved.                                             */
/*                                                                                            */
/* This product is protected by copyright and distributed under licenses restricting copying, */
/* distribution and decompilation.                                                            */
/* No part of this website may be otherwise redistributed, copied, or reproduced in any form  */
/* without the prior written consent of Visual Reality.                                       */
/**********************************************************************************************/
(function($){
  $.fn.NewsCarousel = function(AOptions) {
    var defaults = {   
      width: 600,
      height: 300,
      timeout: 4000,
      fade: false,
      fadetime: 150,
      textoverlay: true,
      overlayheight: 100,
      maxtitlechars: 37,
      createlinks: true
    };
    var options = $.extend(defaults, AOptions);
    var ImageIDPrefix = 'nc_img_';
    var MainElement = this;
    var TimeOut = options.timeout;
    var TO = null;
    var ActiveListItemNr = 0;
    var ActiveListItem = null;
    var ActiveImageDiv = null;
    var PreviousImageDiv = null;
    var ListItems = $(MainElement).find('ul:not(".nc_buttons") li');
    if(!ListItems.length) return; //no list items found, return
    var NewsImage = null;
    var MouseOver = false;
    var divImage = $(MainElement).find('div.nc_image');
    var SubStringWords= function(AString, ALength) {
      if (AString.length <= ALength) return AString;
      var Length = ALength;
      while(Length>0) {
        if(AString.charAt(Length)==' ') break;
        Length--;
      }
      return AString.substr(0,Length);
    }
    if(divImage.length) {
      var imagesource = null;
      var textsource = null;
      var slide = null;
      var title = null;
      var titletag = null;
      var titlelink = null;
      var abstracthtml;
      $('<a>').attr({'href':'#','class':'nc_prev'}).text('Previous').appendTo(divImage);
      $('<a>').attr({'href':'#','class':'nc_next'}).text('Next').appendTo(divImage);
      ListItems.each(function(i) {
        imagesource = $(ListItems[i]).find('img.newsimage');
        titlelink = $(ListItems[i]).find('h3 a');
        title = $(ListItems[i]).find('h3 span.title');
        slide = $('<a>').attr({'id':ImageIDPrefix+i,'class':'imgdiv'}).appendTo(divImage).hide();
        if(titlelink.length && options.createlinks) $(slide).attr({'href':titlelink.attr('href')});
        if(imagesource.length) {
          var titletext = title.text();
          $('<img>').attr({'title':titletext,'alt':titletext,'src':imagesource.attr('src'),'width':options.width}).appendTo(slide);
          $('<div>').addClass('overlay').appendTo(slide);
        }
        if(options.textoverlay) {          
          abstracthtml = $(ListItems[i]).find('div:first').text();
          if(abstracthtml && abstracthtml!='') {
            abstracthtml = '<h4>' + title.text() + '</h4><div>' + abstracthtml + '</div>';
            $('<div>').attr({'class':'textdivbg'}).css({'top':options.height-options.overlayheight+'px','left':'0px','width':options.width-20+'px'}).appendTo(slide);
            $('<div>').attr({'class':'textdiv'}).css({'top':options.height-options.overlayheight+'px','left':'0px','width':options.width-40+'px'}).html(abstracthtml).appendTo(slide);
          }
        }
        if(title.text().length>=options.maxtitlechars) title.text(SubStringWords(title.text(),options.maxtitlechars-3)+'...');
      });
    }
    $(MainElement).find('ul:not(".nc_buttons") li a').mouseover(function() {
      MouseOver = true;
      if(ActiveListItem!=null) $(ActiveListItem).removeClass('active');
      ActiveListItem = $(this).parents('li')[0];
      ActiveListItemNr = jQuery.inArray(ActiveListItem, ListItems);
      SetActiveImage();
      DisplayItem();
      return false;
    }).mouseout(function() {
      if(TO==null) TO = setTimeout(TimerNextItem, TimeOut);
      MouseOver = false;
    });
    $('.imgdiv').live('mouseover', function() {
      MouseOver = true;
    }).live('mouseout', function() {
      if(TO==null) TO = setTimeout(TimerNextItem, TimeOut);
      MouseOver = false;
    });
    $('.nc_next, .nc_prev').live('click', function() {
      //MouseOver = false;
      if($(this).hasClass('nc_next')) {
        NextItem(false);
      } else {
        NextItem(true);
      }
      return false;
    });
    //Methods
    var SetActiveImage= function() {
      PreviousImageDiv = ActiveImageDiv;
      var img = $('#' + ImageIDPrefix + ActiveListItemNr);
      if(img.length) {
        ActiveImageDiv = img;
      } else {
        ActiveImageDiv = null;
      }
    }
    var NextItem = function(AGetPrevItem) {
      if(MouseOver) return;
      if(ActiveListItem==null) {
        ActiveListItemNr = 0;//select first link by default        
      } else {
        $(ActiveListItem).removeClass('active');
        if(AGetPrevItem) {
          //get prev item
          ActiveListItemNr = jQuery.inArray(ActiveListItem, ListItems) - 1;
          if(ActiveListItemNr<0) ActiveListItemNr=ListItems.length-1;
        } else {
          //get next item
          ActiveListItemNr = jQuery.inArray(ActiveListItem, ListItems) + 1;
          if(ActiveListItemNr>=ListItems.length) ActiveListItemNr=0;
        }
      }
      ActiveListItem = ListItems[ActiveListItemNr];
      SetActiveImage();
      DisplayItem();
    };
    var TimerNextItem = function() {
      TO = null;
      NextItem();
    };
    var DisplayItem = function() {
      if(TO!=null) clearTimeout(TO);
      $(ActiveListItem).addClass('active');
      ShowHideImages();
      TO = setTimeout(TimerNextItem,TimeOut);
    };
    var ShowHideImages = function() {
      if(options.fade) {
        if(PreviousImageDiv!=null) {
          $(PreviousImageDiv).fadeOut(options.fadetime, function(){
            if(ActiveImageDiv!=null) $(ActiveImageDiv).fadeIn(options.fadetime); 
          });
        } else if(ActiveImageDiv!=null) {
          $(ActiveImageDiv).fadeIn(options.fadetime);
        }
      } else {
        if(PreviousImageDiv!=null)$(PreviousImageDiv).hide();
        if(ActiveImageDiv!=null)$(ActiveImageDiv).show();
      }
    };
    NextItem();
  };
})(jQuery);
//-->
