tabSection = null;
tabHasMouse = false;

function showTab(index) {
	tabSection = index;
	showTabPages(index);
	for (var i = 0; i < tabsData.length; i++) {
		$('tabs_tab_' + i).removeClassName('active');
	}
	$('tabs_tab_' + index).addClassName('active');
}
	
	function showTabPages(source) {
		$$('#tabs_presentation_pages div').each(function (e) {
			$(e).remove();
		});
		$$('#tabs_presentation_pages span').each(function (e) {
			$(e).remove();
		});
		$('tabs_presentation_pages').insert('<div class="pre"></div>');
		for (i = 0; i < tabsData[source]['items'].length; i++) {
			if (i > 0) {
				$('tabs_presentation_pages').insert('<div></div>');
			}
			$('tabs_presentation_pages').insert('<span><a href="javascript:showTabTeaser(' + i + ');">' + (i+1) + '</a></span>');
		}
		$('tabs_presentation_pages').insert('<div class="post"></div>');
		showTabTeaser(0);
	}
	
	function tabTimer() {
		if (!tabHasMouse) {
			showTabNextPage();
		}
		window.setTimeout("tabTimer()", 5000);
	}
	
	function markTabPages(index) {
		i = 1;
		$$('#tabs_presentation_pages span').each(function (e) {
			if ($(e).hasClassName('active')) {
				$(e).removeClassName('active');
			}
			if (i == index) {
				$(e).addClassName('active');
			}
			i++;
		});
	}
	
	function showTabTeaser(index) {
		markTabPages(index+1);
		var source = null;
		source = tabsData[tabSection]['items'][index];
		
		var title  = source["title"];
		var user   = source["author"];
		var button = source["button"];
		var image  = source["image"];
		var link   = source["link"];
		var desc   = source["description"];
	
		$('tabs_presentation_info_title').innerHTML = title;
		$('tabs_presentation_info_desc').innerHTML = desc;
		$('tabs_presentation_info_user').innerHTML = user;
		$('tabs_presentation_image_loader').src = image;
		$('tabs_presentation_info_link').href = link;
		$('tabs_presentation_info_link').innerHTML = button;
		
		Event.observe($('tabs_presentation_image_loader'), 'load', function (event) {
			$('tabs_presentation_image_loader').setStyle({width: 'auto', height: 'auto'});

			var maxWidth  = $('tabs_presentation_image').getWidth();
			var maxHeight = $('tabs_presentation_image').getHeight();
		
			var imgWidth = $('tabs_presentation_image_loader').getWidth();
			var imgHeight = $('tabs_presentation_image_loader').getHeight();
		
			var ratio = maxWidth / imgWidth;
			if (ratio < maxHeight / imgHeight) {
				ratio = maxHeight / imgHeight;
			}
		
			var newWidth = (Math.ceil(ratio * imgWidth));
			var newHeight = (Math.ceil(ratio * imgHeight));
		
			var verticalOffset = Math.floor((newHeight-maxHeight)/2);
			/*
			if (Math.round(Math.random()) == 0) {
		
				$('tabs_presentation_image_source').setStyle({width: newWidth + 'px', height: newHeight + 'px', top: '-' + (verticalOffset*2) + 'px', left: '0px'});
				$('tabs_presentation_image_source').src = $('tabs_presentation_image_loader').src;
			
				tabEffect = new Effect.Move('tabs_presentation_image_source', { x: 0, y: 0, mode: 'absolute', transition: Effect.Transitions.sinoidal, duration: 3.0, fps: 5, afterFinish: function() { window.setTimeout("showTabNextPage()", 2000); } });
			} else {
			
				$('tabs_presentation_image_source').setStyle({width: newWidth + 'px', height: newHeight + 'px', top: '0px', left: '0px'});
				$('tabs_presentation_image_source').src = $('tabs_presentation_image_loader').src;
				
				tabEffect = new Effect.Move('tabs_presentation_image_source', { x: 0, y: -(verticalOffset*2), mode: 'absolute', transition: Effect.Transitions.sinoidal, duration: 3.0, fps: 5, afterFinish: function() { window.setTimeout("showTabNextPage()", 2000); } });
			
			}
			*/
			$('tabs_presentation_image_source').setStyle({width: newWidth + 'px', height: newHeight + 'px', top: '-' + (verticalOffset) + 'px', left: '0px'});
			$('tabs_presentation_image_source').src = $('tabs_presentation_image_loader').src;
		});
	}
	
	function showTabNextPage() {
		var currentIndex = 0;
		var maxIndex = 0;
		
		var i = 0;
		
		$$('#tabs_presentation_pages span').each(function (e) {
			if ($(e).hasClassName('active')) {
				currentIndex = i;
			}
			i++;
		});
		
		maxIndex = i-1;
		
		var nextIndex = currentIndex+1;
		
		if (nextIndex > maxIndex) {
			nextIndex = 0;
		}
		//alert(nextIndex + " " + currentIndex + " " + maxIndex);
		showTabTeaser(nextIndex);
	}

	function initTabs() {
		
		new Ajax.Request('/teaser/teaser.xml', {
			method:'get',
		    onSuccess: function(transport){
		    	var response = transport.responseXML || "no response xml";
		    	
		    	var root = response.firstChild;
		    	
		    	if ((root.childNodes.length === 0)) {
		    		// IE
		    		root = response.documentElement;
		    	}

				var category = null;
				var item = null;
				
				var categories = new Array();
		    	for (var i = 0; i < root.childNodes.length; i++) {
		    		if (root.childNodes[i].nodeType == 1 && root.childNodes[i].nodeName == 'category') {

		    			category = root.childNodes[i];
		    			var categoryData = new Array();
		    			categoryData['title'] = "";
		    			categoryData['items'] = new Array();
		    			for (var j = 0; j < category.childNodes.length; j++) {
		    				if (category.childNodes[j].nodeType == 1 && category.childNodes[j].nodeName == 'title') {
		    					categoryData['title'] = category.childNodes[j].firstChild.nodeValue;
		    				}
		    				if (category.childNodes[j].nodeType == 1 && category.childNodes[j].nodeName == 'item') {
		    					item = category.childNodes[j];
		    					var itemData = new Array();
		    					itemData['title'] = "";
		    					itemData['description'] = "";
		    					itemData['author'] = "";
		    					itemData['link'] = "";
		    					itemData['image'] = "";
		    					itemData['button'] = "";
		    					for (var k = 0; k < item.childNodes.length; k++) {
		    						if (item.childNodes[k].nodeType == 1 && item.childNodes[k].nodeName == 'title') {
		    							itemData['title'] = item.childNodes[k].firstChild.nodeValue;
		    						}
		    						if (item.childNodes[k].nodeType == 1 && item.childNodes[k].nodeName == 'description') {
		    							itemData['description'] = item.childNodes[k].firstChild.nodeValue;
		    						}
		    						if (item.childNodes[k].nodeType == 1 && item.childNodes[k].nodeName == 'author') {
		    							itemData['author'] = item.childNodes[k].firstChild.nodeValue;
		    						}
		    						if (item.childNodes[k].nodeType == 1 && item.childNodes[k].nodeName == 'link') {
		    							itemData['link'] = item.childNodes[k].firstChild.nodeValue;
		    						}
		    						if (item.childNodes[k].nodeType == 1 && item.childNodes[k].nodeName == 'image') {
		    							itemData['image'] = item.childNodes[k].firstChild.nodeValue;
		    						}
		    						if (item.childNodes[k].nodeType == 1 && item.childNodes[k].nodeName == 'button') {
		    							itemData['button'] = item.childNodes[k].firstChild.nodeValue;
		    						}
		    					}
		    					categoryData['items'].push(itemData);
		    				}
		    			}
		    			categories.push(categoryData);
		    		}
		    	}
		    	
		    	tabsData = categories;

		    	for (var i = 0; i < tabsData.length; i++) {
		    		$('tabs_tabs').innerHTML = $('tabs_tabs').innerHTML + '<a href="javascript:showTab(' + i + ');" id="tabs_tab_' + i + '"><span class="pre"></span>' + tabsData[i]['title'] + '<span class="post"></span></a>';
		    	}
		    	
		    	$('tabs_presentation_info').observe('mouseover', function (event) {
		    		tabHasMouse = true;
		    	});
		    	$('tabs_presentation_info').observe('mouseout', function (event) {
		    		tabHasMouse = false;
		    	});
		    	$('tabs_presentation_pages').observe('mouseover', function (event) {
		    		tabHasMouse = true;
		    	});
		    	$('tabs_presentation_pages').observe('mouseout', function (event) {
		    		tabHasMouse = false;
		    	});
		    	$('tabs_presentation_image_source').observe('mouseover', function (event) {
		    		tabHasMouse = true;
		    	});
		    	$('tabs_presentation_image_source').observe('mouseout', function (event) {
		    		tabHasMouse = false;
		    	});
		    	
		    	showTab(0);
		    	window.setTimeout("tabTimer()", 5000);
		    }
		});

	}


document.observe("dom:loaded", function() {
	if ($$(".box_home_tabs").length > 0) {
		initTabs();
	}
});