/**
 * index.js
 */

// TOPページリスト管理クラス
var toplist = function () {

    var list = [];

    return {
        currentTab : function (tab) {
            if (tab == '1') {

                Ext.fly('list_new').set({cls : 'new_cu'});
                Ext.fly('list_blog').set({cls : 'blog'});
                Ext.fly('list_discipline').set({cls : 'discipline'});

            } else if (tab == '2') {

                Ext.fly('list_new').set({cls : 'new'});
                Ext.fly('list_blog').set({cls : 'blog_cu'});
                Ext.fly('list_discipline').set({cls : 'discipline'});

            } else if (tab == '3') {

                Ext.fly('list_new').set({cls : 'new'});
                Ext.fly('list_blog').set({cls : 'blog'});
                Ext.fly('list_discipline').set({cls : 'discipline_cu'});

            }
        },

        // リスト取得メソッド
        getList : function (tab) {
            if (list.length && list[tab]) {

                // リスト生成
                this.createList(tab);

                // リストナビ変更
                this.changeListNavi(tab);

                writeCookie('pet_portal_top_tab', tab, 60);

                return;

            }

            Ext.Ajax.request({

                url : 'list.html?tab='+tab,

                success: function(response, opts) {

                    list[tab] = Ext.decode(response.responseText);

                    // リスト生成
                    this.createList(tab);

                    // リストナビ変更
                    this.changeListNavi(tab);

                    writeCookie('pet_portal_top_tab', tab, 60);

                },
                scope : this

            });
        },

        // タブチェンジメソッド
        changeTab : function(tab) {

            if (tab != '1' && tab != '2' && tab != '3') {
                tab = '1';
            }

            // タブ変更
            this.currentTab(tab);

            // リスト取得
            this.getList(tab);

        },

        // リスト生成メソッド
        createList : function (tab) {

            var dh = Ext.DomHelper;

            var ul = Ext.get('info_list');

            ul.select('li').remove();

            if (!list[tab] || !list[tab].length) {
                dh.append(
                    ul,
                    {
                        tag : 'li',
                        html : '現在登録がありません。'
                    }
                );
                return;
            }

            if (!Ext.isArray(list[tab]) || !list[tab].length) {
                return;
            }

            var originalA = '<span class="tlist_date">%%DATE%%</span><a href="%%URL%%" target="_blank">%%TITLE%%</a>';
            var originalA2 = '<a href="javascript:void(0);" onclick="createSubmit({action : \'%%ACTION%%\', method : \'post\', opt : {id : \'%%ID%%\'}}); return false;" onkeypress="">%%TITLE%%</a>';
            var originalA2 = '<span class="tlist_date">%%DATE%%</span><a href="%%ACTION%%?id=%%ID%%">%%TITLE%%</a>';

            Ext.each(
                list[tab],
                function(val, n, item) {

                    if (tab == 2) {

                        // 表示幅を確認
                        val.name = this.strimWidth(val.name, 302);

                        tagA = originalA;
                        tagA = tagA.replace(/%%DATE%%/, val.view_date);
                        tagA = tagA.replace(/%%URL%%/, val.url);
                        tagA = tagA.replace(/%%TITLE%%/, val.name);


                        if (n%2) {
                            dh.append(ul, {tag : 'li', html : tagA});
                        } else {
                            dh.append(ul, {
                                tag : 'li',
                                cls : 'style01',
                                html : tagA
                            });
                        }

                    } else {

                        tagA = originalA2;
                        var temp = val.url.split('?');
                        var action = temp[0];
                        var idTemp = '';
                        if (temp[1]) {
                            idTemp = temp[1].split('=');
                        }

                        var idName = '';
                        var idVal = '';
                        if (idTemp[0] && idTemp[1]) {
                            idName = idTemp[0];
                            idVal = idTemp[1];
                        }

                        // 表示幅を確認
                        val.name = this.strimWidth(val.name, 302);

                        tagA = tagA.replace(/%%DATE%%/, val.view_date);
                        tagA = tagA.replace(/%%ACTION%%/, action);
                        tagA = tagA.replace(/%%ID%%/, idVal);
                        tagA = tagA.replace(/%%TITLE%%/, val.name);

                        if (n%2) {
                            dh.append(ul, {tag : 'li', html : tagA});
                        } else {
                            dh.append(ul, {
                                tag : 'li',
                                cls : 'style01',
                                html : tagA
                            });
                        }
                    }
                },
                this
            );
        },

        // リストナビ変更メソッド
        changeListNavi : function (tab) {

            var html = [
                '<p class="onecursor_w"><a href="./news/">新着情報一覧</a></p>',
                '<p class="onecursor_w"><a href="./blog/">ブログ一覧</a></p>',
                '<p class="onecursor_w"><a href="./breed/">しつけ一覧</a></p>'
            ];

            tab = parseInt(tab);

            var dh = Ext.DomHelper;
            // リストナビ変更
            Ext.fly('list_navi').select('p').remove();
            dh.append(
                Ext.fly('list_navi'),
                html[(tab-1)]
            );

        },

        strimWidth : function (val, valWidth) {

            var cfg = {
                tag : 'div',
                id : 'x-width-div',
                style : {
                    position : 'absolute',
                    top : '-500',
                    left : '-500',
//                    'min-width' : '390',
                    'font-style' : 'normal',
                    'font-weight' : 'normal',
                    'font-size' : '12px'

                },
                html: val
            }

            var dh = Ext.DomHelper;
            var el = dh.append(Ext.getBody(), cfg);

            var w = Ext.fly(el).getWidth();

            // 永久ループ回避
            var n = 0;
            while (valWidth < w) {

                var oldW = w;

                val = val.slice(0, (val.length - 4)) + '・・・';
//                val = val.slice(0, (val.length - 4));
                Ext.fly(el).remove();

                cfg.html = val;
                el = dh.append(Ext.getBody(), cfg);
                w = Ext.fly(el).getWidth();
/*
                if (w >= oldW) {
                    break;
                }
*/
                if (n > 200) {
                    break;
                }

                ++n;

            }

            Ext.fly(el).remove();

            return val;

        },

        // onLoad用メソッド
        init : function () {
            // 開いていたタブを取得
            var tab = readCookie('pet_portal_top_tab');

            this.changeTab(tab);
        }

    }

}


// クッキー書き込みメソッド
function writeCookie(kword, kdata, kday)
{

    if(!navigator.cookieEnabled) {
        return;
    }
    sday = new Date();
    sday.setTime(sday.getTime() + (kday * 1000 * 60 * 60 * 24));
    s2day = sday.toGMTString();
    document.cookie = kword + "=" + escape(kdata) + ";expires=" + s2day;
}

// クッキー読み込みメソッド
function readCookie(kword)
{

    if(typeof(kword) == "undefined") {
        return "";
    }
    kword = kword + "=";
    kdata = "";
    scookie = document.cookie + ";";
    start = scookie.indexOf(kword);
    if(start != -1) {
        end = scookie.indexOf(";", start);
        kdata = unescape(scookie.substring(start + kword.length, end));
    }
    return kdata;
}

// クッキー削除メソッド
function deleteCookie(kword)
{
    document.cookie = kword + "=;expires=Thu,01-Jan-70 00:00:01 GMT";
}

function init()
{

    objListManager = null;
    objListManager = new toplist();
//    objListManager.init();

    objAnqManager = null;
    objAnqManager = new anqmanager();

}

function clear() {

    for (var key in objListManager) {

        delete objListManager[key];
    }

    objListManager = null;
    objAnqManager = null;

}

// onLoadイベントに追加
if( window.addEventListener ) {
    window.addEventListener(
        "unload",
        clear
        ,false
    );
} else if( window.attachEvent ) {
    window.attachEvent(
        "onunload",
        clear
    );
} else {
    window.onunload = clear;
}

$(document).ready(function(){

    init();

    simple_tooltip(".newEntryPhoto a","tooltipL");

    // {{{ 画像にょっき設定

//    $(".entryPhoteItem a").prettyPhoto();

    $(".entryPhotoItem").hover(function(){
        $(this).css({'z-index':'10'});
        $(this).find('img').addClass("hover").stop().animate({marginTop:'-10px',marginLeft:'-10px',top:'50%',left:'50%',width:'110px',height:'82px',padding:'0px',opacity:'100'},300);
    },function(){
        $(this).css({'z-index':'0'});
        $(this).find('img').removeClass("hover").stop().animate({marginTop:'0',marginLeft:'0',top:'6px',left:'6px',width:'90px',height:'67px',padding:'0px',opacity:'100'},400);
    });

    // }}}
    // {{{ コンテンツ紹介

    $('li[id^="disp"]').each(function() {
        $(this).bind('mouseover', function () {

            // リンク先取得
            var href = $(this).children('a').attr('href');

            // 画像パス
            var path = $(this).children('img').attr('src');

            // alt
            var alt = $(this).children('a').attr('alt');

            // 紹介テキスト
            var text = $(this).children('p').text();

            $('dt#imgdisp a').attr('href', href);
            $('dt#imgdisp a img').attr('src', path);
            $('dt#imgdisp a img').attr('alt', alt);
            $('p#textdisp').text(text);

        });
    });

    // }}}
    // {{{ モニター切り替え

    $('#monitor_on, #monitor_off').bind('click', function () {

        if ($(this).attr('class').match(/_on/)) {
            return false;
        }

        $('a[id^="monitor_"]').each(function () {

            if ($(this).attr('class').match(/_on/)) {
                var cls = $(this).attr('class');
                var rep = $(this).attr('class').replace(/_on/, '');
                $(this).removeClass(cls);
                $(this).attr('class', rep);
            } else {
                var cls = $(this).attr('class');
                $(this).removeClass(cls);
                $(this).attr('class', cls+'_on');
            }

            var id = $(this).attr('id')+'_disp';

            if ($(this).attr('class').match(/_on/)) {
                $('#'+id).removeClass('unvisible');
            } else {
                $('#'+id).addClass('unvisible');
            }
        });
        heightLine();
    });

    heightLine();

    // }}}

});

