var yt_id = 0;
var editBox = null;
var editPrev = null;
var editTags = null;
function getElm(name, src) {
    if(!src) src = document;
    return src.getElementById ? src.getElementById(name) : eval(src+".all."+name);
}
function replaceYT(vid) {//{{{
    var buf = '<object type="application/x-shockwave-flash" id="yt_'+yt_id+'" style="display: none;" data="http://www.youtube.com/v/'+vid+'" height="350" width="425"><param name="movie" value="http://www.youtube.com/v/'+vid+'" />    <embed src="http://www.youtube.com/v/'+vid+'" type="application/x-shockwave-flash" height="350" width="425" /></object><a href="http://youtube.com/watch?v='+vid+'" target="_blank"><img style="background: transparent url(http://static.twoday.net/banger/images/yti.gif) no-repeat scroll left bottom; display: block; margin-bottom: 3px; padding-bottom: 9px;" id="yti_'+yt_id+'" src="http://img.youtube.com/vi/'+vid+'/1.jpg" alt="YouTube Clip" border="0"></a> [<a class="gensmall" id="yts_'+yt_id+'" style="cursor: pointer;" onclick="getElm(\'yt_'+yt_id+'\').style.display=\'block\'; getElm(\'yth_'+yt_id+'\').style.display=\'inline\'; getElm(\'yti_'+yt_id+'\').style.display=\'none\'; this.style.display=\'none\';">Video einblenden</a><a class="gensmall" id="yth_'+yt_id+'" style="display: none; cursor: pointer;" onclick="getElm(\'yt_'+yt_id+'\').style.display=\'none\'; getElm(\'yti_'+yt_id+'\').style.display=\'block\'; getElm(\'yts_'+yt_id+'\').style.display=\'inline\'; this.style.display=\'none\';">Video ausblenden</a>]';
    yt_id++;
    return buf;
}//}}}
function sar() {//{{{
    var i=0;
    var a = document.getElementsByTagName('a');
    var l;
    var buf;
    for(i=0; i<a.length; i++) {
        if(!a[i].href) continue;
		if(a[i].rel && ("noyt" == a[i].rel)) continue;
        l = a[i].href.match(/http:\/\/(www\.)?youtube.com\/watch\?v=([\w\-]{11})/);
        if(!l) continue;
        buf = document.createElement('div');
        buf.innerHTML = replaceYT(l[2]);
        a[i].parentNode.insertBefore(buf, a[i]);
        i+=3;
        a[i].style.display='none';
    }
    var addjs = document.createElement('script');
    addjs.type = 'text/javascript';
    addjs.src = 'http://banger.guitarworld.de/js/flickr.js';
    document.body.appendChild(addjs);
    editTags = getElm('myTags');
    editPrev = getElm('myPreview');
    if(!editTags) {
        prevImg();
        return;
    }
    var e = document.getElementsByTagName('textarea');
    for(i=0; i<e.length; i++)
        if(e[i].name=='content_text') {
            editBox = e[i];
            break;
        }
    if(!editBox) {
        return;
    }
    var f = document.getElementsByTagName('form');
    for(i=0; i<f.length; i++) {
        if(f[i].action && f[i].action.match(/\/stories\/([^\/]+\/)?(edit|create)/)) {
            f[i].onsubmit=updTags;
        }
    }
    buf = editBox.value.match(/<span style="display:none;">\[img\](.*)\[\/img\]<\/span>/);
    if(buf) {
        editBox.value = editBox.value.replace(/<span style="display:none;">\[img\](.*)\[\/img\]<\/span>[\r\n\s]/, '');
        editPrev.value = buf[1];
    }
    buf = editBox.value.match(/<div class="myTags">Tags: (.*)<\/div>/);
    if(!buf) return;
    editBox.value = editBox.value.replace(/[\r\n\s]*<div class="myTags">Tags: (.*)<\/div>/, '');
    var tgs = buf[1].split(', ');
    var res = new Array();
    for(i=0; i<tgs.length; i++) { 
        buf = tgs[i].match(/\>([^\>]+)\</);
        if(buf.length)
            res.push(buf[1]);
    }
    editTags.value = res.join(', ');
}//}}}
function prevImg() {//{{{
    var divs = document.getElementsByTagName('div');
    var len = divs.length, i, buf, hcnt;
    for(i=0; i<len; ++i) {
        if(("undefined" == typeof divs[i].className) || ("storyContent" != divs[i].className)) continue;
		hcnt = divs[i].innerHTML.replace(/\<span([^\>]+)>([^\<]*)\<\/span\>/g, '');
        buf = hcnt.match(/\[img\]([^\[]+)\[\/img\]/);
        if(!buf) continue;
        buf = buf[1];

        divs[i].innerHTML = divs[i].innerHTML.replace(/\[img\]([^\[]+)\[\/img\]/, '\<a href="'+divs[i-1].firstChild.firstChild.href+'"\>\<img style="display:block;" src="'+buf+'" border="0" alt="" /\>\</a\>');
    }
}//}}}
function trim(s) {//{{{
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}//}}}
function updTags() {
    var pI = trim(editPrev.value);
    if(pI.length) 
        editBox.value = '<span style="display:none;">[img]'+pI+'[/img]</span>\n'+editBox.value;
    
    var tx = trim(editTags.value);
    var buf = tx.split(',');
    if(!buf.length) return;
    var i, out = new Array();
    for(i=0; i<buf.length; i++) {
        buf[i] = trim(buf[i]);
        if(!buf[i].length) continue;
        out.push('<a href="http://banger.twoday.net/search?q='+encodeURIComponent(buf[i].toLowerCase())+'">'+buf[i]+'</a>');
    }
    if(!out.length) return;
    editBox.value += '\n<div class="myTags">Tags: '+out.join(', ')+'</div>';
}
if (("undefined"!=typeof(onload)) && onload){
    var hmOldOnLoad = new Function();
    hmOldOnLoad=onload;
    eval("onload=function(){try{hmOldOnLoad()}catch(e){};sar()}");
} else onload = function(){sar()};
