if ( typeof( IPG ) == 'undefined' ) IPG = {};
IPG.GenreListManager = function( config , refreshMethod ){
    this.version = "0.1";
    this.fontSize="10px";
    
    this.config = config;
    if( !this.config ) return;
    
    this.color_palette = this.config.config_xml.color_info.color_palette;
    
    this.refreshMethod = refreshMethod;
    
    // リムーブ時の指定用に変数化。bind必要ないものは変数化してません。
    if( !this.selectorOpenMethod ) this.selectorOpenMethod = this.selectorOpen.bind( this );
    if( !this.selectorCloseMethod ) this.selectorCloseMethod = this.selectorClose.bind( this );
    if( !this.selectorMouseOverMethod ) this.selectorMouseOverMethod = this.selectorMouseOver.bind( this );
    if( !this.selectorMouseOutMethod ) this.selectorMouseOutMethod = this.selectorMouseOut.bind( this );
    if( !this.paletteOpenMethod ) this.paletteOpenMethod = this.paletteOpen.bind( this );
    if( !this.resetMethod ) this.resetMethod = this.resetColor.bind( this );
    if( !this.submitMethod ) this.submitMethod = this.submitColor.bind( this );
    if( !this.selectColorMethod ) this.selectColorMethod = this.selectColor.bind( this );
    
    this.cookie_manager = new IPG.UserPropertyManager();
    
    this.createData();
    
    return this;
};

IPG.GenreListManager.prototype.createData = function(){
    if( !this.xml_data ){
        var ajaxOpt = {
            'method': 'get'
            ,'asynchronous': false
        };
        var xhrObj = new Ajax.Request( this.config.config_xml.api_url.getGenreList , ajaxOpt );
        if( xhrObj && xhrObj._complete ){
            this.parseData( xhrObj.transport.responseXML.documentElement );
        }else{
            Element.setStyle( $( "epg_cloak" ) , "display: block;" );
            $( "cloak_img" ).src = this.config.error_image;
        }
    }
};

// ジャンルデータの取得とHTML生成を切り分けるか、取得のみロジックを用意する。
IPG.GenreListManager.prototype.createGenreColorSelector = function( target ){
    if( !this.xml_data ) return;
    var targetElement = IPG.Utility.getElement( target );
    if( !targetElement ) return;
    this.makeGenreColorSelectorHTML( targetElement );
    this.bindGenreColorSelectorEvent( targetElement );
};

IPG.GenreListManager.prototype.parseData = function( xml_data ){
    if( this.xml_data ) return;
    if( !xml_data || !xml_data.childNodes || !xml_data.childNodes.length ) return;
    var genrelist = [];
    for( var i = 0 , genrelist_length = xml_data.childNodes.length; i < genrelist_length; i++ ){
        var child = xml_data.childNodes[i];
        genrelist[i] = {
            'name': child.getElementsByTagName( "name" )[0].firstChild.nodeValue,
            'id': child.getElementsByTagName( "id" )[0].firstChild.nodeValue
        };
        
        genrelist[i].color = {};
        var colorElement = child.getElementsByTagName( "color" )[0];
        genrelist[i].color.bg = colorElement.getElementsByTagName( "bg" )[0].firstChild.nodeValue;
        genrelist[i].color.text = colorElement.getElementsByTagName( "text" )[0].firstChild.nodeValue;
        genrelist[i].color.cap = colorElement.getElementsByTagName( "cap" )[0].firstChild.nodeValue;
        
        var monirGenreElement = child.getElementsByTagName( "minor_genre" )[0];
        var monir_genrelist = [];
        for( var j = 0 , minorgenre_length = monirGenreElement.childNodes.length; j < minorgenre_length; j++ ){
            var minorgenre = monirGenreElement.childNodes[j];
            var nameNode = minorgenre.getElementsByTagName( "name" )[0].firstChild;
            var idNode = minorgenre.getElementsByTagName( "id" )[0].firstChild;
            monir_genrelist[j] = {
                'name': ( nameNode ) ? nameNode.nodeValue : undefined,
                'id': ( idNode ) ? idNode.nodeValue : undefined
            };
        }
        genrelist[i].minor_genre = monir_genrelist;
        
        // クッキーからジャンルカラーの取得出来てない時は、ここでデフォルトカラーをセット。
        var colorid = this.getColorId( genrelist[i].color );
        if( colorid != undefined ){
            if( !this.config.cookie.genrecolor ) this.config.cookie.genrecolor = [];
            if( !this.config.cookie.genrecolor[ genrelist[i].id ] ) this.config.cookie.genrecolor[ genrelist[i].id ] = colorid;
        }
    }
    this.xml_data = genrelist;
};

IPG.GenreListManager.prototype.getColorId = function( colorObj ){
    if( !colorObj || !colorObj.bg || !colorObj.text || !colorObj.cap ) return undefined;
    for( var i = 0 , len = this.color_palette.length; i < len; i++ ){
        var palette = this.color_palette[i];
        if( colorObj.bg === palette.bg && colorObj.cap === palette.cap && colorObj.text === palette.text ) return i;
    }
    return undefined;
};

IPG.GenreListManager.prototype.makeGenreColorSelectorHTML = function( targetElement ){
    if( !targetElement || !this.xml_data ) return;
    
    if( this.selectorNode ) return;
    
    var doc = targetElement.ownerDocument || targetElement.document;
    var start_reservegenre = this.config.config_xml.epg_info.startReservedGenreId;
    var color_info = this.config.config_xml.color_info;
    
    // セレクタの出力
    this.selectorNode = doc.createElement("div");
	this.selectorNode.id = "epg_genrecolor_selector";
    targetElement.appendChild( this.selectorNode );
	Element.setStyle( this.selectorNode , "position: absolute; top: 27px; left: 0px; z-index: 1; display: block; visibility: hidden;" );
    
    var selectorParent = doc.createElement("div");
    this.selectorNode.appendChild( selectorParent );
    Element.setStyle( selectorParent , "position: relative; z-index: 1;" );
    
    var html = "<div style=\"width: 135px; height: 8px; position: relative;\"><img src=\""+this.config.config_xml.img_path.genre_selector_top+"\"></div>";
    html += "<div id=\"selector_body_parent\" style=\"z-index: 0; width: 135px; background-image: url("+this.config.config_xml.img_path.genre_selector_rep+"); position: relative;\">";
    for( var i = 0 , len = this.xml_data.length; i < len; i++ ){
        
        // 予備エリアは処理対象から除外
        if( start_reservegenre <= i ) continue;
        
        // デフォルトカラーの取得
        var dispColor = this.xml_data[i].color;
        var colorid = this.getColorId( dispColor );
        // クッキーに指定色の設定があれば、上書き。
        var genreid = eval( this.xml_data[i].id );
        if( this.config.cookie.genrecolor && this.config.cookie.genrecolor[ genreid ] ){
            colorid = this.config.cookie.genrecolor[ genreid ];
            dispColor = this.color_palette[colorid];
        }
        if( !dispColor || !colorid ){
            dispColor = color_info.color_default;
            colorid = 0;
        }
        html += "<div id=\"major_genre_"+i+"\" colorid=\""+colorid+"\" name=\"major_genre\" style=\"width: 124; height: 14px; padding-top: 2px; margin: 0px 4px 4px 4px; position: relative; ; cursor: pointer;\">";
        html += "<div style=\"width: 10px; height: 10px; font-size: 1px; background-color: #"+dispColor.bg.substring( 2 , 8 )+"; border: 1px solid black; position: relative; margin: 0px 2px 2px 2px; float: left;\"></div><span style=\"color: #1a465d; font-size: "+this.fontSize+";\">"+this.xml_data[i].name+"</span>";
        html += "</div>";
    }
    html += "<div id=\"selector_form\" style=\"width: 132px; height: 18px; position: relative;\">"
    html += "<div id=\"selector_reset\" style=\"width: 78px; height: 18px; position: relative; padding-left: 6px; float: left; cursor: pointer;\"><img src=\""+this.config.config_xml.img_path.genre_selector_reset+"\"></div>";
    html += "<div id=\"selector_submit\" style=\"width: 38px; height: 18px; position: relative; padding-left: 4px; padding-right: 6px; float: left; float: left; cursor: pointer;\"><img src=\""+this.config.config_xml.img_path.genre_selector_submit+"\"></div>";
    html += "</div>";
    html += "</div>";
    html += "<div style=\"width: 135px; height: 23px; position: relative;\"><img src=\""+this.config.config_xml.img_path.genre_selector_btm+"\"></div>";
    selectorParent.innerHTML = html;
    if( selectorParent.style.display == "none" ){
        selectorParent.style.visibility = "hidden";
        selectorParent.style.display = "block";
    }
    
    if( !Prototype.Browser.IE || window.XMLHttpRequest ){
        var shadow_top_path = this.config.config_xml.img_path.genre_selector_top_shadow;
        var shadow_rep_path = this.config.config_xml.img_path.genre_selector_rep_shadow;
        var shadow_btm_path = this.config.config_xml.img_path.genre_selector_btm_shadow;
    
        var selectorShadow = doc.createElement("div");
        this.selectorNode.appendChild( selectorShadow );
        Element.setStyle( selectorShadow , "position: absolute; top: 0px; left: 0px; z-index: 0;" );
        html = "<div style=\"width: 140px; height: 8px; position: relative;\"><img src=\""+shadow_top_path+"\"></div>";
        html += "<div id=\"selector_body_shadow\" style=\"width: 140px; background-image: url("+shadow_rep_path+"); position: relative;\"></div>";
        html += "<div style=\"width: 140px; height: 27px; position: relative;\"><img src=\""+shadow_btm_path+"\"></div>";
        selectorShadow.innerHTML = html;
    }
    
    var selector_body = $( "selector_body_parent" );
    var selector_shadow = $( "selector_body_shadow" );
    if( selector_body && selector_shadow ){
        selector_shadow.style.height = selector_body.offsetHeight+"px";
        selector_body.style.height = selector_body.offsetHeight+"px";
    }
    
    // パレットの出力
    this.paletteNode = doc.createElement("div");
    this.paletteNode.id = "epg_genrecolor_palette";
    targetElement.appendChild( this.paletteNode );
    Element.setStyle( this.paletteNode , "position: absolute; z-index: 2; display: none;" );
    var paletteParent = doc.createElement("div");
    paletteParent.id = "palette_parent";
    this.paletteNode.appendChild( paletteParent );
    Element.setStyle( paletteParent , "background-image: url("+this.config.config_xml.img_path.genre_palette+"); width: 77px; height: 72px; position: absolute; z-index: 1;" );
    var html = "<div style=\"font-size: 1px; width: 50px; height: 64px; position: absolute; top: 50%; left: 50%; margin-top: -32px; margin-left: -30px;\">";
    for( var i = 0 , len = this.color_palette.length; i < len; i++ ){
        html += "<div colorid=\""+i+"\" style=\"line-height: 3; background-color: #"+this.color_palette[i].bg.substring( 2 , 8 )+"; border: 1px solid black; position: relative;  width: 10px; height: 10px; margin: 2px 2px 2px 2px; cursor: pointer; float: left;\"></div>";
        if( ( i + 1 ) % 3 == 0 ) html += "<div id=\"cleardiv\" style=\"line-height: 3; clear: left; visibility: hidden;\"></div>"
    }
    html += "</div>";
    paletteParent.innerHTML += html;
    
    if( !Prototype.Browser.IE || window.XMLHttpRequest ){
        var palette_shadow_path = this.config.config_xml.img_path.genre_palette_shadow;
        var paletteShadow = doc.createElement("div");
        this.paletteNode.appendChild( paletteShadow );
        Element.setStyle( paletteShadow , "background-image: url("+palette_shadow_path+"); width: 83px; height: 80px; position: absolute; z-index: 0;" );
    }
};

IPG.GenreListManager.prototype.selectorOpen = function( evt ){
    evt = IPG.Utility.fixEvent( evt , this );
    
    // 左クリック以外は処理しない。
    if( evt.which != 1 ){
        Event.stop( evt );
        return false;
    }
    
    Event.observe( $("epg_container") , "mousedown" , this.selectorCloseMethod );
    
    var selector = this.selectorNode;
    var selector_style = selector.style;
    if( selector_style ){
        if( selector_style.display == "block" && selector_style.visibility == "visible" ){
            this.selectorCloseMethod( evt );
        }else{
            Element.setStyle( this.selectorNode , "display: block; visibility: visible;" );
            $( "epg_genrecolor_controll_button" ).src = this.config.config_xml.img_path.genrectrl_button_over;
            
            // EPGにポップアップ出てたら消す。
            var pg_detail = $( "pg_detail" );
            if( pg_detail ){
                pg_detail.removeAttribute( "target_id" );
                var detail_style = pg_detail.style;
                if( detail_style.display == "block" ) detail_style.display = "none";
            }
        }
    }
    
    Event.stop( evt );
    return false;
};

IPG.GenreListManager.prototype.selectorClose = function( evt ){
    evt = IPG.Utility.fixEvent( evt , this );
    
    // 左クリック以外は処理しない。
    if( evt.which != 1 ){
        Event.stop( evt );
        return false;
    }
    
    // セレクタの初期化+クッキーが有れば反映。
    var selectorBody = $( "selector_body_parent" );
    for( var i = 0 , len = selectorBody.childNodes.length; i < len; i++ ){
        var selectorChild = selectorBody.childNodes[i];
        if( selectorChild && selectorChild.getAttribute("name") == "major_genre" ){
            var dispColor = this.xml_data[i].color;
            var colorid = this.getColorId( dispColor );
            
            // クッキーに指定色の設定があれば、上書き。
            var genreid = eval( this.xml_data[i].id );
            if( this.config.cookie.genrecolor && this.config.cookie.genrecolor[ genreid ] ){
                colorid = this.config.cookie.genrecolor[ genreid ];
                dispColor = this.color_palette[colorid];
            }
            selectorChild.setAttribute( "colorid" , colorid );
            
            var def_color = "#"+dispColor.bg.substring( 2 , 8 );
            Element.setStyle( selectorChild.firstChild , "background-color: "+def_color+";" );
        }
    }
    
    Event.stopObserving( $("epg_container") , "mousedown" , this.selectorCloseMethod );
    Element.setStyle( $("epg_genrecolor_selector") , "display: none; visibility: hidden;" );
    Element.setStyle( $("epg_genrecolor_palette") , "display: none;" );
    Element.setStyle( $("epg_genrecolor_controll") , "background-image: url("+this.config.config_xml.img_path.genrectrl_button+");" );
    $( "epg_genrecolor_controll_button" ).src = this.config.config_xml.img_path.genrectrl_button;
    evt.preventDefault();
    return false;
};

IPG.GenreListManager.prototype.selectorMouseOver = function( evt ){
    evt = IPG.Utility.fixEvent( evt , this );
    if( this.selectorNode.style.visibility == "hidden" ) $( "epg_genrecolor_controll_button" ).src = this.config.config_xml.img_path.genrectrl_button_over;
    Event.stop( evt );
    return false;
}

IPG.GenreListManager.prototype.selectorMouseOut = function( evt ){
    evt = IPG.Utility.fixEvent( evt , this );
    if( this.selectorNode.style.visibility == "hidden" ) $( "epg_genrecolor_controll_button" ).src = this.config.config_xml.img_path.genrectrl_button;
    Event.stop( evt );
    return false;
}

IPG.GenreListManager.prototype.paletteOpen = function( evt ){
    evt = IPG.Utility.fixEvent( evt , this );
    
    // 左クリック以外は処理しない。
    if( evt.which != 1 ){
        Event.stop( evt );
        return false;
    }
    // パレット描画位置の計算と、アクティベート
    var evtTarget = ( evt.srcElement.getAttribute( "name" ) == "major_genre" ) ? evt.srcElement : evt.srcElement.parentNode;
    var new_offset = evtTarget.offsetTop+7+8;
    this.paletteNode.style.display = "block";
    this.paletteNode.style.left = "-68px";
    this.paletteNode.style.top = new_offset+"px";
    this.paletteNode.targetNode = evtTarget;
    Event.stop( evt );
    return false;
};
IPG.GenreListManager.prototype.genreMouseOver = function( evt ){
    evt = IPG.Utility.fixEvent( evt , this );
    
    // バルーン表示されてたら消す。
/**
    if( balloon ){
        var balloon_style = balloon.style;
        var orgColor = balloon.getAttribute("target_org_color");
        var target_id = balloon.getAttribute( "target_id" );
        if( orgColor && target_id ) $( target_id ).firstChild.style.color = orgColor;
        balloon.removeAttribute( "target_id" );
        balloon.removeAttribute( "target_org_color" );
        if( balloon_style.display == "block" ) balloon_style.display = "none";
    }
**/
    
    var evtTarget = ( evt.srcElement.getAttribute( "name" ) == "major_genre" ) ? evt.srcElement : evt.srcElement.parentNode;
    //Element.setStyle( evtTarget , "background-color: #ccffff;" );
    evtTarget.style.backgroundColor = "#ccffff";
    Event.stop( evt );
    return false;
};
IPG.GenreListManager.prototype.genreMouseOut = function( evt ){
    evt = IPG.Utility.fixEvent( evt , this );
    var evtTarget = ( evt.srcElement.getAttribute( "name" ) == "major_genre" ) ? evt.srcElement : evt.srcElement.parentNode;
    //Element.setStyle( evtTarget , "background-color: #ffffff;" );
    evtTarget.style.backgroundColor = "#ffffff";
    Event.stop( evt );
    return false;
};
IPG.GenreListManager.prototype.resetColor = function( evt ){
    evt = IPG.Utility.fixEvent( evt , this );
    
    // 左クリック以外は処理しない。
    if( evt.which != 1 ){
        Event.stop( evt );
        return false;
    }
    
    var genre_color = this.config.cookie.genrecolor;
    
    // セレクタの初期化
    var selectorBody = $( "selector_body_parent" );
    for( var i = 0 , len = selectorBody.childNodes.length; i < len; i++ ){
        var selectorChild = selectorBody.childNodes[i];
        if( selectorChild && selectorChild.getAttribute("name") == "major_genre" ){
            var def_color = "#"+this.xml_data[i].color.bg.substring( 2 , 8 );
            Element.setStyle( selectorChild.firstChild , "background-color: "+def_color+";" );
            var color_id = this.getColorId( this.xml_data[i].color );
            selectorChild.setAttribute( "colorid" , color_id );
            genre_color[i] = color_id;
        }
    }
    
    // EPGに初期色を反映
    var bodyElement = $( "epg_body" );
    if( bodyElement && bodyElement.childNodes ){
        for( var i = 0 , body_length = bodyElement.childNodes.length; i < body_length; i++ ){
            var body_child = bodyElement.childNodes[i];
            if( !body_child || body_child.getAttribute( "name" ) == "timeline_body" ) continue;
            
            for( var j = 0 , pg_length = body_child.childNodes.length; j < pg_length; j++ ){
                var pgFrame = body_child.childNodes[j];
                if( !pgFrame ) return;
                if( pgFrame.id.match( /^pg_frame_.+_top$/ ) || pgFrame.id.match( /^pg_frame_.+_last$/ ) ) continue;
                var pgData = pgFrame.getElementsByTagName("span");
                var pgTitle = undefined , pgSynopsis = undefined;
                if( pgData && pgData[0] ) pgTitle = pgData[0];
                if( pgData && pgData[1] ) pgSynopsis = pgData[1];
                var pgGenre = pgFrame.getAttribute( "genre" );
                var colorObj = undefined;
                if( !pgGenre ){
                    colorObj = this.config.config_xml.color_info.color_default;
                }else{
                    colorObj = this.config.config_xml.color_info.color_palette[genre_color[parseInt(pgGenre,10)]];
                }
                if( colorObj ){
                    pgFrame.style.backgroundColor = "#"+colorObj.bg.substring( 2 , 8 );
                    if( pgTitle ) pgTitle.style.color = "#"+colorObj.cap.substring( 2 , 8 );
                    if( pgSynopsis ) pgSynopsis.style.color = "#"+colorObj.text.substring( 2 , 8 );
                }
            }
        }
    }
    
    // クッキーに書き込む。
    this.cookie_manager.setUserProperty( this.config.config_xml.api_url.setUserProperty , this.config.cookie );
    
    Element.setStyle( this.selectorNode , "display: none; visibility: hidden;" );
    Element.setStyle( this.paletteNode , "display: none;" );
    Element.setStyle( $("epg_genrecolor_controll") , "background-image: url("+this.config.config_xml.img_path.genrectrl_button+");" );
    Event.stop( evt );
    return false;
};
/**
IPG.GenreListManager.prototype.resetMouseOver = function( evt ){
    evt = IPG.Utility.fixEvent( evt , this );
    Element.setStyle( evt.srcElement , "background-color: darkseagreen;" );
    Event.stop( evt );
    return false;
};
IPG.GenreListManager.prototype.resetMouseOut = function( evt ){
    evt = IPG.Utility.fixEvent( evt , this );
    Element.setStyle( evt.srcElement , "background-color: darkkhaki;" );
    Event.stop( evt );
    return false;
};
**/
IPG.GenreListManager.prototype.submitColor = function( evt ){
    evt = IPG.Utility.fixEvent( evt , this );
    //IPG.Utility.debugOut( '[submitColor@GenreListManager]' , "debug" , "darkolivegreen" );
    
    // 左クリック以外は処理しない。
    if( evt.which != 1 ){
        Event.stop( evt );
        return false;
    }
    
    var genre_color = this.config.cookie.genrecolor;
    var selectorBody = $( "selector_body_parent" );
	for( var i = 0 , len = selectorBody.childNodes.length; i < len; i++ ){
        var selectorChild = selectorBody.childNodes[i];
        if( selectorChild && selectorChild.getAttribute("name") == "major_genre" ){
            var color_id = selectorChild.getAttribute( "colorid" );
            genre_color[i] = color_id;
        }
    }
    
    // EPGに反映
    var bodyElement = $( "epg_body" );
    if( bodyElement && bodyElement.childNodes ){
        for( var i = 0 , body_length = bodyElement.childNodes.length; i < body_length; i++ ){
            var body_child = bodyElement.childNodes[i];
            if( !body_child || body_child.getAttribute( "name" ) == "timeline_body" ) continue;
            
            for( var j = 0 , pg_length = body_child.childNodes.length; j < pg_length; j++ ){
                var pgFrame = body_child.childNodes[j];
                if( !pgFrame ) return;
                if( pgFrame.id.match( /^pg_frame_.+_top$/ ) || pgFrame.id.match( /^pg_frame_.+_last$/ ) ) continue;
                var pgData = pgFrame.getElementsByTagName("span");
                var pgTitle = undefined , pgSynopsis = undefined;;
                if( pgData && pgData[0] ) pgTitle = pgData[0];
                if( pgData && pgData[1] ) pgSynopsis = pgData[1];
                var pgGenre = pgFrame.getAttribute( "genre" );
                var colorObj = undefined;;
                if( !pgGenre ){
                    colorObj = this.config.config_xml.color_info.color_default;
                }else{
                    colorObj = this.config.config_xml.color_info.color_palette[genre_color[parseInt(pgGenre,10)]];
                }
                if( colorObj ){
                    pgFrame.style.backgroundColor = "#"+colorObj.bg.substring( 2 , 8 );
                    if( pgTitle ) pgTitle.style.color = "#"+colorObj.cap.substring( 2 , 8 );
                    if( pgSynopsis ) pgSynopsis.style.color = "#"+colorObj.text.substring( 2 , 8 );
                }
            }
        }
    }
    
    // クッキーに書き込む。
    this.cookie_manager.setUserProperty( this.config.config_xml.api_url.setUserProperty , this.config.cookie );
    
    Element.setStyle( this.selectorNode , "display: none; visibility: hidden;" );
    Element.setStyle( this.paletteNode , "display: none;" );
    Element.setStyle( $("epg_genrecolor_controll") , "background-image: url("+this.config.config_xml.img_path.genrectrl_button+");" );
    Event.stop( evt );
    return false;
};
/**
IPG.GenreListManager.prototype.submitMouseOver = function( evt ){
    evt = IPG.Utility.fixEvent( evt , this );
    Element.setStyle( evt.srcElement , "background-color: darkseagreen;" );
    Event.stop( evt );
    return false;
};
IPG.GenreListManager.prototype.submitMouseOut = function( evt ){
    evt = IPG.Utility.fixEvent( evt , this );
    Element.setStyle( evt.srcElement , "background-color: darkkhaki;" );
    Event.stop( evt );
    return false;
};
**/
IPG.GenreListManager.prototype.selectColor = function( evt ){
    evt = IPG.Utility.fixEvent( evt , this );
    
    // セレクタの該当ジャンルの表示色を選択された色に変更。
    var colorid = eval( evt.srcElement.getAttribute( "colorid" ) );
    var selectColor = this.color_palette[colorid];
    if( colorid != undefined && selectColor != undefined ){
        Element.setStyle( this.paletteNode.targetNode.firstChild , "background-color:"+selectColor.bg.substring( 2 , 8 )+";" );
        this.paletteNode.targetNode.setAttribute( "colorid" , colorid );
        Element.setStyle( this.paletteNode , "display: none;" );
    }
    
    Event.stop( evt );
    return false;
};

IPG.GenreListManager.prototype.bindGenreColorSelectorEvent = function( element ){
    if( !element ) return;
    // セレクタ起動ノードへのイベント
    //var child = element.firstChild;
    var child = element;
    if( child ){
        Event.observe( child , "mousedown" , this.selectorOpenMethod );
        Event.observe( child , "mouseover" , this.selectorMouseOverMethod );
        Event.observe( child , "mouseout" , this.selectorMouseOutMethod );
    }
    // セレクタノードへのイベント
    child = $( "epg_genrecolor_selector" );
    if( child ){
        Event.observe( child , "mousedown" , function( evt ){ evt = IPG.Utility.fixEvent( evt , this ); Event.stop( evt ); return false; } );
    }
    
    //child = child.nextSibling;
    child = $( "selector_body_parent" );
    if( child ){
        if( child.childNodes && child.childNodes.length ){
            for( var i = 0 , len = child.childNodes.length; i < len; i++ ){
                var selectorChild = child.childNodes[i];
                if( selectorChild && selectorChild.getAttribute("name") == "major_genre" ){
                    Event.observe( selectorChild , "mousedown" , this.paletteOpenMethod );
                    Event.observe( selectorChild , "mouseover" , this.genreMouseOver );
                    Event.observe( selectorChild , "mouseout" , this.genreMouseOut );
                }
            }
        }
    }
    
    child = $( "selector_reset" );
    if( child ){
        Event.observe( child , "mousedown" , this.resetMethod );
        //Event.observe( child , "mouseover" , this.resetMouseOver );
        //Event.observe( child , "mouseout" , this.resetMouseOut );
    }
    child = $( "selector_submit" );
    if( child ){
        Event.observe( child , "mousedown" , this.submitMethod );
        //Event.observe( child , "mouseover" , this.submitMouseOver );
        //Event.observe( child , "mouseout" , this.submitMouseOut );
    }
    
    // カラーパレットへのイベント
    //child = child.nextSibling;
    child = $( "palette_parent" );
    if( child ){
        if( child.childNodes && child.childNodes.length ){
            for( var i = 0 , len = child.childNodes.length; i < len; i++ ){
                Event.observe( child.childNodes[i] , "mousedown" , this.selectColorMethod );
            }
        }
    }
};
IPG.GenreListManager.prototype.getMajorGenreName = function( major_genre_id ){
    if( major_genre_id == undefined ) return;
    if( !this.xml_data ) return;
    if( typeof( major_genre_id ) == "string" ) major_genre_id = parseInt( major_genre_id ,10 );
    if( major_genre_id >= this.config.config_xml.epg_info.startReservedGenreId ) return undefined;
    for( var i = 0 , len = this.xml_data.length; i < len; i++ ){
        var genre = this.xml_data[i];
        if( genre.id == major_genre_id ) return genre.name;
    }
    return undefined;
};
