﻿var delimiter = '_';

//********************************************************************************
//カスタム
//********************************************************************************
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//submit
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function submit() {
    //取得
    var areaCategory1Id = areaSelector.selector.value;
    var genreCategory1Id = genreSelector.selector.value;

    var areaItems = areaSelector.getCheckedItems();
    var genreItems = genreSelector.getCheckedItems();

    if (areaItems.length + genreItems.length == 0) {
        alert('検索したいジャンルやエリアにチェックを入れてください。');
        return;
    }

    //クエリパラメータ生成
    var query = new Array();

    if (areaCategory1Id) {
        query.push(areaSelector.categoryKey + '1=' + areaCategory1Id);
    }
    if (genreCategory1Id) {
        query.push(genreSelector.categoryKey + '1=' + genreCategory1Id);
    }
    if (areaItems.length > 0) {
        query.push(areaSelector.categoryKey + '2=' + areaItems.join(delimiter));
    }
    if (genreItems.length > 0) {
        query.push(genreSelector.categoryKey + '2=' + genreItems.join(delimiter));
    }

    //フォーム生成
    var objBody = document.getElementsByTagName("body").item(0);

    var form = document.createElement('form');
    form.method = 'post';
    objBody.appendChild(form);

    var input = document.createElement('input');
    input.type = 'hidden';
    input.name = 'GENRES';
    input.value = genreItems.join(',');
    form.appendChild(input);

    input = document.createElement('input');
    input.type = 'hidden';
    input.name = 'AREAS';
    input.value = areaItems.join(',');
    form.appendChild(input);

    //URLを設定してsubmit
    if (query.length > 0) {
        form.action = base + '?cat=2&' + query.join('&');
    }
    else {
        form.action = base + '?cat=2&x=0';
    }

    form.submit();
}





//********************************************************************************
//中分類検索系
//********************************************************************************
var CategoryList = Class.create();
CategoryList.prototype = {
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //initialize
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    initialize : function(category0Id, fieldId, categoryKey) {
        this.category0Id = category0Id;
        this.field = $(fieldId);
        this.categoryKey = categoryKey;

        this.requestXml();
    }
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //XML
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ////////// XML読取開始 //////////////////////////////////////////////////
    ,requestXml : function() {
        var requestUrl = host + 'category/category/get_category1_list/' + this.category0Id;

        var myAjax = new Ajax.Request(
            requestUrl, //requestUrl + (new Date()).gettime(),
            {
                method: 'get'
                ,onComplete: this.requestXmlOnComplete.bindAsEventListener(this)
                ,onFailure: this.requestXmlOnFailure.bindAsEventListener(this)
            });
    }
    ////////// 成功・解析 //////////////////////////////////////////////////
    ,requestXmlOnComplete : function(originalRequest) {
        var xmlDoc = originalRequest.responseXML.documentElement;

        this.category0 = new Category0();
        this.category0.loadXml(xmlDoc.firstChild);

        var len = this.category0.category1List.length;
        if (len > 0) {
            this.field.innerHTML = '';

            //var ul = document.createElement('ul');
            //ul.className = 'flow';
            //this.field.appendChild(ul);

            for (var i = 0; i < len; i++) {
                category1 = this.category0.category1List[i];

                //var li = document.createElement('li');
                //ul.appendChild(li);

                var a = document.createElement('a');
                a.href = base + '?cat=1&' + this.categoryKey + '1=' + category1.category1Id;
                a.innerHTML = category1.text;
                //li.appendChild(a);
                this.field.appendChild(a);
            }
        }
    }
    ////////// 失敗 //////////////////////////////////////////////////
    ,requestXmlOnFailure : function(originalRequest) {
        alert('LOAD_XML Failed');
    }
}

//********************************************************************************
//カテゴリセレクタ
//********************************************************************************
var CategorySelector = Class.create();
CategorySelector.prototype = {
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //initialize
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    initialize : function(category0Id, selectorContainerId, itemsContainerId, categoryKey) {
        this.category0Id = category0Id;
        this.selectorContainer = $(selectorContainerId);
        this.itemsContainer = $(itemsContainerId);
        this.categoryKey = categoryKey;

        this.requestXml();
    }
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //XML
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ////////// XML読取開始 //////////////////////////////////////////////////
    ,requestXml : function() {
        var requestUrl = host + 'category/category/get_category_all/' + this.category0Id;

        var myAjax = new Ajax.Request(
            requestUrl, //requestUrl + (new Date()).gettime(),
            {
                method: 'get'
                ,onComplete: this.requestXmlOnComplete.bindAsEventListener(this)
                ,onFailure: this.requestXmlOnFailure.bindAsEventListener(this)
            });
    }
    ////////// 成功・解析 //////////////////////////////////////////////////
    ,requestXmlOnComplete : function(originalRequest) {
        var xmlDoc = originalRequest.responseXML.documentElement;

        this.category0 = new Category0();
        this.category0.loadXml(xmlDoc.firstChild);

        //======== エレメントの生成 ====================
        //container.selectorPane.selector
        var selector = document.createElement('select');
        selector.onchange = this.onChangeCagetory1.bindAsEventListener(this);
        this.selectorContainer.appendChild(selector);
        this.selector = selector;

        //container.selectorPane.selector.option
        var option = document.createElement('option');
        option.value = '';
        option.innerHTML = '（選択してください）';
        selector.appendChild(option);

        var len = this.category0.category1List.length;
        for (var i = 0; i < len; i++) {
            var category1 = this.category0.category1List[i];

            option = document.createElement('option');
            option.value = category1.category1Id;
            option.innerHTML = category1.text;
            selector.appendChild(option);
        }

        //======== 初期値の選択 ====================
        //-------- category1 ----------
        var r = location.href.match('.*[&\\?]' + this.categoryKey + '1=(\\w+)&?');
        if (r) {
            var category1Id = RegExp.$1;
            var category1Idx = this.getCategory1Idx(category1Id);

            //設定
            if (category1Idx >= 0) {
                this.setCategory1Value(category1Idx);
            }

            //---------- category2 ----------
            var r = location.href.match('.*[&\\?]' + this.categoryKey + '2=([\\w' + delimiter + ']+)&?');
            if (r) {
                var c2 = RegExp.$1.split(delimiter);
                this.setCategory2Value(c2);
            }
            else {
                var r = location.href.match('.*[&\\?]cat=(\\w+)&?');
                if (r) {
                    var cat = RegExp.$1;
                    if (cat == 1) {
                        this.onCheckAll();
                    }
                }
                else {
                    this.onCheckAll();
                }
            }
        }
    }
    ////////// 失敗 //////////////////////////////////////////////////
    ,requestXmlOnFailure : function(originalRequest) {
        alert('LOAD_XML Failed');
    }
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //値の選択
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ////////// category1値設定 //////////////////////////////////////////////////
    ,setCategory1Value : function(category1Idx) {
        this.selector.selectedIndex = category1Idx + 1;
        this.setCategory2Items(category1Idx);
    }
    ////////// category2値設定 //////////////////////////////////////////////////
    ,setCategory2Value : function(category2IdArray) {
        var inputs = this.itemsContainer.getElementsByTagName('input');

        var a_len = category2IdArray.length;
        var i_len = inputs.length;

        for (var i = 0; i < i_len; i++) {
            if (inputs[i].type == 'checkbox') {
                inputs[i].checked = false;
                for (var a = 0; a < a_len; a++) {
                    if (inputs[i].value == category2IdArray[a]) {
                        inputs[i].checked = true;
                    }
                }
            }
        }
    }
    ////////// category2生成 //////////////////////////////////////////////////
    ,setCategory2Items : function(category1Idx) {
        var category2List = this.category0.category1List[category1Idx].category2List;
        var len = category2List.length;
        if (len > 0) {
            this.itemsContainer.innerHTML = '';
            var ul = document.createElement('ul');
            this.itemsContainer.appendChild(ul);

            var li = document.createElement('li');
            ul.appendChild(li);

            var a = document.createElement('a');
            a.innerHTML = 'すべてチェック';
            a.href = 'javascript:void(0);';
            a.className = 'command';
            a.onclick = this.onCheckAll.bindAsEventListener(this);
            li.appendChild(a);

            a = document.createElement('a');
            a.innerHTML = 'クリア';
            a.href = 'javascript:void(0);';
            a.className = 'command';
            a.onclick = this.onCheckClear.bindAsEventListener(this);
            li.appendChild(a);

            for (var i = 0; i < len; i++) {
                var category2 = category2List[i];
                var ctrlId = 'chk' + category2.category2Id;

                li = document.createElement('li');
                ul.appendChild(li);

                //checkbox
                var checkbox = document.createElement('input');
                checkbox.setAttribute('type', 'checkbox');
                checkbox.setAttribute('id', ctrlId);
                checkbox.setAttribute('value', category2.category2Id);
                li.appendChild(checkbox);

                var label = document.createElement('label');
                label.setAttribute('for', ctrlId);
                label.htmlFor = ctrlId;
                label.innerHTML = category2.text;
                li.appendChild(label);
            }
        }
    }
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //値の取得
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ////////// 選択中のcategory1のindexを取得 //////////////////////////////////////////////////
    ,getCategory1Idx : function(category1Id) {
        var len = this.category0.category1List.length;
        for (var i = 0; i < len; i++) {
            if (category1Id == this.category0.category1List[i].category1Id) {
                return i;
            }
        }
        return -1;
    }
    ////////// チェックされているアイテムを収集 //////////////////////////////////////////////////
    ,getCheckedItems : function() {
        var selectedItems = new Array();

        var inputs = this.itemsContainer.getElementsByTagName('input');
        var len = inputs.length;
        for (var i = 0; i < len; i++) {
            if (inputs[i].type == 'checkbox') {
                if (inputs[i].checked) {
                    selectedItems.push(inputs[i].value);
                }
            }
        }

        return selectedItems;
    }
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //event
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ////////// on change //////////////////////////////////////////////////
    ,onChangeCagetory1 : function() {
        var selectedCategory1Id = this.selector.value;
        if (selectedCategory1Id) {
            var category1Idx = this.getCategory1Idx(selectedCategory1Id);

            //選択されたカテゴリ1に属するカテゴリ2を展開
            if (category1Idx >= 0) {
                this.setCategory2Items(category1Idx);
            }
            else {
                this.itemsContainer.innerHTML = '';
            }
        }
        else {
            this.itemsContainer.innerHTML = '';
        }
    }
    ////////// 全てチェック //////////////////////////////////////////////////
    ,onCheckAll : function() {
        var category1Idx = this.getCategory1Idx(this.selector.value);
        var category2IdList = new Array();
        var len = this.category0.category1List[category1Idx].category2List.length;
        for (var i = 0; i < len; i++) {
            category2IdList.push(this.category0.category1List[category1Idx].category2List[i].category2Id);
        }
        this.setCategory2Value(category2IdList);
    }
    ////////// クリア //////////////////////////////////////////////////
    ,onCheckClear : function() {
        this.setCategory2Value(new Array());
    }
}

//********************************************************************************
//Category0:大分類
//********************************************************************************
var Category0 = Class.create();
Category0.prototype = {
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //コンストラクタ
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    initialize : function() {
        this.category0Id = 0;
        this.sortIdx = 0;
        this.text = '';
        this.category1List = new Array();
    }
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //XML
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ////////// XMLノードの解析 //////////////////////////////////////////////////
    ,loadXml : function (node) {
        if (!node) {return;}

        //======== attribute読取 ====================
        //category_id
        this.category0Id = node.getAttribute('category0Id');

        //======== 子読取 ====================
        for (var n = node.firstChild; n; n = n.nextSibling) {
            if (n.nodeType == 1) {
                var category1 = new Category1();
                category1.loadXml(n);
                this.category1List.push(category1);
            }
        }
    }
}

//********************************************************************************
//Category1:中分類
//********************************************************************************
var Category1 = Class.create();
Category1.prototype = {
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //コンストラクタ
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    initialize : function() {
        this.category1Id = 0;
        this.sortIdx = 0;
        this.text = '';
        this.category2List = new Array();
    }
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //XML
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ////////// XMLノードの解析 //////////////////////////////////////////////////
    ,loadXml : function (node) {
        if (!node) {return;}

        //======== attribute読取 ====================
        //category_id
        this.category1Id = node.getAttribute('category1_id');

        //sort_idx
        this.sortIdx = node.getAttribute('sort_idx');

        //text
        this.text = node.getAttribute('text');

        //======== 子読取 ====================
        for (var n = node.firstChild; n; n = n.nextSibling) {
            if (n.nodeType == 1) {
                var category2 = new Category2();
                category2.loadXml(n);
                this.category2List.push(category2);
            }
        }
    }
}

//********************************************************************************
//Category2:小分類
//********************************************************************************
var Category2 = Class.create();
Category2.prototype = {
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //コンストラクタ
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    initialize : function() {
        this.category2Id = 0;
        this.sortIdx = 0;
        this.text = '';
    }
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //XML
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ////////// XMLノードの解析 //////////////////////////////////////////////////
    ,loadXml : function (node) {
        if (!node) {return;}

        //======== attribute読取 ====================
        //category_id
        this.category2Id = node.getAttribute('category2_id');

        //sort_idx
        this.sortIdx = node.getAttribute('sort_idx');

        //text
        this.text = node.getAttribute('text');
    }
}




