/*

YouTube動画を簡単に一行で貼るscript : code

#使い方はhttp://www.kyo.com/blog/2008/07/youtube-javascript/をご覧ください。


*/
/*ver:3.2*/
var body=document.getElementsByTagName("body")[0];
var scripts=body.getElementsByTagName("script");

for(var i=0; i<scripts.length; i++){
    var query=scripts[i].src;
}

var params=getQueryParams();
var v=params.v;
var fmt=params.fmt;
var width=params.width;
var height=params.height;
var wmode=params.wmode;
var start=params.start;
var end=params.end;
var autoplay=params.autoplay;
var fs=params.fs;
var hd=params.hd;

if(v) video(v,width,height,wmode,fmt,hd,start,end,fs,autoplay);

function getQueryParams(){
    var qs=query.slice(query.indexOf("?"));
    if(qs){
        var qsa=qs.substring(1).split('&');
        var params={};
        for(var i=0; i<qsa.length; i++){
            var pair=qsa[i].split('=');
            if(pair[0]){
                params[pair[0]]=/*decodeURIComponent*/(pair[1]);
            }
        }
        return params;
    }
    return null;
}

function video(id,width,height,wmode,fmt,hd,start,end,fs,autoplay){
    if(!id.match(/^[a-zA-Z0-9_\-]+$/)){
        alert('Your query string may be incorrect. http://www.youtube.com/v/'+id);
        return;
    }
    if(!wmode) wmode='transparent';
    if(hd==1 && !width && !height){width=853; height=505;}
    if(!width) width=0; else width=parseInt(width);
    if(!height) height=0; else height=parseInt(height);
    if(width<=0 || width>1280) width=425;
    if(height<=0 || height>745) height=344;
    if(start) id += '&start='+parseInt(start); else if(fmt){
        id += '&ap=%2526fmt%3D'+fmt;
        id += '&fs=1';
    }
    if(hd){
        id += '&hd='+parseInt(hd);
        id += '&fs=1';
    }
    if(end) id += '&end='+parseInt(end);
    if(autoplay==1) id += '&autoplay='+autoplay;
    var html = '<object width="'
        + width
        + '" height="'
        + height
        + '"><param name="wmode" value="'
        + wmode
        + '"><param name="movie" value="http://www.youtube.com/v/'
        + id
        + '"></param><param name="allowFullScreen" value="true"><embed src="http://www.youtube.com/v/'
        + id
        + '" type="application/x-shockwave-flash" allowfullscreen="true" width="'
        + width
        + '" height="'
        + height
        + '" wmode="'
        + wmode
        + '"></embed></object>';
    document.write(html);
}