// JavaScript Document

function submitLogin () {

}

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

var index = 0;
function slide_images (rr) {
	
	if(stop_slide == true) {
		return false;
	}
	if(rr == 'reset') {
		if(y<0) {
			speed += speed * .02;
			y += speed;
			doc('video_mask').style.left = y+'px';
			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;
		doc('video_mask').style.left = (y)+'px';
		setTimeout('slide_images(' + (y) + ')', rate);
	} else {
		y = (-1*max_width);
		speed = start_speed;
		stop_slide = false;
	}
}

function post_video () {
	document.getElementById('new_videos').innerHTML = "<form><input type='hidden' name='video' id='video' value='true'/>"
		+ "Video Title <br /><input style='width:100%' name='video_title' id='video_title'/><br />"
		+ "Video Location <br /><input style='width:100%' name='video_src' id='video_src'/><br />"
		+ "<input type='button' onClick='add_video()' value='Add'/>"
		+ "<input type=\"button\" onClick=\"window.location.reload()\" value=\"Cancel\"/></form>";
}
	
function video_test() {
	width =  ((videos.length) * 100);
	max_width = ((videos.length-4) * 100);
	var html = "<div id='active_video'>" + video_html(0) + "</div>";

	if(videos.length>1) { 
		html += "<div style='float: left; color: #333'>Now showing: " + videos.length + "/" + videos.length + " videos</div><br />";
		html += "<div>" + video_scanner() + "</div>";
	}
	
	if(owner) {
		html += "<div class='white_glass' style='width: 240px; padding: 14px; margin: 18px;' id='new_videos'><span onClick='post_video()'>+ Add Video</span></div>";
	}


	var video_link = "./videos.php?vi=" + videos[index]['entry_id'];	
	var link = " onClick='window.location=\"" + video_link + "\"'";
	if(block_reload_video) { link = " onClick='run_video("+index+")'" }

	var video_title = "<span style='cursor: pointer'"+link+">" + videos[index]['title'];
	video_title += "</span> [<a href='http://www.facebook.com/sharer.php?u=" + video_link + "' target='_blank'>share</a>]";

	if(owner) {
	    video_title += " <span onClick='delete_video(" + videos[index]['entry_id'] + ")'>[remove]</span>";
	}

	document.getElementById('video_frame').innerHTML = "<center>"+html+"</center>";
	document.getElementById('video_frame_title').innerHTML = video_title;	
}

var mainvideo;
function tv_station() {
	width =  ((videos.length) * 90);
	max_width = ((videos.length-4) * 90);
	var html = "<div id='active_video'>" + video_player_html(0) + "</div>";

	if(videos.length>1) { 
		//html += "<div style='float: left; color: #333'>Now showing: " + videos.length + "/" + videos.length + " videos</div><br />";
		//html += "<div>" + video_scanner() + "</div>";
	}
	
	if(owner) {
		html += "<div class='glass' style='width: 240px; padding: 14px; margin: 18px;' id='new_videos'><span onClick='post_video()'>+ Add Video</span></div>";
	}


	var video_link = "./videos.php?vi=" + videos[index]['entry_id'];
	var link = " onClick='window.location=\"" + video_link + "\"'";
	if(block_reload_video) { link = " onClick='load_video("+index+")'" }

	onYouTubePlayerReady = function (playerId) {			
   		ytplayer = document.getElementById("player2");
  		ytplayer.addEventListener('onStateChange', 'next');		
		ytplayer.cueVideoById(mainvideo['src']);
		ytplayer.playVideo();		
  	}


	document.getElementById('video_frame').innerHTML = "<center>"+html+"</center>";

 	document.getElementById('video_extras').innerHTML = '<div style="border: 1px solid #111; width: 100%; clear: both"></div>' + '<div id="playButton" style="float: right"></div>';
	playButtons(1);
	//document.getElementById('video_frame_title').innerHTML = video_title;
	document.getElementById('video_scanner').innerHTML = video_scanner_vert();
}

function playControl(time) {
  if (ytplayer) {
    play = true;
    playButtons(play);
    ytplayer.playVideo();    
  }
}

function pauseControl(time) {
  if (ytplayer) {
    play = false;
    playButtons(play);
    ytplayer.pauseVideo();    
  }
}

function playButtons (play) {
  var button = (!play ? '<a class="nav_buttons" onClick="playControl()">Play</a>' : '<a class="nav_buttons" onClick="pauseControl()">Pause</a>');
  var skip = '<a class="nav_buttons" onClick="next(0);">Skip</a>';
  var buttons = "<div style='padding: 4px 6px 2px 6px;'>"+(button + " / " + skip)+"</div>";

  document.getElementById('playButton').innerHTML = buttons;
}

video_scanner = function() {
	var html = "<table callpadding='0px' cellspacing='0px' border='0px'><tr><td width='24px' class='basic_scroll_arrow' onMouseOver='stop_slide = false; slide_images(\"reset\")' onMouseOut='stop_slider()'> < </td><td>";
	html += "<div id='video_scanner' style='border: 1px solid #CCC; background-color: #111; width: 400px; overflow: hidden'>" + video_images() + "</div>";
	html += "<td width='24px' class='basic_scroll_arrow' style='text-align: right' onMouseOver='stop_slide = false; slide_images()' onMouseOut='stop_slider()'> > </td></tr></table>";	
	return html;
}

video_scanner_vert = function() {
	var x = 0;
	mainvideo = videos[x];
	var thumb = "<img title='" + videos[x]['title'] + "' onclick='load_video(" + x + ")' style='cursor: pointer' height='60px' src='http://i.ytimg.com/vi/" + videos[x]['src'] + "/default.jpg'/>";
	if(videos[x]['origin'] == 'vimeo') { thumb = "<img onclick='load_video(" + x + ")' style='cursor: pointer' height='60px' src='" + videos[x]['thumb'] + "' alt='thumb'/>"; }

	var forum_url = videos[x]['forum_url'];
 	var forum_title = (videos[x]['forum_title']) ? videos[x]['forum_title'] : forum_url;
 	if (forum_url) { var video_link = "http://"+forum_url+"/videos.php?vi="+videos[x]['entry_id']; var forum_link = "<a href='"+video_link+"'>"+forum_title+"</a>" } else { var video_link = forum_url + "/videos.php?vi="+videos[x]['entry_id']; var forum_link = "<a href='" + (window.document.location) + "'>" + window.document.domain + "</a>"; forum_url = "";}

 	var full_title = escape(truncateStr(videos[x]['title'], 100));
 	var main_title = truncateStr(videos[x]['title'], 54);

	var video_share = "<div class='glass' style='float: left; padding: 4px 6px 2px 6px; width:316px; font-size: 11px;'><a onClick='if(auth){auto_video(\"" + full_title + "\",\"" + videos[x]['src'] + "\",\"youtube\",\"1\"); this.innerHTML=\"<div>Saved!</div>\"}' target='_blank' class='or_link'><a href='http://twitter.com/intent/tweet?text=" + full_title + "&url=" + video_link + "' target='_blank' class='or_link'><img style='float:left; margin: 0px 2px 0px 6px' src='http://geoliphicmedia.com/images/social_buttons/twitter_follow.png' width='14px'/></a> <a href='http://apps.facebook.com/bigupsforum/?share=1&u=" + video_link + "' target='_blank' class='or_link'><img style='float:left' src='http://geoliphicmedia.com/images/social_buttons/facebook_friends.png' width='14px'/></a> <div style='float: left'><span style='color: #666'>share video</span></div></div>";

	var main_html = "<div style='float: left; width: 420px; font-size: 11px'><div style='float: left; margin: 0px 3px 3px 0px'>NOW PLAYING: <br />" + thumb + "</div>";
	main_html += "<a href='"+video_link+"'>" + main_title + "</a><br />";
	main_html += "posted on " + forum_link + "<br /><small style='color: #666'>" + truncateStr(video_link, 26) + "</small>" + video_share;
	main_html += "</div>";

	index = 0;
	for(var x = 0; x < (videos.length); x++) {
		if(videos[x]['entry_id'] == mainvideo['entry_id']) { index = x; }
	}

	var html = "<div style='float: right; margin: 4px 0px 4px 0px;'><table cellpadding='0px' cellspacing='0px' border='0px'><tr><td width='10px' class='basic_scroll_arrow' onMouseOver='stop_slide = false; slide_images(\"reset\")' onMouseOut='stop_slider()'> <small><</small> </td><td>";
	html += "<div id='video_scanner' style='border: 1px solid #CCC; background-color: #111; width: 372px; overflow: hidden'>" + video_images_vert(index) + "</div>";
	html += "<td width='10px' class='basic_scroll_arrow' style='text-align: right' onMouseOver='stop_slide = false; slide_images()' onMouseOut='stop_slider()'> <small>></small> </td></tr></table></div>";	
	return main_html + html;
}

video_images = function (index) {
	if(!index) {
		index = 0;
	}

	var div_html = "<div id='video_mask' style='height: 80px; position: relative;";	
	var row_html = "";
	for(var x = 0; x < videos.length; x++) {
		if(index == x) { var faded = true; } else { var faded = false; }
		var thumb = "<img";
		if(faded) { thumb += " class='faded'"; }
		thumb += " title='" + videos[x]['title']  + "' onclick='run_video(" + x + ")' style='cursor: pointer' width='100px' src='http://i.ytimg.com/vi/" + videos[x]['src'] + "/default.jpg'/>";
		if(videos[x]['origin'] == 'vimeo') { thumb = "<img title='" + videos[x]['title']  + "' onclick='run_video(" + x + ")' style='cursor: pointer' width='100px' src='" + videos[x]['thumb'] + "' alt=''/></a>"; }

                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) * 54)*-1) + 155;
	if(index < 3) {
		offset = 0;
	}
	
	if(index > (videos.length-4)) {
		offset = (((videos.length-4) * 54)*-1);
	
	}
	
	y = offset;
	stop_slide = false;
	speed = start_speed;
	div_html += "left:" + offset + "px";
	div_html += "'>";
	
	var html = div_html + "<table><tr>" + row_html + "</tr></table></div></td>";
	return html;
}

function randOrd(){
return (Math.round(Math.random())-0.5); } 

video_images_vert = function (index) {

	if(!index) {
		index = 0;
	}
	
	var x = 0;
	var tmp_videos = videos;
	    //tmp_videos.sort( randOrd );

	var div_html = "<div id='video_mask' style='position: relative; height: 60px; width: " + (max_width*videos.length) + "px;";	

	var row_html = "";
	for(var x = 1; x < (videos.length); x++) {
		if((index) == x) { var faded = true; } else { var faded = false; }
		var thumb = "<img";
		if(faded) { thumb += " class='faded'"; }
		thumb += " title='" + tmp_videos[x]['title']  + "' onclick='load_video(" + x + ")' style='cursor: pointer; float: left' width='90px' src='http://i.ytimg.com/vi/" + tmp_videos[x]['src'] + "/default.jpg'/>";
		if(tmp_videos[x]['origin'] == 'vimeo') { thumb = "<img title='" + videos[x]['title']  + "' onclick='load_video(" + x + ")' style='cursor: pointer; float: left' width='90px' src='" + tmp_videos[x]['thumb'] + "' alt=''/>"; }
		row_html += thumb;
	}
	
	var offset = (((index+1) * (-89)) + 210);		
	if(index > (videos.length-4)) {
		offset = (((videos.length-1) * (-90))+(360));
	}	
	if(index < 3) {
		offset = 0;
	}

	y = offset;
	stop_slide = false;
	speed = start_speed;
	div_html += "; left:" + offset + "px";
	div_html += "'>";
	
	var html = div_html + "<table width='100%'><tr>" + row_html + "</tr></table>";
	return html;
}


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

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;">';
	
	if(auth && videos[index]['bio_id']) {
		video_html += "Band/Artist: ";
		video_html += '<b>' + videos[index]['bio_name'] + '</b><br />';
	} else if (owner){
		var album_title = "<select><option>No artists</option></select>"; 
		try {
			if(artists) {
				album_title = "<select onChange='add_video_band(" + index + ", this.value)'>";
				album_title += "<option value='none'>No band/artist</option>";
				for(var x = 0; x < artists.length; x++) {
					album_title += "<option value='" + artists[x]['entry_id'] + "'>" + artists[x]['bio_name'] + "</option>";	
				}
				album_title += "</select>";
				video_html += "Band/Artist: ";
				video_html += album_title + '<br />';
			} 
		} catch(err) { }
	}
	video_html += '</div><div style="padding-bottom: 8px"><object width="100%" height="480"><param name="movie" value="';
	video_html += video_url;
	video_html += '&fs=1&autoplay=' + videoAutoPlay + '&loop=1;enablejsapi=1&version=3"></param><param name="wmode" value="transparent"></param><param name="allowscriptaccess" claue="alwats"></param><embed src="';
	video_html += video_url;
	video_html += '&fs=1&autoplay=' + videoAutoPlay + '&loop=1;enablejsapi=1&version=3" type="application/x-shockwave-flash" wmode="transparent" width="100%" height="480"></embed></object></div>';		
	return video_html;
}

var video_player_html = function (index) {

	if(videos[index]['origin']=='vimeo') { return vimeo_html(index); }
	
	var video_url = "http://youtube.com/v/" + videos[index]['src'];
	var video_html = '<div style="padding-bottom: 8px;">';
	
	if(auth && videos[index]['bio_id']) {
		video_html += "Band/Artist: ";
		video_html += '<b>' + videos[index]['bio_name'] + '</b><br />';
	} else if (owner){
		var album_title = "<select><option>No artists</option></select>"; 
		try {
			if(artists) {
				album_title = "<select onChange='add_video_band(" + index + ", this.value)'>";
				album_title += "<option value='none'>No band/artist</option>";
				for(var x = 0; x < artists.length; x++) {
					album_title += "<option value='" + artists[x]['entry_id'] + "'>" + artists[x]['bio_name'] + "</option>";	
				}
				album_title += "</select>";
				video_html += "Band/Artist: ";
				video_html += album_title + '<br />';
			} 
		} catch(err) { }
	}

    var params = { allowScriptAccess: "always", movie: "http://www.youtube.com/apiplayer?enablejsapi=1&version=3&playerapiid=ytplayer", wmode: "transparent" };
    var atts = { id: "player2" };
    swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&version=3&playerapiid=ytplayer","active_video", "640", "480", "8", null, null, params, atts);
}

var next = function (state) {
	if(state==0 && index<videos.length-1) {		
		index++;
		load_video(index);
	} else if(state==0 && index==videos.length-1) {
		index = 0;
		index++;	
		load_video(index);
	}
}

var block_reload_video = false;

load_video = function (index) {
	//document.title = "Spotlight TV | " + videos[index]['title'];
	var video_link = "./videos.php?vi=" + videos[index]['entry_id'];
	document.location = video_link;
	return false;
}

run_video = function (index) {	
	document.title = videos[index]['title'];
	var video_link = "./tv.php?vi=" + videos[index]['entry_id'];

	var link = " onClick='window.location=\"" + video_link + "\"'";
	if(block_reload_video) { link = " onClick='run_video("+index+")'" }

	var video_title = "<span style='cursor: pointer'" + link + ">" + videos[index]['title'];
	video_title += "</span> [<a href='http://www.facebook.com/sharer.php?u=" + video_link + "' target='_blank'>share</a>]";

	if(owner) {
		video_title += " <span onClick='delete_video(" + videos[index]['entry_id'] + ")'>[remove]</span>";
	}
	//doc('video_frame_title').innerHTML = video_title;
	doc('active_video').innerHTML = video_html(index); 
	doc('video_scanner').innerHTML = video_images(index);
}

function add_video_band (index, bio_id) {
	
	var video_id = videos[index]['entry_id'];
	var bio_id = bio_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/update_video.php?update_video_band=true&bio_id=' + bio_id + '&entry_id=' + video_id, true);
	XObj.send(null);
}

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 origin = "youtube";
	
	var youtube = src.indexOf('youtube.com');
	var vimeo = src.indexOf('vimeo.com');
	if(youtube <0 && vimeo <0) {
		alert('You can only add youtube and vimeo 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 vimeo_inline = src.indexOf('moogaloop.swf?clip_id=');
	if(vimeo_inline > 0) {
		var start = (vimeo_inline+22);
		var vimeo_code = src.substr(start, 8);
		src=vimeo_code;
		var origin = "vimeo";
	}

	var vimeo_link = src.indexOf('vimeo.com/');
	if(vimeo_link >= 0 && vimeo_inline <=0) {
		var start = (vimeo_link+10);
		var vimeo_code = src.substr(start, 8);
		src=vimeo_code;
		var origin = "vimeo";
	}

	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 + '&user_serial=' + serial + '&origin=' + origin, 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);
}

function remove_event(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_event.php?remove=true&id='+id,true);
	XObj.send(null);
}


function auto_video (title, src, origin, reload) {
	if(reload) {
		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 + '&user_serial=' + serial + '&origin=' + origin, true);
	XObj.send(null);
}

function remove_event(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_event.php?remove=true&id='+id,true);
	XObj.send(null);
}

function add_event() { 
	
	var name = escape(document.getElementById('event_name').value);
	var date = document.getElementById('event_date').value;
	var img = document.getElementById('event_img').value;
	var info = escape(document.getElementById('event_info').value);
	var door = document.getElementById('event_door').value;
	var start = document.getElementById('event_start').value;
	var sales = document.getElementById('event_sales').value;
	
	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_event.php?add=true&name=' + name + '&date=' + date + '&img=' + img + '&info=' + info + '&door=' + door + '&start=' + start + '&sales=' + sales, true);
	XObj.send(null);
}

