﻿/*--------------------------------------------------------------------------
 * Copyright (c) 2006, drk<drk7jp@gmail.com>(http://www.drk7.jp/)
 *
 * License :
 *   Articstic License 2.0
 *
 * Drk7jpUranai.js :
 *   0.01 This library is a utility for Livedoor Uranai RSS (http://fortune.livedoor.com/xml/astro12.rdf)
 *   0.02 This library is a utility for Livedoor Uranai RSS (http://fortune.jp.msn.com/rss.aspx?rsstype=12rank)
 *
 *--------------------------------------------------------------------------*/
var LURSS_NODEID      = 'lurss';
var LURSS_MENU_NODEID = 'lurss_menu';
var LURSS_DEFAULT_ID  = 0;
var LURSS_ID          = 0;
var LURSS_COOKIE_NAME = 'lurss_id';
var LURSS_IMAGE       = {
    '牡羊座': {'img':'http://www.drk7.jp/MT/images/MTUranaiRanking/aries.gif'},
    '牡牛座': {'img':'http://www.drk7.jp/MT/images/MTUranaiRanking/taurus.gif'},
    '双子座': {'img':'http://www.drk7.jp/MT/images/MTUranaiRanking/gemini.gif'},
    '蟹座'  : {'img':'http://www.drk7.jp/MT/images/MTUranaiRanking/cancer.gif'},
    '獅子座': {'img':'http://www.drk7.jp/MT/images/MTUranaiRanking/leo.gif'},
    '乙女座': {'img':'http://www.drk7.jp/MT/images/MTUranaiRanking/virgo.gif'},
    '天秤座': {'img':'http://www.drk7.jp/MT/images/MTUranaiRanking/libra.gif'},
    '蠍座'  : {'img':'http://www.drk7.jp/MT/images/MTUranaiRanking/scorpio.gif'},
    '射手座': {'img':'http://www.drk7.jp/MT/images/MTUranaiRanking/sagittarius.gif'},
    '山羊座': {'img':'http://www.drk7.jp/MT/images/MTUranaiRanking/capricorn.gif'},
    '水瓶座': {'img':'http://www.drk7.jp/MT/images/MTUranaiRanking/aquarius.gif'},
    '魚座'  : {'img':'http://www.drk7.jp/MT/images/MTUranaiRanking/pisces.gif'}
};
var LURSS_IDS         = [
    {'id':0  ,'title':'牡羊座'},
    {'id':1  ,'title':'牡牛座'},
    {'id':2  ,'title':'双子座'},
    {'id':3  ,'title':'蟹座'  },
    {'id':4  ,'title':'獅子座'},
    {'id':5  ,'title':'乙女座'},
    {'id':6  ,'title':'天秤座'},
    {'id':7  ,'title':'蠍座'  },
    {'id':8  ,'title':'射手座'},
    {'id':9  ,'title':'山羊座'},
    {'id':10 ,'title':'水瓶座'},
    {'id':11 ,'title':'魚座'  }
];

var LURSS_IDS_DATE         = [
    {'id':0  ,'title':'(3/21～4/19)'},
    {'id':1  ,'title':'(4/20～5/20)'},
    {'id':2  ,'title':'(5/21～6/21)'},
    {'id':3  ,'title':'(6/22～7/22)'  },
    {'id':4  ,'title':'(7/23～8/22)'},
    {'id':5  ,'title':'(8/23～9/22)'},
    {'id':6  ,'title':'(9/23～10/23)'},
    {'id':7  ,'title':'(10/24～11/21)'  },
    {'id':8  ,'title':'(11/22～12/21)'},
    {'id':9  ,'title':'(12/22～1/19)'},
    {'id':10 ,'title':'(1/20～2/18)'},
    {'id':11 ,'title':'(2/19～3/20)'  }
];


/*--------------------------------------------------------------------------
 * LURSS_Selecter
 *--------------------------------------------------------------------------*/
var LURSS_Selecter = Class.create();
LURSS_Selecter.prototype={

    initialize: function(){
        this.container=$(LURSS_MENU_NODEID);
        this.t_select = document.createElement('select');

        var selected_value = getCookie_ura(LURSS_COOKIE_NAME);
        if(!selected_value) { selected_value= LURSS_DEFAULT_ID; }
        LURSS_ID = selected_value;

        new XML2JSON('http://fortune.jp.msn.com/rss.aspx?rsstype=12rank' , {container:LURSS_NODEID, template:this.template, id:Math.round(Math.random()*1000000000000000000)});

        for(var i=0;i<LURSS_IDS.length;i++){
            
            this.t_select.options[i] = new Option(LURSS_IDS[i].title +" "+ LURSS_IDS_DATE[i].title,LURSS_IDS[i].id,false,false);
        }
        this.t_select.options[selected_value].selected=true;

        this.container.appendChild(this.t_select);
        Event.observe(this.t_select,'change', this.t_onchange.bindAsEventListener(this), false);
        Event.observe(this.t_select,'keydown',this.t_onchange.bindAsEventListener(this), false);
        Event.observe(this.t_select,'keyup',  this.t_onchange.bindAsEventListener(this), false);
    },
    template: function(context) {
        var title = LURSS_IDS[LURSS_ID].title;
        for(var i=0;i<LURSS_IDS.length;i++){
            if ( context.channel.item[i].title.match(title) ) {
                LURSS_ID = i;
                break;
            }
        }
        title = context.channel.item[LURSS_ID].title;
        title = title.replace( /^\d+位　/,'' ); 
        var img   = LURSS_IMAGE[title].img;
        var msg   = context.channel.item[LURSS_ID].description;

        var html = '<table summary="" border="0" cellspacing="2" cellpadding="0">'
        + '<tbody>'
        + '<tr><td width="40" align="center"><img src="' + img + '" title="' + title + '" /></td>'
        + '<td width="175">' + msg + '</td></tr>'
        + '</tbody></table>';

        return html;
    },
    t_onchange :function() {
        var selected_value = $F(this.t_select);
        if(LURSS_ID == selected_value) { return; }
        LURSS_ID = selected_value;

        var expire = new Date('2299','01', '01');
        setCookie_ura(LURSS_COOKIE_NAME, selected_value, expire);
        new XML2JSON('http://fortune.jp.msn.com/rss.aspx?rsstype=12rank' , {container:LURSS_NODEID, template:this.template, id:Math.round(Math.random()*1000000000000000000)});
    }
};


/*--------------------------------------------------------------------------
 * Event
 *--------------------------------------------------------------------------*/
Event.observe(window, "load", function(){
    var d = $(LURSS_NODEID);
    $(LURSS_NODEID).innerHTML = '占い...';

    new LURSS_Selecter();
});




/*--------------------------------------------------------------------------
 * Cookie
 *--------------------------------------------------------------------------*/
function setCookie_ura(name,value,expire){
    document.cookie = name + '=' + escape(value) + ((expire==null)?'':('; expires='+expire.toGMTString()));
}

function getCookie_ura(name){
    var search = name + '=';
    if(document.cookie.length>0) {
        offset = document.cookie.indexOf(search);
        if(offset != -1) {
            offset += search.length;
            end     = document.cookie.indexOf(';',offset);
            if(end == -1) {
                end = document.cookie.length;
            }
            return unescape(document.cookie.substring(offset,end));
        }
    }
    return null;
}


