$.fn.reorder = function() {
 
  function randOrd() { return(Math.round(Math.random())-0.5); }
 
  return($(this).each(function() {
    var $this = $(this);
    var $children = $this.children();
    var childCount = $children.length;
 
    if (childCount > 1) {
      $children.remove();
 
      var indices = new Array();
      for (i=0;i<childCount;i++) { indices[indices.length] = i; }
      indices = indices.sort(randOrd);
      $.each(indices,function(j,k) { $this.append($children.eq(k)); });
 
    }
  }));  
}

$(document).ready(function() {
    
	//$('#feat_list').reorder();
    
  $('#feat_list a').removeClass('cur');  
  $('#feat_list a:first').addClass('cur');
    newRel = $('#feat_list a:first').attr('rel');  
	$('#callout_img a').attr({ href: '/work/'+newRel});
	$('#callout_img img').attr({ src: theme_url+'/images/slide' + newRel + '.jpg'});
  
    $('#feat_list a').click(function() {
    	animateArrow('callout_arrow',$(this));
	    $('#feat_list .cur').removeClass('cur');
	    
	    rel = $(this).attr('rel')
	    var newImgSrc = theme_url+'/images/slide' + rel + '.jpg';
	    $('#callout_img a').attr({ href: '/work/'+rel });
	    $('#callout_img img').fadeOut(350, function(){
	      $(this).attr({ src: newImgSrc }).fadeIn(350);      
	    });
	    
	    return false;
  });
  
  if($('#feat_list a').length > 0) {
    t = setTimeout("timedRotation()",12000);   
  }
 
});//doc ready


function timedRotation(){
  if($('#feat_list a.cur').hasClass('clicked')) {} else {
    if($('#feat_list a.cur').next().is('a')){
      nextBlock = $('#feat_list a.cur').next();
    } else {
      nextBlock = $('#feat_list a:first');
    }

    $('#feat_list a.cur').removeClass('cur');
    nextBlock.addClass('cur');
    animateArrow('callout_arrow', nextBlock);
   
    $('#callout_img a').attr({ href: '/work/'+nextBlock.attr('rel') });
	var newImgSrc = theme_url+'/images/slide' + nextBlock.attr('rel') + '.jpg';
    $('#callout_img img').fadeOut(350, function(){
      $(this).attr({ src: newImgSrc }).fadeIn(350);      
    });  
  
    var t2 = setTimeout("timedRotation()",12000);    
    
    $('#feat_list a.first').addClass('first clicked');
  }
}//timedRotation



function animateArrow(arrow,clickedLink){
  offset = clickedLink.offset();
  offset_col = $('.our_work').offset();
  if(arrow == 'callout_arrow') {
    minusOffset = 682;
  } else {
    minusOffset = 100;
  }
  if(offset != null) {
  goLeft = ((offset.left - offset_col.left) + minusOffset);
  $('#'+arrow).animate({ left: goLeft }, 500);  
  }
}



