/*!
 * DLsite.com JavaScript Library.
 *
 * Copyright (C) 1996-2010 DLsite.com. All rights reserved.
 * http://www.dlsite.com/
 */

(function($) {

$(document).ready(function() {
    $.extend({
        dl_filters: [],
        caution_image: '',
        adult_image: '',
        filter_ready: false,
        has_adlchk: false,
        do_adlfilt: false,

        setupFilter: function() {
            var setting, site, setting_name;
            site = getSiteName();
            if (site == 'girls-pro') {
                $.do_adlfilt = true;
                $.has_adlchk = ($.cookie('adultchecked') == '1');
                if (!$.has_adlchk) {
                    $.dl_filters.push('adl');
                    $.adult_image = $.getAdultImage();
                }
            }

            setting = (getSiteLang() == 'en') ? $.cookie('DL_DISPSETTING_ENG') : $.cookie('DL_DISPSETTING');
            if (setting) {
                $.dl_filters = $.dl_filters.concat(setting.split(','));
                $.caution_image = $.getCautionImage();
            }

            $.filter_ready = true;
        },

        getCautionImage: function(type) {
            var imagepath = getSiteImagepath();
            return imagepath + 'caution_img_sam' + ['','','2','3'][{RJ:1,VJ:3,BJ:3}[type]|0] + '.gif';
        },

        getAdultImage: function() {
            var imagepath = getSiteImagepath();
            return imagepath + 'adult_img_sam3.gif';
        },

        filterImage: function(target) {
            if ($.filter_ready !== true) {
                $.setupFilter();
            }

            if ($.dl_filters.length < 1) {
                return;
            }

            var target_elem;
            if (target) {
                if (target instanceof jQuery) {
                    target_elem = target.find('input.__product_attributes');
                } else {
                    target_elem = $(target + ' input.__product_attributes');
                }

            } else {
                target_elem = $('input.__product_attributes');
            }

            target_elem.each(function(){
                var $input = $(this);
                var attributes = $input.attr('value').split(',');

                filter: for (var i in $.dl_filters) {
                    for (var j in attributes) {
                        if (!attributes[j]) {
                            continue;
                        }
                        // フィルタリング対象
                        if (attributes[j] === $.dl_filters[i]) {
                            var product_id = $input.attr('id').split('_')[1];
                            var $link  = $('#_link_' + product_id + ', ._link_' + product_id);
                            
                            if ($link.hasClass('_filter')) {
                                return;
                            }
                            
                            var $image = $link.find('img');
                            var censored_src;

                            if ($.dl_filters[i] === 'adl') {
                                censored_src = $.adult_image;
                            } else {
                                censored_src = $.getCautionImage(product_id.substr(0,2))
                            }
                            
                            $link.addClass('_filter');
                            $image
                                .hide()
                                .after('<img class="_censored" src="' + censored_src + '">');
                            
                            break filter;
                        }
                    }
                }
            });
        },

        showImage: function(e) {
            e.preventDefault();
            $(e.currentTarget)
                .removeClass('_filter')
                .find('img')
                .show()
            .end()
                .find('img._censored')
                .remove();
        }
    });

    $.filterImage();

    $(document).delegate('._filter', 'click', $.showImage);

});

})(jQuery);

