/* =========================================================

// jquery.innerfade.js

// Datum: 2008-02-14
// Firma: Medienfreunde Hofmann & Baldes GbR
// Author: Torsten Baldes
// Mail: t.baldes@medienfreunde.com
// Web: http://medienfreunde.com

// based on the work of Matt Oakes http://portfolio.gizone.co.uk/applications/slideshow/
// and Ralf S. Engelschall http://trainofthoughts.org/

 *
 *  <ul id="news"> 
 *      <li>content 1</li>
 *      <li>content 2</li>
 *      <li>content 3</li>
 *  </ul>
 *  
 *  $('#news').innerfade({ 
 *    animationtype: Type of animation 'fade' or 'slide' (Default: 'fade'), 
 *    speed: Fading-/Sliding-Speed in milliseconds or keywords (slow, normal or fast) (Default: 'normal'), 
 *    timeout: Time between the fades in milliseconds (Default: '2000'), 
 *    type: Type of slideshow: 'sequence', 'random' or 'random_start' (Default: 'sequence'), 
 *      containerheight: Height of the containing element in any css-height-value (Default: 'auto'),
 *    runningclass: CSS-Class which the container get’s applied (Default: 'innerfade'),
 *    children: optional children selector (Default: null)
 *  }); 
 *

// ========================================================= */


(function($) {

    $.fn.innerfade = function(options) {
        return this.each(function() {   
            $.innerfade(this, options);
        });
    };

    $.innerfade = function(container, options) {
        var settings = {
                'animationtype':    'fade',
            'speed':            'normal',
            'type':             'sequence',
            'timeout':          2000,
            'containerheight':  'auto',
            'runningclass':     'innerfade',
            'children':         null
        };
        if (options)
            $.extend(settings, options);
        if (settings.children === null)
            var elements = $(container).children();
        else
            var elements = $(container).children(settings.children);
        if (elements.length > 1) {
            $(container).css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass);
            for (var i = 0; i < elements.length; i++) {
                $(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
            };
            if (settings.type == "sequence") {
                setTimeout(function() {
                    $.innerfade.next(elements, settings, 1, 0);
                }, settings.timeout);
                $(elements[0]).show();
            } else if (settings.type == "random") {
                    var last = Math.floor ( Math.random () * ( elements.length ) );
                setTimeout(function() {
                    do { 
                                                current = Math.floor ( Math.random ( ) * ( elements.length ) );
                                        } while (last == current );             
                                        $.innerfade.next(elements, settings, current, last);
                }, settings.timeout);
                $(elements[last]).show();
                        } else if ( settings.type == 'random_start' ) {
                                settings.type = 'sequence';
                                var current = Math.floor ( Math.random () * ( elements.length ) );
                                setTimeout(function(){
                                    $.innerfade.next(elements, settings, (current + 1) %  elements.length, current);
                                }, settings.timeout);
                                $(elements[current]).show();
                        }   else {
                            alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
                        }
                }
    };

    $.innerfade.next = function(elements, settings, current, last) {
        if (settings.animationtype == 'slide') {
            $(elements[last]).slideUp(settings.speed);
            $(elements[current]).slideDown(settings.speed);
        } else if (settings.animationtype == 'fade') {
            $(elements[last]).fadeOut(settings.speed);
            $(elements[current]).fadeIn(settings.speed, function() {
                            removeFilter($(this)[0]);
                        });
        } else
            alert('Innerfade-animationtype must either be \'slide\' or \'fade\'');
        if (settings.type == "sequence") {
            if ((current + 1) < elements.length) {
                current = current + 1;
                last = current - 1;
            } else {
                current = 0;
                last = elements.length - 1;
            }
        } else if (settings.type == "random") {
            last = current;
            while (current == last)
                current = Math.floor(Math.random() * elements.length);
        } else
            alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
        setTimeout((function() {
            $.innerfade.next(elements, settings, current, last);
        }), settings.timeout);
    };

})(jQuery);

// **** remove Opacity-Filter in ie ****
function removeFilter(element) {
    if(element.style.removeAttribute){
        element.style.removeAttribute('filter');
    }
}

/*
 * jQuery Easing Scroll - http://moto-mono.net/easingScroll/
 *
 * jQuery required.
 * jQuery Easing Plugin extends this Plugin.
 *
 * Copyright 2009 (c) nori (norimania@gmail.com)
 * http://moto-mono.net
 * Licensed Under the MIT.
 *
 * $Date: 2009-05-13
*/

jQuery.fn.easingScroll=function(b){var d=$.extend({easing:"swing",duration:500},b),c=$.support.boxModel?navigator.appName.match(/Opera/)?"html":"html,body":"body";if(isNaN(d.duration)==null){if(d.duration.match(/fast/)){d.duration=210;}else{if(d.duration.match(/normal/)){d.duration=410;}else{if(d.duration.match(/slow/)){d.duration=610;}}}}$(this).each(function(){if(this.hash&&$(this.hash).length>0&&this.href.match(new RegExp(location.href.split("#")[0]))){$(this).click(function(e){$(c).queue([]).stop();var a=this.hash;var f=$(a).offset();$(c).animate({scrollTop:f.top,scrollLeft:f.left},{duration:d.duration,easing:d.easing});e.preventDefault();e.stopPropagation();});}});$(document).click(function(a){$(c).queue([]).stop();});};

/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * TERMS OF USE - jQuery Easing
 * 
 * Open source under the BSD License. 
 * 
 * Copyright (c) 2008 George McGinley Smith
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
*/

jQuery.easing.jswing=jQuery.easing.swing;jQuery.easing.jswing=jQuery.easing.swing;jQuery.extend(jQuery.easing,{def:"easeOutQuad",swing:function(j,i,b,c,d){return jQuery.easing[jQuery.easing.def](j,i,b,c,d);},easeInQuad:function(j,i,b,c,d){return c*(i/=d)*i+b;},easeOutQuad:function(j,i,b,c,d){return -c*(i/=d)*(i-2)+b;},easeInOutQuad:function(j,i,b,c,d){if((i/=d/2)<1){return c/2*i*i+b;}return -c/2*((--i)*(i-2)-1)+b;},easeInCubic:function(j,i,b,c,d){return c*(i/=d)*i*i+b;},easeOutCubic:function(j,i,b,c,d){return c*((i=i/d-1)*i*i+1)+b;},easeInOutCubic:function(j,i,b,c,d){if((i/=d/2)<1){return c/2*i*i*i+b;}return c/2*((i-=2)*i*i+2)+b;},easeInQuart:function(j,i,b,c,d){return c*(i/=d)*i*i*i+b;},easeOutQuart:function(j,i,b,c,d){return -c*((i=i/d-1)*i*i*i-1)+b;},easeInOutQuart:function(j,i,b,c,d){if((i/=d/2)<1){return c/2*i*i*i*i+b;}return -c/2*((i-=2)*i*i*i-2)+b;},easeInQuint:function(j,i,b,c,d){return c*(i/=d)*i*i*i*i+b;},easeOutQuint:function(j,i,b,c,d){return c*((i=i/d-1)*i*i*i*i+1)+b;},easeInOutQuint:function(j,i,b,c,d){if((i/=d/2)<1){return c/2*i*i*i*i*i+b;}return c/2*((i-=2)*i*i*i*i+2)+b;},easeInSine:function(j,i,b,c,d){return -c*Math.cos(i/d*(Math.PI/2))+c+b;},easeOutSine:function(j,i,b,c,d){return c*Math.sin(i/d*(Math.PI/2))+b;},easeInOutSine:function(j,i,b,c,d){return -c/2*(Math.cos(Math.PI*i/d)-1)+b;},easeInExpo:function(j,i,b,c,d){return(i==0)?b:c*Math.pow(2,10*(i/d-1))+b;},easeOutExpo:function(j,i,b,c,d){return(i==d)?b+c:c*(-Math.pow(2,-10*i/d)+1)+b;},easeInOutExpo:function(j,i,b,c,d){if(i==0){return b;}if(i==d){return b+c;}if((i/=d/2)<1){return c/2*Math.pow(2,10*(i-1))+b;}return c/2*(-Math.pow(2,-10*--i)+2)+b;},easeInCirc:function(j,i,b,c,d){return -c*(Math.sqrt(1-(i/=d)*i)-1)+b;},easeOutCirc:function(j,i,b,c,d){return c*Math.sqrt(1-(i=i/d-1)*i)+b;},easeInOutCirc:function(j,i,b,c,d){if((i/=d/2)<1){return -c/2*(Math.sqrt(1-i*i)-1)+b;}return c/2*(Math.sqrt(1-(i-=2)*i)+1)+b;},easeInElastic:function(o,m,p,a,b){var d=1.70158;var c=0;var n=a;if(m==0){return p;}if((m/=b)==1){return p+a;}if(!c){c=b*0.3;}if(n<Math.abs(a)){n=a;var d=c/4;}else{var d=c/(2*Math.PI)*Math.asin(a/n);}return -(n*Math.pow(2,10*(m-=1))*Math.sin((m*b-d)*(2*Math.PI)/c))+p;},easeOutElastic:function(o,m,p,a,b){var d=1.70158;var c=0;var n=a;if(m==0){return p;}if((m/=b)==1){return p+a;}if(!c){c=b*0.3;}if(n<Math.abs(a)){n=a;var d=c/4;}else{var d=c/(2*Math.PI)*Math.asin(a/n);}return n*Math.pow(2,-10*m)*Math.sin((m*b-d)*(2*Math.PI)/c)+a+p;},easeInOutElastic:function(o,m,p,a,b){var d=1.70158;var c=0;var n=a;if(m==0){return p;}if((m/=b/2)==2){return p+a;}if(!c){c=b*(0.3*1.5);}if(n<Math.abs(a)){n=a;var d=c/4;}else{var d=c/(2*Math.PI)*Math.asin(a/n);}if(m<1){return -0.5*(n*Math.pow(2,10*(m-=1))*Math.sin((m*b-d)*(2*Math.PI)/c))+p;}return n*Math.pow(2,-10*(m-=1))*Math.sin((m*b-d)*(2*Math.PI)/c)*0.5+a+p;},easeInBack:function(l,k,b,c,d,j){if(j==undefined){j=1.70158;}return c*(k/=d)*k*((j+1)*k-j)+b;},easeOutBack:function(l,k,b,c,d,j){if(j==undefined){j=1.70158;}return c*((k=k/d-1)*k*((j+1)*k+j)+1)+b;},easeInOutBack:function(l,k,b,c,d,j){if(j==undefined){j=1.70158;}if((k/=d/2)<1){return c/2*(k*k*(((j*=(1.525))+1)*k-j))+b;}return c/2*((k-=2)*k*(((j*=(1.525))+1)*k+j)+2)+b;},easeInBounce:function(j,i,b,c,d){return c-jQuery.easing.easeOutBounce(j,d-i,0,c,d)+b;},easeOutBounce:function(j,i,b,c,d){if((i/=d)<(1/2.75)){return c*(7.5625*i*i)+b;}else{if(i<(2/2.75)){return c*(7.5625*(i-=(1.5/2.75))*i+0.75)+b;}else{if(i<(2.5/2.75)){return c*(7.5625*(i-=(2.25/2.75))*i+0.9375)+b;}else{return c*(7.5625*(i-=(2.625/2.75))*i+0.984375)+b;}}}},easeInOutBounce:function(j,i,b,c,d){if(i<d/2){return jQuery.easing.easeInBounce(j,i*2,0,c,d)*0.5+b;}return jQuery.easing.easeOutBounce(j,i*2-d,0,c,d)*0.5+c*0.5+b;}});

// main.js 
(function(){
    $('#main-visual-image').innerfade({
        animationtype: 'fade',
        speed: 1500,
        timeout: 5000,
        containerheight: '240px'
    });

    // ページ内目次生成
    var $pagecontents = $('p.pagecontents');
    var parent = $pagecontents.closest('div.asset-content');
    if ($pagecontents.size() > 0) {
        var num = 1, ul = '', li = [];
        $('h2', parent).each(function(i){
            var id = $(this).attr('id');
            if (! id) {
                id = 'page-contents-' + i;
                $(this).attr('id', id);
            }
            li.push('<li><a href="#' + id + '">' + num + '. ' + $(this).html() + '</li>');
            num++;
        });
        if (li.length > 0) {
            ul = '<ul id="page-contents">' + li.join('') + '</ul>';
            $pagecontents.after(ul);
        }
    }

    // .banner-box
    $('#gamma-inner').find('dl.banner-box')
        .hover(
            function(){
                $(this).css({
                    'cursor': 'pointer',
                    'opacity': '0.7'
                });
            },
            function(){
                $(this).removeAttr('style');
            }
        )
        .click(function(e){
            var link = $(e.currentTarget).find('a').attr('href');
            location.href = link;
        });

    // fancybox
    $('a[href$="jpg"], a[href$="JPG"], a[href$="png"]').fancybox({
        autoScale: false
    });

    // するするスクロール
    $("a[href*='#']").easingScroll();

    // ツールチップ
    $(function(){
        
        // 全要素をからTITLE属性を持っている要素だけに絞る
        $("body *").filter(function(){
            return this.title && this.title.length>0;
        }).each(function(){
            // TITLE属性を持っている要素に適用する
            
            // あとで使う
            var self = $(this), title = self.attr("title");
            
            // TITLE属性を持っている要素にhover()で
            self.hover(
            
                // mouseover
                function(e){ // このeはevent自体を意味する
                
                    // TITLEがあるとブラウザのチップが出るので一時的に空にしておく
                    self.attr("title","");
                
                    // とりあえず表示するtip要素を生成しておく
                    $("body").append("<div id='title-tip'>"+title+"</div>");
                    $("#title-tip").css({
                        position: "absolute",
                        
                        // e.pageX(Y)でカーソルが要素に乗った時点でのX(Y)座標を取得する
                        top: e.pageY+(-20), // カーソルと表示したtipが重なるとチラつくので少しずらす
                        left: e.pageX+20
                    });
                },
                
                // mouseout
                function(){
                
                    // mouseoverで空にしたTITLEを戻す
                    self.attr("title",title);
                
                    // 要素から離れた場合はtipを非表示にして削除しておく
                    $("#title-tip").hide().remove();
                }
            );
            
            // 要素上でカーソルが移動した場合は、逐一tipの位置を変える
            self.mousemove(function(e){
                $("#title-tip").css({
                    top: e.pageY+(-20),
                    left: e.pageX+20
                });     
            });
        }); 
    });

})(jQuery);

