// JavaScript Document

var y = 0;
var start_speed = 8;
var speed = start_speed;
var rate = 30;
var width = 360;
var max_width = 94;
var stop_slide = false;

function stop_slider () {
	stop_slide = true;
	speed = start_speed;
}

function slide_images (rr) {
	
	if(stop_slide == true) {
		return false;
	}
	
	if(rr == 'reset') {
		if(y<0) {
			speed += speed * .02;
			y += speed;
			$('video_mask').style.left = y;
			setTimeout('slide_images(\"reset\")', rate);
		} else {
			y = 0;
			speed = start_speed;
			stop_slide = false;	
		}
		return false;
	}
	
	if(y > (-1*max_width)) {
		speed += speed * .02;
		y -= speed;
		
		$('video_mask').style.left = y;
		setTimeout('slide_images(' + y + ')', rate);
	} else {
		y = (-1*max_width);
		speed = start_speed;
		stop_slide = false;
	}
}

function video_test() {
	width =  ((videos.length) * 90);
	max_width = ((videos.length-4) * 90);
	var html = "<div id='active_video' style='text-align: center;'><center><div style='width: 560px'>" + video_html(0) + "</div></center></div>";
	if(videos.length>1) { html += video_scanner(); }
	
	document.getElementById('video_frame').innerHTML = html;
}

video_scanner = function() {
	var html = "<div id='video_scanner'>" + video_images() + "</div>";
	return html;
}

video_images = function (index) {
	if(!index) {
		index = 0;
	}
	
	var html = "<center><table width='560px'><tr><td align='right'><div onMouseOver='stop_slide = false; slide_images(\"reset\")' onMouseOut='stop_slider()' style='width: 20px; cursor: pointer'> < </div></td><td><div style='width: 540px; overflow: hidden; position: relative'><div id='video_mask' style='position: relative;";
	
	var row_html = "";
	for(var x = 0; x < videos.length; x++) {
		var thumb = "<img onclick='run_video(" + x + ")' style='cursor: pointer' width='90px' src='http://i.ytimg.com/vi/" + videos[x]['src'] + "/default.jpg'/>";
		if(videos[x]['origin'] == 'vimeo') { thumb = "<img onclick='run_video(" + x + ")' style='cursor: pointer' width='90px' src='" + videos[x]['thumb'] + "'/>"; }

		row_html += "<td ";
		if(index == x) {
			row_html += "style='margin: 6px; border: 1px solid #999' ";
		}
		row_html += ">";
		row_html += thumb;
		row_html += "</td>";
	}
	var offset = (((index) * 90)*-1) + 155;
	if(index < 3) {
		offset = 0;
	}
	
	if(index > (videos.length-4)) {
		offset = (((videos.length-4) * 90)*-1);
	
	}
	
	y = offset;
	stop_slide = false;
	speed = start_speed;
	html += "left:" + offset + "px";
	html += "'><table><tr>";
	html += row_html + "</tr></table></div></div></td><td width='20px'><div onMouseOver='stop_slide = false; slide_images()' onMouseOut='stop_slider()' style='width: 20px; cursor: pointer'> > </div></td></tr></table></center>";
	return html;
}

video_html = function (index) {
	switch(videos[index]['origin']) {
		case 'vimeo': return video_title(index) + vimeo_html(index); break;
		default: return video_title(index) + bio_video_html(index);
	}
}

var video_title = function (index) {
	var title = "<div style='text-align: left; width: 100%'><span onClick='window.location=\"videos.php?vi=" + videos[index]['entry_id'] + "\"'><a href='videos.php?vi=" + videos[index]['entry_id'] + "'>" + videos[index]['title'] + "</a>";
	if(owner) {
 		title += " [+]</span> <span class='greyLink' onClick='delete_video(" + videos[index]['entry_id'] + ")' class='greyLink'>[x]</span>";
	}
	title += "</div>";
	return title;
}

var vimeo_html = function (index) {
    	var video = videos[index];
    	embed  = '<embed src="http://vimeo.com/moogaloop.swf?clip_id=';
    	embed += video['src'];
    	embed += '&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=f06800&amp;fullscreen=1&amp;autoplay=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="100%" height="480"></embed>';
    return embed;
}

var bio_video_html = function (index) {
	var video_url = "http://youtube.com/v/" + videos[index]['src'];
	var video_html = '<div style="padding-bottom: 8px;">' + '<b>' + videos[index]['title'] + '</b>' + '</div><div style="padding-bottom: 8px"><object width="100%" height="400px"><param name="movie" value="';
	video_html += video_url;
	video_html += '"></param><param name="wmode" value="transparent"></param>';
	video_html  = '<embed src="';
	video_html += video_url + '&autoplay=1"';
	video_html += '" type="application/x-shockwave-flash" wmode="transparent" width="100%" height="400px"></embed></div>';		
	return video_html;
}

run_video = function (index) {
	$('active_video').innerHTML = "<center><div style='width: 600px'>" + video_html(index) + "</div></center>"; 
	$('video_scanner').innerHTML = video_images(index);
}

function delete_video(id) { 
	
	var callback = function () {
		window.location.reload();		
	}
	
	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {
		if(XObj.readyState == 4) {
			if(callback) {
				callback();
			}
		}		
	}		
			
	XObj.open('POST','php/add_videos.php?delete=true&id='+id,true);
	XObj.send(null);
}

function add_video() { 
	
	var title = document.getElementById('video_title').value;
	var src = document.getElementById('video_src').value;

	var youtube = src.indexOf('youtube.com');
	if(youtube <= 0) {
		alert('You can only add youtube videos, sorry.');
		return false;
	}
	
	document.getElementById('video_title').value = '';
	document.getElementById('video_src').value = '';
	
	var youtube_link = src.indexOf('?v=');
	if(youtube_link > 0) {
		var start = (youtube_link+3);
		var youtube_code = src.substr(start, 11);
		src=youtube_code;
	}
	
	var youtube_inline = src.indexOf('v/');
	if(youtube_inline > 0) {
		var start = (youtube_inline+2);
		var youtube_code = src.substr(start, 11);
		src=youtube_code;
	}
	
	var callback = function () {
		window.location.reload();		
	}
	
	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {
		if(XObj.readyState == 4) {
			if(callback) {
				callback();
			}
		}		
	}		
			
	XObj.open('POST','php/add_videos.php?add=true&title=' + title + '&src=' + src, true);
	XObj.send(null);
}

function guest_video() { 
	
	var title = document.getElementById('video_title').value;
	var src = document.getElementById('video_src').value;

	var youtube = src.indexOf('youtube.com');
	if(youtube <= 0) {
		alert('You can only add youtube videos, sorry.');
		return false;
	}
	
	document.getElementById('video_title').value = '';
	document.getElementById('video_src').value = '';
	
	var youtube_link = src.indexOf('?v=');
	if(youtube_link > 0) {
		var start = (youtube_link+3);
		var youtube_code = src.substr(start, 11);
		src=youtube_code;
	}
	
	var youtube_inline = src.indexOf('v/');
	if(youtube_inline > 0) {
		var start = (youtube_inline+2);
		var youtube_code = src.substr(start, 11);
		src=youtube_code;
	}
	
	var callback = function () {
		window.location.reload();		
	}
	
	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {
		if(XObj.readyState == 4) {
			if(callback) {
				callback();
			}
		}		
	}		
			
	XObj.open('POST','php/add_videos.php?uid=1&add=true&title=' + title + '&src=' + src, true);
	XObj.send(null);
}
