  /*************************/
 /*****   FUNCTIONS   *****/
/*************************/

	function $(d){return document.getElementById(d);}

function addToStart(fnc){if(!window.listStart)window.listStart=new Array();window.listStart.push(fnc)}
window.onload=function(){var ls=window.listStart;if(ls){for(iIp=0;iIp<ls.length;iIp++){fnc=ls[iIp];try{if(typeof(fnc)=='function')fnc();else eval(fnc);}catch(e){trace(e);}}}}


  /************************/
 /*****   SCROLLER   *****/
/************************/

	// Scroller function!
	function yo_scroll(id, backwards) {

		if (tsrScroll_on) return;
		tsrScroll_on = 1;

		// Calculate element width to scroll (get first element that has a width)
		var container = $(id);
		if (!container) return;
		for (var i = 0; i < container.childNodes.length; i++) {
			el = container.childNodes[i];
			if (el.nodeType == 1) break;
		}
		var item_width = el.scrollWidth;   // Width of one element, in pixel

		var startPosition = 0;
		var targetPosition = -item_width;
		
		if (backwards) {
			moveElementStack(id, 1);			
			targetPosition = 0;
			var startPosition = -item_width;
		}
		
		container.style.marginLeft = startPosition + 'px';
		
		tsrScroll_anim(container, startPosition, targetPosition, function(){
			container.style.marginLeft = 0;			
			if (!backwards) moveElementStack(id);			
			tsrScroll_on = 0;
		});

		return;
	}
	tsrScroll_on = 0;

	function tsrScroll_anim(obj, startPosition, targetPosition, callBack) {	
		var step = (startPosition - targetPosition) / 4;			
		step = -((step > 0) ? Math.ceil(step) : Math.floor(step));
				
		if (!step) {
			obj.style.marginLeft = targetPosition+'px';
			if (typeof callBack == 'function') callBack();
			return;
		}

		// nav.style.marginBottom = '-'+start+'px';
		obj.style.marginLeft = startPosition+'px';

		window.setTimeout(function(){
			tsrScroll_anim(obj, startPosition+step, targetPosition, callBack);
		},33);
	}


	// Take the first DOM child element and put it at the end, or the other way around if second argument is given
	function moveElementStack(id, backwards) {

		// remove all children from element
		var element = $(id);

		if (!backwards) {
			for (var i = 0; i < element.childNodes.length; i++) {
				el = element.childNodes[i];
				if (el.nodeType == 1) {
					element.removeChild(el);
					element.appendChild(el);
					break;
				}
			}
		}
		else {
			for (var i = element.childNodes.length-1; i > 0; i--) {
				el = element.childNodes[i];
				if (el.nodeType == 1) {
					element.removeChild(el);
					element.insertBefore(el, element.firstChild);
					break;
				}
			}
		}
	}

  /***************/
 /***   SMS   ***/
/***************/

	var smsIndex = 0;
	sms_switching = 0;

	function nextSMS() {

		if (sms_switching) return;
		sms_switching = 1;

		var container = $('all-sms');
		if (!container) return;

		var allElmts = new Array();
		var elDisappear = null;
		var elAppear = null;

		// First, count how many elements
		var j = 0;
		for (var i = 0; i < container.childNodes.length; i++) {
			el = container.childNodes[i];
			if (el.nodeType != 1) continue;
			allElmts[j++] = el;
		}
		var count = allElmts.length;
		var nextIndex = (smsIndex + 1) % count;

		elAppear = allElmts[nextIndex];
		elDisappear = allElmts[smsIndex];
		smsIndex = nextIndex;
		opacityAppear(elDisappear, -0.1, function(){opacityAppear(elAppear, 0.1, function(){sms_switching = 0});});
	}

	addToStart(nextSMS);
	window.setInterval(nextSMS, 8888);

	function opacityAppear(el,dir,callback) {

		if (!dir) dir = 0.1;
		if(typeof el == 'string') el = $(el);
		op = parseFloat(el.style.opacity);
		if (!op || op==1) op = dir > 0 ? 0 : 1;
		op += dir;
		el.style.display = op <= 0 ? 'none' : 'block';
		
		// For IE, play with text color instead of transparency
		if (navigator.userAgent.indexOf('MSIE') > 0) {
			var cc = {
				'0' : 'C','1' : 'B','2' : 'A','3' : '9','4' : '8','5' : '7',
				'6' : '5','7' : '3','8' : '2','9' : '1','10': '0','11': '0'
			};
			var c = Math.round(op*10);
			if (c < 0) c = 0;
			c = cc[c];			
			el.style.color = '#'+c+c+c;
		}		
		el.style.opacity = op;


		if (op >= 1 || op <= 0) {
			if (callback) callback(el);
			return;
		}
		window.setTimeout(function(){opacityAppear(el,dir,callback)},20)
	}

  /************************/
 /***   VIDEO PLAYER   ***/
/************************/

	// small-player-container

	function loadSmallVideo() {

		// Create tsrkit object
		mainPlayer = new tsrkit('mainPlayer');

		// Big player
		if (forumBroadcastId) {
			var playerSuffix = 'large';
			var broadcastId = forumBroadcastId;
			mainPlayer.videoWidth = 480;
			mainPlayer.videoHeight = 270;
		}

		//Small player
		else if (lastBroadcastId) {
			var playerSuffix = 'small';
			var broadcastId = lastBroadcastId;
			mainPlayer.videoWidth = 266;
			mainPlayer.videoHeight = 166;		
		} 
		
		// LOL FAIL PLAYER
		else return; 		

		mainPlayer.videoContainer = playerSuffix+'-player-container';
		mainPlayer.videoTarget = playerSuffix+'_video_target';
		mainPlayer.videoTitleTarget = playerSuffix+'-player-title';
		mainPlayer.autoPlay = false;

		var newRequest = mainPlayer.searchOnSolr({'xobix_broadcast_id':broadcastId});

		newRequest.rTarget = playerSuffix+'-player-playlist-content';
		newRequest.rTemplate = '<li onclick="mainPlayer.playVideo($id);">'
			+ '<span>$title $parsed_duration</span>'
			+ '$img'
			+ '</li>';

		// Show first result (play, but autoPlay is false, so it'll just be an idle player)
		newRequest.rCallback = function(){
			mainPlayer.playVideo(newRequest.firstVideoId);
		}
		newRequest.launch();

	}

	addToStart(loadSmallVideo);


  /***********************/
 /***   MIX & REMIX   ***/
/***********************/

	function switchImg(galleryId,imgFile, str_legend, imgWidth, imgheight){

		var img = document.getElementById('big-'+galleryId);

		opacityAppear(img, -0.1, function(){
			img.src = imgFile;
			if (imgheight) img.height = imgheight;
			if (imgWidth) img.width = imgWidth;
			opacityAppear(img, 0.1);
		});
	}

  /*****************************/
 /***   Library functions   ***/
/*****************************/

	// Add or remove a class to an element, according to whether or not the element has the class already.
	// Will keep other classes the element has
	function switchClass(el_id, class_to_switch) {

		// Get the element
		var el = $(el_id)
		if (!el) return;

		// Get an array with all the classes
		var el_classes = el.className.split(' ');
		var new_classes = [];
		var class_found = 0;

		// Remove the class from the array
		for (var i in el_classes) {
			var el_class = el_classes[i];
			if (el_class == class_to_switch) class_found = 1;
			else new_classes.push(el_class);
		}

		// Id the class has not been found, add it.
		if (!class_found) new_classes.push(class_to_switch);

		// Assich the modified class array to the element
		el.className = new_classes.join(' ');
	}