/**
 * newsScroller v1.0
 * http://www.facebook.com/marco.lestuzzi
 *
 * Copyright 2010, Marco Lestuzzi
 * Dual licensed under the MIT or GPL Version 2 licenses.
 *
 * Date: Thu Aug 06 12:41:45 2010 +0200
 */
 
var newsScroller_blocks = null;
var newsScroller_blocksIndex = 0;
var newsScroller_bh = 57;
var newsScroller_nob = 3;
var newsScroller_speed = 1000;
var newsScroller_wait = 3000;
var newsScroller_movepixel = 1;
var newsScroller_mouseover = false;
var newsScroller_mouseover_is = false;

function newsScrollerCSS() {
	document.writeln('<style type="text/css">');
	document.writeln('.newsBlock { display: none; }');
	document.writeln('</style>');
}

function newsScroller_GetValue(string_value) {
	return string_value.replace(/px/,"") * 1;
}

function newsScroller_GetExtraHeight(block_instance, nocalc_margintop) {
	try{
		var height = getComputedStyle(block_instance, '').getPropertyValue('height');
		var border_top = getComputedStyle(block_instance, '').getPropertyValue('border-top-width');
		var border_bottom = getComputedStyle(block_instance, '').getPropertyValue('border-bottom-width');
		var margin_top = getComputedStyle(block_instance, '').getPropertyValue('margin-top');
		var margin_bottom = getComputedStyle(block_instance, '').getPropertyValue('margin-bottom');
		var padding_top = getComputedStyle(block_instance, '').getPropertyValue('padding-top');
		var padding_bottom = getComputedStyle(block_instance, '').getPropertyValue('padding-bottom');
	} catch(e){
		var height = block_instance.currentStyle.height;
		var border_top = block_instance.currentStyle.borderTopWidth;
		var border_bottom = block_instance.currentStyle.borderBottomWidth;
		var margin_top = block_instance.currentStyle.marginTop;
		var margin_bottom = block_instance.currentStyle.marginBottom;
		var padding_top = block_instance.currentStyle.paddingTop;
		var padding_bottom = block_instance.currentStyle.paddingBottom;
	}
	height = newsScroller_GetValue(height);
	border_top = newsScroller_GetValue(border_top);
	border_bottom = newsScroller_GetValue(border_bottom);
	margin_top = newsScroller_GetValue(margin_top);
	margin_bottom = newsScroller_GetValue(margin_bottom);
	padding_top = newsScroller_GetValue(padding_top);
	padding_bottom = newsScroller_GetValue(padding_bottom);
	
	if (isNaN(height)) height = newsScroller_bh;
	if (isNaN(border_top)) border_top = 0;
	if (isNaN(border_bottom)) border_bottom = 0;
	if (isNaN(margin_top)) margin_top = 0;
	if (isNaN(margin_bottom)) margin_bottom = 0;
	if (isNaN(padding_top)) padding_top = 0;
	if (isNaN(padding_bottom)) padding_bottom = 0;
	
	block_instance.style.height = height+"px";
	block_instance.style.borderTopWidth = border_top+"px";
	block_instance.style.borderBottomWidth = border_bottom+"px";
	block_instance.style.marginTop = margin_top+"px";
	block_instance.style.marginBottom = margin_bottom+"px";
	block_instance.style.paddingTop = padding_top+"px";
	block_instance.style.paddingBottom = padding_bottom+"px";
	
	if (nocalc_margintop) {
		return height+border_top+border_bottom+margin_bottom+padding_top+padding_bottom;
	} else {
		return height+border_top+border_bottom+margin_top+margin_bottom+padding_top+padding_bottom;
	}
}

function newsScrollerWait(is_first) {
	if (is_first == 0) {
		var block = newsScroller_blocks[newsScroller_blocksIndex];

		var actual_top = (block.style.top.replace(/px/,"") * 1);
		var move_of = 0;
		for (var i = 0; i < newsScroller_blocks.length; i++) {
			move_of += newsScroller_GetExtraHeight(newsScroller_blocks[i], false);
		}
		
		block.style.top = (actual_top + move_of)+"px";

		newsScroller_blocksIndex = ((newsScroller_blocksIndex+1) % newsScroller_blocks.length)
	}
	setTimeout("newsScrollerMove(0)", newsScroller_wait);
}

function newsScrollerMove(moved) {
	if ((newsScroller_mouseover)&&(newsScroller_mouseover_is)) {
		if (moved == 0) {
			setTimeout("newsScrollerMove(0)", 300);
		} else {
			setTimeout("newsScrollerMove("+moved+")", 300);
		}
	} else {
	
		var timeout = Math.floor(newsScroller_speed / (newsScroller_bh / (newsScroller_movepixel * 1.0)));
		var height_of_block = newsScroller_GetExtraHeight(newsScroller_blocks[newsScroller_blocksIndex], false);

		if ((moved + newsScroller_movepixel) >= height_of_block) {
			var difference = height_of_block - moved;
			moved = 0;
		} else {
			moved = moved + newsScroller_movepixel;
			var difference = newsScroller_movepixel;
			if (moved >= height_of_block) moved = 0;
		}
		
		for (var i = 0; i < newsScroller_blocks.length; i++) {
			var block = newsScroller_blocks[i];
			block.style.top = ((block.style.top.replace(/px/,"") * 1) - difference)+"px";
		}
		
		if (moved == 0) {
			newsScrollerWait(0);
		} else {
			setTimeout("newsScrollerMove("+moved+")", timeout);
		}
	}
}

// MAIN FUNCTION!
//
// width = width of "newsContainer"
// block_height = height of "newsBlock";
// number_of_blocks = number of "newsBlock" visible in "newsContainer"
// speed = how milliseconds to complete a move
// wait = how milliseconds to wait before the next move
// mouse_over = pause when the mouse is over
function newsScroller(width, block_height, number_of_blocks, speed, wait, mouse_over) {
	if ((width * 1)+"" != width) return false;
	if ((block_height * 1)+"" != block_height) return false;
	if ((number_of_blocks * 1)+"" != number_of_blocks) return false;
	if ((speed * 1)+"" != speed) return false;
	if ((wait * 1)+"" != wait) return false;
	
	if (width < 1) return 0;
	if (block_height < 1) return 0;
	if (number_of_blocks < 1) return 0;
	if (speed < 1) return 0;
	if (wait < 1) return 0;

	if (mouse_over) newsScroller_mouseover = true;
	else newsScroller_mouseover = true;
	
	newsScroller_bh = block_height;
	newsScroller_nob = number_of_blocks;
	newsScroller_speed = speed;
	newsScroller_wait = wait;

	newsScroller_blocks = new Array();
	var newsContainer = document.getElementById("newsContainer");
	if (newsContainer) {
		newsContainer.onmouseover = function() { newsScroller_mouseover_is = true; }
		newsContainer.onmouseout = function() { newsScroller_mouseover_is = false; }
			
		newsContainer.style.width = width+"px";
		newsContainer.style.overflow = "hidden";

		var tags = newsContainer.getElementsByTagName("div");
		for (var i = 0; i < tags.length; i++) {
			if (tags[i].className.search(/\bnewsBlock\b/) >= 0) {
				tags[i].style.width="auto";
				tags[i].style.height=block_height+"px";
				tags[i].style.position="relative";
				newsScroller_blocks.push(tags[i]);
			}
		}
		var height_of_all = 0;
		for (var i = 0; i < number_of_blocks; i++) {
			if (i < newsScroller_blocks.length) {
				height_of_all += newsScroller_GetExtraHeight(newsScroller_blocks[i], false);
			}
		}
		
		newsContainer.style.height = height_of_all+"px";
		for (var i = 0; i < newsScroller_blocks.length; i++) {
			newsScroller_blocks[i].style.display = "block";
		}
		if (newsScroller_blocks.length > number_of_blocks) {
			newsScrollerWait(1);
		}
	}
}

