var isPlaying = new Boolean(false);
var progBar; 
var progBkg;
var btn;
var video;
var durationText;
var durationTime;
var videoTitle;
var dlBtn;

	
$(document).ready(function() {
   	progBar = document.getElementById("progressBar");
  	progBkg = document.getElementById("progressBKG"); 	
  	video = document.getElementById("videoscreen");
	btn = document.getElementById("playPauseBtn");
	
	//video.onLoadedMetaData = showDuration();
	
	durationText = document.getElementById("duration");
	
	$(progBkg).bind('click',timejump,false);
	
	videoTitle = "Title";
	dlBtn = document.getElementById("downloadBtn");


});

function showDuration(){ 
  	//if (this.video.duration) this.durationText.innerHTML = videoTitle  + "<br />" + formatTime(video.duration);
}

function modalClicked(){
	closePlayModal();
	showDefaultVideo();
//	playVid();
}

function closePlayModal()
{
	$("#controlBox").show();
	$("#videogoeshere").show();
	$("#modalplay").hide();
}

function loadHtmlVideoMovie(url, size, name, time, downloadUrl){

	closePlayModal();
	var videoElement = $("#videoscreen");
	videoElement[0].pause();
	videoElement.remove();
	
	document.getElementById('downloadbtntootltip').innerHTML = 'Download movie in higher resolution:<br />File size ' + size;
	this.videoTitle = name;
	this.durationText.innerHTML = videoTitle + " " + time;
	this.dlBtn.setAttribute("title", size);
	
	var videoSource= videoElement.find("#thesource");
	
	// remove the original extension to replace it with the new video extension
	// we do this so we dont need to privde too many different video urls from the original data
	url = url.substring(0, url.lastIndexOf("."));
	
	var newVideoElement = $("<video id='videoscreen' height='247' width='439'><source id='thesource' src=''></div></video>");
	
	thetype = "";
	switch(BrowserDetect.browser)
	{
	
		case "Safari":
			thetype += 'video/mp4';
			url += ".mp4";
			newVideoElement.attr("src", url);
			break;
			
		case "Firefox":
			thetype += 'video/ogg';
			url += ".ogg";
			break;
			
		case "Opera":
			thetype += 'video/mp4';
			url += ".mp4";
			break;
			
		case "Chrome":
			thetype += 'video/ogg';
			url += ".ogg";
			break;

		default:
			thetype += 'video/ogg';
			url += ".ogg";
			break;
	}
	
	videoSource.attr("src", url);
	videoSource.attr("type", thetype);
	
	
	newVideoElement.find("#thesource").attr("src", url);
	newVideoElement.find("#thesource").attr("type", thetype);
	
	if (thetype == "video/mp4")
	{
		newVideoElement.find("#thesource").remove();
	}
	
	$("#videogoeshere").prepend(newVideoElement);
//	}
	//document.getElementById('thesource').type = thetype;
	
	
	isPlaying = true;
	
	newVideoElement[0].load();
	playVid();
	
	video = newVideoElement[0];
	
	
}

function playVid()
{
	var video = $("#videoscreen")[0];
	if(isPlaying)
	{
		video.play();
		trackPlayProgress();
		btn.style.backgroundPosition = '18px -18px';
	}
	else
	{
		video.pause();
		stopTrackingPlayProgress();
		btn.style.backgroundPosition = '18px 0px';
	}
	isPlaying = !isPlaying;
	
	$("#videoscreen").unbind("ended").bind("ended", 
		function()
		{
			showNextMovie();
			isPlaying = true;		
		}
	);
}

function playOver(){
	if(isPlaying){
		btn.style.backgroundPosition = '18px 0px';
	}else{
		btn.style.backgroundPosition = '18px -18px';
	}
}

function playOut(){
	if(isPlaying){
		btn.style.backgroundPosition = '0px 0px';
	}else{
		btn.style.backgroundPosition = '0px -18px';
	}
}

function mOver(who){
	who.style.backgroundPosition = '18px 0px';	
}

function mOut(who){
	who.style.backgroundPosition = '0px 0px';	
}

function trackPlayProgress(){
  playProgressInterval = setInterval(updatePlayProgress, 33);
}
 
function stopTrackingPlayProgress(){
  clearInterval(playProgressInterval);
}

function updatePlayProgress(){
	var video = $("#videoscreen")[0];
	showDuration();
	var maxWidth = $("#progressBKG").width();
  	progBar.style.width = ((video.currentTime / video.duration) * parseInt(maxWidth)) + "px";
}

function formatTime(seconds) {
  seconds = Math.round(seconds);
  minutes = Math.floor(seconds / 60);
  minutes = (minutes >= 10) ? minutes : "0" + minutes;
  seconds = Math.floor(seconds % 60);
  seconds = (seconds >= 10) ? seconds : "0" + seconds;
  return minutes + ":" + seconds;
}

function download(){
	alert("Download file");
}

function faq(){
	alert("FAQ");
}

function timejump(event){
	var video = $("#videoscreen")[0];
	video.currentTime = getRelativePosition(event.pageX, progBkg) * video.duration;
	updatePlayProgress();
}

function getRelativePosition(x, relativeElement){
    return Math.max(0, Math.min(1, (x - findPosX(relativeElement)) / relativeElement.offsetWidth));
}

 function findPosX(obj) {
    var curleft = obj.offsetLeft;
    while(obj = obj.offsetParent) {
      curleft += obj.offsetLeft;
    }
    return curleft;
  }
  
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

	BrowserDetect.init();

