/*
 * RV embed by blueshade (http://www.wave460.net/)
 * based on WMP embed by Kovan Abdulla (kovan@imetasoft.com) - www.imetasoft.com
 * based on WMP/Flash Object by Geoff Stearns (geoff@deconcept.com, http://www.deconcept.com/
 
 * v1.0.0 - 04-24-2006
 * Embeds an rv object 
 * Usage:

				var rvVid = new RVObject('pathtofile.asf', 'RVPlayer1', 320, 240);
				rvVid.addParam('TYPE', 'audio/x-pn-realaudio-plugin');
				rvVid.addParam('PLUGINSPACE', 'http://forms.real.com/real/realone/intl/focus.html?loc=pl&lang=en&tagtype=ie&type=dl');
				rvVid.write("video");

 */

RVObject = function(src, id, w, h, detectmsg) {
	this.src = src;
	this.id = id;
	this.width = w;
	this.height = h;
	this.redirect = "";
	this.sq = document.location.search.split("?")[1] || "";
	this.altTxt = detectmsg ? "Do wyświetlenia zasobu, wymagana jest wtyczka Real Video. <a href='http://forms.real.com/real/realone/intl/focus.html?loc=pl&lang=en&tagtype=ie&type=dl'>Download Real Video</a>." : "";
	this.bypassTxt = detectmsg ? "<p>Jeśli masz ją zainstalowaną, to możesz spróbować <a href='?detectRV=false&"+ this.sq +"'>pominąć detekcję.</a></p>" : "";
	this.params = new Object();
	this.params2 = new Object();
	this.doDetect = getQueryParamValue('detectRV');
}

RVObject.prototype.addParam = function(name, value) {
	this.params[name] = value;
}

RVObject.prototype.getParams = function() {
    return this.params;
}

RVObject.prototype.getParam = function(name) {
    return this.params[name];
}

RVObject.prototype.getParamTags = function() {
    var paramTags = "";
    for (var param in this.getParams()) {
        paramTags += param + '="' + this.getParam(param) + '" ';
    }
    if (paramTags == "") {
        paramTags = null;
    }
    return paramTags;
}

RVObject.prototype.getHTML = function() {
    var RVHTML = "";
	if (navigator.plugins && navigator.plugins.length) { // not ie
        RVHTML += '<embed type="audio/x-pn-realaudio-plugin" src="' + this.src + '" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '"';
        for (var param in this.getParams()) {
            RVHTML += ' ' + param + '="' + this.getParam(param) + '"';
        }
        RVHTML += '></embed>';
    }
    else { // pc ie
        RVHTML += '<embed width="' + this.width + '" height="' + this.height + '" id="' + this.id + '" ';
        this.addParam("src", this.src);
        if (this.getParamTags() != null) {
            RVHTML += this.getParamTags();
        }
        RVHTML += '></embed>';
    }

    return RVHTML;
}


RVObject.prototype.getVariablePairs = function() {
    var variablePairs = new Array();
    for (var name in this.getVariables()) {
        variablePairs.push(name + "=" + escape(this.getVariable(name)));
    }
    if (variablePairs.length > 0) {
        return variablePairs.join("&");
    }
    else {
        return null;
    }
}

RVObject.prototype.write = function(elementId, append) {
	
	if(isRVInstalled() || this.doDetect=='false') {
		if (elementId) {
		   if (append) {
			document.getElementById(elementId).innerHTML += this.getHTML();
		   } else {
			document.getElementById(elementId).innerHTML = this.getHTML();
	           }
		} else {
			document.write(this.getHTML());
		}
	} else {
		if (this.redirect != "") {
			document.location.replace(this.redirect);
		} else {
			if (elementId) {
			   if (append) {
				document.getElementById(elementId).innerHTML += this.altTxt +""+ this.bypassTxt;
			   } else {
				document.getElementById(elementId).innerHTML = this.altTxt +""+ this.bypassTxt;
		           }
			} else {
				document.write(this.altTxt +""+ this.bypassTxt);
			}
		}
	}		
}

function isRVInstalled() {
	var RVInstalled = false;
	RVObj = false;
	if (navigator.plugins && navigator.plugins.length) {
		for (var i=0; i < navigator.plugins.length; i++ ) {
         var plugin = navigator.plugins[i];
         if (plugin.name.indexOf("RealPlayer") > -1) {
			RVInstalled = true;
         }
      }
	} else {
	   execScript('on error resume next: RVObj = IsObject(CreateObject("RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)"))','VBScript');
	   RVInstalled = RVObj;
	   if (!RVInstalled) {
	      execScript('on error resume next: RVObj = IsObject(CreateObject("rmocx.RealPlayer G2 Control"))','VBScript');
	      RVInstalled = RVObj;
	   }
	   if (!RVInstalled) {
	      execScript('on error resume next: RVObj = IsObject(CreateObject("RealVideo.RealVideo(tm) ActiveX Control (32-bit)"))','VBScript');
	      RVInstalled = RVObj;
	   }
	}
	return RVInstalled;
}

/* get value of querystring param */
function getQueryParamValue(param) {
	var q = document.location.search;
	var detectIndex = q.indexOf(param);
	var endIndex = (q.indexOf("&", detectIndex) != -1) ? q.indexOf("&", detectIndex) : q.length;
	if(q.length > 1 && detectIndex != -1) {
		return q.substring(q.indexOf("=", detectIndex)+1, endIndex);
	} else {
		return "";
	}
}
