$(function() {
  
  // Init *******************************************
  $('.services-content').css("display", "none");
  $('.services-header').css("cursor", "pointer");
  $('#works-thumb ul li img').fadeTo(100, 0.75);

  
  // ColorBox
  $("#works-thumb ul li a").colorbox({
    opacity: 0.75
  });
  
  // ColorBox YouTube
  $("#works-thumb ul li a.youtube").colorbox({
    iframe: true,
    innerWidth: 425,
    innerHeight: 344
  });
  
  // URL ********************************************
  var url = getURL();
  
  if(url.match(/#cat/i)) {
    var classname = url.replace(/#/, '.');
    var applyCat = classname + " img"
    $(applyCat).fadeTo(100, 1.0).addClass('selected');
    
    var applySelector = "#works-selector " + url + " a"
    
    $(applySelector).css({
        //color: "#920000",
        fontWeight: "bold"
      });
    
    targetOffset = $('#works').offset().top;
    $('html, body').animate({scrollTop: targetOffset}, 250);
  }
  else if(url != "") {
    scrollTOP(url, 250);
    $(url).find(".services-content").slideDown("slow");
  }
  
  
  
  // Services ***************************************
  $('.services-header')
    .mouseover(
      function() {
        $(this).css({
          //background: "#f2f0e9"
          backgroundImage: "url('/img/services/services-header-bg.gif')"
        });
      }
    )
    .mouseout(
      function() {
        $(this).css({
          background: "none"
        });
      }
    )
    .click(
      function() {
        scrollTOP(this, 300);
        
        $(this).next().animate({
          height: "toggle",
          opacity: "toggle"},
          500,
          "easeOutExpo"
        );
      }
     );
   
  $('.close a').click(
    function() {
      scrollTOP(this, 250);
      
      $(this).closest('.services-content').animate({
          height: "toggle",
          opacity: "toggle"},
          500,
          //"easeInOutCirc"
          "easeOutExpo"
        );
      
    }
  );
  
  // works ********************************************
  /*
  $('#works-selector li a').toggle(
    function() {
      var getAnc = $(this).closest("li").attr("id");
      
      var applyCat = "." + getAnc + " img";
      //$(applyCat).fadeTo(250, 1.0);
      $(applyCat).fadeTo(200, 1.0).addClass('selected');
      
      $(this).css({
        color: "#920000",
        fontWeight: "bold"
      })
    },
    function() {
      var getAnc = $(this).closest("li").attr("id");
      
      var applyCat = "." + getAnc + " img";
      $(applyCat).fadeTo(250, 0.75).removeClass('selected');
      
      $(this).css({
        color: "#222222",
        fontWeight: "normal"
      })
    }
  );
  */
  
  $('#works-thumb ul li a').hover(
    function() {
      $(this).find('img').fadeTo(200, 1.0);
    },
    function() {
      var thisclass = $(this).find('img').attr("class");
      if(thisclass == "") {
        $(this).find('img').fadeTo(250, 0.75);
      }
    }
  );
  
});


/* ****************************************************
 * SUBROUTINE
 */

// ***
function scrollTOP(selector, time) {
  var targetAnc = $(selector).closest('.services-section').attr("id");
      targetAnc = "#" + targetAnc;
    
  var targetOffset = $(targetAnc).offset().top;
  $('html, body').animate({scrollTop: targetOffset}, time);
}

// ***
function getURL() {
  var url = location.hash;
  return url;
  
}
