var bc_dir = '1225810356';
var bc_token = 'Pkxykh7a2LzGiVDbIXQbyLqlmzPxD6yK17c9B2q3xjM.';
var bc_base_url = 'http://medien.niedersachsen.com/VIDEO/HAZ/HAZ_online3v8/index.html';
var bc_width = 426;
var bc_page = 0;
var bc_currentTab = 1;
var bc_scriptCount = 1;
var xml = false;
var bc_videoCount_1, bc_pageCount_1, bc_videoCount_2, bc_pageCount_2;
var bc_player_id_1 = '1896788706';
var bc_playlist_id_1 = '1704115725';
//var bc_player_id_2 = '1896802339';
//var bc_playlist_id_2 = '1704093166';
var bc_player_id_2 = '1896837901';
var bc_playlist_id_2 = '1704094694';

bc_generateWidget();

window.onload = function() {	
	bc_retrieveOne();
}

function utfDecode(s) {
    var ret = '';

    for(var i = 0; i < s.length; i++)
    {
        if(s.charCodeAt(i) >= 194) {
            var first = s.charCodeAt(i);
            var second = s.charCodeAt(i+1)
            var pre = first.toString() + second.toString();
            var letter = s.charAt(i);
            var letter2 = s.charAt(i+1);

            switch(pre) {
                case '195164':
                    ret += '&auml;';
                    break;
                case '195182':
                    ret += '&ouml;';
                    break;
                case '195188':
                    ret += '&uuml;';
                    break;
                case '1958222':
                    ret += '&Auml;';
                    break;
                case '1958211':
                    ret += '&Ouml;';
                    break;
                case '195339':
                    ret += '&Uuml;';
                    break;
                case '195376':
                    ret += '&szlig;';
                    break;
                default:
                    ret += letter+letter2;
                    break;
            }
                        
            i++;
        } else {
            ret += s.substr(i,1);
        }
    }
    
    return ret;
}






function bc_generateWidget() {
	document.write('<link rel="stylesheet" href="http://medien.niedersachsen.com/VIDEO/HAZ/Widget_1225810356/BCW_' + bc_dir + '/bc_widget.css" />');
	document.write('<!--[If IE 6]><link rel="stylesheet" href="http://medien.niedersachsen.com/VIDEO/HAZ/Widget_1225810356/BCW_' + bc_dir + '/bc_widget_ie6.css" /><![endif]-->');
	document.write('<!--[If IE 7]><link rel="stylesheet" href="http://medien.niedersachsen.com/VIDEO/HAZ/Widget_1225810356/BCW_' + bc_dir + '/bc_widget_ie7.css" /><![endif]-->');
	document.write('<script type="text/javascript" src="http://admin.brightcove.com/js/BrightcoveExperiences.js"></script>');
	document.write('<script type="text/javascript" src="http://admin.brightcove.com/js/APIModules_all.js"></script>');
	document.write('<div id="bc_widget"><div id="bc_header"><div id="bc_tab_1" class="selected" onclick="bc_switchLineup(1)"></div><div id="bc_tab_2" class="deselected" onclick="bc_switchLineup(2)"></div><span><a href="' + bc_base_url + '" '+ 'target="_top">Alle Videos</a> &raquo;</span><br clear="all" /></div><div id="bc_paging"><ul id="bc_paging_list"></ul><br clear="all" /></div><div id="bc_container_1" class="bc_container">');
	document.write('<img src="http://medien.niedersachsen.com/VIDEO/HAZ/Widget_1225810356/BCW_' + bc_dir + '/arrow_left.png" id="bc_arrow_left_1" class="bc_arrow_left_disabled" width="19" height="19" onclick="bc_slide(\'left\')" onmouseover="bc_arrowUpdate(1,1,1)" /><div id="bc_outerContainer_1" class="bc_outerContainer"><div id="bc_innerContainer_1" class="bc_innerContainer"></div></div>');
	document.write('<img src="http://medien.niedersachsen.com/VIDEO/HAZ/Widget_1225810356/BCW_' + bc_dir + '/arrow_right.png" id="bc_arrow_right_1" class="bc_arrow_right" width="19" height="19" onclick="bc_slide(\'right\')" onmouseover="bc_arrowUpdate(1,2,1)" /></div><div id="bc_container_2" class="bc_container" style="display: none;">');
	document.write('<img src="http://medien.niedersachsen.com/VIDEO/HAZ/Widget_1225810356/BCW_' + bc_dir + '/arrow_left.png" id="bc_arrow_left_2" class="bc_arrow_left_disabled" width="19" height="19" onclick="bc_slide(\'left\')" onmouseover="bc_arrowUpdate(2,1,1)" /><div id="bc_outerContainer_2" class="bc_outerContainer"><div id="bc_innerContainer_2" class="bc_innerContainer"></div></div>');
	document.write('<img src="http://medien.niedersachsen.com/VIDEO/HAZ/Widget_1225810356/BCW_' + bc_dir + '/arrow_right.png" id="bc_arrow_right_2" class="bc_arrow_right" width="19" height="19" onclick="bc_slide(\'right\')" onmouseover="bc_arrowUpdate(2,2,1)" /></div></div>');
}

function bc_retrieveOne() {
	if (parseInt(bc_playlist_id_1) == bc_playlist_id_1) {
		bc_getVideos_Auto(1);
	} else {
		bc_getVideos_Manual(1);
	}
}

function bc_retrieveTwo() {
	if (parseInt(bc_playlist_id_2) == bc_playlist_id_2) {
		bc_getVideos_Auto(2);
	} else {
		bc_getVideos_Manual(2);
	}
}

function bc_getVideos_Auto(pCount) {
	if (pCount == 1) {
		var bc_playlist_id = bc_playlist_id_1;
	} else {
		var bc_playlist_id = bc_playlist_id_2;
	}
	
	var new_script = document.createElement("script");
	new_script.setAttribute('src', 'http://api.brightcove.com/services/library?command=find_playlist_by_id&playlist_id=' + bc_playlist_id + '&token=' + bc_token + '&callback=bc_callbackVideos_Auto');
	new_script.setAttribute('type', 'text/javascript');
	document.getElementsByTagName('head')[0].appendChild(new_script);
}

function bc_getVideos_Manual(pCount) {
	var httpRequest, tabs, 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;
				tabs = xml.getElementsByTagName('lineup');
				
				for (var i = 0; i < tabs.length; i++) {
					var tabId = tabs[i].getAttribute('tab');
					
					if (tabId == pCount) {
						tab = tabs[i];
					}
				}
				
				var tabName = tab.getAttribute('name');
				document.getElementById('bc_tab_' + pCount).innerHTML = tabName;
				
				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 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,publishedDate&callback=bc_callbackVideos_Manual');
				new_script.setAttribute('type', 'text/javascript');
				document.getElementsByTagName('head')[0].appendChild(new_script);
			}
		}
	};
	
	httpRequest.open('GET', 'http://medien.niedersachsen.com/VIDEO/HAZ/Widget_' + bc_dir + '/lineup.xml', true);
	httpRequest.send('');
}

function bc_callbackVideos_Auto(pData) {
	if (pData['videos'].length > 15) {
		var bc_max = 15;
	} else {
		var bc_max = pData['videos'].length;
	}
	
	if (bc_scriptCount == 1) {
		bc_videoCount_1 = bc_max;
		bc_pageCount_1 = Math.ceil(bc_max / 3);
		for (var i = 0; i < bc_pageCount_1; i++) {
			if (i == 0) {
				document.getElementById('bc_paging_list').innerHTML = '<li class="selected"></li>';
			} else {
				document.getElementById('bc_paging_list').innerHTML = document.getElementById('bc_paging_list').innerHTML + '<li></li>';
			}
		}
	} else {
		bc_videoCount_2 = bc_max;
		bc_pageCount_2 = Math.ceil(bc_max / 3);
	}
	
	document.getElementById('bc_tab_' + bc_scriptCount).innerHTML = pData['name'];

	for (var i = 0; i < bc_max; i++) {
		var bc_vidId = pData['videos'][i]['id'];
		var bc_vidTitle = pData['videos'][i]['name'];
		var bc_vidThumb = pData['videos'][i]['thumbnailURL'];
		var pMin = Math.floor((pData['videos'][i]['length'] / 1000) / 60);
		var pSec = Math.round((((pData['videos'][i]['length'] / 1000) % 60) * 60) / 60);
		if (pSec < 10) {
			pSec = '0' + pSec;
		}
		var bc_vidLength = pMin + ':' + pSec;
		var pDate = new Date();
		pDate.setTime(pData['videos'][i]['publishedDate']);
		var bc_vidDate = pDate.getDate() + '.' + (pDate.getMonth() + 1) + '.' + (pDate.getYear() + 1900);

		if (bc_scriptCount == 1) {
			var bc_player_id = bc_player_id_1;
			var bc_playlist_id = bc_playlist_id_1;
		} else {
			var bc_player_id = bc_player_id_2;
			var bc_playlist_id = bc_playlist_id_2;			
		}
		if (navigator.appName=="Microsoft Internet Explorer") 
			{
		pHtml = '<div class="bc_video"><div class="bc_thumb" onclick="top.frames.location=\'' + bc_base_url + '?bcpid=' + bc_player_id + '&bclid=' + bc_playlist_id + '&bctid=' + bc_vidId + '\'" onmouseover="bc_overlay(this,1)" onmouseout="bc_overlay(this,0)"><img src="' + bc_vidThumb + '" width="120" height="90" /><div class="bc_thumb_overlay" style="visibility:hidden;"></div></div><div class="title">' + utfDecode(bc_vidTitle) +  '</div><div class="time">' + bc_vidLength + ' | ' + bc_vidDate + '</div></div>';
		document.getElementById('bc_innerContainer_' + bc_scriptCount).innerHTML = document.getElementById('bc_innerContainer_' + bc_scriptCount).innerHTML + pHtml;
			}
	else
		{
		pHtml = '<div class="bc_video"><div class="bc_thumb" onclick="top.frames.location=\'' + bc_base_url + '?bcpid=' + bc_player_id + '&bclid=' + bc_playlist_id + '&bctid=' + bc_vidId + '\'" onmouseover="bc_overlay(this,1)" onmouseout="bc_overlay(this,0)"><img src="' + bc_vidThumb + '" width="120" height="90" /><div class="bc_thumb_overlay" style="visibility:hidden;"></div></div><div class="title">' + bc_vidTitle +  '</div><div class="time">' + bc_vidLength + ' | ' + bc_vidDate + '</div></div>';
		document.getElementById('bc_innerContainer_' + bc_scriptCount).innerHTML = document.getElementById('bc_innerContainer_' + bc_scriptCount).innerHTML + pHtml;
		}
	
	
	
	
	}

	bc_configureWidget();

	if (bc_scriptCount == 1) {
		bc_scriptCount++;
		bc_retrieveTwo();
	}
}

function bc_callbackVideos_Manual(pData) {
	if (pData['items'].length > 15) {
		var bc_max = 15;
	} else {
		var bc_max = pData['items'].length;
	}
	
	if (bc_scriptCount == 1) {
		bc_videoCount_1 = bc_max;
		bc_pageCount_1 = Math.ceil(bc_max / 3);
		for (var i = 0; i < bc_pageCount_1; i++) {
			if (i == 0) {
				document.getElementById('bc_paging_list').innerHTML = '<li class="selected"></li>';
			} else {
				document.getElementById('bc_paging_list').innerHTML = document.getElementById('bc_paging_list').innerHTML + '<li></li>';
			}
		}
	} else {
		bc_videoCount_2 = bc_max;
		bc_pageCount_2 = Math.ceil(bc_max / 3);
	}

	for (var i = 0; i < bc_max; i++) {
		var bc_vidId = pData['items'][i]['id'];
		var bc_vidTitle = pData['items'][i]['name'];
		var bc_vidThumb = pData['items'][i]['thumbnailURL'];
		var pMin = Math.floor((pData['items'][i]['length'] / 1000) / 60);
		var pSec = Math.round((((pData['items'][i]['length'] / 1000) % 60) * 60) / 60);
		if (pSec < 10) {
			pSec = '0' + pSec;
		}
		var bc_vidLength = pMin + ':' + pSec;
		var pDate = new Date();
		pDate.setTime(pData['items'][i]['publishedDate']);
		var bc_vidDate = pDate.getDate() + '.' + (pDate.getMonth() + 1) + '.' + (pDate.getYear() + 1900);

		if (bc_scriptCount == 1) {
			var bc_player_id = bc_player_id_1;
			var bc_playlist_id = bc_playlist_id_1;
		} else {
			var bc_player_id = bc_player_id_2;
			var bc_playlist_id = bc_playlist_id_2;			
		}

		var tabs = xml.getElementsByTagName('lineup');
		
		for (var q = 0; q < tabs.length; q++) {
			var tabId = tabs[q].getAttribute('tab');
			
			if (tabId == bc_scriptCount) {
				tab = tabs[q];
			}
		}
		
		var tabName = tab.getAttribute('name');
		var videos = tab.getElementsByTagName('video');
		
		var video = videos[i];
		var lineup_id = video.getElementsByTagName('lineup_id')[0].childNodes[0].nodeValue;
		var player_id = video.getElementsByTagName('player_id')[0].childNodes[0].nodeValue;
		
		pHtml = '<div class="bc_video"><div class="bc_thumb" onclick="top.frames.location=\'' + bc_base_url + '?bcpid=' + player_id + '&bclid=' + lineup_id + '&bctid=' + bc_vidId +  'target="_top"' +'\'" onmouseover="bc_overlay(this,1)" onmouseout="bc_overlay(this,0)"><img src="' + bc_vidThumb + '" width="120" height="90" /><div class="bc_thumb_overlay" style="visibility:hidden;"></div></div><div class="title">' + bc_vidTitle + '</div><div class="time">' + bc_vidLength + ' | ' + bc_vidDate + '</div></div>';
		document.getElementById('bc_innerContainer_' + bc_scriptCount).innerHTML = document.getElementById('bc_innerContainer_' + bc_scriptCount).innerHTML + pHtml;
	}

	bc_configureWidget();

	if (bc_scriptCount == 1) {
		bc_scriptCount++;
		bc_retrieveTwo();
	}
}

function bc_configureWidget() {
	var bc_elWidth = (bc_width - 58) / 2;
	var bc_modulus = (bc_elWidth - 128) % 2;
	var bc_padding = ((bc_elWidth - 128) - bc_modulus) / 2;

	document.getElementById('bc_widget').style.width = bc_width + 'px';
	document.getElementById('bc_paging_list').style.left = Math.ceil((bc_width - document.getElementById('bc_paging_list').offsetWidth)/2) + 'px';
	
	if (bc_scriptCount == 1) {
		document.getElementById('bc_outerContainer_1').style.width = (bc_width - 58) + 'px';
		document.getElementById('bc_innerContainer_1').style.width = Math.ceil((bc_elWidth + 10) * bc_videoCount_1) + 'px';
		document.getElementById('bc_arrow_right_1').style.left = (bc_width - 24) + 'px';
		var bc_videoEls = document.getElementById('bc_innerContainer_1').childNodes;
		for (var i = 0; i < bc_videoEls.length; i++) {
			bc_videoEls[i].style.padding = '0 ' + Math.round(bc_padding + bc_modulus) + 'px 0 ' + Math.ceil(bc_padding) + 'px';
		}
		
		bc_arrowCheck();
	}

	if (bc_scriptCount == 2) {
		document.getElementById('bc_outerContainer_2').style.width = (bc_width - 58) + 'px';
		document.getElementById('bc_innerContainer_2').style.width = Math.ceil((bc_elWidth + 10) * bc_videoCount_2) + 'px';
		document.getElementById('bc_arrow_right_2').style.left = (bc_width - 24) + 'px';
		var bc_videoEls = document.getElementById('bc_innerContainer_2').childNodes;
		for (var i = 0; i < bc_videoEls.length; i++) {
			bc_videoEls[i].style.padding = '0 ' + Math.round(bc_padding + bc_modulus) + 'px 0 ' + Math.ceil(bc_padding) + 'px';
		}
	}
}

function bc_arrowCheck() {
	if (bc_currentTab == 1) {
		bc_pageCount = bc_pageCount_1;
	} else {
		bc_pageCount = bc_pageCount_2;
	}

	if (bc_page + 1 == bc_pageCount) {
		document.getElementById('bc_arrow_right_' + bc_currentTab).className = 'bc_arrow_right_disabled';
	} else {
		document.getElementById('bc_arrow_right_' + bc_currentTab).className = 'bc_arrow_right';
	}

	if (bc_page == 0) {
		document.getElementById('bc_arrow_left_' + bc_currentTab).className = 'bc_arrow_left_disabled';
	} else {
		document.getElementById('bc_arrow_left_' + bc_currentTab).className = 'bc_arrow_left';
	}
}

function bc_arrowUpdate(pCount, pSide, pState) {
	switch(pSide) {
		case 1:
			var side = 'left';
			break;
		case 2:
			var side = 'right';
			break;
	}
	
	var arrow = document.getElementById('bc_arrow_' + side + '_' + pCount);
	
	if (pState == 1) {
		arrow.src = 'http://medien.niedersachsen.com/VIDEO/HAZ/Widget_1225810356/BCW_' + bc_dir + '/arrow_' + side + '_hover.png';
		arrow.onmouseover = function() { };
		arrow.onmouseout = function() { bc_arrowUpdate(pCount, pSide, 0); }
	} else {
		arrow.src = 'http://medien.niedersachsen.com/VIDEO/HAZ/Widget_1225810356/BCW_' + bc_dir + '/arrow_' + side + '.png';
		arrow.onmouseover = function() { bc_arrowUpdate(pCount, pSide, 1); };
		arrow.onmouseout = function() { }
	}
}

function bc_overlay(pEl, pState) {
	if (pState == 1) {
		pEl.style.border = '1px solid #0088bd';
		pEl.lastChild.style.visibility = 'visible';
	} else {
		pEl.style.border = '1px solid #ffffff';
		pEl.lastChild.style.visibility = 'hidden';
	}
}

function bc_switchLineup(pTab) {
	bc_currentTab = pTab;
	bc_page = 0;
	
	bc_arrowCheck();

	document.getElementById('bc_innerContainer_1').style.left = '0px';
	document.getElementById('bc_innerContainer_2').style.left = '0px';
	document.getElementById('bc_arrow_left_1').className = 'bc_arrow_left_disabled';
	document.getElementById('bc_arrow_left_2').className = 'bc_arrow_left_disabled';
	document.getElementById('bc_paging_list').innerHTML = '';
	bc_slideFinished();
	
	if (pTab == 1) {
		document.getElementById('bc_container_1').style.display = 'block';
		document.getElementById('bc_tab_1').className = 'selected';
		document.getElementById('bc_container_2').style.display = 'none';
		document.getElementById('bc_tab_2').className = 'deselected';
		for (var i = 0; i < bc_pageCount_1; i++) {
			if (i == 0) {
				document.getElementById('bc_paging_list').innerHTML = '<li class="selected"></li>';
			} else {
				document.getElementById('bc_paging_list').innerHTML = document.getElementById('bc_paging_list').innerHTML + '<li></li>';
			}
		}
	} else {
		document.getElementById('bc_container_1').style.display = 'none';
		document.getElementById('bc_tab_1').className = 'deselected';
		document.getElementById('bc_container_2').style.display = 'block';
		document.getElementById('bc_tab_2').className = 'selected';
		for (var i = 0; i < bc_pageCount_2; i++) {
			if (i == 0) {
				document.getElementById('bc_paging_list').innerHTML = '<li class="selected"></li>';
			} else {
				document.getElementById('bc_paging_list').innerHTML = document.getElementById('bc_paging_list').innerHTML + '<li></li>';
			}
		}
	}
	
	document.getElementById('bc_paging_list').style.left = Math.ceil((bc_width - document.getElementById('bc_paging_list').offsetWidth)/2) + 'px';
}

function bc_slide(pDirection) {
	if (bc_currentTab == 1) {
		var bc_pageCount = bc_pageCount_1;
	} else {
		var bc_pageCount = bc_pageCount_2;
	}
	
	if (pDirection == 'right') {
		if (bc_pageCount > (bc_page + 1)) {
			bc_page = bc_page + 1;
		}
	} else {
		if (bc_page > 0) {
			bc_page = bc_page - 1;
		}
	}
	
	if (bc_page == 0) {
		document.getElementById('bc_arrow_left_' + bc_currentTab).className = 'bc_arrow_left_disabled';
	} else {
		document.getElementById('bc_arrow_left_' + bc_currentTab).className = 'bc_arrow_left';
	}
	
	if ((bc_page + 1) == bc_pageCount) {
		document.getElementById('bc_arrow_right_' + bc_currentTab).className = 'bc_arrow_right_disabled';
	} else {
		document.getElementById('bc_arrow_right_' + bc_currentTab).className = 'bc_arrow_right';
	}
	
	if ((bc_width % 3) > 0) {
		var left = bc_page * -(bc_width - 56);
	} else {
		var left = bc_page * -(bc_width - 54);
	}
	
	bc_prepSlide(document.getElementById('bc_innerContainer_' + bc_currentTab),left);
}

function bc_getNum(num) {
	if (num) {
		if(num.indexOf('px') > -1) {
			return parseInt(num.substring(0, num.indexOf('px')));
		} else {
			return parseInt(num);
		}
	} else {
		return 0;
	}
}

function bc_prepSlide(pElementToMove, pMoveEnd) {
    var moveStart = bc_getNum(pElementToMove.style['left']);
    var amountToMove = pMoveEnd - moveStart;
    var timeStart = new Date().getTime ();
    var timeEnd = timeStart + 1000;
    
    bc_doSlideElement(pElementToMove, 'left', amountToMove, moveStart, 1000, timeEnd);
}

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_slideFinished();
	}
}

function bc_slideFinished() {
	var parentEl = document.getElementById('bc_paging_list');
	var childrenEl = parentEl.childNodes;
	
	for (var i = 0; i < childrenEl.length; i++) {
		if (i != bc_page) {
			childrenEl[i].className = '';
		} else {
			childrenEl[i].className = 'selected';
		}
	}
}
