﻿String.prototype.changeIntProperty = function(propName, toValue)
{
	var i = this.length;
	var c = this;
	newarray = this.split(propName);
	for(var x=1; x<newarray.length; x++)
	{
	    if(newarray[x].substr(0,2)=="=\"" || 
	        newarray[x].substr(0,2)=="=\'")
	    {
	        newarray[x] = propName + newarray[x].replace(newarray[x].substr(0,5), newarray[x].substr(0,2) + toValue);
	    }
	    else
	    {
	        newarray[x] = propName + newarray[x].replace(newarray[x].substr(0,4), newarray[x].substr(0,1) + toValue);
	    }
	}
	c = "";
	for(var x=0; x<newarray.length; x++)
	{
	    c += newarray[x]
	}
	return c;
}
function MetacafeEmbedTag(param, setWidth, setHeight) {

    var MetacafeId = param.split('/')[4];
    var MetacafeTitle = param.split('/')[5];
    //<div style='background:#000000;width:" + setWidth + "px;height:" + setHeight + "px'>
    var str = "<embed flashVars='playerVars=showStats=yes|autoPlay=yes|videoTitle=" + MetacafeTitle + "' ";
    str = str + "src='http://www.metacafe.com/fplayer/" + MetacafeId + "/" + MetacafeTitle + ".swf' width='" + setWidth + "' height='" + setHeight + "' wmode='transparent' allowFullScreen='true' allowScriptAccess='always' ";
    str = str + "name='Metacafe_" + MetacafeId + "' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash'></embed> ";
    //</div>
    str = str.replace("'", ""); 
    str = str.replace(/'/g, "");
    return str;
}

function GetEmbedForPreview(param, ServerUrl, setWidth, setHeight) {
    var sFlash = "";
    if (param.indexOf(ServerUrl) >= 0 && (param.indexOf("<object") != 0))   //for flash
    {
        sFlash = PlayVideo(param, 0, 0, 0, ServerUrl, setWidth, setHeight); //This function is declare in global.js file.
    }
    else if ((param.indexOf("<object") >= 0) && (param.toLowerCase().indexOf(ServerUrl) > 0)) {//embed youtube
        sFlash = param;
    }
    else if (param.indexOf("<object") >= 0) 
    {

        //embed google
        if(param.toLowerCase().indexOf("video.google.com") > 0)
        {
            param = param.replace("http://www.video.google.com/watch?v=", "http://www.video.google.com/v/");
            //param = param.replace("autoplay=1", "autoplay=0");
            //param = param.replace("autoplay=true", "");
        }
        //embed youtube
        else if(param.toLowerCase().indexOf("youtube.com") > 0)
        {
            param = param.replace("http://www.youtube.com/watch?v=", "http://www.youtube.com/v/");
            //param = param.replace("autoplay=1", "autoplay=0");
            //param = param.replace("autoplay=true", "");
            param = param.changeIntProperty("width", setWidth);
            param = param.changeIntProperty("height", setHeight);
        }
        // this is for MSNBC, The Onion & Funnyordie, Hulu
        else if (param.indexOf("msnbc.msn.com") >= 0 || 
                param.indexOf("theonion.com") >= 0 || 
                param.indexOf("player.ordienetworks.com") >= 0 || 
                param.indexOf("www.hulu.com") >= 0 || 
                param.indexOf("vimeo.com") >= 0 || 
                param.indexOf("cdn.turner.com") >= 0) 
       {
            param = param.changeIntProperty("width", setWidth);
            param = param.changeIntProperty("height", setHeight);
        }
        //other players
        else {
            //param = param.replace("autoplay=1", "autoplay=0");
           //param = param.replace("autoPlay=true", "");
        }
        sFlash = param;
    }
    else if (param.indexOf("<embed") >= 0 && param.indexOf("video.google.com") >= 0) {//embed video.google
        param = param.changeIntProperty("width", setWidth);
        param = param.changeIntProperty("height", setHeight);
  
        sFlash = param;
    }
    //embed other than video.google
    else if (param.indexOf("<embed") >= 0) {
        sFlash = param;
    }
    //For Web Url.
    else if (param.indexOf("video.google.com") >= 0) // for Google Videos
    {
        param = param.replace(/,/g, "%2C");
        param = param.replace(/&/g, "%26");
        sFlash = PlayYouTube(param, setWidth, setHeight);
    }
    else if (param.indexOf("metacafe.com") >= 0) // for Google Videos
    {
        
//        param = param.replace(/,/g, "%2C");
//        param = param.replace(/&/g, "%26");
        sFlash = MetacafeEmbedTag(param, setWidth, setHeight);
    }
    else // for You Tube
    {
        param = param.replace("http://www.youtube.com/watch?v=", "http://www.youtube.com/v/");
        //param = param.replace("autoplay=1", "autoplay=0");
        //param = param.replace("autoPlay=true", "");
        sFlash = PlayYouTube(param, setWidth, setHeight); //This function is declare in global.js file.

    }
    
    return sFlash;
}
