/* IE PNG fix multiple filters */
(function ($) {
    if (!$) return;
    $.fn.extend({
        fixPNG: function(sizingMethod, forceBG) {
                if (!($.browser.msie)) return this;
                var emptyimg = "../_media/_graphics/empty.gif"; //Path to empty 1x1px GIF goes here
                sizingMethod = sizingMethod || "scale"; //sizingMethod, defaults to scale (matches image dimensions)
                this.each(function() {
                        var isImg = (forceBG) ? false : jQuery.nodeName(this, "img"),
                                imgname = (isImg) ? this.src : this.currentStyle.backgroundImage,
                                src = (isImg) ? imgname : imgname.substring(5,imgname.length-2);
                        this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + sizingMethod + "')";
                        if (isImg) this.src = emptyimg;
                        else this.style.backgroundImage = "url(" + emptyimg + ")";
                });
                return this;
        }
    });
})(jQuery);



	var element = {};
	$.fn.jshaker = function(){
		element = $(this);
		element.css('position', 'relative');
		element.find('*').each(function(i, el){
			$(el).css('position', 'relative');
		});
		
        var iFunc = function(){$.fn.jshaker.animate($(element));};
        setTimeout(iFunc, 50);
	};
	
	$.fn.jshaker.animate = function(el){
		$.fn.jshaker.shake(el);
        el.find('*').each(function(i, el){$.fn.jshaker.shake(el);});        
        var iFunc = function(){$.fn.jshaker.animate(el);};
        setTimeout(iFunc, 50);
	}
	


    var element = {};
    $.fn.jshaker = function(){
      element = $(this);
      element.css('position', 'relative');
      element.run = true;
      element.find('*').each(function(i, el){
	  $(el).css('position', 'relative');
	});
      var iFunc = function(){$.fn.jshaker.animate($(element));};
      setTimeout(iFunc, 50);
    };    
    $.fn.jshaker.animate = function(el){
      if( element.run == true ) {
	$.fn.jshaker.shake(el);
	el.find('*').each(function(i, el){$.fn.jshaker.shake(el);});        
	var iFunc = function(){$.fn.jshaker.animate(el);};
	setTimeout(iFunc, 50);
      }
    }
    $.fn.jshaker.stop = function(el) {
      element.run = false;
      element.css("top","0px");
      element.css("left","0px");
    }

	$.fn.jshaker.shake = function(el){
        var pos = $(el).position();
        if(Math.random() > 0.5){
            $(el).css('top', pos['top'] + Math.random() * 5 < 2.5 ? (Math.random() * 5 * (-1)) : Math.random() * 5);
        } else {
            $(el).css('left', pos['left'] + Math.random() * 5 < 2.5 ? (Math.random() * 5 * (-1)) : Math.random() * 5);
        }
	}

function shakeApple(appleID) {
  $("#apple" + appleID).jshaker();
}
function fallApple(fallDistance, appleID) {
  apple = $("#apple" + appleID);
  apple.jshaker.stop();
  apple.css({"top" : "0px","left" : "5px"});
  apple.animate({
    marginTop: '-=10px'
  }, 50, function() {
    $("#shadow" + appleID).fadeIn();
    $(this).animate({
    marginTop: '+=' + fallDistance
  }, 120);
  // Animation complete.
  });
}
function dropApple(fallDistance, appleID) {
  shakeApple(appleID);
  setTimeout(function(){fallApple(fallDistance,appleID)}, 1000);
}

function animateTree() {setTimeout(function(){dropApple('192','1')}, 500);
setTimeout(function(){$("#tagline1").fadeIn()}, 1500);
setTimeout(function(){dropApple('197','3')}, 2000);
setTimeout(function(){$("#tagline2").fadeIn()}, 3000);
}

$(document).ready(function(){

/*
	if($('#home ul.newslist li').length > 1) {
	
		$('#home ul.newslist').cycle({
			timeout: 10000,
			next: $('ul.newslist')
		}).css('cursor', 'default');
	}
*/
	
	/* Indicates rendering mode of the browser.
	 * BackCompat indicates the browser is operating in
	 * quirksmode. CSS1Compat indicates the browser is
	 * rendering in standards mode. */
	   
	// if($('#home').length > 0) alert(document.compatMode);
	
/*
	var active = false;
	var searchval = "Search Niles Barton";
	$('input', '#searchbox').val(searchval);
	$('#nav_search').hover(function(){
		if(!active) $(this).addClass('focused');
		active = true;
	}, function(){
		if(!active) $(this).removeClass('focused');
		active = false;
	});
	
	$('input', '#searchbox').focus(function(){
		if($(this).val() == searchval) $(this).val('').addClass('focused');
		active = true;
	}).blur(function(){
		active = false;
		if($(this).val() == '') $(this).val(searchval).removeClass('focused');
	}); 
	
	$('*').not($('#nav_search').add('#nav_search *')).click(function(){$('#nav_search').removeClass('focused');});
*/
/*
	var focused = false;
	var defaultval = 'Search Niles Barton';
	var currval = '';
	$('#searchbox').val(defaultval);

	$('#nav_search').mouseover(function(){$(this).addClass('selected');}).mouseout(function(){
		if(!focused) $(this).removeClass('selected');
	});

	$('input', '#nav_search').focus(function(){
		currval = $('#searchbox').val(); 
		if($('#searchbox').val() == defaultval) $('#searchbox').val('');
		$('#nav_search').addClass('selected');
		focused = true;
	}).blur(function(){
		if($('#searchbox').val() == '') $('#searchbox').val(defaultval);
		$('#nav_search').removeClass('selected');
	});
*/

/* Apple Tree Animation */

	// Header Search
	$("form#search input#searchfield").keypress(function(e){
		if(e.which == 13){
			searchquery = encodeURIComponent($("form#search input#searchBox").val());
			searchquery = searchquery.replace(/\'/g,'+sq+').replace(/%3F/g,'+qm+').replace(/%22/g,'');
			window.location = 'http://www.nilesbarton.com/search/' + searchquery;
		}
	});
	$("form#search input#submit").click(function(e){
			searchquery = encodeURIComponent($("form#search input#searchBox").val());
			searchquery = searchquery.replace(/\'/g,'+sq+').replace(/%3F/g,'+qm+').replace(/%22/g,'');
			window.location = 'http://www.nilesbarton.com/search/' + searchquery;
  });
});

