/**
 *
 * Brightcove Widget : Right Box B
 * Version: 2009.03.25.01
 * Bugfixes by Tony Simbine <t.simbine (at) oms.eu>
 *
**/

if(typeof bc_id != 'array') {
	var bc_id = new Array();
}

function bc_widget_rightboxb(arg) {
	var id = bc_id.length;
	bc_id.push(new bc_make_widget_rightboxb(arg, id));
	bc_id[id].generate();
	bc_id[id].retrieve();
}

function bc_make_widget_rightboxb(arg, id) {
	this.id = id;
	this.postfix = arg.postfix || '';
	this.token = arg.token || (this.id > 0? bc_id[0].token: 'myReadToken.');
	this.url = arg.url || (this.id > 0? bc_id[this.id - 1].url: 'http://medien.niedersachsen.com/VIDEO/GT/WIDGETs/GTp70LW_lokales/');
	this.dir = arg.dir || 'http://medien.niedersachsen.com/VIDEO/GT/WIDGETs/GTp70LW_lokales/bcw_rightboxb';
	this.playerId = (!arg.playerIds) ? null : arg.playerIds[0];
	this.playlistId = (!arg.playlistIds) ? null : arg.playlistIds[0];
	this.xmlUrl = arg.xml || false;
	this.xml = '';
	this.httpRequest = null;
	this.videoCount = null;
	this.page = 0;
	this.sliding = false;
	this.referName = 'rightboxb';
	this.ie = (document.all) ? true: false;
	this.TStab = new Array(null);
	if (arg.TsTabs)	{
		if (arg.TsTabs[0] != null && arg.TsTabs[0].length > 1)
			this.TStab[0] = arg.TsTabs[0];
	}
	
	this.generate = function() {
		var code = document.createElement("script");
		code.setAttribute('src', 'http://admin.brightcove.com/js/BrightcoveExperiences.js');
		code.setAttribute('charset', 'UTF-8');
		code.setAttribute('type', 'text/javascript');
		document.getElementsByTagName('head')[0].appendChild(code);
		var code = document.createElement("script");
		code.setAttribute('src', 'http://admin.brightcove.com/js/APIModules_all.js');
		code.setAttribute('charset', 'UTF-8');
		code.setAttribute('type', 'text/javascript');
		document.getElementsByTagName('head')[0].appendChild(code);
		var code = document.createElement("link");
		code.setAttribute('href', this.dir + this.postfix + '/brightcove.css');
		code.setAttribute('rel', 'stylesheet');
		code.setAttribute('type', 'text/css');
		document.getElementsByTagName('head')[0].appendChild(code);
		
		if(this.ie) {
			document.write('<style type="text/css">');
			document.write('div.bc_widget_rightboxb'+ this.postfix + ' { width: 323px; }');
			document.write('div.bc_widget_rightboxb'+ this.postfix + ' div.bc_container_one'+ this.postfix + ' div.bc_video'+ this.postfix + ' div.bc_meta'+ this.postfix + ' div.bc_data'+ this.postfix + ' { width: 194px; }');
			document.write('div.bc_widget_rightboxb'+ this.postfix + ' div.bc_container_one'+ this.postfix + ' div.bc_video'+ this.postfix + ' div.bc_meta'+ this.postfix + ' div.bc_data'+ this.postfix + ' img { margin: 2px 5px 0 0; }');
			document.write('div.bc_widget_rightboxb'+ this.postfix + ' div.bc_footer'+ this.postfix + ' { padding-bottom: 8px; }');
			document.write('div.bc_widget_rightboxb'+ this.postfix + ' div.bc_container_two'+ this.postfix + ' { width: 298px; }');
			document.write('div.bc_widget_rightboxb'+ this.postfix + ' div.bc_container_two'+ this.postfix + ' div.bc_inner'+ this.postfix + ' div.bc_video'+ this.postfix + ' div.bc_title'+ this.postfix + ' { width: 206px; }');
			document.write('div.bc_widget_rightboxb'+ this.postfix + ' div.bc_header'+ this.postfix + ' img.bc_tab_left'+ this.postfix + ' { left: 245px; }');
			document.write('div.bc_widget_rightboxb'+ this.postfix + ' div.bc_header'+ this.postfix + ' img.bc_tab_right'+ this.postfix + ' { left: 278px; }');
			document.write('div.bc_widget_rightboxb'+ this.postfix + ' div.bc_container_two'+ this.postfix + ' div.bc_arrow_left'+ this.postfix + ' { background: none; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + this.dir + this.postfix + '/bc_arrow_left.png",sizingMethod="scale"); }');
			document.write('div.bc_widget_rightboxb'+ this.postfix + ' div.bc_container_two'+ this.postfix + ' div.bc_arrow_right'+ this.postfix + ' { background: none; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + this.dir + this.postfix + '/bc_arrow_right.png",sizingMethod="scale"); }');
			document.write('</style>');
		}

		document.write('<div class="bc_widget_rightboxb' + this.postfix + '"><div class="bc_header' + this.postfix + '">');
		document.write('<div class="bc_title' + this.postfix + '" id="' + this.id + 'BCWRB_title"></div><img src="' + this.dir + this.postfix + '/tab_1_active.png" id="' + this.id + 'BCWRB_tab1" class="bc_tab_left' + this.postfix + '" onclick="bc_id[' + this.id + '].view(1)" /><img src="' + this.dir + this.postfix + '/tab_2_inactive.png" id="' + this.id + 'BCWRB_tab2" class="bc_tab_right' + this.postfix + '" onclick="bc_id[' + this.id + '].view(2)" /></div>');
		document.write('<div class="bc_container_one' + this.postfix + '" id="' + this.id + 'BCWRB_containerOne"></div>');
		document.write('<div class="bc_container_two' + this.postfix + '" id="' + this.id + 'BCWRB_containerTwo" style="display: none;"><div class="bc_arrow_left' + this.postfix + '" id="' + this.id + 'BCWRB_arrowLeft" style="display: none;" onclick="bc_id[' + this.id + '].slide(0)"></div><div class="bc_arrow_right' + this.postfix + '" id="' + this.id + 'BCWRB_arrowRight" onclick="bc_id[' + this.id + '].slide(1)"></div><div id="' + this.id + 'BCWRB_inner" class="bc_inner' + this.postfix + '"></div></div>');
		document.write('<div class="bc_footer' + this.postfix + '"><a href="' + this.url + '">Alle Videos</a> &raquo;</div>');
		document.write('</div>');
	}
	
	this.retrieve = function() {
		if(this.xmlUrl) {
			this.manual();
		} else {
			this.auto();
		}
	}
	
	this.auto = function() {
		var code = document.createElement('script');
		code.setAttribute('src', 'http://api.brightcove.com/services/library?command=find_playlist_by_id&playlist_id=' + this.playlistId + '&token=' + this.token + '&callback=bc_id[' + this.id + '].autoReturn');
		code.setAttribute('charset', 'UTF-8');
		code.setAttribute('type', 'text/javascript');
		document.getElementsByTagName('head')[0].appendChild(code);
	}
	
	this.autoReturn = function(pData) {
		document.getElementById(this.id + 'BCWRB_title').innerHTML = this.TStab[0] || pData.name;
		var videos = pData['videos'];
		var maximum = 5;
		
		if(videos.length < maximum) {
			maximum = videos.length;
		}
		
		this.videoCount = maximum;
		
		for(var i = 0; i < maximum; i++) {
			var minutes = Math.floor((videos[i]['length'] / 1000) / 60);
			var seconds = Math.round((((videos[i]['length'] / 1000) % 60) * 60) / 60);
			
			if(seconds < 10) {
				seconds = '0' + seconds;
			}
			
			var timeString = minutes + ':' + seconds;
			
			if(videos[i]['thumbnailURL']) {
				var image1 = '<img src="' + videos[i]['thumbnailURL'] + '" width="80" height="60" />';
				var image2 = '<img src="' + videos[i]['videoStillURL'] + '" width="240" height="180" />';
			} else {
				var image1 = '';
				var image2 = '';
			}
			
			var code = document.createElement('div');

			if(i == 0) {
				code.className = 'bc_video' + this.postfix + ' bc_video_over' + this.postfix;
			} else {
				code.className = 'bc_video' + this.postfix;
			}
			
			code.innerHTML = '<div class="bc_left' + this.postfix + '" onmouseover="bc_id[' + this.id + '].show(' + i + ')"><span>' + (i + 1) + '</span> ' + videos[i]['name'] + '</div><div class="bc_right' + this.postfix + '">' + timeString + '</div><div class="bc_meta' + this.postfix + '"><a href="' + this.url + '?bcpid=' + this.playerId + '&bclid=' + this.playlistId + '&bctid=' + videos[i].id + '&refer=' + this.referName + '">' + image1 + '</a><div class="bc_data' + this.postfix + '"><div class="bc_desc' + this.postfix + '">' + videos[i]['shortDescription'] + '</div><a href="' + this.url + '?bcpid=' + this.playerId + '&bclid=' + this.playlistId + '&bctid=' + videos[i].id + '&refer=' + this.referName + '"><img src="' + this.dir + this.postfix + '/bc_arrow.png" /></a><div class="bc_link' + this.postfix + '"><a href="' + this.url + '?bcpid=' + this.playerId + '&bclid=' + this.playlistId + '&bctid=' + videos[i].id + '&refer=' + this.referName + '">Abspielen</a></div></div></div></div>';
			
			if(i == 0) {
				if(this.ie) {
					code.style.height = '78px';
				} else {
					code.style.height = '78px';
				}
			} else {
				if(this.ie) {
					code.style.height = '10px';
				} else {
					code.style.height = '10px';
				}
			}

			document.getElementById(this.id + 'BCWRB_containerOne').appendChild(code);
			
			var code = document.createElement('div');
			code.className = 'bc_video' + this.postfix;
			code.innerHTML = '<a href="' + this.url + '?bcpid=' + this.playerId + '&bclid=' + this.playlistId + '&bctid=' + videos[i].id + '&refer=' + this.referName + '">' + image2 + '</a><div class="bc_title' + this.postfix + '">' + videos[i]['name'] + '<div class="bc_time' + this.postfix + '">' + timeString + '</div></div>';
			document.getElementById(this.id + 'BCWRB_inner').appendChild(code);
		}
	}
	
	this.manual = function(pCount) {		
		if(window.XMLHttpRequest) {
			this.httpRequest = new XMLHttpRequest();
			if(this.httpRequest.overrideMimeType) {
				this.httpRequest.overrideMimeType('text/xml');
			}
		} else if(window.ActiveXObject) {
			try {
				this.httpRequest = new ActiveXObject('Msxml2.XMLHTTP');
			}
			catch (e) {
				try {
					this.httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
				}
			catch (e) {}
			}
		}
		
		if(!this.httpRequest) {
			return false;
		}
		
		var thispointer = this;
		
		this.httpRequest.onreadystatechange = function() {
			thispointer.handler();
		};
	
		this.httpRequest.open('GET', this.xmlUrl, true);
		this.httpRequest.send('');
	}
	
	this.handler = function() {
		var tabs, tab, videos;
		var videoIds = '';
		
		if(this.httpRequest.readyState == 4) {
			if(this.httpRequest.status == 200) {
				this.xml = this.httpRequest.responseXML;
				tabs = this.xml.getElementsByTagName('lineup');
				tab = tabs[0];

				document.getElementById(this.id + 'BCWRB_title').innerHTML = this.TStab[0] || tab.getAttribute('name');
				videos = tab.getElementsByTagName('video');
				
				for(var i = 0; i < videos.length; i++) {
					var video = videos[i];
					var title = video.getElementsByTagName('title_id')[0].childNodes[0].nodeValue;
					
					videoIds += title;
					
					if(i + 1 < videos.length) {
						videoIds += ',';
					}
				}
				
				var code = document.createElement('script');
				code.setAttribute('src', 'http://api.brightcove.com/services/library?command=find_videos_by_ids&token=' + this.token + '&video_ids=' + videoIds + '&callback=bc_id[' + this.id + '].manualReturn');
				code.setAttribute('charset', 'UTF-8');
				code.setAttribute('type', 'text/javascript');
				document.getElementsByTagName('head')[0].appendChild(code);
			}
		}
	}

	this.manualReturn = function(pData) {
		var videos = pData['items'];
		var maximum = 5;
		var tabs = this.xml.getElementsByTagName('lineup');
		var tab = tabs[0];
		var xml_videos = tab.getElementsByTagName('video');
		
		for (var i = 0; i < videos.length; i++) {
			if (videos[i] == null)	{
				videos.splice(i, 1);
				var removednode = xml_videos[i];
				removednode.parentNode.removeChild(removednode);
				i--;
			}
			if (i >= maximum)
				break;
		}
		
		if(videos.length < maximum) {
			maximum = videos.length;
		}
		
		this.videoCount = maximum;
		
		for(var i = 0; i < maximum; i++) {
			var playerId = xml_videos[i].getElementsByTagName('player_id')[0].childNodes[0].nodeValue;
			var playlistId = xml_videos[i].getElementsByTagName('lineup_id')[0].childNodes[0].nodeValue;
			
			var minutes = Math.floor((videos[i]['length'] / 1000) / 60);
			var seconds = Math.round((((videos[i]['length'] / 1000) % 60) * 60) / 60);
			
			if(seconds < 10) {
				seconds = '0' + seconds;
			}
			
			var timeString = minutes + ':' + seconds;
			
			if(videos[i]['thumbnailURL']) {
				var image1 = '<img src="' + videos[i]['thumbnailURL'] + '" width="80" height="60" />';
				var image2 = '<img src="' + videos[i]['videoStillURL'] + '" width="240" height="180" />';
			} else {
				var image1 = '';
				var image2 = '';
			}
			
			var code = document.createElement('div');
			
			if(i == 0) {
				code.className = 'bc_video' + this.postfix + ' bc_video_over' + this.postfix;
			} else {
				code.className = 'bc_video' + this.postfix;
			}
			
			code.innerHTML = '<div class="bc_left' + this.postfix + '" onmouseover="bc_id[' + this.id + '].show(' + i + ')"><span>' + (i + 1) + '</span> ' + videos[i]['name'] + '</div><div class="bc_right' + this.postfix + '">' + timeString + '</div><div class="bc_meta' + this.postfix + '"><a href="' + this.url + '?bcpid=' + playerId + '&bclid=' + playlistId + '&bctid=' + videos[i].id + '&refer=' + this.referName + '">' + image1 + '</a><div class="bc_data' + this.postfix + '"><div class="bc_desc' + this.postfix + '">' + videos[i]['shortDescription'] + '</div><a href="' + this.url + '?bcpid=' + playerId + '&bclid=' + playlistId + '&bctid=' + videos[i].id + '&refer=' + this.referName + '"><img src="' + this.dir + this.postfix + '/bc_arrow.png" /></a><div class="bc_link' + this.postfix + '"><a href="' + this.url + '?bcpid=' + playerId + '&bclid=' + playlistId + '&bctid=' + videos[i].id + '&refer=' + this.referName + '">Abspielen</a></div></div></div></div>';
			
			if(i == 0) {
				if(this.ie) {
					code.style.height = '78px';
				} else {
					code.style.height = '78px';
				}
			} else {
				if(this.ie) {
					code.style.height = '107px';
				} else {
					code.style.height = '10px';
				}
			}

			document.getElementById(this.id + 'BCWRB_containerOne').appendChild(code);
			
			var code = document.createElement('div');
			code.className = 'bc_video' + this.postfix;
			code.innerHTML = '<a href="' + this.url + '?bcpid=' + playerId + '&bclid=' + playlistId + '&bctid=' + videos[i].id + '&refer=' + this.referName + '">' + image2 + '</a><div class="bc_title' + this.postfix + '">' + videos[i]['name'] + '<div class="bc_time' + this.postfix + '">' + timeString + '</div></div>';
			document.getElementById(this.id + 'BCWRB_inner').appendChild(code);
		}
	}

	this.show = function(pCount) {
		var children = document.getElementById(this.id + 'BCWRB_containerOne').childNodes;
		
		for(var i = 0; i < children.length; i++) {
			if(i == pCount) {
				if(this.ie) {
					children[i].style.height = '78px';
				} else {
					children[i].style.height = '78px';
				}
				
				children[i].className = 'bc_video' + this.postfix + ' bc_video_over' + this.postfix;
			} else {
				if(this.ie) {
					children[i].style.height = '10px';
				} else {
					children[i].style.height = '10px';
				}
				
				children[i].className = 'bc_video' + this.postfix;
			}
		}
	}
	
	this.view = function(pId) {
		if(pId == 1) {
			document.getElementById(this.id + 'BCWRB_containerOne').style.display = 'block';
			document.getElementById(this.id + 'BCWRB_containerTwo').style.display = 'none';
			document.getElementById(this.id + 'BCWRB_tab1').setAttribute('src', this.dir + this.postfix + '/tab_1_active.png');
			document.getElementById(this.id + 'BCWRB_tab2').setAttribute('src', this.dir + this.postfix + '/tab_2_inactive.png');
		} else {
			document.getElementById(this.id + 'BCWRB_containerOne').style.display = 'none';
			document.getElementById(this.id + 'BCWRB_containerTwo').style.display = 'block';
			document.getElementById(this.id + 'BCWRB_tab1').setAttribute('src', this.dir + this.postfix + '/tab_1_inactive.png');
			document.getElementById(this.id + 'BCWRB_tab2').setAttribute('src', this.dir + this.postfix + '/tab_2_active.png');
		}
	}
	
	this.slide = function(pDir) {
		if(!this.sliding) {
			el = document.getElementById(this.id + 'BCWRB_inner');
			
			if(pDir == 0) {
				if(this.page > 0) {
					this.page--;
					this.sliding = true;
					this.prepSlide(el, (this.page * -325));
				}
			} else {
				if(this.page + 1 < this.videoCount) {
					this.page++;
					this.sliding = true;
					this.prepSlide(el, (this.page * -325));
				}
			}
			
			this.arrows();
		}
	}
	
	this.arrows = function() {
		if(this.page > 0) {
			document.getElementById(this.id + 'BCWRB_arrowLeft').style.display = 'block';
		} else {
			document.getElementById(this.id + 'BCWRB_arrowLeft').style.display = 'none';
		}
		
		if(this.page + 1 < this.videoCount) {
			document.getElementById(this.id + 'BCWRB_arrowRight').style.display = 'block';
		} else {
			document.getElementById(this.id + 'BCWRB_arrowRight').style.display = 'none';
		}
	}
	
	this.getNum = function(pNum) {
		if(pNum) {
			if(pNum.indexOf('px') > -1) {
				return parseInt(pNum.substring(0, pNum.indexOf('px')));
			} else {
				return parseInt(pNum);
			}
		} else {
			return 0;
		}
	}

	this.prepSlide = function(pEl, pEnd) {
		var moveStart = this.getNum(pEl.style['left']);
		var amountToMove = pEnd - moveStart;
		var timeStart = new Date().getTime();
		var timeEnd = timeStart + 1000;
		
		this.doSlide(this.id, pEl, 'left', amountToMove, moveStart, 1000, timeEnd);
	}
	
	this.doSlide = function(id, pEl, pType, pDistance, pStart, pTimeTotal, pTimeEnd) {
		var currentTime = new Date().getTime();
		var timeRemaining = Math.max(0, pTimeEnd - currentTime);
		var currentMove = parseInt(pDistance - (Math.pow(timeRemaining, 3) / Math.pow(pTimeTotal, 3)) * pDistance);
		pEl.style[pType] = (pStart + currentMove) + 'px';
		
		if(timeRemaining > 0) {
		  setTimeout(function () { bc_id[id].doSlide(id, pEl, pType, pDistance, pStart, pTimeTotal, pTimeEnd); }, 10);
		} else {			
			this.sliding = false;
		}	
	}
}
