/*
 * Base functions that Wordpress Theme is Sawai Hiroshi Base Theme
 * http://www.info-town.jp/, http://www.creationlabs.net/
 *
 * Copyright 2011, Sawai Hiroshi
 *
 * Date: Thu Nov 17 2011
 */

// jQuery バブリング
//  http://blog.neo.jp/dnblog/index.php?module=Blog&action=Entry&blog=pg&entry=3108&rand=0615d


jQuery(function ($) {
    // 定数の名前空間
    var Util = {};
    // jQueryの拡張
    jQuery.extend({'util': Util});
    // 静的メソッドの追加
    // 末尾のスラッシュなし
    Util.getURL = function () {
        var url = window.location.href;
        return url;
    };
    Util.getFileName = function (url) {
        var file = url.substring(url.lastIndexOf('/')+1,url.length);
        return file;
    };
    Util.getDirName = function (url) {
        var dir = url.substring(0, url.lastIndexOf('/')+1);
        return dir;
    };
    // remove delimitor most end of global navigation
    var navlists = $('#nav li');
    $(navlists[navlists.length-1]).css({
        'borderRight' : '1px solid #000'
    });
    // remove delimitor most end of functional navigatio
    var func_navlists = $('#func-nav li');
    $(func_navlists[func_navlists.length-1]).css({
        'borderLeft' : '1px solid #000'
    });
    // aside height justify
    if ( $('#aside-left').height() < $('#main').height() ) {
        var main_height =  $('#main').height();
        $('#aside-left').css('height', main_height + 'px');
    }
    // Tab Menu  * Google Mapのときはinitilize()
    var tab_menus = $('div.tab-menu li');
    tab_menus.each(function () {
            if ( $(this).attr('class') !== 'selected' ) {
                $('div#tab-panel-' + $(this).attr('id')).hide();
            }
    });
    tab_menus.click(function () {
        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();
        return false;
    });
    // Slide
    $('div#slide-thumb img').click(function () {
        $('div#slide-main img').hide();
        var path = $(this).attr('src'); // クリックされた画像imgのパス
        var path_new = path.replace(/-thumb/, '');
        var img = new Image();
        img.src = path_new;
        $('div#slide-main img').remove(); // 自然な処理にするために削除(remove)してから追加(append)する。
        $('div#slide-main').append(img);
        $('div#slide-main img').fadeIn('fast');
        return false;
    });
    // Toggle
    var godetail = $('p.go-detail a');
    if (godetail) {
        godetail.click(function (event) {
            var detail = $(this).parent().parent().next();
            if ( detail.is(':hidden') ) {
                detail.slideDown('fast');
            }
            return false;
        });
    }
    var closedetail = $('p.close-detail a');
    if (closedetail) {
        closedetail.click(function () {
            $(this).parent().parent().slideUp('fast');
            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(); // new Date().getTime()はIEのキャッシュ対策。必ずImage.onloadを発生させるために必要。
            img.onload = function () { // 画像の幅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/modal/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;
        }
    });
    // Dropdown
    var dropdown_container = $('div#dropdown-container');
    var dropdown_timer;
    dropdown_container.hover(function () {
        clearTimeout(dropdown_timer);
        $('> ul', this).show();
    }, function () {
        var self = this;
        dropdown_timer = setTimeout(function () {
            $('> ul', self).hide();
        }, 350);
    });
    $('div#dropdown-container ul li:first-child').css('border-top', '1px solid #ddd');
    $('div#dropdown-container ul li:last-child').css('border-bottom', 'none');
    // Horizonal Dropdown Navi
    var target_menu = $('ul.horizonal-nav-sub');
    target_menu.each(function () {
        var li = $(this).children();
        $(li[li.length-1]).css('border-bottom', '1px solid #000');
    });
    $('a.noclick').click(function () {
        return false;
    });
    target_menu.hide();
    var horizonal_nav_timer = [];
    var j;
    target_menu.each(function (i) {
        var self = this;
        $(this).parent().hover(function () {
            for ( j=0; j<horizonal_nav_timer.length; j++ ) {
                clearTimeout(horizonal_nav_timer[j]);
            }
            //target_menu.show();
            $(self).show();
        }, function () {
            horizonal_nav_timer[i] = setTimeout(function() {
               $(self).hide();
            }, 300);
        });
    });

    // 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() {
        if ($(this).attr('class') === 'noscroll') {
            return false;
        }
        // スクロールの速度
        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;
    });
});

