// 外部ファイル読込jQuery Plug-in
// @param options
(function($){
    $.fn.prefstore = function(options){
        options = $.extend({
            preftarget: '.pref',
            resultarea: '#searchResultArea',
            resultscroll: '#searchResult',
            resulttarget: '#searchItems',
			resultmaxheight: 300,
			loadingimg: 'images/img_all_japan/search/nowloading.gif',
            scriptCharset:'utf-8',
            type: 'GET',
            dataType: 'html',
            timeout: 30000,
            url: 'prefsearch.php'
        }, options);
        var resultarea = $(options.resultarea);
		var resultscroll = $(options.resultscroll);
        var result = $(options.resulttarget);
        var nowPid = '';
        
        function _init() {
            //resultarea.hide();
            $(options.preftarget).each(function () {
                $(this).attr('href', 'javascript:;');
                $(this).unbind().click(function () {
                    var prefid = $(this).attr('pid');
                    if (nowPid != prefid) {
	                    _showResult();
                        nowPid = prefid;
                        _getStoreResult(prefid);
                    }
                });
            });
	        _showResult();
            _getStoreResult('');
        }
        
        function _showResult() {
            //resultarea.show();
			resultscroll.css('height', '');
            $(result).html('<div id="ssloading"><img src="' + options.loadingimg + '" alt="" width="50" height="50" />');
        }

        function _deleteLoading() {
            $('#ssloading').remove();
        }

        function _getStoreResult(pid) {
            var query = 'PID=' + pid + '&ID=www.kora-honten.jp';
            var htmlData, statusType = '';
            $.ajax({
                type: options.type,
                dataType: options.dataType,
                timeout: options.timeout,
                url: options.url,
                cache: false,
                data: query,
                scriptCharset: options.scriptCharset,
                beforeSend: function(XMLHttpRequest){
                    //XMLHttpRequest.overrideMimeType("text/html;charset=" + options.scriptCharset);
                },
                success: function(data, dataType){
                    if (data) {
                        htmlData = data;
                        statusType = 'ok';
                    }
                    else {
                        htmlData = '';
                        statusType = 'ng';
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown){
                    statusType = 'no_connect';
                },
                complete: function(XMLHttpRequest, textStatus){
                    var __mode = statusType;
                    var data;
                    switch (__mode) {
                        case 'ok':
                            data = htmlData;
                            break;
                        case 'ng':
                            data='';
                            break;
                        case 'no_connect':
                            data = '';
                            break;
                        default:
                            data = '';
                            break;
                    }
					_deleteLoading();
                    result.hide().html(data);
					var rHeight = result.height();
					if (rHeight >= options.resultmaxheight) {
						rHeight = options.resultmaxheight;
						//resultscroll.addClass('maxheight');
						resultscroll.css('height', options.resultmaxheight);
					} else {
						//resultscroll.removeClass('maxheight');
						resultscroll.css('height', '');
					}
					result.fadeIn('500');
                }
            });
        }

        return _init();
    };
})(jQuery);

