// Google Analytics Async Code /////////////////////////////////////////////////////////////////

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1215282-1']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


$(function(){
// start onload /////////////////////////////////////////////////////////////////////////////////

(function(){
  var ua = navigator.userAgent ;
  var browser = "unkown" ;
  var os      = "unkown" ;

  if(ua.match(/Gecko/i)  != null) {browser = "Gecko";}
  if(ua.match(/Webkit/i) != null) {browser = "Webkit";}
  if(ua.match(/MSIE/i)   != null) {browser = "Msie";}
  if(ua.match(/Opera/i)  != null) {browser = "Opera";}

  if(ua.match(/Windows/i) != null) {os = "Win";}
  if(ua.match(/Mac/i)     != null) {os = "Mac";}

  $("body").addClass(browser).addClass(os);
})();

var root = "/";

$.getScript(root + "js/namespace.js",function(){
// Namespace onload /////////////////////////////////////////////////////////////////////////////////

var NS = window.getNameSpace();


// Common ----------------------------------------
(function(){
  
  // Popup Opener 
  $.getScript(root + "js/popup.js",function(){
    var popup = NS.popup = new NS.Popup();
    popup.set();  
  });
  
  $.getScript(root + "js/viewport.js");
  
  // font size Script
  var target = $("#contents .article .header");
  if(!target[0]) return ;
  
  $.getScript(root + "js/cookie.js",function(){
    $.getScript(root + "js/fontsize.js",function(){
                                  
      var fontnav = NS.fontnav = new NS.Fontsize(target);
      fontnav.setChangeTarget("#contents .article");
      fontnav.create();
    
    });  
  });
  
})();


// page fadeIn ----------------------------------------
(function(){
  var page = $("#page");
  if(!page[0]) return ;
  
  page.addClass("pageFadeIn").bind("webkitAnimationEnd",function(){
    page.addClass("pageShow").removeClass("pagePadeIn");
  });

})();


// contents article height ----------------------------------------
(function(){
  var article = $("#contents .article");
  var aside = $("#contents .aside");
  if(!article[0]) return ;
  if(!aside[0]) return ;
  
  if($.support.boxModel) article.animate({minHeight:aside.outerHeight() + parseInt(aside.css("marginTop")) - article.find(".footer").outerHeight()},300);
  else article.height(aside.outerHeight() + parseInt(aside.css("marginTop")) - 1);
})();





// top page Script ----------------------------------------
(function(){
  if(!$("#topIndex")[0]) return ;
 
  (function(){
    if(!$("body").hasClass("Webkit")) return ;

    var AnimationEnd = "webkitAnimationEnd";
    var boxItems     = $("#contents > .section:not(#boxContact):not(#boxNews)");
    /*
    var boxarr = [];
    $.each(boxItems,function(i,val){
      if(this.id == "boxContact") return ;
      if(this.id == "boxNews") return ;

      var _this = $(this);
      
      _this.bind("webkitAnimationEnd",function(){
        _this.removeClass("fadeAnimation");
      });
      boxarr.push(_this);
    });

    setInterval(function(){
      if(boxarr.length > 0){
        var num = Math.floor(boxarr.length*Math.random());
        boxarr[num].addClass("fadeAnimation");
      }
    },1000);
    setInterval(function(){
      if(boxarr.length > 0){
        var num = Math.floor(boxarr.length*Math.random());
        boxarr[num].addClass("fadeAnimation");
      }
    },1200);
    */

    boxItems
      .bind("mouseover",function(e){
        $(this).addClass("hoverAnimation").bind(AnimationEnd,handleHoverAnimationEnd);
      })
      .bind("click",function(e){
        var _this = $(this) ;
        if(!_this.find("a")[0]) return ;
        _this.addClass("clickAnimation").bind(AnimationEnd,handleClickAnimationEnd);
        e.preventDefault();
      });
    
    function handleHoverAnimationEnd(e){
      $(this).removeClass("hoverAnimation");
    }

    function handleClickAnimationEnd(e){
      var _this = $(this) ;
      _this.removeClass("clickAnimation").addClass("clickAnimationEnd").unbind(AnimationEnd,handleClickAnimationEnd);
      window.location = _this.find("a").attr("href");
      setTimeout(function(){
        _this.removeClass("clickAnimationEnd").addClass("clickAnimationAfter").bind(AnimationEnd,handleClickAnimationAfterEnd)
      },500)
    }
    
    function handleClickAnimationAfterEnd(e){
      $(this).removeClass("clickAnimationAfter").unbind(AnimationEnd,handleClickAnimationAfterEnd);
    } 
  })();

  if(!$("#tweetMusic")[0]) return ;
  // tweet 
  $.getScript(root + "js/loader.js",function(){
    $.getScript(root + "js/tweet.js",function(){
                            
      var tweet = NS.tweet = new NS.Tweets("#tweetMusic");
      tweet.cacheExpire = 60*1000 ;

      function tweetload(){
        tweet.getUserTimeline({
          userid : "sgni_itunes",
          maxlength : 1 ,
          callback : function(thisobj,res){
                        
            var _this = thisobj , res_length = res.length , count = 0 ;
            
            var url = _this.URL + res[0].user.name ;
            var name = res[0].user.screen_name ;
            
            _this.items = [] ;    
            $.each(res,function(){              
              var text = _this.textInit(this.text);    
              var itm = $("<div>").addClass("tweet").hide();
              var itm_dl = $("<dl>").prependTo(itm);
              var itm_text = $("<dt>").html(text).appendTo(itm_dl);
              var itm_data = $("<dd>").html(NS.methods.toDateFormatTimeAgo(this.created_at)).appendTo(itm_dl);
              var itm_soft = $(this.source).attr("target","_blank").appendTo(itm_data);
              itm.appendTo(_this.target).fadeIn(300);
              _this.items.push(itm);
              
              count++ ;
            });
          }
        });
      }
      tweetload();
      setInterval(tweetload,60000);
    });
  });

  // reflect
  $.getScript(root + "js/reflect.js",function(){
    if(!$("#boxMusic")[0]) return ;
    
    var reflect = NS.reflect = new NS.Reflect($("#boxMusic .photo img"));
    reflect.setRefColor({r : 125 , g : 125 , b : 125 , alpha : 0.5});
    reflect.create();
    
    reflect.onloadReflect(function(_reflect){
      
      $(_reflect.canvas).css({
        position : "absolute",
        bottom : 0 ,
        marginBottom : -(_reflect.target.height()/2) + "px" ,
        marginLeft : -(_reflect.canvas.width()) + "px"
      });
              
    });
  });
  
})();

// works details page Script ----------------------------------------
(function($){
  if(!$("#worksDetails")[0]) return ;

  $.getScript(root + "js/slideshow.js",function(){
    setTimeout(function(){
    if($("#summary").find(".photo").children().length > 1){

      var items_0 = $("#summary").find(".photo").children().first();
      if(items_0[0].tagName == "A") items_0 = $(items_0.find("img")[0]);
      var firstslideheight = items_0.height();
      $("#summary").find(".photo").children().height(firstslideheight);

      var slideshow = NS.slideshow = new NS.Slideshow({
        target     : $("#summary").find(".photo")[0],
        width      : 450 ,
        height     : firstslideheight ,
        interval   : 5000 ,
        effecttime : 1000 ,
        autoPlay   : true ,
        enable     : { nav : true }
      });
      
      slideshow.nav.css({
        width   : slideshow.navbtns.length*20 ,
        left    : (450 - slideshow.navbtns.length*20)*0.5 +10 ,
        padding : "10px 0 0 0" 
      });
    }
    },100);
  });

})($);



// blog page Script ----------------------------------------
(function(){
  if(!$(".blogPage")[0]) return ;
  
  $(".pagesNumber").children().first().addClass("first");

  $.getScript(root + "js/youtubeload.js");

})();


// Company Access Script ----------------------------------------
(function($){
  if(!$("#companyAccess")[0]) return ;
  if(!$("#map")[0]) return ;

  $.getScript(root + "js/googlemaps.js",function(){
    var ings_pos = [35.693750, 139.761967];

    var map = NS.map = new NS.GoogleMaps({
      target  : "#map",
      zoom    : 17 ,
      latlng  : ings_pos ,
      maptype : "ROADMAP"
    });

    var ings = map.createCustomMarker({
      latlng : ings_pos ,
      title  : "株式会社イングス" ,
      icon   : {
        path   : "/images/marker_sprite.png" ,
        size   : [19,34],
        anchor : [10,34]
      },
      shadow   : {
        path   : "/images/marker_sprite.png" ,
        size   : [29,34],
        origin : [29,0],
        anchor : [0,34]
      },
      shape  : {
        coord  : [9,0,6,1,4,2,2,4,0,8,0,12,1,14,2,16,5,19,7,23,8,26,9,30,9,34,11,34,11,30,12,26,13,24,14,21,16,18,18,16,20,12,20,8,18,4,16,2,15,1,13,0]
      },
      zIndex : 99
    });

    var info_ings = map.setInfoWindow({
      marker  : ings ,
      content : "<div class='infoWindowInner'><h3>株式会社イングス</h3></div>"
    });
    info_ings.open(map,ings);

    var jinbocho = map.createCustomMarker({
      latlng : [35.693991,139.758764] ,
      title  : "神保町駅 入り口" ,
      icon   : {
        path   : "/images/marker_sprite_lb.png" ,
        size   : [19,34],
        anchor : [10,34]
      },
      shadow   : {
        path   : "/images/marker_sprite_lb.png" ,
        size   : [29,34],
        origin : [29,0],
        anchor : [0,34]
      },
      shape  : {
        coord  : [9,0,6,1,4,2,2,4,0,8,0,12,1,14,2,16,5,19,7,23,8,26,9,30,9,34,11,34,11,30,12,26,13,24,14,21,16,18,18,16,20,12,20,8,18,4,16,2,15,1,13,0]
      },
      zIndex : 99
    });
//    var jinbocho = map.createMarker([35.693991,139.758764],"神保町駅 入り口");
    map.setInfoWindow({
      marker  : jinbocho ,
      content : "<div class='infoWindowInner'><dl><dt>●神保町駅</dt><dd>（東京メトロ：半蔵門線、都営三田線・新宿線）</dd></dl></div>"
    });

    var ogawamachi = map.createCustomMarker({
      latlng : [35.694253,139.765153] ,
      title  : "小川駅・新御茶ノ水駅 入り口" ,
      icon   : {
        path   : "/images/marker_sprite_lb.png" ,
        size   : [19,34],
        anchor : [10,34]
      },
      shadow   : {
        path   : "/images/marker_sprite_lb.png" ,
        size   : [29,34],
        origin : [29,0],
        anchor : [0,34]
      },
      shape  : {
        coord  : [9,0,6,1,4,2,2,4,0,8,0,12,1,14,2,16,5,19,7,23,8,26,9,30,9,34,11,34,11,30,12,26,13,24,14,21,16,18,18,16,20,12,20,8,18,4,16,2,15,1,13,0]
      },
      zIndex : 99
    });
//    var ogawamachi = map.createMarker([35.694253,139.765153],"小川駅・新御茶ノ水駅 入り口");
    map.setInfoWindow({
      marker  : ogawamachi ,
      content : "<div class='infoWindowInner'><dl><dt>●小川駅</dt><dd>（都営新宿線）</dd><dt>●新御茶ノ水駅</dt><dd>（東京メトロ：千代田線）</dd></dl></div>"
    });
  });

})($);






// end Namespace onload /////////////////////////////////////////////////////////////////////////////////
});
// end onload ///////////////////////////////////////////////////////////////////////////////////
});
