YAHOO.namespace('hhk.view');

YAHOO.hhk.view.Slider = (function(aId, aImages, aConf, aImageConf) {
			var id = aId;
		    var images = aImages;
			var conf = aConf;
			var imageConf = aImageConf;
		
		    var slideInterval = null;
		    var slideTimeout = null;

		    var activeInterval = null;
		    
		    var carouselInstance = null;
		    
			
			if(images.length > conf['itemsPerPage']) {
				var scrollAmount = parseInt(conf['itemsPerPage'], 10);

				$('#' + id).jcarousel({
				wrap: 'circular',
				scroll: scrollAmount,
				initCallback: function(carousel, state) {
					carouselInstance = carousel;
				},			
				itemVisibleInCallback: {onBeforeAnimation: function(carousel, item, i, state, evt) {
					var idx = carousel.index(i, images.length);
					var imgTag = images[idx - 1];
					carousel.add(i, imgTag);
					// Koennte auch kuerzer sein - IE 7 bugfix, wenn der DOM zu frueh manipuliert wird,
					// ist das Bild noch nicht eingefuegt und der titletag wird hinzugefuegt - FATAL Error im IE...
					YAHOO.lang.later(100, null, function() {
					YAHOO.hhk.Tooltips.reAttach();
					}); 
				}},
				itemVisibleOutCallback: {onAfterAnimation: function(carousel, item, i, state, evt) {
					carousel.remove(i);
				}}
				});
					
				$('#' + id + '-wrap').mouseover(function() {
				    stopSliding();
				    cancelActiveInterval();
				    $(this).addClass('active');
				});
				
				$('#' + id + '-wrap').mouseout(function() {
				    initSliding();
				    //slideTimeout = YAHOO.lang.later(250, null, scrollNow);
				    
				    cancelActiveInterval();
				    
				    var self = this;
				    activeInterval = YAHOO.lang.later(100, null, function() {
					    $(self).removeClass('active');
				    });
				});
			}
			else {
				insertImagesAsList();
			}
		
			function insertImagesAsList() {
				for(var i = 0; i < conf['itemsPerPage']; i++) {
					var currentImage = images[i];
					var cssClass = (i == conf['itemsPerPage'] - 1) ? 'last' : '';
					var cssWidthHeight = 'width:' + imageConf.width + 'px; height:' + imageConf.height + 'px;';

					if(currentImage == null) {
						$('#' + id + '-wrap').append('<div style="' + cssWidthHeight + '" class="staticItem placeholder ' + cssClass + '"></div>');
					}
					else {
						$('#' + id + '-wrap').append('<div class="staticItem image ' + cssClass + '">' + currentImage + '</div>');
					}
				}
			}
			
		    
		    function cancelActiveInterval() {
			if(activeInterval != null) {
			    activeInterval.cancel();
			}
		    }
		    
		    function stopSliding() {
			if(slideInterval != null) {
			    slideInterval.cancel();
			}
			
			slideInterval = null;
		    
			if(slideTimeout != null) {
			    slideTimeout.cancel();
			}
			
			slideTimeout = null; 
		    }
		    
		    function scrollNow() {
			carouselInstance[conf['go']]();
		    }

		    function initSliding() {
			slideInterval = YAHOO.lang.later(5000, null, scrollNow, null, true);
		    }	
	
	
	return {
		initSliding: initSliding
	};	
});