
var web_root = "";
var video_ids;
var video_list;
var video_links;

var original_video_list;
var original_video_ids;

var ad_video_ids;
var ad_video_list;
var ad_freq;

var zoomed = false;

var active_video;

Event.observe(window, 'load', function() {
	
	// save these in their original order for later on
	original_video_list = video_list;
	original_video_ids = video_ids;
	
	if ($('videolist')) {
		video_links = $('videolist').getElementsByTagName('a');
	
		$A(video_links).each( function(el) {
			el.onclick = function() {
				ider = getVideoId(el);
				recreateVideoList(ider);
				return false;
			}
		});
	}
});

function recreateVideoList(selected_video_id) {
	
	// create arrays of the orginal video lists
	var ar_video_list = original_video_list.split(",");
	var ar_video_ids = original_video_ids.split(",");
	
	var ar_ad_video_list = ad_video_list.split(",");
	var ar_ad_video_ids = ad_video_ids.split(",");
	
	// counter
	var c = 0;
	
	// finder
	var found = false;
	
	// the strings needed for below
	var new_video_ids = "";
	var new_video_list = "";
	var videos_before = "";
	var video_ids_before = "";
	var ad_last = false;
	
	$A(video_links).each( function(el) {		
		if (found) {
			if (ad_last != true && ad_video_ids.length > 0) {
				if (Math.floor(Math.random()*100) <= ad_freq*1.5) {
					var ind = Math.floor(Math.random()*ar_ad_video_ids.length);
					new_video_ids += ","+ar_ad_video_ids[ind];
					new_video_list += ","+ar_ad_video_list[ind];
				}
				ad_last = true;
			} else {
				ad_last = false;
			}
			new_video_ids += ","+ar_video_ids[c];
			new_video_list += ","+ar_video_list[c];
		}
		else {
			if (selected_video_id == getVideoId(el)) {
				new_video_ids = ar_video_ids[c];
				new_video_list = ar_video_list[c];
				found = true;
			}
			else {
				if (ad_last != true && ad_video_ids.length > 0) {
					if (Math.floor(Math.random()*100) <= ad_freq) {
						var ind = Math.floor(Math.random()*ar_ad_video_ids.length);
						video_ids_before += ","+ar_ad_video_ids[ind];
						videos_before += ","+ar_ad_video_list[ind];
					}
					ad_last = true;
				} else {
					ad_last = false;
				}
				video_ids_before += ","+ar_video_ids[c];
				videos_before += ","+ar_video_list[c];
			}
		}
		c++;
	});

	// update the lists that are passed into flash
	video_ids = new_video_ids+video_ids_before;
	video_list = new_video_list+videos_before;
	
	playVideo(440, 280, true);
}

function getVideoId(el) {
	var url = el.getAttribute('href');
	var ider = url.replace(web_root+"videos/view/", "");
	ider = ider.replace("/", "");
	return ider;
}


function playVideo(width, height, autostart) {
	
	var vid = video_ids.split(",");
	
	if (vid.length > 1) {
		v = new SWFObject(web_root+"flash/video.swf", "video", width, height, "9", "#000000");
		v.addParam("quality", "best");
		v.addParam("allowFullScreen", "true");
		v.addVariable("video_ids", video_ids);
		v.addVariable("videos", video_list);
		v.addVariable("root_dir", web_root);
	}
	
	else {
		v = new SWFObject(web_root + "flash/player.swf", "video", width, height, "9", "#000000");
		v.addParam("allowfullscreen","true");
		v.addParam("allowscriptaccess","always");
		v.addParam("flashvars","file=" + web_root + "uploads/videos/" + video_list + "&backcolor=000000&frontcolor=FFFFFF");
	}
	if (autostart != null) {
		v.addVariable("auto_start", 0);
	}
	else {
		viewCount(vid[0]);
	}
	v.write("video_player");
}

function viewCount(id) {

	active_video = id;
	
	// loading
	if ($('video_info')) { $('video_info').innerHTML = "<p>Loading video info..</p>"; }
	
	// add to the view count
	new Ajax.Request( web_root + "videos/watched/"+id+"/", { method:'get' } );
	
	//check if an ad is playing
	var is_ad = false;
	
	if (ad_video_ids) {
		$A(ad_video_ids.split(",")).each(function(el) {	if (el == id) { is_ad = true; }	});
	}
	
	if (is_ad) {
		$('video_info').innerHTML = "<p><b>Advertisement</b></p>";
	} else {
	// update the video info being displayed
		var url = web_root + "videos/view/"+id+"/ajax/";
		if (zoomed) { url +=  "1/"; }
		new Ajax.Request( url, { method:'get', onComplete: processAjax.bindAsEventListener(this) } );
	}
	
	if ($('videolist')) {
	
		var videos = $('videolist').getElementsByTagName('li');
		
		$A(videos).each( function(el) {
			el.className = "not-selected";
		});
		
		if ($('video'+id)) { 
			$('video'+id).className = 'selected';
		}
		
	}
	
}

function setDefaultText() {
	if ($('video_info')) {
		$('video_info').innerHTML = "<p>Press play to start video.</p>";
	}
}
	
function processAjax(request) {
	if ($('video_info')) {
		$('video_info').innerHTML = request.responseText;
		setUpZoomLink();
		setUpFullScreenLink();
		
		var toggles = $('video_info').getElementsByClassName('toggle');
		var t = new Array();
		
		$A(toggles).each (function(toggle) {
			t[toggle.id] = toggle.innerHTML;
			toggle.onclick = function() {
				var ider = toggle.id;
				var current_text = toggle.innerHTML;
				if ($(ider+"_content")) {
					if ($(ider+"_content").offsetHeight>0) {
						new_display = "none";
						new_html = t[ider];
					}
					else { 
						// hide all
						$A(toggles).each (function(toggle_inner) {
							if ($(toggle_inner.id+"_content")) {
								if ($(toggle_inner.id+"_content").offsetHeight>0) {
									toggle_inner.innerHTML = t[toggle_inner.id];
									$(toggle_inner.id+"_content").style.display = "none";
								}
							}
						});
						new_display = "inline";
						new_html = toggle.getAttribute('rel');
					}
					$(ider+"_content").style.display = new_display;
					toggle.innerHTML = new_html;
				}
				return false;
			}			
		});

	}
}

function setUpZoomLink() {

	if ($('video_zoom')) {
		$('video_zoom').onclick = function() {
	
			var third_column = $('content').getElementsByClassName('col-med3');
			if (third_column[0]) {
				var vid_holder = third_column[0].getElementsByClassName('video_holder');
				if (vid_holder[0]) {
					if (!zoomed) {
						zoomed = true;
						vid_holder[0].innerHTML = "";
						var d = document.createElement('div');
						$('content').style.height = "1000px";
						d.innerHTML = "<div id=\"video_player\"></div><div id=\"video_info\"><p>Loading video info...</p></div>\n";
						d.id = "video_zoom_div";
						d.className = "video_large";
						$('wrapper').appendChild(d);
						playVideo(600, 525);
						window.scrollTo(0, 235);
					}
					else {
						zoomed = false;
						if ($('video_zoom_div')) {
							$('wrapper').removeChild($('video_zoom_div'));
						}
						vid_holder[0].innerHTML = "<div id=\"video_info\"><p>Loading video info...</p></div><div id=\"video_player\"></div>\n";
						playVideo(440, 280);
					}
					
				}
				else {
					alert("unable to find the item");
				}
			}
			else {
				alert("unable to zoom at the moment!");
			}
			return false;
		}
	}
}

function getVideoObj() {
	var third_column = $('content').getElementsByClassName('col-med3');
	if (third_column[0]) {
		var vid_holder = third_column[0].getElementsByClassName('video_holder');
		return vid_holder[0];
	}
	else {
		return false;
	}
}


// currently this works by opening a new window
// this is a dodgy way of doing this
// and we should use the built in flash 9 true full screen 
// see: http://www.adobe.com/devnet/flash/articles/flvplayback_programming_06.html

function setUpFullScreenLink() {
	if ($('video_fullscreen')) {
	
		$('video_fullscreen').onclick = function() {
		
			// kill the current video holder
			vid_holder = getVideoObj();
			vid_holder.innerHTML = "<p>You have entered full screen mode. Click <a href=\"#\" onClick=\"location.reload(true)\">HERE</a> to get the video back.";
			
			var h = this.getAttribute('href');
			window.open(h, null, 'fullscreen=yes,resizable=no,location=no,titlebar=no,toolbar=no,scrollbars=no,directories=no');
			
			return false;
		}
	}

}

