/*******************************
This js file contains only functions of the portal side
**********************************/
var baseURL = '/VideoActive';
var selectedPage = '';
var jsReady = false;

function pageInit() {
	jsReady = true;
}

function copyYears() {
	document.getElementById('yearAfter').value 	= document.getElementsByName('yearAfterF')[0].options[document.getElementsByName('yearAfterF')[0].selectedIndex].value;
	document.getElementById('yearBefore').value	= document.getElementsByName('yearBeforeF')[0].options[document.getElementsByName('yearBeforeF')[0].selectedIndex].value;
}

function doItToThis(title, engtitle, publisher){
	var country = '';
	var logo = '';
	var pub = publisher;
	if(publisher == 'BBC'){
		country = document.getElementById('country_bbc').value;
		logo = 'bbc.png';
	}
	if(publisher == 'DR'){
		country = document.getElementById('country_dr').value;
		logo = 'dr.png';
	}
	if(publisher == 'DW'){
		country = document.getElementById('country_dw').value;
		logo = 'dw.png';
		pub = 'Deutsche Welle';
	}
	if(publisher == 'HENAA'){
		country = document.getElementById('country_henaa').value;
		logo = 'henaa.jpg';
	}
	if(publisher == 'IL'){
		country = document.getElementById('country_il').value;
		logo = 'il.png';
		pub = 'Istituto Luce';
	}
	if(publisher == 'INA'){
		country = document.getElementById('country_ina').value;
		logo = 'ina.png';
	}
	if(publisher == 'MIC'){
		country = document.getElementById('country_mic').value;
		pub = 'Moving Images';
	}
	if(publisher == 'NAVA'){
		country = document.getElementById('country_nava').value;
		logo = 'nava.png';
	}
	if(publisher == 'NOTERIK'){
		country = document.getElementById('country_noterik').value;
		logo = 'noterik.png';
	}
	if(publisher == 'ORF'){
		country = document.getElementById('country_orf').value;
		logo = 'orf.png';
	}
	if(publisher == 'RTBF'){
		country = document.getElementById('country_rtbf').value;
		logo = 'rtbf.png';
	}
	if((publisher == 'S&V')||(publisher == 'SV')||(publisher == 'SEV')){
		country = document.getElementById('country_sev').value;
		logo = 'sv.png';
		pub = 'Beeld en Geluid';
	}
	if(publisher == 'SLBA'){
		country = document.getElementById('country_slba').value;		
		logo = 'kb.jpeg';
	}
	if(publisher == 'TVC'){
		country = document.getElementById('country_tvc').value;
		logo = 'tvc.png';
	}
	if(publisher == 'VRT'){
		country = document.getElementById('country_vrt').value;
		logo = 'vrt.png';
	}
	var tt = '<b>' + title + '</b><br>';
	tt += engtitle + '<br>';
	tt += pub + ' (' + country + ')<br>';
	if(logo != ''){
		tt += '<img src=\"'+baseImagePath + '/' + logo + '\">';
	}
	Tip(tt);
}

function showRssContent(rssDiv,rssDesc){
	for (var i=0; i<5; i++){
		if(('rssItem_'+i)==rssDiv){
			document.getElementById(rssDiv).style.height="180px";
			document.getElementById(rssDesc).style.display="block";
			document.getElementById("rssLess_"+i).style.display="block";
			document.getElementById("rssMore_"+i).style.display="none";
		}else{
			document.getElementById('rssItem_'+i).style.height="50px";
			document.getElementById('rssDesc_'+i).style.display="none";
			document.getElementById("rssLess_"+i).style.display="none";
			document.getElementById("rssMore_"+i).style.display="block";
		}
	}
}

function hideRssContent(rssDiv,rssDesc,rssMore,rssLess){
	document.getElementById(rssDiv).style.height="50px";
	document.getElementById(rssDesc).style.display="none";
	document.getElementById(rssMore).style.display="block";
	document.getElementById(rssLess).style.display="none";
}

function goToHome() {
	document.location.href = baseURL + '/Home.do?menu_page=menu-home&menu=m_home';
}

function goToLogin() {
	document.location.href = baseURL + '/login/GetIn.do?menu_page=menu-home';
}

function goToBlog(url){
	document.location.href = url;
}

function goToRegister() {
	document.location.href = baseURL + '/Register.do';
	selectedPage = 'register';
}

function goToAdvancedSearch() {
	document.location.href = baseURL + '/search/AdvancedSearch.do';
}

function goToTopicSearch() {
	document.location.href = baseURL + '/search/TopicSearch.do';
}

function goToArticleSearch() {
	document.location.href = baseURL + '/search/KNASearch.do';
}

function goToWebShop() {
	document.location.href = baseURL + '/WebShop.do?action-type=view&menu_page=menu-webshop';
}

function goToTimeLine() {
	document.location.href = baseURL + '/Timeline.do?action-type=view&menu_page=menu-webshop';
}

function goToWebShopAdmin(){
	document.location.href =  baseURL + '/WebShopAdmin.do';
}

function goToAbout(){
	document.location.href =  baseURL + '/About.do?menu_page=menu-about';
}

function goToLogout() {
	document.location.href = baseURL + '/Logout.do';
}

function goToAssetManagement() {

	document.location.href = baseURL + '/assets/AssetManagement.do?menu_page=menu-asset';
}

function goToUserManagement() {
	document.location.href = baseURL + '/users/management/UserManagement.do?menu_page=menu-user&action=view';
}
function goToUserWorkspace() {
	document.location.href = baseURL + '/users/workspace/UserWorkspace.do?page=settings&menu_page=menu-workspace';
}

function goToCommunity() {
	document.location.href = baseURL + '/community/Community.do?page=get-in-contact&menu_page=menu-community';
}

function goToHelp() {
	document.location.href = baseURL + '/Home.do?menu_page=menu-home&menu=m_help';
}

function goToExternalPage(link){
	var url;
	if(link.indexOf('http://') == -1){
		url = 'http://' + link;
	} else {
		url = link;
	}
	window.open(url ,"_new",'width=700,height=600,scrollbars=yes');
}

var statusPopUp = false;

function openLogin() {
	// set Status to true (popup is open)
	statusPopUp = true;

	//initialize variables
	var boxLoginBg = document.getElementById("boxLoginBg");
	var boxLogin = document.getElementById("boxLogin");
	var currentTop = "";
	var currentWindowHeight = "";
	var currentWindowWidth = "";
	var pass = document.getElementById("forgetPass");
	var show = document.getElementById("show").value;

	// get top of screen
	if (window.innerHeight) {
		tempTop = window.pageYOffset
		currentWindowHeight = window.innerHeight;
		currentWindowWidth = window.innerWidth;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		tempTop = document.documentElement.scrollTop;
		currentWindowHeight = document.documentElement.innerHeight;
		currentWindowWidth = document.documentElement.innerWidth;
	} else if (document.body) {
		tempTop = document.body.scrollTop
		currentWindowHeight = document.body.clientHeight
		currentWindowWidth = document.body.clientWidth
	}

	// show transparant background and popup
	boxLoginBg.style.display = "block";
	boxLogin.style.display = "block";

	// set PopUp into right place
	boxLogin.style.top = tempTop + ((currentWindowHeight / 2) - (boxLogin.offsetHeight / 2))
	boxLogin.style.left = (currentWindowWidth - boxLogin.offsetWidth) / 2

	//hide or show forget password field
 	if (show == "1"){
 		pass.style.display = "block";
 	}
 	else{
 		pass.style.display = "none";
 	}
}

function closeLogin() {
	document.getElementById("boxLoginBg").style.display = "none";
	document.getElementById("boxLogin").style.display = "none";
}

function refreshPage(params) {
	var url = document.location.href.substr(0, document.location.href.length);
	if(url.indexOf('?') == -1) {
		document.location.href =  url + '?' + params;
	}
	else{
		if(url.indexOf('language')== -1) {
			document.location.href = url + '&' + params;
		}
		else{
			var before 	= url.substr(0, url.lastIndexOf('language'));
			var after	= url.substr(url.lastIndexOf('language') + 10, url.length);						
			document.location.href =  before + params + after;
		}
	}
}

function expand(id) {
	var pass = document.getElementById(id);
	pass.style.display = "block";
}

function doLogin(){

	var name = document.getElementById('user1').value;
	var pass = document.getElementById('pass1').value;

	document.getElementById('logname').value = name;
	document.getElementById('logpass').value = pass;

	document.firstForm.submit();
	document.pwForm.submit();
}

function getHttpRequest(){
	var http;
	if (window.ActiveXObject){
	    http = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest){
	    http = new XMLHttpRequest();
	}
	return http;
}

function getSelectedItem(selectBox){
	var box = document.getElementById(selectBox);
	var item = '';
	for(var i=0;i<box.options.length;i++){
		if(box.options[i].selected == true){
			item = box.options[i].value;
		}
	}
	return item;
}

function hasSelectedItems(selectBox){
	var box = document.getElementById(selectBox);
	for(var i=0;i<box.options.length;i++){
		if(box.options[i].selected == true){
			return true;
		}
	}
	return false;
}

function changeHomeTab(view){
	if(view == "mostView") {
		document.getElementById("mostViewedVideo").style.display = "block";
		document.getElementById("latestViewedVideo").style.display = "none";
		document.getElementById("mostViewedButton").className = "rightMenuItemActive";
		document.getElementById("latestViewedButton").className = "rightMenuItem";
	}
	if(view == "latestView") {
		document.getElementById("mostViewedVideo").style.display = "none";
		document.getElementById("latestViewedVideo").style.display = "block";
		document.getElementById("mostViewedButton").className = "rightMenuItem";
		document.getElementById("latestViewedButton").className = "rightMenuItemActive";
	}
	if(view == "rssView") {
		document.getElementById("mostViewedVideo").style.display = "none";
		document.getElementById("latestViewedVideo").style.display = "none";
		document.getElementById("mostViewedButton").className = "rightMenuItem";
		document.getElementById("latestViewedButton").className = "rightMenuItem";
	}
}

function getSearchWord(){
	var se = document.getElementById('searchWord');
	if(se != undefined){
		return se.value;
	} else {
		return '';
	}
}

function keywordSearch(keyword){
	document.location.href = baseURL +'/search/AdvancedSearch.do?action-type=keywordSearch&kw=' + keyword;
}

function watchVideo(id, currentItem, currentPage){
	var url = baseURL + '/VideoDetails.do?id='+ id + '&sw=' + getSearchWord();
	url += '&curitem=' + currentItem + '&curpage=' + currentPage;
	document.location.href = url;
}

function goToImageDetails(id, currentItem, currentPage){
	var url = baseURL + '/ImageDetails.do?id='+ id + '&sw=' + getSearchWord();
	url += '&curitem=' + currentItem + '&curpage=' + currentPage;
	document.location.href = url;
}

function goToDocumentDetails(id){
	document.location.href = baseURL + '/DocumentDetails.do?id='+ id + '&sw=' + getSearchWord();
}

function goToContextInfoDetails(id){
	document.location.href = baseURL + '/ArticleDetails.do?id=' + id + '&type=ci' + '&sw=' + getSearchWord();
}

function goToComparativeAnalysisDetails(id){
	document.location.href = baseURL + '/ArticleDetails.do?id=' + id + '&type=ca' + '&sw=' + getSearchWord();
}

function goToKNADetails(id, type){
	document.location.href = baseURL + '/KNASearch.do?id=' + id + '&type=' + type + '&sw=' + getSearchWord();
}

function goToTopic(){
	var setFilter ="";
	var selects = document.getElementsByTagName("option");
	var lenSelect = selects.length;
	setFilter += getCheckedIds(selects, lenSelect);

	document.location.href = baseURL +'/search/AdvancedSearch.do?action-type=topicSearch&topic-type='+document.getElementById("filter_topic").value+'&checkSelec='+setFilter;
}

function getCheckedIds(checkSelect, len){

	var setFilter="";
	for (var i = 0; i < len; i++)
	{
		if (checkSelect[i].selected || checkSelect[i].checked)
		{
			if(checkSelect[i].id != null && checkSelect[i].id != "" && checkSelect[i].id != " ");
			{
				setFilter += checkSelect[i].id + "@";
			}
		}
	}
	return setFilter;
}

function getHttpRequest(){
	var xmlHttp;
	try {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
 	} catch (e) {
		// Internet Explorer
 		try	{
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	} catch (e)	{
    		try	{
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		} catch (e)	{
      			alert("Your browser does not support AJAX!");
      			return false;
      		}
    	}
   	}
   	return xmlHttp;
}

var viewing = false;

function stopViewingScreenshots(){
	viewing = false;
}

function showScreenshotsInDiv(elmName, assetId){
	var http = getHttpRequest();
	var x = new Date().getMilliseconds();
	http.onreadystatechange=function(){
	    if(http.readyState==4) {
	    	if(http.responseText != 'empty' && http.responseText != ''){
		    	document.getElementById(elmName).src = baseURL + '/filedata?type=th&url=' + http.responseText;
		    	if(viewing){
	    			var func = "showScreenshotsInDiv('" + elmName + "','" + assetId + "')";
		    		setTimeout(func, 500);
		    	}
	    	}
		}
	}
	var url = baseURL + '/screenshots?assetid=' + assetId + '&x=' + x;
	http.open("GET",url,true);
	http.send(null);
}

function showPartnerInfo(partner){
	if(partner=='BBC'){
		Tip('BBC ');
	}
	if(partner=='DR'){
		Tip('DR ');
	}
	if(partner=='SLBA'){
		Tip('SLBA ');
	}
	if(partner=='DW'){
		Tip('DW ');
	}
	if(partner=='ORF'){
		Tip('ORF ');
	}
	if(partner=='NAVA'){
		Tip('NAVA ');
	}
	if(partner=='NTUA'){
		Tip('NTUA ');
	}
	if(partner=='IL'){
		Tip('IL ');
	}
	if(partner=='TVC'){
		Tip('TVC ');
	}
	if(partner=='RTBF'){
		Tip('RTBF ');
	}
	if(partner=='NOTERIK'){
		Tip('NOTERIK ');
	}
	if(partner=='SV'){
		Tip('SV ');
	}
	if(partner=='UU'){
		Tip('UU ');
	}
	if(partner=='RHUL'){
		Tip('RHUL ');
	}
}

function goToPartnerPage(partner){
	//TODO go to a special page with partner information (contextual information) and top 10 movies
	alert(partner);
}

function goBack(){
	//TODO this can be smarter
	history.go(-1);
}