function appendLink(lnk) {
	var paramExists = jQuery(lnk).attr('href').match(/\?/);
	if (paramExists) {
		jQuery(lnk).attr('href', jQuery(lnk).attr('href') + "&c=" + currentPage);
	} else {
  	jQuery(lnk).attr('href', jQuery(lnk).attr('href') + "?c=" + currentPage);
  }
}

(function () {
  
    jQuery.fn.produktSlider = function () {
      
        function repeat(str, n) {
            return new Array( n + 1 ).join(str);
        }
        
        return this.each(function () {
            
            var $wrapper = jQuery('> div', this).css({overflow: 'hidden'}),
                $slider = $wrapper.find('> ul').width(9999),
                $items = $slider.find('> li'),
                $itemLinks = $items.find('> a'),
                $single = $items.filter(':first')
                
                pages = $items.length;
                currentPage = 1;
                
                if (jQuery('> span#items_width', this).html() != null) {
                  singleWidth = parseInt(jQuery('> span#items_width', this).html());
                } else {
                  singleWidth = $single.outerWidth();
                }
                
                //visible = (jQuery('> span#items_visible', this).html() != null) ? parseInt(jQuery('> span#items_visible', this).html()) : 3;
                //autodir = (jQuery('> span#init_scrolldir', this).html() != null) ? parseInt(jQuery('> span#init_scrolldir', this).html()) : 1;
                //startoffset_set = (jQuery('> span#start_offset', this).html() != null) ? parseInt(jQuery('> span#start_offset', this).html()) : 1;
                
                visible = tx_ps_itemsvisible;
                autodir = tx_ps_initscrolldir;
                startoffset_set = tx_ps_startoffset;
            
            $wrapper.width(visible * singleWidth);
            jQuery(this).width(
              parseInt($wrapper.css("margin-left").replace("px",""))
              +
              parseInt($wrapper.css("margin-left").replace("px",""))
              +
              $wrapper.innerWidth()
            );
            
            // create the carousel padding on left and right (cloned)
            $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
            $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
            $items = $slider.find('> li');
            
            $itemLinks = $items.find('> a');
            
            /*            
            $itemLinks.hover(function() {
                jQuery(this).find('> img').stop().animate({
                        //opacity: 0.5
                        //borderWidth: 5
                        
                    }, 250);
              }, function() {
                jQuery(this).find('> img').stop().animate({
                        //opacity: 1
                        //borderWidth: 0
                    }, 250);
            });
            */
            
            
            $itemLinks.hover(function() {
                jQuery(this).parent().addClass('hoverstyle');
              }, function() {
                jQuery(this).parent().removeClass('hoverstyle');
            });
            
            
            currentPage = startoffset_set;
            
            // reset scroll
            $wrapper.scrollLeft(singleWidth * (visible + currentPage - 1));
            
            try {
            	DD_belatedPNG.fix('img, div, ul, #flowpanes h2, .produktSlider, .produktSlider img, .arrow');
            	//DD_belatedPNG.fix('.produktSlider, .produktSlider img, .arrow');
            } catch(r) {}
            
            // paging function
            function gotoPage(page) {
                              
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir;
                
                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft : '+=' + left
                }, 500, function () {
                    // if page == last page - then reset position
                    
                    if (page > pages) {
                        $wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                      //console.log("NULL! " + pages);
                        page = pages;
                        
                        $wrapper.scrollLeft(singleWidth * (visible + pages - 1));
                        
                    }
                    
                    currentPage = page;
                    
                    //document.cookie = "slider_cp="+currentPage;
                    //console.log("slider_cp: "+currentPage);
                    
                });
                
            }
            
            if (pages > visible) {
              
              // insert the back and forward link
              $wrapper.after('<a href="#" class="arrow back">BACK</a><a href="#" class="arrow forward">NEXT</a>');
              //$wrapper.after('<a href="#" class="gstop">'+(($generalscrolling) ? "STOP" : "PLAY")+'</a>');
              
              // bind the back and forward links
              jQuery('a.back', this).click(function () {
                  autodir = -1;
                  jQuery('.produktSlider').trigger('next');
                  return false;
              });
              
              jQuery('a.forward', this).click(function () {
                  autodir = 1;
                  jQuery('.produktSlider').trigger('next');
                  return false;
              });
            
            }
            
            jQuery('a.gstop', this).click(function () {
                if ($generalscrolling) {
                  $generalscrolling = false;
                  jQuery(this).text("PLAY");
                } else {
                  $generalscrolling = true;
                  jQuery(this).text("STOP");
                  jQuery('.produktSlider').trigger('next');
                }
                jQuery(this).blur();
                return false;
            });
            
            jQuery(this).bind('goto', function (event, page) {
                gotoPage(page);
            });
            
            jQuery(this).bind('next', function () {
                gotoPage(currentPage + autodir);
            });
            
            
        });
    };
})(jQuery);

jQuery(document).ready(function () {
    
    if (tx_ps_initscrolldir == 0) {
      $generalscrolling = false;
    } else {
      $generalscrolling = true;
    }
    var autoscrolling = true;
    
    jQuery('.produktSlider').produktSlider().mouseover(function () {
        autoscrolling = false;
    }).mouseout(function () {
      if ($generalscrolling) {
        setTimeout(function() {
          autoscrolling = true;
        }, 2000);
      }
    });
    
    
    dasInterval = setInterval(function () {
        if (autoscrolling && $generalscrolling) {
            jQuery('.produktSlider').trigger('next');
        }
    }, 2000);
    

});