// 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 = "<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='submit' onClick='add_video()' value='Add'/>"
		+ "<input type=\"submit\" onClick=\"window.location.reload()\" value=\"Cancel\"/>";
}
	
function video_station() {
	width =  ((videos.length) * 94);
	max_width = ((videos.length-4) * 94);
	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='basic_scroll_arrow' style='border: 1px solid #CCC; width: 240px; padding: 14px; margin: 18px;' id='new_videos'<span onClick='post_video()'>+ Add Video</span></div>";
	}
	
	var video_title = "<span style='cursor: pointer' onClick='window.location=\"http://bigupsforum.com/videos.php?vi=" + videos[index]['entry_id'] + "\"'>" + videos[0]['title'] + " [+]</span>";	
	if(owner) {
		video_title += " <span onClick='delete_video(" + videos[index]['entry_id'] + ")'>[remove]</span>";
	}

	document.getElementById('video_frame').innerHTML = html;
	document.getElementById('video_frame_title').innerHTML = video_title;
}

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: 450px; overflow: hidden;'>" + video_images() + "</div>";
	html += "<td width='24px' class='basic_scroll_arrow' onMouseOver='stop_slide = false; slide_images()' onMouseOut='stop_slider()'> > </td></tr></table>";
	
	return html;
}

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

	var div_html = "<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) * 94)*-1) + 155;
	if(index < 3) {
		offset = 0;
	}
	
	if(index > (videos.length-4)) {
		offset = (((videos.length-4) * 94)*-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;
}

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 += '&autoplay=' + videoAutoPlay + '&loop=1"></param><param name="wmode" value="transparent"></param><embed src="';
	video_html += video_url;
	video_html += '&autoplay=' + videoAutoPlay + '&loop=1" type="application/x-shockwave-flash" wmode="transparent" width="100%" height="480"></embed></object></div>';		
	return video_html;
}


run_video = function (index) {
	var video_title = "<span style='cursor: pointer' onClick='window.location=\"http://bigupsforum.com/videos.php?vi=" + videos[index]['entry_id'] + "\"'>" + videos[index]['title'] + " [+]</span>";	
	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 = "";
	
	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 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);
}
