﻿// ----------------------------------------------------------------------------------------------------
// metaタグ読み取り
// ----------------------------------------------------------------------------------------------------
function readMetaContent(name, httpEquiv){
    var metaElm;
    var retVal;

    retVal = '';
    
    // metaタグ取りにいきます
    metaElm = document.getElementsByTagName('meta');
    
    // metaタグ取得判定
    if (!metaElm){
        // 取れませんでした
        return;
    }
    
    // タグ種類の判定
    var elmName;
    var elmEquiv;
    for (var i = 0 ; i < metaElm.length ; i++){
        // 属性取得
        elmName = metaElm[i].attributes['name'];
        elmEquiv = metaElm[i].attributes['http-equiv'];
        if (name != null && elmName != null && elmName.value == name){
            // 取れました
            retVal = metaElm[i].attributes['content'].value;
            break;
        }
        if (httpEquiv != null && elmEquiv != null && elmEquiv.value == httpEquiv){
            // 取れました
            retVal = metaElm[i].attributes['http-equiv'].value;
            break;
        }
    }

    return retVal;
}

// ----------------------------------------------------------------------------------------------------
// Descriptionのmetaタグ内容を出力
// ----------------------------------------------------------------------------------------------------
function readMetaDescription(){
    document.write(readMetaContent('description', null));
}

// ----------------------------------------------------------------------------------------------------
// Hidden値取得
// ----------------------------------------------------------------------------------------------------
function getHiddenValue(id){
    var elms;
    var attr;
    var retVal;

    // inputタグを捜索
    elms = document.body.getElementsByTagName('input');
    attr = '';
    retVal = '';
    for (var i = 0 ; i < elms.length ; i++){
        attr = elms[i].type;
        if (id != null && attr != null && attr == 'hidden' & elms[i].id == id){
            retVal = elms[i].value;
            break;
        }
    }
    
    return retVal;
}

// ----------------------------------------------------------------------------------------------------
// Anchor作成
// ----------------------------------------------------------------------------------------------------
function createAnchor(name, directory){
    var anchorText = '<a href="{0}{1}{2}">{3}</a>';    // 置換用
    var rootPath;
    var directoryPath;
    var filePath;
    var showText;
    
    rootPath = getHiddenValue('rootPath');
    directoryPath = ''
    for (var i = 0 ; directory != null && i < directory.length ; i++){
        // ディレクトリ階層を追う
        directoryPath += directory[i] + '/';
        showText = directory[i];
    }
    filePath = name + '.html';
    if (showText == null || showText == ''){
        showText = name;
    }
    showText = showText.toUpperCase();
    
    document.write(anchorText.replace('{0}', rootPath).replace('{1}', directoryPath).replace('{2}', filePath).replace('{3}', showText));
}

// ----------------------------------------------------------------------------------------------------
// パンくずリスト作成
// ----------------------------------------------------------------------------------------------------
function createBreadcrumbs(arg){
    // 決め打ち部分作成
    createAnchor('index', null);
    document.write(' &gt;&gt; ');
    createAnchor('top', null);
    
    // 引数部分作成
    var argWk = null;
    for (var i = 0 ; i < arg.length ; i++){
        argWk = arg.slice(0, i + 1);
        document.write(' &gt;&gt; ');
        createAnchor('index', argWk);
    }
}

// ----------------------------------------------------------------------------------------------------
// Googole Analytics
// ----------------------------------------------------------------------------------------------------
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-22831681-1']);
    _gaq.push(['_trackPageview']);

    (function() {
      var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
      ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();

// ----------------------------------------------------------------------------------------------------
// アンカーのビヘイビア(偽)
// ----------------------------------------------------------------------------------------------------
function setAnchorBehavior(){
    var aElms;
    var rootPath;
    var wk;
    var wkArr;

    // aタグ抽出
    aElms = document.links;
    if (!aElms){
        // いじわるされたのでaタグが取れませんでした
        return;
    }

    // rootPath抽出(利用頻度高いので)
    rootPath = getHiddenValue('rootPath');
    if (!rootPath){
        // いじわるされたのでrootPathが取れませんでした
        return;
    }

    for (var i = 0 ; i < aElms.length ; i++){
        if (!aElms[i].hasAttribute('rel')){
            // rel属性指定が無い場合はスルー
            continue;
        }
        
        wk  = aElms[i].getAttribute('rel');
        
        if (!wk || wk.length == 0){
            // 属性値が取れなかった場合は泣く泣くスルー
            continue;            
        }
        
        wkArr = wk.split(' ');
        
        if (!wkArr || wkArr.length == 0){
            // 属性値を綺麗にセパレートできなかった場合は華麗にスルー
            continue;
        }
        
        // 属性値のコレクションから拡張内容を決定
        for (var j = 0 ; j < wkArr.length ; j++){

            // TODO; trimやhogehogeCaseいるかな？
            switch (wkArr[j]){
                case 'index' :
                    aElms[i].href = rootPath;
                    break;
                case 'top' :
                    aElms[i].href = rootPath + getHiddenValue('topPath');
                    break;
                case 'about' :
                    aElms[i].href = rootPath + getHiddenValue('aboutPath');
                    break;
                case 'create' :
                    aElms[i].href = rootPath + getHiddenValue('createPath');
                    break;
                case 'misc' :
                    aElms[i].href = rootPath + getHiddenValue('miscPath');
                    break;
                case 'diary' :
                    aElms[i].href = rootPath + getHiddenValue('diaryPath');
                    break;
                case 'link' :
                    aElms[i].href = rootPath + getHiddenValue('linkPath');
                    break;
                case 'mail' :
                    aElms[i].href = rootPath + getHiddenValue('mailPath');
                    break;
                case 'rss' :
                    aElms[i].href = rootPath + getHiddenValue('rssPath');
                    break;
                case 'kp' :
                    aElms[i].href = rootPath + getHiddenValue('kpPath');
                    break;
                case 'dd' :
                    aElms[i].href = getHiddenValue('ddPath');
                    break;
                case 'external' :
                    aElms[i].target =  '_blank';
                    break;
                default :
                    break;
            }
        }
    }
}

// ----------------------------------------------------------------------------------------------------
// imgのビヘイビア(偽)
// ----------------------------------------------------------------------------------------------------
function setImgBehavior(){
    var imgElms;
    var rootPath;
    var pictPath;

    // imgタグ抽出
    imgElms = document.images;
    // パス抽出
    rootPath = getHiddenValue('rootPath');
    pictPath = getHiddenValue('pictPath');
    
    if (!imgElms){
        // いじわるされたのでimgタグが取れませんでした
        return;
    }
    if (!rootPath){
        // いじわるされたのでrootPathが取れませんでした
        return;
    }
    if (!pictPath){
        // いじわるされたのでpictPathが取れませんでした
        return;
    }

    for (var i = 0 ; i < imgElms.length ; i++){
        var imgLongdesc = imgElms[i].getAttribute('longdesc');
        if (!imgLongdesc || imgLongdesc == ''){
            // longdesc属性が定義されていない場合はスルー
            continue;
        }

        // src属性を書き換え
        // これってある種遅延読込だよね
        imgElms[i].src = rootPath + pictPath + imgLongdesc;
        
        // 音声読み上げされたり参照しにいかれても困るので
        // longdesc属性は取り除いておく
        imgElms[i].removeAttribute('longdesc');
    }
}

// ----------------------------------------------------------------------------------------------------
// onload
// ----------------------------------------------------------------------------------------------------
window.onload = function(){
    // アンカーのハック
    setAnchorBehavior();

    // imgのハック
    setImgBehavior();
};

// ----------------------------------------------------------------------------------------------------
// 最終更新日取得
// ----------------------------------------------------------------------------------------------------
function getLastWriteDt(){
    // こんなものは決め打ちでいい
    document.write('2011/04/30 13:07');
}


