var scrollSize = 50;		// width for each element (assuming every element has the same width)
var noElements = 5;		// number of elements
var curElement = 1;
var scrollRunning = false;
var oldOpacity = Array();
var fadeRunning = Array();
var transLock = false;
var activeEpId = 0;
var url = "/dot/";
var justCameIn = true;

// DIGG STUFF
digg_media = 'video';
digg_topic = 'gaming_news';
digg_skin = 'compact';
digg_window = 'new';

function scroll(way) {
	justCameIn = false;
	
	if (!scrollRunning) {
	
		var goMax, goMin;
		
		if (way == 'plus')
			curElement++;
		else if (way == 'min')
			curElement--;
			
		if (curElement > noElements) {
			curElement = 1;
			goMax = true;
		}
			
		if (curElement < 1) {
			curElement = noElements;
			goMin = true;
		}
			
		
		if (way == 'plus') {
			var new_start = (curElement-2)*scrollSize;
			var new_end = (curElement-1)*scrollSize;
		}
		else if (way == 'min') {
			var new_start = (curElement)*scrollSize;
			var new_end = (curElement-1)*scrollSize;		
		}
	
		if (goMin) new_start = 0;
		else if (goMax) new_start = (noElements-1)*scrollSize;
		
		scrollStart(document.getElementById('scroller'), new_start, new_end);
		fadeScroll(curElement);
	}
}


var scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function scrollStart(elem, start, end, direction) {

	if (scrollanim.timer != null) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	scrollanim.time = 0;
	scrollanim.begin = start;
	scrollanim.change = end - start;
	scrollanim.duration = 25;
	scrollanim.element = elem;
	scrollRunning = true;
	
	if (direction == "horiz") {
		scrollanim.timer = setInterval("scrollHorizAnim();", 15);
	}
	else {
		scrollanim.timer = setInterval("scrollVertAnim();", 15);
	}
}

function scrollVertAnim() {
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
		scrollRunning = false;
	}
	else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollTop = move; 
		scrollanim.time++;
	}
}

function scrollHorizAnim() {
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
		scrollRunning = false;
	}
	else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollLeft = move;
		scrollanim.time++;
	}
}

function linear(t, b, c, d) {
	return c*t/d + b;
}

function sineInOut(t, b, c, d) {
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}

function fixScrollPos() {
	$('scroller').scrollTop = (curElement-1)*scrollSize;
}

function itemOver(obj) {
	if (!fadeRunning[obj.id]) {
		obj.style.backgroundColor = "#b0b0b0";
		oldOpacity[obj.id] = obj.getOpacity();
		fadeRunning[obj.id] = true;
		
		new Effect.Opacity(obj.id, { from: obj.getOpacity(), to: 1.0, duration: 0.25 });
		
		setTimeout("unsetFadeRunning('" + obj.id + "')",250);
	}
	else {
	}
}

function itemOut(obj) {
	if (fadeRunning[obj.id]) {
		setTimeout(function() { itemOut(obj); },250);
	}
	else {
		obj.style.backgroundColor = "#c0c0c0";
		new Effect.Opacity(obj.id, { from: obj.getOpacity(), to: oldOpacity[obj.id], duration: 0.25 });
	}
}

function unsetFadeRunning(id) {
	fadeRunning[id] = false;
}

function fadeScroll(from) {
	new Effect.Opacity('sc_' + from, { from: $('sc_' + from).getOpacity(), to: 1.0, duration: 0.25 });
	new Effect.Opacity('sc_' + (from+1), { from: $('sc_' + (from+1)).getOpacity(), to: 0.8, duration: 0.25 });
	new Effect.Opacity('sc_' + (from+2), { from: $('sc_' + (from+2)).getOpacity(), to: 0.6, duration: 0.25 });
	new Effect.Opacity('sc_' + (from+3), { from: $('sc_' + (from+3)).getOpacity(), to: 0.4, duration: 0.25 });
}

function showEp(id) {
	if (!transLock) {
		justCameIn = false;
		transLock = true;
		getEp(id);
		
		$('movie_info').style.zIndex = 1;
		$('selection').style.zIndex = 2;
		$('movie_info').toggle();
		$('movie_info').setOpacity(1.0);
		$('arrow_left').setOpacity(0.0);
		$('arrow_left').toggle();
		new Effect.Opacity('arrow_left', { from: 0.0, to: 1.0, duration: 1.00 });
		new Effect.Opacity('selection', { from: 1.0, to: 0.0, duration: 1.00 });
		
		setTimeout(function() { $('selection').toggle(); transLock = false; }, 1000);
	}
}

function goBack() {
	if (!transLock) {
		transLock = true;
		
		$('movie_info').style.zIndex = 2;
		$('selection').style.zIndex = 1;
		$('selection').toggle();
		$('selection').setOpacity(1.0);
		new Effect.Opacity('arrow_left', { from: 1.0, to: 0.0, duration: 1.00 });
		new Effect.Opacity('movie_info', { from: 1.0, to: 0.0, duration: 1.00 });	
		
		setTimeout(function() { $('movie_info').toggle(); $('arrow_left').toggle(); transLock = false; }, 1000);
	}
}

function getEp(id) {
	var dest = url + "episode/getmovie/" + id;
	
	new Ajax.Request(dest, {
	  method: 'get',
	  onSuccess: function(transport) {
	  		var text = transport.responseText.split("||");
	  		$('movie_info').innerHTML = text[0];
	  		if (id != activeEpId) {
	  			activeEpId = id;
	  			setTimeout(function() {
	  				$('video').innerHTML = getMovie(text[1], text[4]);
	  			}, 1000);
	  			
	  			//sendToActionScript("videoplayer/1/videocanvas/1","playFlv(" + text[1] + ")")
	  		}
	  		$('title_img').src = url + 'title/cache/' + text[2] + ".png";
	  		document.title = ".astute - " + text[3];
	  		
	  		//digg_url = 'http://cc-tv.nl/episode/' + id + '/';
	  		//digg_title = 'CC-TV episode ' + text[3];
	  		
	  		//doDigg();
	  }
	});
}

function epOver() {
	if (justCameIn) {
		justCameIn = false;
		showEp(activeEpId);
	}
}

function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
	 return window[movieName];
	} else {
	 return document[movieName];
	}
}

function sendToActionScript(value1,value2) {
	try { 
		thisMovie("customVideoPlayer").sendToActionScript(value1,value2);
	} catch(error) {
		alert (error)
	}
		 
}

function emptyEmail(obj) {
	if (obj.value == "email@address.com")
		obj.value = "";
}

function subscribe() {
	new Ajax.Request(url + 'subscribe/', {
  		parameters: $('subscribe_form').serialize(true),
  		onSuccess:function(transport) {
  			alert(transport.responseText);
  			if (transport.responseText == "You now have subscribed for the CC-TV e-mail update service!")
  				$('subscribe_form').email.value = '';
  		}
  	});

}

function submit_form (form) {
	form.encoded_pw.value = calcMD5(form.password.value);
	form.password.value = "";
				
	return true;
}

function submit_form_reg (form) {
	form.encoded_pw1.value = calcMD5(form.password1.value);
	form.password1.value = "";
		
	form.encoded_pw2.value = calcMD5(form.password2.value);
	form.password2.value = "";
				
	return true;
}

var activeBlog, activeBlogRM;

function getBlog(id) {
	
	var t;
	
	if (!activeBlog) {
		var first = true;
		t = 10;
	}
	else {
		t = 1500;
	}
	
	if ((!activeBlog) || (activeBlog.id != ("blog_" + id))) {
		
		var dest = url + 'blog/getentry/' + id;
		
		new Ajax.Request(dest, {
		  method: 'get',
		  onSuccess: function(transport) {
		  		//if (!first) new Effect.BlindUp(activeBlog.id, { queue: 'end' });
		  		
		  		//setTimeout(function() { 
		  			if (!first) activeBlog.style.display = 'none';
		  		
			  		if (activeBlogRM) {
			  			activeBlogRM.style.display = 'block';
			  		}
			  		activeBlog = $('blog_' + id);
			  		activeBlogRM = $('read_more_' + id);
			  		activeBlog.innerHTML = transport.responseText;
			  		new Effect.BlindDown('blog_' + id, { queue: 'end' });
			  		activeBlog.style.display = 'block';
			  		activeBlogRM.style.display = 'none';
		  		
		  		//}, t);
		  }
		});	
		
	}
	else {
		void(0);
	}
}

function selectAllCheckbox(container) {
	var el = container.getElementsByTagName('INPUT');
	
	for (var i = 0; i < el.length; i++) {
		el[i].checked = !el[i].checked;
	}
}