jQuery(function ($) {
    // Drop Down Menu
    var target_menu = $('ul.menu-nav-sub');
    target_menu.each(function () {
        var li = $(this).children();
        $(li[li.length-1]).css('border-bottom', '1px solid #000');
    });
    $('a.no-action').click(function () {
        return false;
    });
    target_menu.hide();
    var timer = [];
    target_menu.each(function (i) {;
        $(this).parent().hover(function () {
            for ( var j=0; j<timer.length; j++ ) {
                clearTimeout(timer[j]);
            }
            target_menu.show();
        }, function () {
            timer[i] = setTimeout(function() {
               target_menu.hide();
            }, 300); 
        });
    });
    // Style Filter
    var filter_style = $('div#filter-style > ul')
    var timer_style;
    filter_style.parent().hover(function () {
        
        clearTimeout(timer_style);
        $('> ul', this).show();
    }, function () {
        var self = this;
        timer_style = setTimeout(function () {
            $('> ul', self).hide();
        }, 350);
    });
    $('div#filter-style ul li:first-child').css('border-top', '1px solid #ddd');
    $('div#filter-style ul li:last-child').css('border-bottom', 'none');
    // Staff Filter
    var filter_staff = $('div#filter-staff > ul')
    var timer_staff;
    filter_staff.parent().hover(function () {
        clearTimeout(timer_staff);
        $('> ul', this).show();
    }, function () {
        var self = this;
        timer_staff = setTimeout(function () {
            $('> ul', self).hide();
        }, 350);
    });
    $('div#filter-staff ul li:first-child').css('border-top', '1px solid #ddd');
    $('div#filter-staff ul li:last-child').css('border-bottom', 'none');
    // Slide for Salon Page
    $('.thumb img').click(function () {
        $('div#salon-photo-main img').hide();
        var path = $(this).attr('src');
        var path_new = path.replace(/-thumb/, '');
        var img = new Image();
        img.src = path_new;
        $('div#salon-photo-main img').remove();
        $('div#salon-photo-main').append(img);
        $('div#salon-photo-main img').fadeIn('fast');
        return false;
    });
    // Tab for Style Page
    var tab_menus = $('ul.tab-menu li');
    tab_menus.each(function () {
        if ( $(this).attr('class') !== 'tab-button' ) {
            if ( $(this).attr('class') !== 'selected' ) {
                $('div#tab-panel-' + $(this).attr('id')).hide();
            }
        }
    }); 
    tab_menus.click(function () {
        if ( $(this).attr('class') == 'tab-button' ) {
            return true;
        }
        tab_menus.each(function (i) {
            $(tab_menus[i]).removeClass('selected');
        });
        $(this).addClass('selected');
        $('div.tab-panel > div').hide();
        $('div#tab-panel-' + $(this).attr('id')).show();
        if ( this.id == 'tab2'&& $(this).text() != 'creative') {
            initialize();
        }
        return false;
    });
    // Modal Window
    var hasModal = false;
    $('a.modal').click(function () {
        if ( $.browser.msie && $.browser.version < 7 ) { // for IE 6
            this.target = "_blank";
        } else {
            if ( !hasModal ) {
                $('body').append('<div id="modal-back"></div><div id="modal-front"></div>');
                $('#modal-back').click(function () {
                    $("#modal-back").hide();
                    $("#modal-front").hide();
                });
                hasModal = true;
            }
            $('#modal-back').show();
            var img = new Image();
            img.src = $(this).attr('href') + '?'+ new Date().getTime(); // (1) new Date().getTime()はIEのキャッシュ対策。必ずImage.onloadを発生させるために必要。
            img.onload = function () { // (2) 画像の幅width, 高さheightを必ず取得するためにImage.onloadの中で取得する。
                var template_dir;
                var scripts = document.getElementsByTagName('script');
                var i = scripts.length;
                while (i--) {
                    var match = scripts[i].src.match(/(.+)base.js/);
                    if (match) {
                        template_dir = match[1];
                        break;
                    }
                }
                $('#modal-front').html('<img src="' + template_dir + 'images/close.png" class="close" /><img src="' + this.src + '" />');
                var left = Math.floor(($(window).width() - this.width) / 2); // (3) 画像の中央は位置
                var top  = Math.floor(($(window).height() - this.height) / 2);
                $('#modal-front').css({
                    "z-index" : 20,
                    "top": top,
                    "left": left
                });
                $('#modal-front').fadeIn('fast');
                $('#modal-front img.close').click(function(){
                    $("#modal-back").hide();
                    $("#modal-front").hide();
                });
            }
            return false;
        }
    });
    // Window Height
    var location = window.location;
    var path = location.pathname;
    if ( /ct_style_staff/.test(path) ) {
        if ( $('div#main').height() < 620 ) {
            $('div#main').css('height', '620px');
        }
    }
    // Pop Up Window
    $('a.popup').click(function () {
        var url = $(this).attr('href');
        var name = 'clear OF HAIR';
        var option = 'width=680, height=520, menubar=yes, toolbar=yes, scrollbars=yes';
        if ( !window.open( url, name, option ) ) {
            blank.location.href = url;
        }
        return false;
    });
    // ページ内スクロール
    $('a[href^=#]').click(function() {
        // スクロールの速度
        var speed = 400;// ミリ秒
        // アンカーの値取得
        var href= $(this).attr('href');
        // 移動先を取得
        var target = $(href == '#' || href == '' ? 'html' : href);
        // 移動先を数値で取得
        var position = target.offset().top;
        // スムーススクロール
        $(jQuery.support.safari ? 'body' : 'html').animate({scrollTop:position}, speed, 'swing');
        return false;
    });
});

