//---------------------------------------
// eXpansys.com - Core jQuery functions (6e)
//---------------------------------------

//start jquery
$(document).ready(function(){

//*************************
//Flyout menus
   $("#nav_primary > ul > li").hover(
      function () {
        $(".ac_results").hide(); //hide search auto suggest panel
        $(".menu", this).show(); // show the current menu
        $(this).addClass("force_z"); //workaround IE7 z-index issues 
      }, 
      function () {
        $(".menu", this).hide(); // hide the current menu
        $(this).removeClass("force_z");
      }
    );
    
    
  //flip the orientation of the gaming menu if browser width is ~1024 (otherwise we get a horizontal scroll bar)
  $("#nav_primary > ul > li:nth-child(8)").hover( // nth-child(8) = gaming
      function () {
        var body_width = $("body").width(); 
          if (body_width < 1030) {
            $(".menu", this).addClass("flip");
          }  
        }, 
        function () {
          $(".menu", this).removeClass("flip"); // always remove class on mouseout
        }     
    );
    
  //fix for menus not always hiding if you hover directly onto search bar
  $("#search_bar").hover(
    function () {
        $("#nav_primary .menu").hide(); // hide all menus
      }
  );
  

//*************************
//Search input hint text
  $("#search #query").hint(); //uses hint plugin

  
//*************************
//Search autocomplete
$("#search #query").autocomplete("/iaAjax.ashx?action=search&", {
    delay: 50,
    minChars: 3,
    cacheLength: 1,
    matchSubset: 0, //disable local cache (for now)
    width: 200,
		max: 10,
    selectFirst: false,
		scroll: false
	});
	
$("#search #query").result( //auto submit form on selection
   function () {
      $("#search").submit();
   }
);
  
//*************************
//Newsletter sign-up hint text
if ( $("#newsletter_signup").length > 0 ) {
  $("#newsletter_signup #newsletter_emailaddress").hint(); //uses hint plugin
}

//*************************
//Recently viewed image preview

if ( $("#recently_viewed").length > 0 ) { //check that recently viewed panel exists 
  $('#recently_viewed li a').imgPreview({
      containerID: 'imgPreviewWithStyles',
      //Change srcAttr to rel:
      srcAttr: 'rel',
      imgCSS: {
          height: 100 // Limit preview size:
      },
      // When container is shown:
      onShow: function(link){
          $('<span>' + link.href + '</span>').appendTo(this);
      },
      // When container hides: 
      onHide: function(link){
          $('span', this).remove();
      }
  });
}    
});

//inline pugins - put frequent used plugins inline to save on https requests

//hint source: http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
jQuery.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }

  return this.each(function () {
    // get jQuery version of 'this'
    var $input = jQuery(this),

    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = jQuery(this.form),
      $win = jQuery(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title

      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};