var PJSEARCH_FPATH = 'http://ranksp.podcastjuice.jp/new_feed_update/output_data/';
var STATEIMG_DIR   = "http://www.podcastjuice.jp/material/image/podfeed/";

pjSearch_main();

function pjSearch_main() {
    var params = getParams();

    if ( params != undefined && isNum(params['id']) ){
        path = PJSEARCH_FPATH + parseInt(params['id']/100) + "/" + params['id'] + ".json";
    }else{
        return;
    }

    // param不正時のデフォルト値
    var rank_flg    = false;
    var regist_flg  = false;
    var collar_type = 1;

    if ( params['rank'] == "on" ){
        rank_flg = true;
    }

    if ( params['count'] == "on" ){
        regist_flg = true;
    }

    if ( params['collar'] == "red" ){
        collar_type = 1;
    }else if ( params['collar'] == "green" ){
        collar_type = 2;
    }else if ( params['collar'] == "blue" ){
        collar_type = 3;
    }else if ( params['collar'] == "black" ){
        collar_type = 4;
    }else if ( params['collar'] == "yellow" ){
        collar_type = 5;
    }else if ( params['collar'] == "pink" ){
        collar_type = 6;
    }

    document.write(createHTML(regist_flg, rank_flg, collar_type, params['id']));
    pjSearch_load(path);
}

function pjSearch_load(path){
    scriptObj = document.createElement("script");
    scriptObj.setAttribute("type",    "text/javascript");
    scriptObj.setAttribute("charset", "utf-8");
    scriptObj.setAttribute("src",     path);

    document.getElementsByTagName("head").item(0).appendChild(scriptObj);
}

// 非同期呼び出しファンクション
function pod_chg_function(data){
    if (isURL(data.rss_convert_url)){
        var elems = document.getElementsByName("pjSearch_url" + data.blog_id);
        for (var i = 0; i < elems.length; i++){
            elems.item(i).setAttribute("href", data.rss_convert_url);
        }
    }

    if (isNum(data.registration)){
        elems = document.getElementsByName("pjSearch_registration" + data.blog_id);
        for (var i = 0; i < elems.length; i++){
            elems.item(i).innerHTML = data.registration;
        }
    }

    if (isNum(data.rank)){
        elems = document.getElementsByName("pjSearch_rank" + data.blog_id);
        for (var i = 0; i < elems.length; i++){
            elems.item(i).innerHTML = data.rank;
        }
    }

    if (isNum(data.max_program)){
        elems = document.getElementsByName("pjSearch_max_program" + data.blog_id);
        for (var i = 0; i < elems.length; i++){
            elems.item(i).innerHTML = data.max_program;
        }
    }

    var stateImg = STATEIMG_DIR;
    var alt = "Stay";
    if (data.state > 0){
        stateImg += 'btn_up.gif';
        alt = "UP";
    }else if(data.state < 0){
        stateImg += 'btn_down.gif';
        alt = "DOWN";
    }else if(data.state == 0){
        stateImg += 'btn_stay.gif';
        alt = "Stay";
    }
    var elems = document.getElementsByName("pjSearch_rankImg" + data.blog_id);
    for (var i = 0; i < elems.length; i++){
        elems.item(i).setAttribute("src", stateImg);
        elems.item(i).setAttribute("alt", alt);
    }
}

// パラメータ取得ファンクション
function getParams(){
    var s = (function (e) { if(e.nodeName.toLowerCase() == 'script') return e; return arguments.callee(e.lastChild) })(document);
    var work;
    if ( s.src.split("?")[1] ){
        work = s.src.split("?")[1].split("&");
    }else{
        return;
    }
    var params = new Object();
    for( var i=0; i<work.length; i++ ){
        var a = work[i].split("=");
        params[a[0]] = a[1].split(",");
    }
    return params
}

// HTML生成ファンクション
function createHTML(regist_flg, rank_flg, collar, id){
    var btn_img = createBtnImg(collar);

    var htmlStr = "<div style=\"text-align:left; background-color: transparent; background-image:none; font-size:small; color:#333; line-height:1.35; font-family: 'MS PGothic',Verdana, Arial,'ヒラギノ角ゴ Pro W3','Hiragino Kaku Gothic Pro', Osaka, sans-serif; word-break:break-all; overflow:hidden; margin:10px 0; padding:0; width:100%;\">\n" + 
                  '<table width="160" cellspacing="0" cellpadding="0" border="0" align="center" style="margin:0 auto;">\n' + 
                  '<tbody>\n' + 
                  '<tr>\n' + 
                  '<td valign="top" align="left" style="background:url(http://www.podcastjuice.jp/material/image/podfeed/ttl_pj.gif) no-repeat 0 0; margin:0; padding:0; border:0;"><a href="http://podfeed.podcastjuice.jp/app/feed/feedurl_convert.cgi" target="_blank"><img src="http://www.podcastjuice.jp/material/image/podfeed/ttl_podfeed.gif" alt="Podfeed" width="102" height="42" border="0" style="margin:0 3px 0 9px; border:0; vertical-align:top;" /></a><a href="http://www.podcastjuice.jp/" target="_blank"><img src="http://www.podcastjuice.jp/material/image/podfeed/ttl_juice.gif" alt="ポッドキャスティングジュース" width="37" height="42" border="0" style="border:0; vertical-align:top; margin:0; padding:0;" /></a></td>\n' + 
                  '</tr>\n' + 
                  '<tr>\n' + 
                  '<td valign="top" align="center" style="background:#fff url(http://www.podcastjuice.jp/material/image/podfeed/bg_pj.gif) repeat-y 0 0; margin:0; padding:0; border:0; text-align:center; width:160px;"><div style="margin: 0px 15px; font-size: 11px; text-align: left; padding: 10px 0 0; color:#333;"><a href="http://www.apple.com/jp/itunes/download/" target="_blank" style="text-decoration:none;"><span style="font-size:11px; color:#1f44fe; text-decoration:none;" onmouseover="PDFDOver(this)" onmouseout="PDFDOut(this)">iTunes</span></a>のようなポッドキャストの受信ソフトにこのバナーをドラッグ＆ドロップすることで、番組を登録できます。<a target="_blank" href="http://www.podcastjuice.jp/what/" style="text-decoration:none;"><span style="font-size:11px; color:#1f44fe; text-decoration:none;" onmouseover="PDFDOver(this)" onmouseout="PDFDOut(this)">詳しくはこちら</span></a>。</div>\n' + 
                  '<a name="pjSearch_url' + id + '" href="#"><img src="' + btn_img + '" alt="RSS Poscasting" width="114" height="44" border="0" style="margin:10px 0 14px 0; vertical-align:top; padding:0; border:0;" /></a>\n';

    if ( regist_flg || rank_flg ){
        htmlStr += '<table cellspacing="0" cellpadding="0" border="0" width="142" style="margin:0 9px 13px; padding:0;">\n' + 
                   '<tr>\n' + 
                   '<td style=" margin:0; padding:0; border:0;"><img src="http://www.podcastjuice.jp/material/image/podfeed/bg_inner_top.gif" alt="" width="142" height="5" style="border:0; margin:0; padding:0;" /></td>\n' + 
                   '</tr>\n' + 
                   '<tr>\n' + 
                   '<td style="background:url(http://www.podcastjuice.jp/material/image/podfeed/bg_inner.gif) repeat-y 0 0; text-align:center; margin:0; padding:0; border:0;">\n';

        if ( regist_flg ){
            htmlStr += '<img src="http://www.podcastjuice.jp/material/image/podfeed/ttl_pnum.gif" alt="登録者数" style="margin:3px 0 2px; vertical-align:top; border:0;" />\n' + 
                       '<div style="font-size:11px; font-weight:bold; color:#333;"><a name="pjSearch_registration' + id + '" style="font-size:11px; font-weight:bold; color:#333; text-decoration: none;">----</a>人</div>\n';
        }

        if ( rank_flg ){
            htmlStr += '<img src="http://www.podcastjuice.jp/material/image/podfeed/ttl_rank.gif" alt="Podfeedランキング" width="126" height="13" style="margin:3px 0 2px; vertical-align:top; border:0;" />\n' + 
                       '<div style="position:relative; width:118px; margin:0 12px;">\n' + 
                       '<div style="border-bottom:#d4d4d4 1px solid; font-size:11px; font-weight:bold; color:#333; padding:0 27px 0 0;"><a name="pjSearch_rank' + id + '" style="font-size:11px; font-weight:bold; color:#333; text-decoration: none;">--</a>位</div>\n' + 
                       '<div style="font-size:11px; color:#333; padding:0 27px 0 0;"><a name="pjSearch_max_program' + id + '" style="font-size:11px; color:#333; text-decoration: none;">----</a>番組</div>\n' + 
                       '<img name="pjSearch_rankImg' + id + '" src="http://www.podcastjuice.jp/material/image/podfeed/btn_stay.gif" alt="Stay" style="position:absolute; top:0.7em; right:0; border:0; margin:0; padding:0;" />' + 
                       '</div>\n' + 
                       '<div style="margin:2px 0 4px"><a href="http://portal.podcastjuice.jp/ranking/ranking.cgi?ranking=dl_weeklyRanking&start=0&limit=20" style="text-decoration:none;"><span style="font-size:11px; color:#1f44fe; text-decoration:none;" onmouseover="PDFDOver(this)" onmouseout="PDFDOut(this)">他のランキングを見る≫</span></a></div>\n';
        }

        htmlStr += '</td>\n' + 
                   '</tr>\n' + 
                   '<tr>\n' + 
                   '<td style=" margin:0; padding:0; border:0;"><img src="http://www.podcastjuice.jp/material/image/podfeed/bg_inner_btm.gif" alt="" width="142" height="5" style="vertical-align:top; border:0; margin:0; padding:0;" /></td>\n' + 
                   '</tr>\n' + 
                   '</table>\n';

    }

    htmlStr += '<form action="" method="get" style="margin:0 10px 0; background:url(http://www.podcastjuice.jp/material/image/podfeed/ico_search.gif) no-repeat 0px 1px; text-align:center; line-height:1.3;" onSubmit="return goSearch(this);">\n' + 
               '<div style="font-size:11px; margin:0 0 0 12px; text-align:left; color:#333;">ポットキャスト検索</div>\n' + 
               '<input name="ref" type="hidden" value="newBanner" />\n' + 
               '<input name="word" type="text" style="width:130px; margin:0 0 5px;" maxlength=200/>\n' + 
               '<input type="image" src="http://www.podcastjuice.jp/material/image/podfeed/btn_search.gif" alt="検索" style="border:0;" />\n' + 
               '</form>\n' + 
               '</td>\n' + 
               '</tr>\n' + 
               '<tr>\n' + 
               '<td valign="top" align="left" style=" margin:0; padding:0; border:0;"><img src="http://www.podcastjuice.jp/material/image/podfeed/bg_pj_btm.gif" alt="" width="160" height="13" style="vertical-align:top; border:0; margin:0; padding:0;" /></td>\n' + 
               '</tr>\n' + 
               '</tbody>\n' + 
               '</table>\n' + 
               '</div>\n';

    return htmlStr;
}


function createBtnImg(collar){
    var btn_img = "http://www.podcastjuice.jp/material/image/podfeed/";
    switch (collar){
        case 1:
            btn_img += "btn_feed_red.gif";
            break;
        case 2:
            btn_img += "btn_feed_green.gif";
            break;
        case 3:
            btn_img += "btn_feed_blue.gif";
            break;
        case 4:
            btn_img += "btn_feed_black.gif";
            break;
        case 5:
            btn_img += "btn_feed_yellow.gif";
            break;
        case 6:
            btn_img += "btn_feed_pink.gif";
            break;
        default:
            btn_img += "btn_feed.gif";
            break;
    }

    return btn_img;
}

// nullチェック
function isNotEmpty(str){
    var flg = !/^[\s|　]*$/.test(str);
    return flg;
}

// 数字チェック
function isNum(str){
    var flg = this.isNotEmpty(str);
    if (flg){
        flg = /^[0-9]+$/.test(str);
    }

    return flg;
}

// URLチェック（ココログ投票）
function isURL(str){
    var flg = this.isNotEmpty(str);
    if (flg){
        str = str.toLowerCase();
        flg = /^((http(s?))\:\/\/)\S+$/.test(str);
    }

    return flg;
}


function PDFDOver(obj) {
  obj.style.textDecoration = 'underline';
}
function PDFDOut(obj) {
  obj.style.textDecoration = 'none';
}

// サブミットアクション
function goSearch(form) {
  var pre = "http://portal.podcastjuice.jp/dirretrieval/pjSearch.cgi?ref=newBanner&word=";
  // UTF-8でのURLエンコード
  var searchURL  = pre + encodeURIComponent(form.word.value);

  window.open(searchURL);
  return false;
}

