/**
 * at_cosme site - common
 *
 * @author      K.Saitoh
 * @require     jQuery 1.3.2+
 * @version     1.0.0
 */

jQuery.atcosme = {};

/**
 * テキストエリアの文字色をグレイに変更
 * フォーカスで文字を消去
 */
jQuery.atcosme.input = {
    conf : {
        otherSearch : "＠ｃｏｓｍｅサイト全体から探す",
        deepSearchBox : "気になるキーワードを入力してください",
        s_deepSearchBox : "入力してください"
    },
    addEvent : function(target, word) {
        if(target.val() == word) {
            target.css("color", '#c3c3c3');
        }
        else if(target.val() == "") {
            target.val(word).css("color", '#c3c3c3');
        }
        else {
            target.css("color", "#000");
        }

        target.focus(function() {
            if(this.value == word) {
                target.val("").css("color","#000");
            }
        });

        target.blur(function(){
            if(this.value == "") {
                $(this).val(word).css("color", '#c3c3c3');
            }
            if(this.value != word) {
                $(this).css("color", "#000");
            }
        });
    },
    /**
     * 絞込み条件内にて、条件をクリア
     */
    addClearEvent : function() {
        var bodyId = $('body').attr('id');
        var action;

        var dispatch = function (elem, type) {      // HTML イベント呼び出し
            if (elem.dispatchEvent) {       // DOM2
                var event = document.createEvent('HTMLEvents');
                event.initEvent( type, true, true );
                elem.dispatchEvent( event );
            } else if (elem.fireEvent) {    // IE
                elem.fireEvent( 'on' + type );
            }
        };

        if (bodyId == 'brand')
            action = function(){        // ブランド用
                $(this).val( this.id != 'extrd' ? 0 : 6 );

                if ($(this).attr('name') == 'itm')
                    dispatch( this, 'change' );
            };
        else if (bodyId == 'category' || bodyId == 'tag')
            action = function(){        // キーワード・タグ用
                var option = $(this).children().val();
                if (option.length == 0 || option == '0')
                    $(this).val( option );
                else
                    $(this).val( 4 );

                if ($(this).attr('name') == 'itm')
                    dispatch( this, 'change' );
            };
        else
            action = function(){        // 商品用
                var option = $(this).children().val();
                $(this).val( option );
        };

        $('#deepSearchBox').find('.floatRight').click(function(){
            $('#deepSearchBox').find('input').each(function(){
                if($(this).attr('type') == "text") {
                    $(this).val('').focus().blur();
                }else if($(this).attr('type') == "checkbox") {
                    $(this).attr("checked", false);
                }
            });

            $('#deepSearchBox').find('select').each(action);
        });
    }
};

/**
 * 絞り込み条件の開閉
 */
jQuery.atcosme.openSearchCondition = function() {

    $('#deepSearchOn').find('a').click(function(e){
        if($('#deepSearchBox').next().attr('id') == 'deepSearchBoxResult'){
            $('#deepSearchBoxResult').toggle();
        }
        $('#deepSearchBox').toggle();

        //ボタンの変更
        var path = chagePath(e.target.src);
        if(path != null){
            e.target.src = path;
        }

        return false;
    });

    $('#deepSearchOff').find('a').click(function(e){
        if($('#deepSearchBox').next().attr('id') == 'deepSearchBoxResult'){
            $('#deepSearchBoxResult').toggle();
        }
        $('#deepSearchBox').toggle();

        //ボタンの変更
        var path = chagePath(e.target.src);
        if(path != null){
            e.target.src = path;
        }

        return false;
    });

     // pathの末端が"on.gif"だったら、"off.gif"に
     //"off.gif"だったら、"on.gif"に変換する
    function chagePath(path) {
        var index = path.indexOf('on.gif');
        if(index != -1) {
            path = path.substring(0,index) + "off.gif";
            $('#deepSearchOn').css('margin-bottom', '20px');
            $('#deepSearchOff').css('margin-bottom', '20px');
            $('#deepSearchBoxResult').css('display', 'block');
            return path;
        }

        index = path.indexOf('off.gif');
        if(index != -1){
            path = path.substring(0,index) + "on.gif";
            $('#deepSearchOn').css('margin-bottom', '0px');
            $('#deepSearchOff').css('margin-bottom', '0px');
            $('#deepSearchBoxResult').css('display', 'none');
            return path;
        }

        return null;
    }
};

/**
 * リクエストのキャンセル処理
 */
jQuery.atcosme.cancelRequest = function(xhr){
    try{
        if(xhr && xhr.abort){ xhr.abort(); }
    }catch(error){}
    finally{
        return xhr = null;
    }
};

/**
 * IE用にチェックボックスの切り替えイベント
 */
jQuery.atcosme.checkbox = function(){
    $(".checkboxLabel").click(function(){
        if($(this).prev().attr('checked') == ''){
            $(this).prev().attr("checked", "checked");
        }
        else {
            $(this).prev().attr("checked", "");
        }

    })
};
/**
 * オーバーレイ
 */
jQuery.atcosme.overlay = {
    conf: {
        target: '#overlay',
        isIE6: null,
        timer: 500
    },
    show: function(){
        var overlay = $(this.conf.target);
        if(this.conf.isIE6==null){ this.conf.isIE6 = $.atcosme.browser.isIE6(); }

        if (overlay.length == 0) {
            $('body').append('<div id="overlay"></div>');
            overlay = $(this.conf.target);
        }

        overlay.click(function(){
            $.atcosme.overlay.hide();
            if($.atcosme.slideShow.photo.is()){
                $.atcosme.slideShow.photo.hide();
            }
        });

        if(!overlay.data('visible')){

            if(this.conf.isIE6){
                // for IE
                var doc = $(document.body);
                $('select').css('visibility', 'hidden');
                overlay
                    .css({
                        backgroundColor:'#fff', position: 'absolute',
                        width: doc.width(), height: doc.height(),  'z-index': '9999',
                        top:'0px', left: '0px', opacity: '0',   filter: 'alpha(opacity=50)'
                    })
                    .data('visible', true)
                    .show().css('opacity', 0.7);

            } else {
                // for others
                overlay
                    .css({
                        backgroundColor:'#fff', position: 'fixed',
                        width: '100%', height: '100%',  'z-index': '9999',
                        top:'0px', left: '0px', opacity: '0',   filter: 'alpha(opacity=50)'
                    })
                    .data('visible', true)
                    .show().css('opacity', 0.7);
            }
        }
    },
    hide: function(){
        if(this.conf.isIE6){
            $('select').not('.dummy').css('visibility', 'visible');
            if(this.conf.timer){ clearInterval(this.conf.timer); }
        }
        $(this.conf.target)
            .data('visible', false)
            .hide();
    },
    is: function(){
        return ($(this.conf.target).data('visible'));
    },
    hidePhoto: function(){
        $('#largePicture').hide();
    }
};


/**
 * IE6識別
 */
jQuery.atcosme.browser = {
    ie6flg: null,
    isIE6: function(){
        if(this.ie6flg!=null){ return this.ie6flg }
        return this.ie6flg = navigator.userAgent.match(/.*MSIE\s*6\.[0-9].*/)? true: false;
    }
}

/**
 * 全ページ共通の処理
 */
jQuery.atcosme.common = function() {
    //$.atcosme.input.addEvent( $('#otherSearch').find('.searchKeyword').find('input'), $.atcosme.input.conf.otherSearch);
    //$.atcosme.input.addEvent( $('#deepSearchBox').find('.keyword').find('input'), $.atcosme.input.conf.deepSearchBox);
    //$.atcosme.input.addEvent( $('#deepSearchBox').find('.keyword01').find('input'), $.atcosme.input.conf.deepSearchBox);
    //$.atcosme.input.addEvent( $('#deepSearchBox').find('.keyword02').find('input'), $.atcosme.input.conf.s_deepSearchBox);
    $.atcosme.input.addClearEvent();

    $.atcosme.openSearchCondition();

    if($.browser.msie){
        jQuery.atcosme.checkbox();
    }
};

/* dom ready */
$(function() {
    $.atcosme.common();

});


/* plug in*/

/**
 * ポップアップを画面中央に表示する
 */
jQuery.fn.extend({
    setCenterPos: function(){
        var target = $(this);
        var win = $(window);
        return target.css({
            left :(win.width() - target.width()) / 2 + win.scrollLeft(),
            top :(win.height() - target.height()) / 2 + win.scrollTop()
        })
    }
});

jQuery.fn.extend({
    setPos: function(x, y){
        var target = $(this);
        var win = $(window);
        return target.css({
            left : x,
            top : y
        })
    }
});

/**
 * http://16c.jp/2008/0528214632.php
 */
jQuery.scope = function(target, func) {
    return function() {
        return func.apply(target, arguments);
    }
};

/*
*
*   jQuery Timer plugin v0.1
*       Matt Schmidt [http://www.mattptr.net]
*
*   Licensed under the BSD License:
*       http://mattptr.net/license/license.txt
*
*/

jQuery.timer = function (interval, callback)
{
    var interval = interval || 100;

    if (!callback)
        return false;

    _timer = function (interval, callback) {
        this.stop = function () {
            clearInterval(self.id);
        };

        this.internalCallback = function () {
            callback(self);
        };

        this.reset = function (val) {
            if (self.id)
                clearInterval(self.id);

            var val = val || 100;
            this.id = setInterval(this.internalCallback, val);
        };

        this.interval = interval;
        this.id = setInterval(this.internalCallback, this.interval);

        var self = this;
    };

    return new _timer(interval, callback);
};

