$(document).ready(function() {
  $(".logo img").hover(function() {
    $(this).attr("src", "/img/logo_color.jpg");							
  }, function() {
    $(this).attr("src", "/img/logo_grey.jpg");
  })
})


var that;

function setContentMargin() {
  $(".main_content").css("margin-top", 0);
  var body_height = $(document).height();
  var head_height = $(".head").height();
  var footer_height = $(".footer").height();
  var diff = body_height - (head_height + footer_height);
  var content_height = $(".main_content").height();
  if (diff - content_height > 50) {
    if (content_height > 30) {
      $(".main_content").css("margin-top", (diff - content_height) / 2 - 25);
    } else {
      $(".main_content").css("margin-top", (diff - content_height) / 2 - 80);
    }
  }
}

function mCustomScrollbars() {
  $("#portfolio_container").mCustomScrollbar("horizontal",300,"easeOutCirc",1,"fixed","yes","yes",30);
}
/* function to fix the -10000 pixel limit of jquery.animate */
$.fx.prototype.cur = function() {
  if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
      return this.elem[ this.prop ];
  }
  var r = parseFloat( jQuery.css( this.elem, this.prop ) );
  return typeof r == 'undefined' ? 0 : r;
}
/* function to load new content dynamically */
function LoadNewContent(id,file) {
  $("#"+id+" .customScrollBox .content").load(file, function() {
    mCustomScrollbars();
    setContentMargin();
  });
}

$(function() {
  $("#portfolio_container a").overlay({effect: 'apple', fixed: false, mask: 'grey',
    onBeforeLoad: function() {
      that = this;
      $.ajax({
        url: "/getwork/" + this.getOverlay().attr("id") + "/",
        context: document.body,
        success: function(data){
          that.getOverlay().html(data);
        }
      });
    },
    onLoad: function() {
      show_gallery();
      $(".client_other_works_list a").click(function() {
        var that = $(this);
        $(this).closest("ul").find("a").removeClass("act");
        $(this).addClass("act");
        $.ajax({
          url: that.attr("href"),
          context: document.body,
          dataType: 'json',
          success: function(json_data) {
            if (json_data) {
              $(".work_body").html(json_data.description);
              $(".work_gallery").html(json_data.gallery);
              $(".details_title").html(json_data.title);
              show_gallery();
            }
          }
        });
        return false;
      });
      $(".close").click(function() {
        that.close();
        return false;
      });
    }
  });
});

$(window).load(function() {
  var hash = window.location.hash;
  var pattern = /^#category([\d]+)/;
  var result = pattern.exec(hash);
  if (result && result[1]) {
    ajax_url = "/getwork/category" + result[1] + "/";
    ajax_get_work(ajax_url);
  } else {
    $("#portfolio_container").fadeIn();
    mCustomScrollbars();
    setContentMargin();
  }
});
$(document).ready(function() {
  $("li.menu3 a").click(function() {
    var that = $(this);
    var ajax_url;
    window.location.replace(that.attr("href"));
    var hash = window.location.hash;
    if (hash != '#category_all') {
      var pattern = /^#category([\d]+)/;
      var result = pattern.exec(hash);
      if (result && result[1]) {
        ajax_url = "/getwork/category" + result[1] + "/";
      } 
    } 
    if (!ajax_url) {
      ajax_url = "/getwork/category/all/";
    }  
    
    ajax_get_work(ajax_url);
    return false;
  });
  $(".main_menu li").hover(function() {
    var prev_class = $(this).attr("rel");
    $(this).addClass(prev_class + "_hover");
    if ($(this).find("ul").length) {
        $(this).find("ul").slideDown();
    }
  }, function() {
    var prev_class = $(this).attr("rel");
    $(this).removeClass(prev_class + "_hover");
    if ($(this).find("ul").length) {
      $(".main_menu li").each(function() {
          $(this).find("ul").slideUp();
      });
    }
  });
  $(".client_other_works_wrapper a").live("click", function() {
    $(this).parent().next().toggle();
    return false;
  });
});

function ajax_get_work(ajax_url) {
  $.ajax({
    url: ajax_url,
    context: document.body,
    success: function(data){
      $(".portfolio_wrapper").html(data);
      $("#portfolio_container").fadeIn();
      mCustomScrollbars();
      setContentMargin();
      $("#portfolio_container a[rel]").overlay({effect: 'apple', fixed: false, mask: 'grey',
      onBeforeLoad: function() {
        that = this;
        $.ajax({
          url: "/getwork/" + this.getOverlay().attr("id") + "/",
          context: document.body,
          success: function(data){
            that.getOverlay().html(data);
          }
        });
      },
      onLoad: function() {
        //$('.infiniteCarousel').infiniteCarousel();
        $(".close").click(function() {
          that.close();
          return false;
        });
        show_gallery();
        $(".client_other_works_list a").click(function() {
          var that = $(this);
          $(this).closest("ul").find("a").removeClass("act");
          $(this).addClass("act");
          $.ajax({
            url: that.attr("href"),
            context: document.body,
            dataType: 'json',
            success: function(json_data) {
              if (json_data) {
                $(".work_body").html(json_data.description);
                $(".work_gallery").html(json_data.gallery);
                $(".details_title").html(json_data.title);
                show_gallery();
              }
            }
          });
          return false;
        });
        //jQuery('#mycarouse').jcarousel().css("visibility", "visible");
      }});
    }
  });
}

function show_gallery() {
  var galleries = $('.ad-gallery').adGallery({
    loader_image: '/img/loader.gif'
  });
}


