YAHOO.namespace('hhk.view.menuItemSwitch');

YAHOO.hhk.view.menuItemSwitch.Job = (function(aContainer, aContent, aIndex) {
    var container = aContainer;
    var content = aContent;
    var index = aIndex;
    
    function start(delayed) {
	if(delayed) {
	    setTimeout(function() {
			$(container).html(content);
			
			// 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();
			});			
	    }, (index + 1) * 1000);
	}
	else {
	    $(container).html(content);
	}
    }
    
    return {
	start: start
    };
});

YAHOO.hhk.view.MenuItemSwitch = (function(aId, aMatrix, aSlotCount, aImageConf) {
    var id = aId;
    var matrix = aMatrix;
    var slotCount = aSlotCount;
    var imageConf = aImageConf;
    
    var current = 0;

    function start() {
	insertNewRow(false);
	
	setInterval(function() {
	    insertNewRow(true);
	}, 5000);
    }
    
    function insertNewRow(delayed) {
	for(var i = 0; i < slotCount; i++) {
	    var slotContent = matrix[current][i];
	    
	    if(!slotContent) {
		var cssWidthHeight = 'width:' + imageConf.width + 'px; height:' + imageConf.height + 'px;';
		slotContent = '<div style="' + cssWidthHeight + '" class="placeholder"></div>';
	    }
	    
	    var j = new YAHOO.hhk.view.menuItemSwitch.Job($('#' + id + ' .slot-0' + (i + 1)), slotContent, i);
	    j.start(delayed);
	}				
	
	current++;

	if(current > matrix.length - 1) {
	    current = 0;
	}
    }
    
    return {
	start: start
    };
});