/**
 * Bugfix by Tony Simbine <t.simbine@oms.eu>
**/
var bc_baseUrl = '/videos/aktuelles/index.htm';
var bc_dir = '/brightcove/zgzblue/lw';
var bc_page = 0;
var bc_videoCount = 0;
var bc_sliding = false;
var bc_xmlPlayers = new Array();
var bc_xmlLineups = new Array();
var bc_isIE = (navigator.appVersion.toLowerCase().indexOf('msie 6.0') != -1);
var bc_isXml, bc_data;
var bc_refer_name = 'rightboxb';
bc_create();

function bc_getNum(pNum) {
	if (pNum) {
		if (pNum.indexOf('px') > -1) {
			return parseInt(pNum.substring(0, pNum.indexOf('px')));
		} else {
			return parseInt(pNum);
		}
	} else {
		return 0;
	}
}

function bc_create() {
	document.write('<link rel="stylesheet" href="' + bc_dir + '/bc_widget.css" />');
	document.write('<!--[If IE 6]><link rel="stylesheet" href="' + bc_dir + '/bc_widget_ie6.css" /><![endif]-->');
	document.write('<!--[If IE 7]><link rel="stylesheet" href="' + bc_dir + '/bc_widget_ie7.css" /><![endif]-->');
	document.write('<script type="text/javascript" src="http://admin.brightcove.com/js/BrightcoveExperiences.js" charset="UTF-8"></script>');
	document.write('<script type="text/javascript" src="http://admin.brightcove.com/js/APIModules_all.js" charset="UTF-8"></script>');
	document.write('<div id="bc_widget"><div id="bc_header"><div id="bc_widget_title">Videos</div><div id="bc_tab_1" onclick="bc_switch(1);"><img src="' + bc_dir + '/tab_1_active.png" /></div><div id="bc_tab_2" onclick="bc_switch(2);"><img src="' + bc_dir + '/tab_2_inactive.png" /></div></div>');
	document.write('<div id="bc_container_1" class="bc_container"><ul id="bc_video_list"></ul></div>');
	document.write('<div id="bc_container_2" class="bc_container" style="display: none;"><div id="bc_outerContainer"><div id="bc_arrow_left" onclick="bc_slide(0);"></div><div id="bc_arrow_right" onclick="bc_slide(1);"></div><div id="bc_innerContainer"></div></div></div>');
	document.write('<div id="bc_footer"><div><a href="' + bc_baseUrl + '">Alle Videos &raquo;</a></div></div></div>');
}

function bc_retrieve() {
	if (bc_lineupId) {
		bc_isXml = false;
		var new_script = document.createElement("script");
		new_script.setAttribute('src', 'http://api.brightcove.com/services/library?command=find_playlist_by_id&playlist_id=' + bc_lineupId + '&token=' + bc_token + '&callback=bc_return&video_fields=id,name,shortDescription,thumbnailURL,videoStillURL,length');
		new_script.setAttribute('type', 'text/javascript');
		new_script.setAttribute('charset','UTF-8');
		document.getElementsByTagName('head')[0].appendChild(new_script);
	} else {
		bc_isXml = true;
		var httpRequest, tab, videos;
		var videoIds = '';
		
		if (window.XMLHttpRequest) {
			httpRequest = new XMLHttpRequest();
			if (httpRequest.overrideMimeType) {
				httpRequest.overrideMimeType('text/xml');
			}
		} else if (window.ActiveXObject) {
			try {
				httpRequest = new ActiveXObject('Msxml2.XMLHTTP');
			}
			catch (e) {
				try {
					httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
				}
			catch (e) {}
			}
		}
		
		if (!httpRequest) {
			return false;
		}
		
		httpRequest.onreadystatechange = function() {
			if (httpRequest.readyState == 4) {
				if (httpRequest.status == 200) {
					xml = httpRequest.responseXML;
					tab = xml.getElementsByTagName('lineup')[0];
					
					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;
						bc_xmlPlayers[i] = video.getElementsByTagName('lineup_id')[0].childNodes[0].nodeValue;
						bc_xmlLineups[i] = video.getElementsByTagName('player_id')[0].childNodes[0].nodeValue;
						
						videoIds += title;
						
						if (i + 1 < videos.length) {
							videoIds += ',';
						}
					}
					
					var new_script = document.createElement("script");
					new_script.setAttribute('src', 'http://api.brightcove.com/services/library?command=find_videos_by_ids&token=' + bc_token + '&video_ids=' + videoIds + '&fields=id,name,thumbnailURL,length,shortDescription,videoStillURL&callback=bc_return');
					new_script.setAttribute('type', 'text/javascript');
					new_script.setAttribute('charset','UTF-8');
					document.getElementsByTagName('head')[0].appendChild(new_script);
				}
			}
		};
		
		httpRequest.open('GET', bc_dir + '/lineup.xml', true);
		httpRequest.send('');
	}
}

function bc_return(pData) {
	bc_data = pData;
	
	bc_generate();
}

function bc_generate() {
	if(bc_isXml) {
		var videos = bc_data.items;
	} else {
		var videos = bc_data.videos;
	}
	
	var containerOne = $('bc_video_list');
	var containerTwo = $('bc_innerContainer');
	var videosLength = 0;
	
	if(videos.length > 5) {
		videosLength = 5;
	} else {
		videosLength = videos.length;
	}
	
	for (var i = 0; i < videosLength; i++) {
		bc_videoCount++;
		var video = videos[i];
		var pMin = Math.floor((video.length / 1000) / 60);
		var pSec = Math.round((((video.length / 1000) % 60) * 60) / 60);

		if(pSec < 10) {
			pSec = '0' + pSec;
		}
		
		if(bc_isXml) {
			playerId = bc_xmlPlayers[i];
			lineupId = bc_xmlLineups[i];
		} else {
			playerId = bc_playerId;
			lineupId = bc_lineupId;
		}
		
		var html;
		
		if (i == 0) {
			html = '<li onmouseover="bc_toggle(' + i + ')" class="active">';
			html = html + '<div class="header"><span class="number">' + (i + 1) + '</span> <span class="title">' + video.name + '</span></div><div class="time">' + pMin + ':' + pSec + '</div><div class="metaData"><a href="' + bc_baseUrl + '?bcpid=' + playerId + '&bclid=' + lineupId + '&bctid=' + video.id + '&refer=' + bc_refer_name + '"><img src="' + video.thumbnailURL + '" width="80" height="60" /></a><div><div class="shortDescription">' + video.shortDescription + '</div><div class="playVideo"><a href="' + bc_baseUrl + '?bcpid=' + playerId + '&bclid=' + lineupId + '&bctid=' + video.id + '&refer=' + bc_refer_name + '"><img src="' + bc_dir + '/bc_arrow.png" /></a> <a href="' + bc_baseUrl + '?bcpid=' + playerId + '&bclid=' + lineupId + '&bctid=' + video.id + '&refer=' + bc_refer_name + '">Abspielen</a></div></div></div>';
			html = html + '</li>';
		} else {
			html = '<li onmouseover="bc_toggle(' + i + ')">';
			html = html + '<div class="header"><span class="number">' + (i + 1) + '</span> <span class="title">' + video.name + '</span></div><div class="time">' + pMin + ':' + pSec + '</div><div class="metaData" style="display: none;"><a href="' + bc_baseUrl + '?bcpid=' + playerId + '&bclid=' + lineupId + '&bctid=' + video.id + '&refer=' + bc_refer_name + '"><img src="' + video.thumbnailURL + '" width="80" height="60" /></a><div><div class="shortDescription">' + video.shortDescription + '</div><div class="playVideo"><a href="' + bc_baseUrl + '?bcpid=' + playerId + '&bclid=' + lineupId + '&bctid=' + video.id + '&refer=' + bc_refer_name + '"><img src="' + bc_dir + '/bc_arrow.png" /></a> <a href="' + bc_baseUrl + '?bcpid=' + playerId + '&bclid=' + lineupId + '&bctid=' + video.id + '&refer=' + bc_refer_name + '">Abspielen</a></div></div></div>';
			html = html + '</li>';
		}
		containerOne.innerHTML = containerOne.innerHTML + html;
		
		var videoTwo = document.createElement('div');
		videoTwo.className = 'bc_video';
		videoTwo.innerHTML = '<a href="' + bc_baseUrl + '?bcpid=' + playerId + '&bclid=' + lineupId + '&bctid=' + video.id + '&refer=' + bc_refer_name + '"><img src="' + video.videoStillURL + '" class="reflect" width="240" height="180" /></a><div class="title">' + video.name + '<div class="time">' + pMin + ':' + pSec + '</div></div>';
		containerTwo.appendChild(videoTwo);
	}
}

function bc_switch(pView) {
	if (pView == 1) {
		$('bc_container_1').style.display = 'block';
		$('bc_container_2').style.display = 'none';
		$('bc_tab_1').innerHTML = '<img src="' + bc_dir + '/tab_1_active.png" />';
		$('bc_tab_2').innerHTML = '<img src="' + bc_dir + '/tab_2_inactive.png" />';
	} else {
		$('bc_container_1').style.display = 'none';
		$('bc_container_2').style.display = 'block';
		$('bc_tab_1').innerHTML = '<img src="' + bc_dir + '/tab_1_inactive.png" />';
		$('bc_tab_2').innerHTML = '<img src="' + bc_dir + '/tab_2_active.png" />';
		
		if(bc_page == 0) {
			$('bc_arrow_left').style.display = 'none';
		} else {
			$('bc_arrow_left').style.display = 'block';
		}
	}
}

function bc_toggle(pCount) {
	var videoList = $('bc_video_list');
	var videos = videoList.getElementsByTagName('li');
	
	for (var i = 0; i < videos.length; i++) {
		var elements = videos[i].getElementsByTagName('div');
		var metaData = elements[2];
		
		if (i != pCount) {
			videos[i].className = '';
			metaData.style.display = 'none';
		} else {
			videos[i].className = 'active';
			metaData.style.display = 'block';
		}
	}
	
	if(bc_isIE) {
		videos[0].style.height = '10px';
		setTimeout('bc_ieFix', 10);
	}
}

function bc_ieFix() {
	var videoList = $('bc_video_list');
	var videos = videoList.getElementsByTagName('li');
	videos[0].style.height = 'auto';
}

function bc_slide(pDir) {
	if ((pDir == 0 && bc_page != 0) && !bc_sliding || (pDir == 1 && (bc_page + 1) < bc_videoCount) && !bc_sliding) {
		bc_sliding = true;
		
		if (pDir == 0) {
			bc_page--;
		} else {
			bc_page++;
		}
		
		if((bc_page + 1) == bc_videoCount) {
			$('bc_arrow_right').style.display = 'none';
		} else {
			$('bc_arrow_right').style.display = 'block';
		}
		
		if(bc_page == 0) {
			$('bc_arrow_left').style.display = 'none';
		} else {
			$('bc_arrow_left').style.display = 'block';
		}
		
		var position = bc_page * -310;		
		var element = $('bc_innerContainer');
		bc_prepSlide(element, position);
	}
}

function bc_prepSlide(pElementToMove, pMoveEnd) {
    var moveStart = bc_getNum(pElementToMove.style['left']);
    var amountToMove = pMoveEnd - moveStart;
    var timeStart = new Date().getTime ();
    
    bc_doSlideElement(pElementToMove, 'left', amountToMove, moveStart, 1000, timeStart + 1000);
}

function bc_doSlideElement(pElementToMove, pType, pAmountToMove, pMoveStart, pTimeToTake, pTimeEnd) {
    var currentTime = new Date().getTime();
    var timeRemaining = Math.max(0, pTimeEnd - currentTime);
    var currentMove = parseInt(pAmountToMove - (Math.pow(timeRemaining, 3) / Math.pow(pTimeToTake, 3)) * pAmountToMove);
    pElementToMove.style[pType] = (pMoveStart + currentMove) + 'px';
    
    if (timeRemaining > 0) {
      setTimeout(function() { bc_doSlideElement(pElementToMove, pType, pAmountToMove, pMoveStart, pTimeToTake, pTimeEnd); }, 10);
    } else {
    	bc_sliding = false;
    }
}
