/**
 * Bugfix by Tony Simbine <t.simbine@oms.eu>
**/
var bc_token = 'gPVgciZpiW8xZzAfOe8t6W0uP9xs6Y_gLbNA4ZH5LaQ.';
var bc_baseUrl = '/videos/index.html';
var bc_dir = 1232722442;
if(typeof(bc_playerId) == "undefined") var bc_playerId = '8557953001';
if(typeof(bc_lineupId) == "undefined") var bc_lineupId = '8522166001';
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();

window.onload = function() {
	try {
		$
	} catch(e) {
		$ = function(pId) {
			return document.getElementById(pId)
		};
	}
	
	bc_retrieve();
}

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="BCW_' + bc_dir + '/bc_widget.css" />');
	document.write('<!--[If IE 6]><link rel="stylesheet" href="BCW_' + bc_dir + '/bc_widget_ie6.css" /><![endif]-->');
	document.write('<!--[If IE 7]><link rel="stylesheet" href="BCW_' + bc_dir + '/bc_widget_ie7.css" /><![endif]-->');
	document.write('<div class="brightcove_bigthumb">');
        document.write('<div class="overlay" id="bc_big_thumb"></div>');
        document.write('<div class="playicon">');
        document.write('<a href="." id="bclink">');
        document.write('<img style="filter:alpha(opacity=33);opacity:0.33" onmouseover="this.style.filter=\'alpha(opacity=75)\';this.style.opacity=\'0.75\'" onmouseout="this.style.filter=\'alpha(opacity=33)\';this.style.opacity=\'0.33\'" src="/pics/brightcove/brightcove_playicon.gif" width="300" height="169" border="0">');
        document.write('</a>');
        document.write('</div>');
        document.write('</div>');
        document.write('<div class="brightcove_description"><div id="brightcove_description">&nbsp;</div></div>');
        document.write('<div id="bc_container_1" class="bc_container"><div id="bc_video_list"></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');
		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 < 9; 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', 'BCW_' + 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 videosLength = 0;
	
	if(videos.length > 10) {
		videosLength = 10;
	} else {
		videosLength = videos.length;
	}


        var anzahl = 3;

        html = '<table cellspacing="0" cellpadding="0" border="0">';
        html += '<tr>';
        html +='<td class="brightcove_preview_arrow" align="right" valign="top"><img id="brightcove_links" src="http://www.ruhrnachrichten.de/pics/brightcove/pfeil_links.gif" onMouseOver="this.src=\'http://www.ruhrnachrichten.de/pics/brightcove/pfeil_links_aktiv.gif\'" onMouseOut="this.src=\'http://www.ruhrnachrichten.de/pics/brightcove/pfeil_links.gif\'" width="11" height="50" style="margin-right:1px;cursor:pointer" onclick="scroll(\'left\')"></td>';
        html += '<td>';
        html += '<div class="brightcove_preview_box" id="brightcove_preview">';
	html += '<table cellspacing="0" cellpadding="0" border="0" id="brightcove_preview_elements">';
        html += '<tr>';

        bc_bigthumbs = new Array();

	
	for (var i = 0; i < 9; 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;
		}
		
		playerlink = bc_baseUrl + '?bcpid=' + playerId + '&bclid=' + lineupId + '&bctid=' + video.id + '&refer=' + bc_refer_name;

                if ( i == 0 ) {
                  if(typeof(video.videoStillURL) == "string") {
	                  var videoStillURL = video.videoStillURL;
                  } else {
	                  var videoStillURL = 'http://www.ruhrnachrichten.de/pics/brightcove/keinbild_300.jpg';
		  }
                  document.getElementById('bc_big_thumb').innerHTML = '<img src="' + videoStillURL + '" width="300" id="brightcove_bigthumbnail">';
                  document.getElementById('brightcove_description').innerHTML = video.name;
                  document.getElementById('bclink').href = playerlink;
                }

		var html;
                var videoStill = new Image();
                if(typeof(video.videoStillURL) == "string") {
                  videoStill.src = video.videoStillURL;
		} else {
	          videoStill.src = 'http://www.ruhrnachrichten.de/pics/brightcove/keinbild_300.jpg';
		}
                bc_bigthumbs.push ( new Array( videoStill, video.name ) );
	        html += '<td class="brightcove_preview" valign="top"><div class="crop">';
                if(typeof(video.videoStillURL) == "string") {
	          var imgsrc = video.thumbnailURL;
                } else {
	          var imgsrc = 'http://www.ruhrnachrichten.de/pics/brightcove/keinbild_88.jpg';
                }
                html += '<div style="top:-8px;position:relative"><a href="' + playerlink + '" onMouseOver="document.getElementById(\'bclink\').href=this.href"><img src="' + imgsrc + '" width="88" onMouseOver="brightcove_changer('+i+')" border="0"';
                if (i != 0) {
                  html += ' style="filter:alpha(opacity=50);opacity:0.5"';
                }
	        html += '></a>';
	        html += '</div></td>';


	}

        html += '</tr>';
        html += '</table>';
        html += '</div>';
        html += '</td>';
        html += '<td class="brightcove_preview_arrow" align="left" valign="top"><img id="brightcove_rechts" src="http://www.ruhrnachrichten.de/pics/brightcove/pfeil_rechts.gif" onMouseOver="this.src=\'http://www.ruhrnachrichten.de/pics/brightcove/pfeil_rechts_aktiv.gif\'" onMouseOut="this.src=\'http://www.ruhrnachrichten.de/pics/brightcove/pfeil_rechts.gif\'" width="11" height="50" style="margin-left:1px;cursor:pointer" onclick="scroll(\'right\')"></td>';
        html += '</tr>';
        html += '</table>';

	containerOne.innerHTML = containerOne.innerHTML + html;

}

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="BCW_' + bc_dir + '/tab_1_active.png" />';
		$('bc_tab_2').innerHTML = '<img src="BCW_' + 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="BCW_' + bc_dir + '/tab_1_inactive.png" />';
		$('bc_tab_2').innerHTML = '<img src="BCW_' + 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 < 9; 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 * -300;		
		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;
    }
}

    function scroll (direction) {
      var brightcove_cols = document.getElementById('brightcove_preview_elements').getElementsByTagName('TD');
      var brightcove_distance = brightcove_cols[1].offsetLeft;
      if ( direction == "right" ) {
        if ( typeof ( next_position ) == "undefined" && typeof ( last_position ) == "undefined" ) {
          if ( typeof ( position ) == "undefined" ) {
            position = 3;
          } else {
            position += 3;
          }
          if ( position <= brightcove_cols.length - 3 ) {
            this_position = brightcove_cols[position-3].offsetLeft;
            next_position = brightcove_cols[position].offsetLeft;
            scrolling = window.setInterval("scroll('right')",20);
            mover = 1;
          } else {
            position -= 3;
          }
        }
        if ( typeof ( mover ) != "undefined" ) {
          if ( document.getElementById( 'brightcove_preview' ).scrollLeft + mover >= next_position ) {
            document.getElementById( 'brightcove_preview' ).scrollLeft = next_position;
            window.clearInterval(scrolling);
            mover = 0;
          }
          document.getElementById('brightcove_preview').scrollLeft += mover;
          if ( document.getElementById( 'brightcove_preview' ).scrollLeft == next_position ) {
            delete ( mover );
            delete ( next_position );
            delete ( this_position );
          } else if ( document.getElementById( 'brightcove_preview' ).scrollLeft < this_position + ( next_position - this_position ) / 2 ) {
            mover = Math.round(mover*1.5);
          } else {
            mover = Math.round(mover/1.4);
          }
        }
      }
      if ( direction == "left" ) {
        if ( typeof ( next_position ) == "undefined" && typeof ( last_position ) == "undefined" ) {
          if ( position != 0 ) {
            position -= 3;
            this_position = brightcove_cols[position+3].offsetLeft;
            last_position = brightcove_cols[position].offsetLeft;
            scrolling = window.setInterval("scroll('left')",20);
            mover = 1;
          }
        }
        if ( typeof ( mover ) != "undefined" ) {
          if ( document.getElementById( 'brightcove_preview' ).scrollLeft - mover <= last_position ) {
            document.getElementById( 'brightcove_preview' ).scrollLeft = last_position;
            window.clearInterval(scrolling);
            mover = 0;
          }
          document.getElementById('brightcove_preview').scrollLeft -= mover;
          if ( document.getElementById( 'brightcove_preview' ).scrollLeft == last_position ) {
            delete ( mover );
            delete ( next_position );
            delete ( last_position );
          } else if ( document.getElementById( 'brightcove_preview' ).scrollLeft > this_position - ( this_position - last_position ) / 2 ) {
            mover = Math.round(mover*1.5);
          } else {
            mover = Math.round(mover/1.4);
          }
        }
      }
    }

    function brightcove_changer ( id ) {
      document.getElementById('brightcove_bigthumbnail').src = bc_bigthumbs[id][0].src;
      document.getElementById('brightcove_description').innerHTML = bc_bigthumbs[id][1];
      var preview_elements = document.getElementById('brightcove_preview_elements').getElementsByTagName('IMG');
      for(i=0;i<preview_elements.length;i++){
        if (i == id) {
          preview_elements[i].style.opacity = "1";
          preview_elements[i].style.filter = "alpha(opacity=100)";
        } else {
          preview_elements[i].style.opacity = "0.5";
          preview_elements[i].style.filter = "alpha(opacity=50)";
        }
      }
    }
