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

// Spinner
var $count = 0;
var $spinning = false;
var $spinner;

function toogleSpinner() {
	$('spinner-wrapper').toggle();
}

function rotate() {
	var elem = $('spinner');
	elem.style.MozTransform = 'scale(0.2) rotate(' + $count + 'deg)';
	elem.style.WebkitTransform = 'scale(0.2) rotate(' + $count + 'deg)';
	if ($count == 360) {
		$count = 0;
	}
	$count += 45;
	$spinner = window.setTimeout(rotate, 100);
}


function startSpinner() {
	if (!$spinning) {
		$spinner = window.setTimeout(rotate, 100);
		$('spinner-wrapper').show();
		$spinning = true;
	}
}

function stopSpinner() {
	clearTimeout($spinner);
	$('spinner-wrapper').hide();
	$spinning = false;
}

//Loading notifications
var MessageList = function() {
	this.messages = $A();
	this.size = 0;
	this.lock = false;
	
	this.add = function($message) {
		this.messages[this.size] = $message;
		
		this.size++;
		this.sort();
		
		return this;
	};
	
	this.remove = function($id) {
		var $found = false;
		var $new = $A();
		var $i = 0;

		this.messages.each(function(item) {
			if (item.id != $id || $found) {
				$new[$i++] = item;
			} else {
				$found = true;
			}
		});
		
		if ($found) {
			this.size--;
			this.messages = $new;
		}
		
		return this;
	};
	
	this.removeAll = function($id) {
		var $found = false;
		var $new = $A();
		var $i = 0;

		this.messages.each(function(item) {
			if (item.id != $id) {
				$new[$i++] = item;
			} else {
				$found = true;
			}
		});
		
		if ($found) {
			this.size--;
			this.messages = $new;
		}
		
		return this;
	};
	
	this.peek = function() {
		if (this.size > 0) {
			return this.messages[0];
		} else {
			return false;
		}
	};
	
	this.pop = function() {
		var $item = this.peek();
		if ($item) {
			this.remove($item.id);
		}
		return $item;
	};
	
	this.sort = function() {
		for (var $i = 1; $i <= this.size-1; $i++) {
			var $value = this.messages[$i];
			var $j = $i - 1;
			var $done = false;
			
			do {
				if (this.messages[$j].priority > $value.priority) {
					this.messages[$j + 1] = this.messages[$j];
					$j = $j - 1;
					if ($j < 0){
						$done = true;
					}
				} else {
					$done = true;
				}
			} while(!$done);
			this.messages[$j + 1] = $value;
		}
		
		return this;
	};
	
	return this;
};

var Message = function($id, $message) {
	this.id = $id;
	this.content = $message;
	this.priority = 1;
	this.type = 'info';
	
	return this;
};

var $activeNotifications = new MessageList();

function changeNotification($text) {
	if ($activeNotifications.size > 1) {
		$text = $text + " (and " + ($activeNotifications.size - 1) + " more)";
	}

	$('notifications').update($text);
}

function toggleNotification() {
	$('notifications').toggle();
}

function showNotification() {
	$('notifications').show();
}

function hideNotification(){
	$('notifications').hide();
}

function addNotification($message) {
	$activeNotifications.add($message);
	
	var $topNotification = $activeNotifications.peek();

	changeNotification($topNotification.content);

	if (!$spinning) {
		showNotification();
		startSpinner();
	}
}

function removeNotification($id, $all) {
	if ($all) {
		$activeNotifications.removeAll($id);
	} else {
		$activeNotifications.remove($id);
	}

	var $last = false;
	if ($activeNotifications.size === 0) {
		hideNotification();
		stopSpinner();
		$last = true;
	} else {
		var $newNotification = $activeNotifications.peek();
		changeNotification($newNotification.content);
	}
	
	Event.fire(window, 'tvwall:notificationEnd', {
		id : $id,
		all : $all,
		last : $last
	});
}

/* Popup Messages */
var $activePopups = new MessageList();
var $popupsTime = 3000; // 3 seg

function addPopup($message) {
	$activePopups.add($message);

	var $popups =$$('.popup');
	if ($popups.size() === 0) {
		showPopup();
	}
}

function removePopup() {
	var $popup = $$('.popup').first();

	var effect = Effect.Fade($popup, {
		duration : 1.0,
		queue : {
			position : 'end',
			scope : 'popups'
		},
		delay : 4.0,
		afterFinish : function(effect) {
			effect.element.remove();

			if ($activePopups.size > 0) {
				showPopup();
			}
		}
	});
}

function showPopup() {
	var $message = $activePopups.pop();

	var $divID = "popup-" + $message.type;

	var $popup = new Element('div', {
		'class' : 'popup',
		'id' : $divID
	}).update('<h1>' + $message.type.camelize() + '</h1><p>' + $message.content + '</p>').hide();
	$('global').insert( {
		after : $popup
	});

	Effect.Appear($divID, {
		duration : 0.5,
		queue : {
			position : 'end',
			scope : 'popups'
		}
	});

	setTimeout(removePopup, $popupsTime);
}


