$.fn.imagesLoaded = function(callback){
  var elems = this.filter('img'),
      len   = elems.length;
      
  elems.bind('load',function(){
      if (--len <= 0){ callback.call(elems,this); }
  }).each(function(){
     // cached images don't fire load sometimes, so we reset src.
     if (this.complete || this.complete === undefined){
        var src = this.src;
        // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
        // data uri bypasses webkit log warning (thx doug jones)
        this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
        this.src = src;
     }  
  }); 

  return this;
};


var addFoldOutMenu = function() { 
	var li = $(this), sub = li.find('.sub'), arrow = $('> .arrow', sub);

		// show sub
		li.addClass('over');
		sub.stop().show();

	// calculate position of arrow
	var arrowX = Math.floor(li.width() / 2 - 9);
	var offset = sub.position();

	if(offset)
	arrow.css({left: arrowX-offset.left});

	}

var removeFoldOutMenu = function() { $(this).removeClass('over').find('.sub').stop().hide(); }		
var conf = {interval: 0, sensitivity: 4, timeout: 100, over: addFoldOutMenu, out: removeFoldOutMenu};

// big menu fold out
$("ul#navigation").children('li').hoverIntent(conf);

$(function(){

	// new window/tab for external pages
	$('a:external').attr({target: '_blank'});

	var zoom_options = {
		zoomWidth: 330,
		zoomHeight: 355,
		xOffset: 10,
		yOffset: 0,
		position: "right"
	};

	if(jQuery().jqzoom) { $('.item-image').jqzoom(zoom_options); }


	$('.item-list').children('li').each(function() {
	
		$('section.thumb img', $(this)).imagesLoaded(function() {
	
			var img = $(this),
				container_w = 200,
				offset = 0;
			
			if(img.length) {
			
				var img_w = img.width();
				
				if(img_w > container_w) {
					
					offset = -((img_w - container_w ) / 2);
		
				} else if(img_w < container_w) {
					
					offset = (container_w - img_w) / 2;
		
				}
				
				// no 'sub-pixels' please
				offset = Math.round(offset);
				
				img.css({left: offset + 'px'});
			
			}
		
		});
							 
	});

	
	// default select
	$('input, textarea').focus(function(){ if(this.value == this.defaultValue) { this.select(); } });

	/* grow up IE */
	$("#navigation li .sub nav:last-child").css({border: 0});
	$("#quick-links .column:last-child").css({border: 0});
	
	/* gives error in IE ?
	$("#bottom #meta nav ul li:after").css({content: "|"});
	$("#bottom #meta nav ul li:last-child").css({content: ""});
	*/
	
	
	$('#sidebar nav ul li:not(:has( > a.active))').find('ul').hide();
	
	
	
	
	$(".item-list li:nth-child(3n)").css({marginRight: 0});

	if(jQuery().nivoSlider) {

		$('#slider').nivoSlider({
			effect:'sliceDown', //Specify sets like: 'fold,fade,sliceDown'
			slices:15,
			animSpeed:500, //Slide transition speed
			pauseTime:6000,
			startSlide:0, //Set starting Slide (0 index)
			directionNav:true, //Next & Prev
			directionNavHide:true, //Only show on hover
			controlNav:false, //1,2,3...
			keyboardNav:true, //Use left & right arrows
			pauseOnHover:true, //Stop animation while hovering
			manualAdvance:false, //Force manual transitions
			captionOpacity:0.8 //Universal caption opacity
    	});
	
	}
	
});
