//Special Content ClientSide Parser
//By Aytek Üstündağ (HolyOne)
//Version: 0.9
//http://www.tahribat.com

var getQValue = function(url, gkey){
        var returned = null;
        if (url.indexOf("?") != -1){
          var list = url.split("?")[1].split("&"),
          gets = [];
          for (var ind in list){
            var kv = list[ind].split("=");
            if (kv.length>0)
                gets[kv[0]] = kv[1];
        }
        returned = gets;
        if (typeof gkey != "undefined")
            if (typeof gets[gkey] != "undefined")
                returned = gets[gkey];
        }
        return returned;
};

String.prototype.format = function() {
    var txt = this,
        i = arguments.length;

    while (i--) {
        txt = txt.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
    }
    return txt;
};
String.prototype.startsWith = function (str) {
  return this.indexOf(str) === 0;
};

String.prototype.endsWith = function(str)
{
    var lastIndex = this.lastIndexOf(str);
    return (lastIndex != -1) && (lastIndex + str.length == this.length);
}

//'{0} {0} {1} {2}'.format(3.14, 'abc', 'foo');

//youtube chanel
//http://gdata.youtube.com/feeds/api/users/aytekustundag/uploads?orderby=updated
function appendContent(elm, content)
{
 var pTag = document.createElement("div");
 pTag.innerHTML = content;
 elm.parentNode.insertBefore(pTag, elm.nextSibling);
}
    function doSpecialParse(contentid)
    {
        var mdoc = document.getElementById(contentid);
        var links = mdoc.getElementsByTagName('a');
        for (var i = 0; i < links.length; i++) {

            var link = links[i];
            
		//	if( link.parent.className==whereparentclassnot) continue;
			var url = link.href;

            if (url.startsWith('javascript:')) continue;

  if (url.startsWith('http://www.youtube.com/') ||url.startsWith('http://youtube.com/') ) {

    var youtubeEmbedCode = " <br><embed src=\"http://www.youtube.com/v/{0}?fs=1&amp;hl=en_GB&amp;border=0\"\n\
type=\"application/x-shockwave-flash\"\n\
allowscriptaccess=\"always\" allowfullscreen=\"true\"\n\
width=\"500\" height=\"405\">\n\
</embed>\n\
";
    var youtubeObjectCode = " <br><object width=\"500\" height=\"405\">\n\
<param name=\"movie\"\n\
value=\"http://www.youtube.com/v/{0}?fs=1&amp;hl=en_GB&amp;border=0\"></param>\n\
<param name=\"allowFullScreen\" value=\"true\"></param>\n\
<param name=\"allowscriptaccess\" value=\"always\"></param>\n"+youtubeEmbedCode+"</object><br>\n";

                var ytvideocode = "";
                ytvideocode = (getQValue(url, "v"));
//alert(ytvideocode);
 if(ytvideocode==null || ytvideocode=='') continue;
 
					appendContent(link,youtubeEmbedCode.format(escape(ytvideocode)));
            }
            else if (url.startsWith('http://www.metacafe.com/')) {

				var metacafeEmbedCode= " <div style=\"background:#000000;width:540px;height:334px\">\n\
<embed flashVars=\"playerVars=showStats=no|autoPlay=no\"\n\
src=\"http://www.metacafe.com/fplayer/{1}\"\n\
width=\"540\" height=\"334\"\n\
wmode=\"transparent\"\n\
allowFullScreen=\"true\"\n\
allowScriptAccess=\"always\"\n\
name=\"Metacafe_{0}\"\n\
pluginspage=\"http://www.macromedia.com/go/getflashplayer\"\n\
type=\"application/x-shockwave-flash\">\n\
</embed></div>\n";

                var regex = /http\:\/\/www\.metacafe\.com\/watch\/((\d+)[\/\w\d\_].+)/;
                var vididm = url.match(regex);
                if (vididm == null) continue;
                var vidid = vididm[2];
                var fullid = vididm[1];
                if ((fullid == null) || (vidid == null)) continue;
                if (fullid.endsWith('/')) {
                    fullid = fullid.substring(0, fullid.length - 1);
                }
                fullid += '.swf';
				appendContent(link,metacafeEmbedCode.format(escape(vidid), escape(fullid)));
                
                //   $(this).after(metacafeEmbedCode.format(escape(vidid),escape(fullid)));
            }else
            if (url.startsWith('http://vimeo.com/')||url.startsWith('http://www.vimeo.com/')) {
				
var vimeoEmbedCode=" <br><iframe src=\"http://player.vimeo.com/video/{0}\" width=\"400\" height=\"300\" frameborder=\"0\"></iframe><br>";
//http://www.metacafe.com/watch/5103700/erotic_exam_naked_and_funny_5159/

                var regex = /http\:\/\/(www\.)?vimeo\.com\/(\d+)/;
                var vididm = url.match(regex);
                if (vididm == null) continue;
                vidid = vididm[2];
                if (vidid == '') continue;

   
appendContent(link,vimeoEmbedCode.format(escape(vidid)));
                //$(this).after(vimeoEmbedCode.format(escape(vidid)));
            }
           
 else if (url.startsWith('http://www.dailymotion.com/video/')) {
                var regex = /http\:\/\/www\.dailymotion\.com\/video\/([\w\d\-\_]+)/;
                var vididm = url.match(regex);
				if(vididm==null)continue;
				var vidid=  vididm[1];

dailymotionEmbedCode="<embed type=\"application/x-shockwave-flash\"\n\
src=\"http://www.dailymotion.com/swf/video/{0}?additionalInfos=0\"\n\
width=\"480\" height=\"360\"\n\
allowfullscreen=\"true\" allowscriptaccess=\"always\"></embed>\n";

var dailymotionObjectCode="<object width=\"480\" height=\"360\">\n\
<param name=\"movie\" value=\"http://www.dailymotion.com/swf/video/{0}?additionalInfos=0\"></param>\n\
<param name=\"allowFullScreen\" value=\"true\">\n\
</param><param name=\"allowScriptAccess\" value=\"always\"></param>\n"+dailymotionEmbedCode+"</object>";
appendContent(link,dailymotionEmbedCode.format(escape(vidid)));
 
 }
 else if (url.startsWith('http://www.izlesene.com/video/') || url.startsWith('http://izlesene.com/video/')) {
 
                 var regex = /http\:\/\/(www\.)?izlesene\.com\/video\/([a-z\w\d\-\_]+\/(\d+))/;
                var vididm = url.match(regex);
				if(vididm==null)continue;
				var vidid= vididm[3];

 //http://www.izlesene.com/video/yetenek-asli-turk-inanki-super-yorum/2528887
var izleseneEmbedCode=" <embed src=\"http://www.izlesene.com/embedplayer.swf?video={0}\" wmode=\"window\"\n\
 bgcolor=\"#000000\" allowfullscreen=\"true\" allowscriptaccess=\"always\"\n\
 menu=\"false\" scale=\"noScale\" width=\"400\" height=\"300\"\n\
 type=\"application/x-shockwave-flash\"></embed>\n";

var izleseneObjectCode="<object width=\"400\" height=\"300\">\n\
<param name=\"allowfullscreen\" value=\"true\" />\n\
<param name=\"allowscriptaccess\" value=\"always\" />\n\
<param name=\"movie\" value=\"http://www.izlesene.com/embedplayer.swf?video={0}\" />"+izleseneEmbedCode+"</object>";

 
appendContent(link,izleseneEmbedCode.format(escape(vidid)));
 

 
 }
        }
    }
//$(document).ready(function() {
 
 doSpecialParse('mainforumconent');



//});


