(function($){

  $(document).ready(function(){

    var class_array = $('body').attr('class').split(' ');
    var cate = class_array[1];
/* ?theme=classic */

//$('.photos a').lightBox();

    function hookHover () {
      $('div#ghNav a.hover').each(function(){
        $(this).children('img').each(function(){
          this.orgsrc = this.src;
          var src = this.src.split('.');
          src[src.length - 2] = src[src.length - 2] + '_o';
          this.hoversrc = src.join('.');

/* alert($(this).parent().parent().attr('class')) */
          
          if ($(this).parent().parent().attr('class') == cate) {
            $(this).attr('src', this.hoversrc);
            this.orgsrc = this.hoversrc;
          } else if ((cate == 'product_pov' || cate == 'product_emergensee') && $(this).parent().parent().attr('class') == 'products') {
            $(this).attr('src', this.hoversrc);
            this.orgsrc = this.hoversrc;
          }
        });
      });

/*
		var current = $('body').attr('class');
		var postfix = '_o';
		$('a.hover img').not('[src*="'+ postfix +'."]').each(function() {
			var img = $(this);
			var src = img.attr('src');
			var src_on = src.substr(0, src.lastIndexOf('.'))
									+ postfix
									+ '.png';
			if (img.parent().parent().hasClass(current)) {
				img.attr('src', src_on);
			}
		})
*/

      $('a.hover').mouseover(function(){
        $(this).children('img').each(function(){
          this.src = this.hoversrc;
        });
      });

      $('a.hover').mouseout(function(){
        $(this).children('img').each(function(){
          this.src = this.orgsrc;
        });
      });
      
      $('body.product_pov a.cover-menu, body.product_emergensee a.cover-menu').each(function() {
        if ($(this).attr('href') == location.href) {
          var theSrc = $(this).children('img').attr('src').split('.');
          theSrc[theSrc.length - 2] = theSrc[theSrc.length - 2] + '_o';
          $(this).children('img').attr('src', theSrc.join('.'));
        }
      });
    }

    function hookRel () {
      $('a').each(function(){
        if($(this).attr("rel") == "external"){
          $(this).attr("target", "_blank");
        }
      });
    }
    
    function sidebarCurrent () {
      $('div#sideBar a').each(function(){
        if ($(this).attr("href") == location.href) {
          $(this).parent().attr("class", "current");
        }
      });
    }


    function slideCover(theDuration) {
/*
      with (document.getElementById("covers").style) {
        position = "relative";
        overflow = "hidden";
      }
*/

      var theWidth = '960px';
      var theHeight = '295px';
      var intervalTime = 8000;
 
      var covers = $('div#covers div.cover');
      if (covers.size() >= 2) {
        var currentCover = 0;
        covers.each(function(){
          var coverArray = $(this).get();
          with (coverArray[0].style) {
            position = "absolute";
            width = theWidth;
            height = theHeight;
            top = "0";
            left = "0";
          };
          $(this).css('display', 'none');
        });
        covers.eq(currentCover).css('display', 'block');
        
        function changeCover() {
          covers.eq((currentCover + 1) % covers.size()).css('left', theWidth);
          covers.eq((currentCover + 1) % covers.size()).css('display', 'block');
          covers.eq(currentCover).animate({
            left: '-' + theWidth
          }, {
            duration: theDuration,
            easing: 'jswing'
          });
          covers.eq((currentCover + 1) % covers.size()).animate({
            left: '0'
          }, {
            duration: theDuration,
            easing: 'jswing',
            complete: function() {
              covers.eq(currentCover).css('display', 'none');
              covers.eq(currentCover).css('left', '0');
              currentCover = (currentCover + 1) % covers.size();
            }
          });
          
          if ($('body').hasClass('home')) {
            if ((currentCover + 1) % covers.size() == 0) {
              intervalTime = 9000;
            } else if ((currentCover + 1) % covers.size() == 1) {
              intervalTime = 13750;
            } else if ((currentCover + 1) % covers.size() == 2) {
              intervalTime = 8750;
            }
          }
          
          setTimeout(changeCover, intervalTime);
        }
        
        setTimeout(changeCover, intervalTime);
      }
    }
    
    function slideCompany (theDuration) {
      var theWidth = "960px";
      var currentContent = $('div#staff-contents div#s0-content');
      var animating = false;
      function setClick(elementId) {
        $('div#' + elementId).click(function(){
          var theContent = $('div#' + elementId.replace('thumb', 'content'));
          if (theContent.attr('id') != currentContent.attr('id') && !animating) {
            animating = true;
            theContent.css('display', 'block');
            theContent.css('left', theWidth);
            currentContent.animate({
              left: '-' + theWidth
            }, {
              duration: theDuration,
              easing: 'jswing'
            });
           theContent.animate({
              left: '0'
            }, {
              duration: theDuration,
              easing: 'jswing',
              complete: function() {
                animating = false;
                currentContent.css('display', 'none');
                currentContent = theContent;
              }
            });
          }
        });
      }
      var maxHeight = 0;
      $('div#staff-contents div').each(function(){
        if ($(this).attr('id').match(/^s[0-9]+-content$/)) {
          if ($(this).height() > maxHeight) {
            maxHeight = $(this).height();
          }
          if ($(this).attr('id') != currentContent.attr('id')) {
            $(this).css('display', 'none');
          } 
          $(this).css('width', theWidth);
          $(this).css('position', 'absolute');
          $(this).css('top', '0');
          $(this).css('left', '0');
        }
      });
      $('div#staff-contents div.wrapper').css('position', 'relative');
      $('div#staff-contents div.wrapper').css('overflow', 'hidden');
      $('div#staff-contents div.wrapper').css('height', maxHeight + 'px');
      $('div#staff-thumbs div').each(function(){
         if ($(this).attr('id').match(/^s[0-9]+-thumb$/)) {
          setClick($(this).attr('id'));
        }
      });
    }
    
    function slidePictures (theDuration) {
      var animating = false;
      var photoLi = $('div#photo-list li');
      var currentPhoto = 0;
      $('div#photo-list #photo-prev').css('visibility', 'hidden');
      if (photoLi.size() < 4) {
        $('div#photo-list #photo-next').css('visibility', 'hidden');
      }
      $('div#photo-list ul').css('width', photoLi.size() * 320 + 'px');
      $('div#photo-list div.wrapper').css('overflow', 'hidden');
      
      function doSlide(photoNumber) {
        if (!animating) {
          if (photoNumber < 0 || photoNumber > photoLi.size() - 3) {
            return;
          }
          currentPhoto = photoNumber;
          animating = true;
          $('div#photo-list ul').animate({
            marginLeft: '-' + currentPhoto * 320 + 'px'
          }, {
            duration: theDuration,
            easing: 'jswing',
            complete: function() {
              animating = false;
              if (currentPhoto > 0) {
                $('div#photo-list #photo-prev').css('visibility', 'visible');
              } else {
                $('div#photo-list #photo-prev').css('visibility', 'hidden');
              }
              if (currentPhoto < photoLi.size() - 3) {
                $('div#photo-list #photo-next').css('visibility', 'visible');
              } else {
                $('div#photo-list #photo-next').css('visibility', 'hidden');
              }
            }
          });
        }
      }
      
      $('div#photo-list #photo-next').click(function(){
        doSlide(currentPhoto + 1);
      });
      $('div#photo-list #photo-prev').click(function(){
        doSlide(currentPhoto - 1);
      });
    }
    
    
    function setMailto() {
      $('span.mailto').each(function(){
        $(this).html('<a href="mailto:contact@adlens.com">contact@adlens.com</a>');
      });
    }
    
    function setTheme() {
      var theTheme = false;
      var hashes = location.href.slice(location.href.indexOf('?') + 1).split('&');
      for (var i = 0; i < hashes.length; i++) {
        var theHash = hashes[i].split('=');
        if (theHash[0] == 'theme') {
          var theTheme = theHash[1];
        }
      }
      if (theTheme) {
        $('a').each(function(){
          if ($(this).attr('href').indexOf('?') == -1) {
            $(this).attr('href', $(this).attr('href') + '?theme=' + theTheme);
          } else {
            $(this).attr('href', $(this).attr('href') + '&theme=' + theTheme);
          }
        });
      }
    }



/*
    var bodyElement = document.getElementsByTagName('body')[0];
    var bodyClasses = bodyElement.className;
    classNames = bodyClasses.toString();
    var className_array = classNames.split(" ");
    langName = className_array[0];

*/
    hookHover();
    hookRel();
    sidebarCurrent();
    slideCover(500);	//アニメーション継続時間(msec)
    slideCompany(500);	//アニメーション継続時間(msec)
    slidePictures(700);	//アニメーション継続時間(msec)
    setMailto();
    setTheme();
  });
  
  
  $(document).resize(function(){
  });

})(jQuery.noConflict());

