function xLoadScript(url)
{
  if (document.createElement && document.getElementsByTagName) {
    var s = document.createElement('script');
    var h = document.getElementsByTagName('head');
    if (s && h.length) {
      s.src = url;
      h[0].appendChild(s);
    }
  }
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
     output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function str_replace(a, b, str) {
 return str_replace2(str, a, b);
}
function str_replace2(SRs, SRt, SRu) {
  /*
  **  Replace a token in a string
  **    s  string to be processed
  **    t  token to be found and removed
  **    u  token to be inserted
  **  returns new String
  */
  SRRi = SRs.indexOf(SRt);
  SRRr = '';
  if (SRRi == -1) return SRs;
  SRRr += SRs.substring(0,SRRi) + SRu;
  if ( SRRi + SRt.length < SRs.length)
    SRRr += str_replace2(SRs.substring(SRRi + SRt.length, SRs.length), SRt, SRu);
  return SRRr;
}


var imgs = document.getElementsByTagName('IMG');
for(i=0; i<imgs.length; i++) {
	var img = imgs[i];
	var VidSrc = img.src+'';
	var ext = VidSrc.substring(VidSrc.length-4, VidSrc.length).toLowerCase();
	if(ext!='.avi' && ext!='.mpg' && ext!='.mov' && ext!='.flv') continue;
	img.id = 'imgvideo_'+i;
	var file = VidSrc.substring(VidSrc.indexOf(location.host)+location.host.length, VidSrc.length);

	if(ext=='.flv') {
	  args = "file="+URLEncode(file+'.data.flv')+"&autoStart=false"; //&image="+escape(file+'.jpg');
	  var newObj = document.createElement("DIV"); newObj.style.cssText = img.style.cssText+"background-color:black;"; newObj.style.width=img.offsetWidth+"px"; newObj.style.height=img.offsetHeight+"px"; if(img.align)newObj.style.cssText+="float:"+img.align+";"; if(img.vspace>0)newObj.style.cssText+="margin-top:"+img.vspace+"px;margin-bottom:"+img.vspace+"px;"; if(img.hspace>0)newObj.style.cssText+="margin-left:"+img.hspace+"px;margin-right:"+img.hspace+"px;";   
	  newObj.innerHTML='<object type="application/x-shockwave-flash" width="'+img.offsetWidth+'" height="'+img.offsetHeight+'" wmode="transparent" data="/jwplayer43.swf?'+args+'"><param name=flashvars value="'+args+'"><param name="movie" value="/jwplayer43.swf?'+args+'" /><param name=allowfullscreen value=true /><param name=allowscriptaccess value=always /><embed type="application/x-shockwave-flash" width="'+img.offsetWidth+'" height="'+img.offsetHeight+'" allowfullscreen=true allowscriptaccess=always src="/jwplayer43.swf?'+args+'"></embed></object>'; 
	  img.parentNode.insertBefore(newObj, img); img.parentNode.removeChild(img);
	  continue;
	}

	var php = '/showvideo.php?id='+img.id+'&width='+img.offsetWidth+'&height='+img.offsetHeight+'&file='+file;
	xLoadScript(php);
}


