//version 2.0.2.1
// true  to test on local URl for isma format on http://localhost:8080/wps-europarl-internet/faces/players-prototype/video/....
var isLocalIsma = false;

/**
 * This method change the url according the param format=""
 * formatParam : is the format of the player (wmv or mp4)
 */
function changeUrlAccordingPlayerFormat(formatParam){	
	var newUrl;
    var oldUrl;
	// get the value of the selected radio (mediaplayer or quicktime)
	var typeFormat = getRadioValue(window.document.getElementsByName(formatParam));

    var strHref = window.location.href;

  	if ( strHref.indexOf("?") > -1 ){
	    var strQueryString = strHref.substr((strHref.indexOf("?"))+1);
	    newUrl = setOrAddParms(strQueryString, "format", typeFormat);
        oldUrl = strHref.substr(0,(strHref.indexOf("?")));
   	}
   	else{
        oldUrl =  strHref;
   		newUrl = "format=" + typeFormat;
   	}

    if(strHref.indexOf("ep-live")>0) {
         strHref = oldUrl + '?'+ newUrl;
    } else{
        strHref = '/wps-europarl-internet/frd/vod/player?'+ newUrl;
    }
	window.location.href = strHref;
}

function reloadUsingGivenParam(paramName, parameValue){	
    var strHref = window.location.href;
    var urlWithoutParams;
  	if ( strHref.indexOf("?") > -1 ){
	    var strQueryString = strHref.substr((strHref.indexOf("?"))+1);
	    newUrl = setOrAddParms(strQueryString, paramName, parameValue); 	
		urlWithoutParams = strHref.substr(0, (strHref.indexOf("?")));
   	}
   	else{
   		newUrl = paramName + "=" + parameValue;
   		urlWithoutParams = strHref;
   	}
   	   	
	//window.location.replace('/wps-europarl-internet/faces/vod/player.jsp?'+ newUrl);
	strHref = urlWithoutParams + '?'+ newUrl;
	window.location.href = strHref;
}


/**
This method sets or adds a param standing in the url
* pageUrl    : is the url
* paramNam   : the name of the param to add or to set the value
* paramValue : the value to set 
**/
function setOrAddParms(pageUrl, paramName, paramValue) {

    if (pageUrl.length > 1) {
        var parms = pageUrl.split("&");
	    var parameterName;
	    var parameterValue;
        var paramExist = false;
        var paramsLikeString = "";
        for (var i = 0; i < parms.length; i++) {
            parameterName = parms[i].substring(0, parms[i].indexOf("="));
            parameterValue = parms[i].substring(parms[i].indexOf("=") + 1);

            if(parameterName == paramName){
            	parameterValue = paramValue;
            	paramExist = true;
            }
            paramsLikeString = paramsLikeString + parameterName + "=";
            paramsLikeString += parameterValue;
            paramsLikeString += "&";
            
        }
        // add the param if it doesn't exist
        if(paramExist == false) {
	        paramsLikeString = paramsLikeString + paramName + "=";
            paramsLikeString += paramValue;         
        }   
        
        // to delete the last &
        var lastcaracter = paramsLikeString.substring(paramsLikeString.length-1);
 		if( lastcaracter == "&"){
 			paramsLikeString = paramsLikeString.substring(0, paramsLikeString.length-1);
 		}

        return paramsLikeString;
    } else {
        return null;
    }
}

/**
 * This method return the value of the selected radio button
 * here wmv or mp4
 */
function getRadioValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined){
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	}	
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


function UtilCommon(format) {
	
}

/**
    Util method to declare a javascript inheritance
*/
UtilCommon.extend = function(subClass, baseClass) {
   function inheritance() {}
   inheritance.prototype = baseClass.prototype;

   subClass.prototype = new inheritance();
   subClass.prototype.constructor = subClass;
   subClass.baseConstructor = baseClass;
   subClass.superClass = baseClass.prototype;
}

/**
 recuperation parametres url
**/
function getParms() {
    if (window.location.search.length > 1) {
        var parms = window.location.search.substring(1).split("&");
        var parmList = new Array();
        for (var i = 0; i < parms.length; i++) {
            parmList[i] = new Object;
            parmList[i].name = parms[i].substring(0, parms[i].indexOf("="));
            parmList[i].value = parms[i].substring(parms[i].indexOf("=") + 1);
        }
        return parmList;
    } else {
        return null;
    }
}

/**
 method returns an url parameter
**/
function getUrlParam(paramName) {
    var params = getParms();
    var cRet = "";
    if (params) {
        for (var i = 0; i < params.length; i++) {
            if (params[i].name == paramName) {
                cRet = params[i].value;
            }
        }
    }
    return cRet;
}

/**
    Open a new window iun function of the parameters : 
    pageUrl, pageName, weight, height and hasScroll
*/
function popUp(mypage, myname, w, h, scroll) {
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    var settings = "height=" + h + ",";
    settings += "width=" + w + ",";
    settings += "top=" + wint + ",";
    settings += "left=" + winl + ",";
    settings += "scrollbars=" + scroll + ",";
    settings += "resizable=yes";
    win = window.open(mypage, myname, settings);
    if (parseInt(navigator.appVersion) >= 4) {
        win.window.focus();
    }
}

function popUplocal(mypage, myname, w, h, scroll) {
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    var settings = "height=" + h + ",";
    settings += "width=" + w + ",";
    settings += "top=" + wint + ",";
    settings += "left=" + winl + ",";
    settings += "scrollbars=" + scroll + ",";
    settings += "resizable=yes";
    win = window.open("http://" + window.location.host + mypage, myname, settings);
    if (parseInt(navigator.appVersion) >= 4) {
        win.window.focus();
    }
}

/**
    open a "print" controle in the browser, allowing to print the current page
*/
function printHelp() {
    if (navigator.appName == "Microsoft Internet Explorer") {
        intOLEparam = 7;
        intOLEcmd = 7;
        var WebBrowser = "<OBJECT ID=\"WebBrowser1\" WIDTH=0 HEIGHT=0 CLASSID=\"CLSID:8856F961-340A-11D0-A96B-00C04FD705A2\"></OBJECT>";
        document.body.insertAdjacentHTML("beforeEnd", WebBrowser);
        if (!intOLEparam || intOLEparam < -1 || intOLEparam > 1) {
            intOLEparam = 1;
        }
        WebBrowser1.ExecWB(intOLEcmd, intOLEparam);
        WebBrowser1.outerHTML = "";
    } else {
        window.print();
    }
}

/**
    Method checks if a page exists.
    Method sends a request to the given URL, and check the stats of this request.<b>
    This found status makes it possible to know if the page exists. 
*/
function Verify_files(ThisFile) {
    var files_exist = false;
    if (window.XMLHttpRequest) {// Firefox
        files_exist = new XMLHttpRequest();
        if (!files_exist) {
            return false;
        }
        if (files_exist.overrideMimeType) {
            files_exist.overrideMimeType("text/xml");
        }
    } else {
        if (window.ActiveXObject) {     // Internet Explorer
            files_exist = new ActiveXObject("Microsoft.XMLHTTP");
            if (!files_exist) {
                return false;
            }
        } else { // XMLHttpRequest non support? par le navigateur 
            return false;
        }
    }
    files_exist.open("GET", ThisFile, false);
    files_exist.send("");
    if (files_exist.readyState != 4) {
        return false;
    }
    if (files_exist.responseText.indexOf("content404") > 0) {
        return false;
    }
    return true;
}

/**
 * This method will return the valide url, replacing ${language} by the given language code.
 * Warning : If we are reading an mp4 file, the dit use le language code in Uppercase
 * For wmv files, we use the language code in lowercase. 
 */
function createVideoUrlFromLanguageCode(videoUrlToConvert, languageCode, playerType){

	var languageCodeToUse = languageCode.toLowerCase();
	if (playerType == "mp4") {
		languageCodeToUse = languageCode.toUpperCase();
	}
	var newurl= videoUrlToConvert;


	newurl.replace('${language}', languageCodeToUse);
	if(isLocalIsma){
		   newurl.replace("http://vod.europarl.europa.eu/nasvod01/", "http://localhost:8080/wps-europarl-internet/faces/players-prototype/video/");
		}

	return newurl;
}


