/*
 *  Beispielaufruf: 
 * 	new cst_scroller('#angebot','.angebot',250,'#imgbox',5,'/static/i/cst-circle');
 *  
 *  parentname: die box au�enrum
 *  childname: die einzelnen zu verschiebenden objekte
 *  movevalue: wieweit soll geschoben werden (in px)
 *  imgbox: in diesem element werden die kringel platziert
 *  interval: zeit zwischen zwei schaltvorg�ngen (in sec)
 *  path: pfad zu den bildern incl bildname
 *  ( daraus wird im prinzip /static/i/cst-circle.gif und /static/i/cst-circle-filled.gif )
 *   
 */
function article_scroller( parentname, childname, movevalue, imgbox, interval, path ) {
  
  position = 1;
  var last_move = 'left';
  var childlength = $( parentname + " " + childname ).size();
  var autoscroll;
  var interval = interval * 1000;
  var imgbox = imgbox;
  var path = path;
  __cst_scroller = function() {
  	var item = $( imgbox );
    for( i=0; i<childlength; i++ ) {
     item.append( "<img src=" + path + ".gif>" ); 
    }
    set_active_circle();
    bind_links();
    
    if($( parentname ).get( 0 ) == undefined) return false;
    
    $( parentname ).get( 0 ).style.marginLeft = '0px';
  }
   
  set_active_circle = function() {
  	$( imgbox+" img" ).each(
  		function() {
  			active_element = $( imgbox+" img:eq("+(position-1)+")" ).get(0);
  			if( active_element == this ) {
  				this.src = path + '-filled.gif';
   			} else {
	  			this.src = path + '.gif';
  			}
  		}
  	);
  }

  bind_links = function() {
	  if ( childlength > 1 ) {
	  	var leftlink = $( '<a id="article-switch-back" class="article-switch-back" href="#"></a>' );
	  	leftlink.click(function() {move_left(); return false;});
	  	var rightlink = $( '<a id="article-switch-next" class="article-switch-next" href="#"></a>' );
	  	rightlink.click( function() {move_right(); return false;} );
	  	$( imgbox ).after( rightlink );
	  	$( imgbox ).after( leftlink );
	  }
  }
  
  var auto_move = function() {
	  if ( childlength > 1 ) {
		if ( position == 1 || position == childlength ) {
			if ( last_move == 'left' ) move_right();
			else move_left();
		}
		else if ( last_move == 'left' ) move_left();
		else move_right();
		autoscroll = window.setTimeout( auto_move, interval );
	  }
	  else {
		  
	  }
  }
  	
  var move_right = function() {
    if ( position == childlength ) return false;
    last_move = 'right';
    position++;
    move_ani('right');
  }
  this.move_right = move_right;
  
  var move_left = function() {
    if ( position == 1 ) {
    	position = 1;
    	return false;
    }
    last_move = 'left';
    position--;
    move_ani('left');
  }        
  this.move_left = move_left;
 
  move_ani = function( direction ) {
  	var ml = $( parentname ).css( 'margin-left' ) == 'auto' ? 0 : $( parentname ).css( 'margin-left' ).replace( /px|pt/, '' );
    ml = parseInt( ml, 10 );
    var newvalue = direction == 'left' ? ml + movevalue : ml - movevalue;
    $( parentname ).animate(
      {
      marginLeft : newvalue
      }, 500
    );
    window.clearInterval( autoscroll );
    set_active_circle();
  }
  __cst_scroller();
  autoscroll = window.setTimeout( auto_move, interval );
};

