/**
 * @project TV Wall
 * @version 2010.06
 * @author Luís Nabais
 * @copyright 2010 by the author
 * @license CC-BY-SA
 */

// Scrollbar
var $scrollbar;

function moveScrollBar(value) {
	var $contents = $('content');
	var $absoluteScrollValue = getContentSize() - (document.viewport.getWidth() * 0.96);
	var $scrollValue = value * $absoluteScrollValue / 100;
	$contents.scrollLeft = Math.abs($scrollValue);
}

function getScrollBarPosition() {
	var $contents = $('content');

	if ($contents.scrollLeft <= 0) {
		return 0;
	}

	var $absoluteScrollValue = getContentSize() - (document.viewport.getWidth() * 0.96);
	var $value = 100 * $contents.scrollLeft / $absoluteScrollValue;

	return Math.abs($value);
}

function addScrollBar() {
	$scrollbar = new Control.Slider('scrollbar-handle', 'scrollbar', {
		range : $R(0, 100),
		sliderValue : getScrollBarPosition(),
		onSlide : moveScrollBar,
		onChange : moveScrollBar,
		axis : 'horizontal'
	});
	
	if (getContentSize() < document.viewport.getWidth()) {
		$('scrollbar').hide();
	}
}

function updateScrollBar() {
	if (getContentSize() >= document.viewport.getWidth()) {
		$('scrollbar').show();
	} else {
		$('scrollbar').hide();
	}
	$scrollbar.setValue(getScrollBarPosition());
}

function onScrollWheelMove(event) {
	var $speed = 2; // 2x speed
    var $delta = Event.wheel(event);
    $scrollbar.setValue(getScrollBarPosition() - ($delta * $speed));
}

// Forms
function formOnFocus(event) {
	var $defvalue = this.readAttribute('placeholder');
	if (this.value == $defvalue) {
		this.value = "";
		this.isPlaceholder = false;
	}
	
	this.hasFocus = true;
}

function formOnBlur(event) {
	var $defvalue = this.readAttribute('placeholder');
	if (this.value == "") {
		this.isPlaceholder = true;
		this.value = $defvalue;
	}
	
	this.hasFocus = false;
}

function showAddForm() {
	if (!$('add-show').visible()) {
		Effect.SlideDown('add-show');
	
		clearSearchResults();
		
		var $input = $('show-name');
		$input.value = $input.readAttribute('placeholder');
		$input.isPlaceholder = true;
		
		window.location.hash = "#/add-show";
	}
}

function hideAddForm() {
	$('show-name').blur();
	
	if ($('add-show').visible()) {
		Effect.SlideUp('add-show');
	}
	
	window.location.hash = "#";
}

// Sizing
function resizeFooter(event) {
	var $footer = $('footer');
	var $content = $('content');
	var $height = document.viewport.getHeight() - $content.viewportOffset()['top'] - $content.getHeight() - 12;
	if ($height <= 0) {
		$height = 1;
	}

	$footer.setStyle( {
		'height' : $height + 'px'
	});
}

function updateBoxesSize() {
	var $boxes = 0;
	var $buffer = 10;

	$$('#content .box').each(function(item) {
		if (item.empty()) {
			item.remove();
		} else {
			var $children = item.childElements();
			var $childrenWidth = 0;
			$children.each(function(i) {
				$childrenWidth = $childrenWidth + i.getWidth() + 
									parseInt(i.getStyle('margin-right').match(/\d+/)) + $buffer;
			});
			
			if (($childrenWidth) < item.getWidth()) {
				item.setStyle( {
					'width' : $childrenWidth + 'px'
				});
			} else {
				item.setStyle( {
					'width' : ''
				});
			}
			$boxes++;
		}
	});

	return $boxes;
}

function getContentSize() {
	var $boxes_width = 0;
	$$('#content .box').each(function(item) {
		$boxes_width += item.getWidth();
	});

	return $boxes_width;
}

function resizeContent() {
	var $content = $('contents-inner');
	var $boxes = updateBoxesSize();
	var $boxes_width = getContentSize();

	$boxes_width += ($boxes * 10) - 10;
	$content.setStyle( {
		'width' : $boxes_width + 'px'
	});
}

// TODO: Scrolling
// $('show-id').observe('mouseover', startScrolling).observe('mouseout', stopScrolling);

function scrollBack(effect) {
	if (effect.element.scrolling) {
		effect.element.scroller = new Effect.Morph(effect.element.id, {
			style : 'margin-left: 0px;',
			duration : 3,
			afterFinish : scroll
		});
	}
}

function scroll(effect) {
	if (effect.element.scrolling) {
		var $w = effect.element.getWidth() - effect.element.getStyle('margin-right').match(/\d+/);
		var $v = effect.element.up('.meta').getWidth();
	
		effect.element.scroller = new Effect.Morph(effect.element.identify(), {
			style : 'margin-left: -' + ($v - $w) + 'px;',
			duration : 3,
			afterFinish : scrollBack
		});
	}
}

function resetScrolling(item) {
	item.scrolling = false;
	if (item.scroller) {
		item.scroller.cancel();
	}
}

function startScrolling(event) {
	var $element = this.down('h1 a');
	var $w = $element.getWidth();
	var $v = $element.up('.meta').getWidth();
	
	resetScrolling($element);
	$element.scrolling = true;

	$element.scroller = new Effect.Morph($element.identify(), {
		style : 'margin-left: -' + ($v - $w) + 'px;',
		duration : 3,
		afterFinish : scrollBack
	});
	
	var $description = this.down('.description a');
	if ($description.visible()) {
		var $x = $description.getWidth() - $description.getStyle('margin-right').match(/\d+/);
		var $y = $description.up('.meta').getWidth();
		
		resetScrolling($description);
		$description.scrolling = true;
	
		$description.scroller = new Effect.Morph($description.identify(), {
			style : 'margin-left: -' + ($y - $x) + 'px;',
			duration : 3,
			afterFinish : scrollBack
		});
	}
}

function stopScrolling(event) {
	var $element = this.down('h1 a');
	resetScrolling($element);
	$element.scroller = new Effect.Morph($element.identify(), {
		style : 'margin-left: 0px;',
		duration : 0.5
	});
	
	var $description = this.down('.description a');
	if ($description.visible()) {
		resetScrolling($description);
		$description.scroller = new Effect.Morph($description.identify(), {
			style : 'margin-left: 0px;',
			duration : 0.5
		});
	}
}

// Other
function inputDisable() {
	$('modal-inner').hide();
	$('modal-wrapper').show();
}

function inputEnable() {
	if (!$('modal-inner').visible()) {
		$('modal-wrapper').hide();
	}
}

