var $j = jQuery.noConflict();


var linkGallery = {
  lgPreview: function(el) {
    // removes all hover classes and adds to the current
    $j('.gallery-link').removeClass('hover');
    $(el).addClassName('hover');    
  },
  lgDisplay: function(el) {
    // displays the new screenshot attached to the id of the event
    if ($j(el).hasClass('selected')) {return false;};
    $j('#feature-gallery .screenshot').fadeOut('');
    var target = el.id.replace('-target','');
    $j("#"+target).fadeIn('');
    $j('.gallery-link').removeClass('selected');
    $j(el).addClass('selected');
  },
  lgDefaultize: function() {
    // drops all hover elements and returns to origional
    $j('.gallery-link').removeClass('hover');
    $j('.gallery-link.selected').addClass('hover');
  },
  init: function() {
    // console.log("init");
    $j('.gallery-link').click(function() { linkGallery.lgDisplay(this); return false; });
    $j('.gallery-link').hover(
      function() { linkGallery.lgPreview(this); return false; },
      function() { linkGallery.lgDefaultize(this); return false; }
    );
  }
}

var hcUtils = {
  centerOnElement: function(ele, target) {
    // aligns centers on another element's center
    ele = $j(ele);
    target = $j(target);
    target.css({"left":"0px"});
    ele.offLeft = ele.offset().left+(ele.width()/2);
    target.offLeft = target.offset().left+(target.width()/2);
    var diff = (ele.offLeft - target.offLeft);
    target.css({"left":diff+"px"});
  },
  getPath: function(suffix) {
    var regex = /\/$/;
    var path = location.pathname;
    path = path.match( regex ) ? path+suffix : path+"/"+suffix;
    return path;
  },
  trackEvents: function(suffix) {
    // grab the location
    var path = hcUtils.getPath(suffix);
    // test for google analytics first.     
    if (typeof pageTracker !== 'undefined'){
      pageTracker._trackPageview(path);
    };
  },
  headerActions: function() {
    // create dropdowns for category and my acount links
    if ($j('#category-hover-link')) {
      $j('#category-hover-link').hover(
        function() {
          if(this.timer) {
            clearTimeout(this.timer);
          };
          this.timer = setTimeout(function(){ 
            $j('#category-ddm-target').fadeIn('fast');
            hcUtils.centerOnElement("#category-hover-link > img","#category-ddm-target > img"); }, 420);
            hcUtils.trackEvents('category-hover-link');
        },
        function() {
          if(this.timer) {
            clearTimeout(this.timer);
          };
          this.timer = setTimeout(function(){$j('#category-ddm-target').fadeOut('fast');}, 420);
        }
      );
    };
    //  suckerfish style hover animation..   extend this to use a unique class
    $j("#user-ddm-target li").hover(
      function() {
        $j(this).addClass("hover");
      },
      function() {
        $j(this).removeClass("hover");
      }
    );
    // automatically focus on the search box.
    if ($j('#srch')) {
      $j('#srch').focus();
    };
  }, // end headerActions
  init: function() {    
    hcUtils.headerActions();
  }  // end init
};

$j(document).ready(function(){ hcUtils.init(); });
$j(document).ready(function(){ linkGallery.init() });
