/*
 * General Script Functions
 *
 * uses jQuery 1.4+
 */
jQuery(document).ready( function($) {
   
  /*
   * External links
   *
   * For HTML compatibility add rel="external" to your external links instead of target="_blank"
   */
  $('a[rel~=external]').attr('target', 'blank');
  
  /*
   * Header search term replacement
   */
  var header_term = $('input[name=query]');
  var header_term_replace = 'Search Atmail...';
  if (header_term.val() == '')
    header_term.val(header_term_replace);

  header_term.focus(function () {
    if ($(this).val() == header_term_replace)
      $(this).val('');
  });
  header_term.blur(function () {
    if ($(this).val() == '')
      $(this).val(header_term_replace);
  });
  
  /*
   * Footer newsletter text replacement
   */
  var footer_text = $('input.searchinput');
  var footer_text_replace = 'email';
  if (footer_text.val() == '')
    footer_text.val(footer_text_replace);

  footer_text.focus(function () {
    if ($(this).val() == footer_text_replace)
      $(this).val('');
  });
  footer_text.blur(function () {
    if ($(this).val() == '')
      $(this).val(footer_text_replace);
  });
  
  /*
   * Navigation
   */
   $('li:has(.child)').hoverIntent({
    sensitivity: 2,
    interval: 100,
    over: showNav,
    timeout: 200,
    out: hideNav
  });
  function showNav() {
    $(this).addClass('child-hover').find('ul').fadeIn('fast');
  }
  function hideNav() {
    $(this).removeClass('child-hover').find('ul').fadeOut('fast');
  }
  $('.parent > li').click(function () {
    $(this).addClass('active');
  });
  
  /*
   * Thumbnail lens
   */
  $('ul.screen-shots li a').hover(
    function () {
      $(this).append($('<span>&nbsp;</span>'));
      $(this).find("span:last").fadeIn();
    }, 
    function () {
      $(this).find("span:last").remove();
    }
  );
  
  /*
   * Sidebar search text replacement
   */
  var sidebar_text = $('#s');
  var sidebar_text_replace = 'Search Our Blog...';
  if (sidebar_text.val() == '')
    sidebar_text.val(sidebar_text_replace);

  sidebar_text.focus(function () {
    if ($(this).val() == sidebar_text_replace)
      $(this).val('');
  });
  sidebar_text.blur(function () {
    if ($(this).val() == '')
      $(this).val(sidebar_text_replace);
  });
  
  /*
   * Sidebar List
   */
  $('.sidebar-list li.active').append($('<span>&nbsp;</span>'));
  $('.sidebar-list li.active').prev().find('a').css({backgroundImage:"none"});
  
  /*
   * Sidebar Show/Hide archives
   */
  $("ul.archives").find("li:gt(11)").hide(); 
	
	$("ul.archives").has("li:nth-child(12)").prev('h4').append('<a href="javascript:void(0)" class="showhide">More &darr;</a>'); 
	
  $("a.showhide").live("click", function(){
    var regex = /^More/;
    (regex.test($(this).text())) ? $(this).html('Less &uarr;') : $(this).html('More &darr;');
    $("ul.archives").find("li:gt(11)").toggle("fast");
  });
  
  /*
   * Fancybox
   */
  $("a.fancybox").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	200, 
		'speedOut'		:	200, 
		'overlayShow'	:	false
	});
	
});

/**
* hoverIntent
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);
