jQuery.extend( jQuery.easing,
{
    def: 'easeOutExpo',
    easeInExpo: function (x, t, b, c, d) {
        return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
    },
    easeOutExpo: function (x, t, b, c, d) {
        return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
    },
    easeInOutExpo: function (x, t, b, c, d) {
        if (t==0) return b;
        if (t==d) return b+c;
        if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
        return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
    }
});

$(function(){
    //半透明マウスオーバー
    $(".hover").hover(function() {
        $(this).animate({opacity: 0.5},{duration: 150});
    }, function () {
        $(this).animate({opacity: 1},{duration: 150});
    });
});

$.fn.imgchange = function(options){
    //初期値設定
    var m = $.extend({
        name:  "_over"
    }, options);

    return this.each(function(){
        if (!this) return false;
        //ファイル名取得
        var src   = $(this).attr("src");
        var ftype = src.substring(src.lastIndexOf('.'), src.length);
        var hsrc  = src.replace(ftype, m.name + ftype);
        //オーバー画像読込み
        var img = new Image();
        img.src = hsrc;
        //ロールオーバー処理
        $(this).hover(function (){
            $(this).attr("src",hsrc);
        }, function(){
            $(this).attr("src",src);
        });
    });
    return this;
}

$.fn.imgchange2 = function(options){
    //初期値設定
    var m = $.extend({
        name:  "_over"
    }, options);

    return this.each(function(){
        if (!this) return false;
        //ファイル名取得
        var src   = $(this).attr("src");
        var ftype = src.substring(src.lastIndexOf('.'), src.length);
        var hsrc  = src.replace(ftype, m.name + ftype);
        //オーバー画像読込み
        var img = new Image();
        img.src = hsrc;
        //ロールオーバー処理
        $(this).hover(function (){
            $(this).attr("src",hsrc);
        }, function(){
            $(this).attr("src",src);
        });
    });
    return this;
}
$.fn.scrollPage = function (){
    var _url  = location.href.split('#')[0];
    var _hash = location.hash;
    $('a', this ).each(function() {
        if (this.href.indexOf(_url + '#') == 0) {
            var _id = this.href.split('#')[1];
            $(this).click(function(){
                    var _top = $('#' + _id).offset().top;
                    $($.browser.safari?'body':'html').animate({scrollTop:_top-20},1500,'easeInOutExpo');
            });

            if(!($.browser.msie && $.browser.version==6))
                $(this).removeAttr('href');
            $(this).css('cursor','pointer');
        }

    });
    if(_hash != ('undefined'|''|null)){
        var _target = $(_hash);
        if(_target.length){
            var _top = _target.offset().top;
            $($.browser.safari?'body':'html').animate({scrollTop:_top-20},1500,'easeInOutExpo');
        }
    }
}


var cache = [];
$.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
}

var _body;

function init(){

    _body = $('body');

    $(".over", _body).imgchange({
        name: "-o"
    });
    $('.onover',_body).imgchange2({
        name: "-o"
    });

    $(".acc", _body).each(function(){
        $(".acc_link,.fxmn > a", this).each(function(index){
            var $this = $(this);

            if(index >= 0) $this.next().hide();

            $this.click(function(){
                var params = {height:"toggle", opacity:"toggle"};
                $(this).next().animate(params).parent().siblings()
                    .children("ul:visible").animate(params);
                return false;
            });
        });
    });

    $("li.mainNav", _body).hover(
    function(){
        $("> .subNav:not(:animated)" , this).animate({
            height : "toggle",
            opacity : "toggle"
        }, 500 );
    },
    function(){
        $("> .subNav" , this).fadeOut("slow");
    });


    if(_body.is('.single'))
        $('ul#gallary-list',_body).galleria({
            history   : false,
            clickNext : false,
            lightbox: true,
            insert    : '.galleria_container',
            onImage   : function(image,caption,thumb) {
                image.css( 'opacity',0).animate({ opacity: 1 },1000,'easeOutExpo');
                caption.css('display','none');
                var _li = thumb.parents('li');
                _li.siblings().children('img.selected').fadeTo(500,0.3);
                thumb.fadeTo('fast',1).addClass('selected');

               $('a[rel=lightbox]',thumb.parents.parents).lightBox({
                imageLoading: './lib/img/lightbox-ico-loading.gif',
                imageBtnClose: './lib/img/lightbox-btn-close.gif',
                imageBtnPrev: './lib/img/lightbox-btn-prev.gif',
                imageBtnNext: './lib/img/lightbox-btn-next.gif',
                imageBlank: './lib/img/lightbox-blank.gif'
                //containerResizeSpeed: 350
                //txtImage: 'Imagem',
                //txtOf: 'de'
                //fixedNavigation: true
               });
            },
            onThumb : function(thumb) {
                var _li = thumb.parents('li');
                var _fadeTo = _li.is('.active') ? '1' : '0.3';
                thumb.css({ opacity:_fadeTo}).fadeIn(1500);
                thumb.hover(
                    function() { thumb.fadeTo('fast',1); },
                    function() { _li.not('.active').children('img').fadeTo('fast',0.3); }
                )
            }
        });

        var d = new Date();
        var t = d.getTime();
        var flashvars = {};
        var attributes = {};
        var params = {};
        attributes.id = 'flash-content';
        params.quality = 'best';
        params.bgcolor = '#000000';

        if(_body.is('.home')){
            params.scale = 'noscale';
            params.wmode = 'opaque';
            swfobject.embedSWF('./site/swf/preloader/bin/preloader.swf?'+t, 'flash', '100%', '366', '10.1', './lib/js/swfobject/swfobject/expressInstall.swf', flashvars, params, attributes);
            //swfobject.embedSWF('/site/swf/top/bin/Main.swf?'+t, 'flash', '100%', '366', '10', '/lib/swf/expressInstall.swf', flashvars, params, attributes);
        }

        if(_body.is('.page-parent-264,.page-parent-256,.page-parent-279,.page-parent-283')){

            var _url,_la,_lo;

            if(_body.is('.page-256,.page-828')){
                 _url = './site/swf/m/nagoya.swf';
                 _la = '35.24194';
                 _lo = '136.851924';
            }
            else if(_body.is('.page-279,.page-827')){
                _url = './site/swf/m/tokyo.swf';
                _la = '35.621905';
                _lo = '139.71932';

            }
            else if(_body.is('.page-283,.page-829')){
                _url = './site/swf/m/osaka.swf';
                _la = '34.698287';
                _lo = '135.505062';
            }

            params.allowFullScreen = true;
            params.allowScriptAccess = true;
            params.loop = true;
            params.play = true;
            params.scale = 'noscale';
            params.wmode = 'transparent';
            params.bgcolor = "#000000";
            swfobject.embedSWF( _url, 'noflash', '400', '280', '10.1', './lib/js/swfobject/swfobject/expressInstall.swf', flashvars, params, attributes);
            $("#gmap",_body).gmap3({
                latitude: _la,
                longitude: _lo,
                zoom: 14,
                navigationControl: true,
                mapTypeControl: true,
                scaleControl: true,
                markers: [
                    {
                        latitude: 35.24194,
                        longitude: 136.851924,
                        title: '名古屋本社',
                        content: '<div class="inner"><h4>名古屋本社</h4><p>〒481-0046<br />愛知県北名古屋市石橋字白目64番地</p></div>'
                    },
                    {
                        latitude: 35.621905,
                        longitude: 139.71932,
                        title: '東京支店',
                        content: '<div class="inner"><h4>東京支店</h4><p>〒141-0046<br />東京都品川区西五反田7丁目22番17号<br />TOC1F </p></div>'
                    },
                    {
                        latitude: 34.698287,
                        longitude: 135.505062,
                        title: '大阪支店',
                        content: '<div class="inner"><h4>大阪支店</h4>〒530-0047<br />大阪府大阪市北区西天満6丁目7番2号<br />新日本梅新ビル1F</div>'
                    }
                ]
            });
        }
        if( $("#head-area-navi", _body).length ){
            $("#head-area-navi",_body)
                .prepend('<div></div>').find('>div')
                .css({
                    backgroundColor: '#000000',
                    height: 30,
                    float: 'left',
                    width: '100%',
                    marginLeft: -560,
                    opacity: 0.85
                });
        }
    var ua = $.browser;
    if($.browser.msie && $.browser.version == '6.0'){
        DD_belatedPNG.fix('.png');
    }
}


var cache = [];
$.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
}

function comp(){

    if(!_body.is('.page-parent-264,.page-parent-256,.page-parent-279,.page-parent-283')){
        $(_body).scrollPage();
    }
    $.preLoadImages(
        "./site/img/background/bg-classics.jpg",
        "./site/img/background/bg-dining.jpg",
        "./site/img/background/bg-italian-classic-living.jpg",
        "./site/img/background/bg-italian-modern-dining.jpg",
        "./site/img/background/bg-italian-modern-living.jpg",
        "./site/img/background/bg-lighting.jpg",
        "./site/img/background/bg-living.jpg",
        "./site/img/background/bg-mirror.jpg",
        "./site/img/background/bg-sb-lighting.jpg",
        "./site/img/background/bg-sb-smallfurniture.jpg",
        "./site/img/background/bg-sb-smallinterior.jpg",
        "./site/img/background/bg-smallfurniture.jpg",
        "./site/img/background/bg-smallinterior.jpg",
        "./site/img/showroom/bg-256.jpg",
        "./site/img/showroom/bg-279.jpg",
        "./site/img/showroom/bg-283.jpg"
    );
    $.preloadCssImages();
}

$(init);
$(window).load(comp);
