//iphone redirect
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
  location.href='http://i.zion.com';
}

$.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)); });
      $("#feat_list a").show();
    }
  }));  
}


$(document).ready(function() {
	
  //check if safari - if so, add a body class to change base font size
  if ($.browser.safari) {
   $('body').addClass('safari');
  }
    
  //homepage feature
  //-----------------
  
  $('#feat_list .online').reorder();
  $('#feat_list .online a:gt(1)').remove();

  $('#feat_list .mature').reorder();
  $('#feat_list .mature a:gt(1)').remove();
  
  //set first visible item after reorder
  $('#feat_list .online a').removeClass('cur');  
  $('#feat_list .online a:first').addClass('cur');
  newRel = $('#feat_list .online a:first').attr('rel');  
  $('#callout_img a').attr({ href: '/work/'+newRel});
  $('#callout_img img').attr({ src: '/images/zion/work/'+newRel+'_home.jpg'});
  $('#callout_desc span').hide();
  $('#callout_desc span.'+newRel).fadeIn(500);
    
  
  $('#feat_list a').click(function() {
    animateArrow('callout_arrow',$(this));
    $('#feat_list .cur').removeClass('cur');
    $(this).addClass('cur clicked');
    
    rel = $(this).attr('rel')
    var newImgSrc = '/images/zion/work/' + rel + '_home.jpg';
    $('#callout_img a').attr({ href: '/work/'+rel });
    $('#callout_desc span:visible').fadeOut(350, function(){
      $('#callout_desc span.'+rel).fadeIn(350);      
    });    
    $('#callout_img img').fadeOut(350, function(){
      $(this).attr({ src: newImgSrc }).fadeIn(350);      
    });
    
    return false;
  });
  
  if($('#feat_list a').length > 0) {
    t = setTimeout("timedRotation()",4000);   
  }
  
  //end homepage feat
  //------------------  
  


  //homepage blogs
  //-----------------
  var recent_blog = $("#blog_selector").attr("class");
  var base_height = $("#blogs").children('#' + recent_blog).height();
  
  $("#blogs div").not('#' + recent_blog).hide();
  $("#blogs").children('#' + recent_blog);
  $("#blogs").css("height", base_height);
  $("#four_descriptions").children("." + recent_blog).addClass('current');
  $('#four_descriptions div').hover(
      function () {
	
		if($(this).hasClass("current") == false){
	
	  		var hover_blog = $(this).attr("class");

  			$("#blogs").stop();	
	  		$("#blogs div:visible").css('position','absolute').hide() 
			$("#four_descriptions div.current").not('.' + hover_blog).removeClass("current");

			$('#' + hover_blog).css({
				position: "absolute",
				left: "-10000px",
				height: "auto",
				display: "block"
				}).show(function() {
					
				 	var blog_height = $('#' + hover_blog).height();
			        if(blog_height == 0){
						var blog_height = 400;
					}
					$("#blogs").animate({height: "" + blog_height + "px"}, 650);	
					$('#' + hover_blog).css({left:"0px"});							
				});

				$('#' + hover_blog + " .posts").hide().fadeIn(650);
				$('#' + hover_blog + " .posts li").hide().fadeIn(650);
				$('#' + hover_blog + " .comments").hide().fadeIn(650);
			$(this).addClass("current");
		}

      }, 
      function () { }
    );
  
  //homepage blogs
  //------------------  



  
  //photo switching
  //----------------

  $('#photo_thumbs a').click(function() {
    
    if($(this).is(':not(.cur)')) {      

      newImg = $(this).attr('href');
      $('#photo_thumbs .cur').removeClass('cur');
      $(this).addClass('cur');
      
      $('#photo_img img').fadeOut(350, function(){
        $('#photo_img').css({ height: $("#photo_img img").height() });
        animateArrow('feat_arrow',$('#photo_thumbs .cur'));         
        $('#loading').show();
				$newImgObj = $('#photo_img img').attr({ src: newImg });
				
				if ($newImgObj.data('loaded') == true) {
					shiftAndSize();
				} else {
					$newImgObj.bind('load',function(e){
					  $(e.target).data('loaded',true).unbind('load');
						shiftAndSize();
					});
				}
				
				function shiftAndSize() {
	        $newImgObj.css({ margin: "0", visibility: "hidden" }).show();          
	        $('#photo_img').animate({ height: $("#photo_img img").height() }, 350, function(){
	          $("#loading").hide();
	          $('#photo_img img').css({ visibility: "visible", display: "none" }).fadeIn(350);       
	        });
				}
				
      });

    }//if

    return false;
    
  });//photo switching
  //------------------
  
  




  //blog top posts
  //----------------
	$("div#top_post_wrap a:first").addClass('selected');
	$("div#top_post_wrap ul").hide();
	$("div#top_post_wrap ul:first").show().addClass('selected');	
	$("div#top_post_wrap div.selector a").click(function(){
		var thisTarget = $(this).attr('href');
		$('div#top_post_wrap ul.selected').removeClass('selected').hide();
		$(thisTarget).show().addClass('selected');
		$('div#top_post_wrap a.selected').removeClass('selected');
		$(this).addClass('selected');		
		return false; 
	});	//blog top posts
	//------------------


  //blog contact form
  //----------------
  $("#contact-note .buttons .alert").hide();
  $("#contact-note .buttons").append("<input type='submit' name='button' id='sendit' value='Submit' />")
	//blog contact form
	//------------------


  //work re-org
  //----------------
  $('#work_wrap a').click(function() {
    
    if($(this).hasClass('selected')) {} else {
    
      $('.selected').removeClass('selected');
      $(this).addClass('selected');
    
      $('#work_groups li a.'+$(this).attr('rel')).parent().each(function() {
        $(this).addClass('remove');
        $('#work_groups ul').prepend('<li class="selected" style="display: none;">'+$(this).html()+'</li>');     
      });
      
      //ajax call to find category desc from EE db
      //$.post("/js/functions.php", { category: $(this).attr('rel') }, function(data){
      //  $('#work_paper').html(data);        
      //});   
      $('#work_paper').html('<p>' + $(this).next().html() + '</p>');
      
      $('.remove').remove();
      $('#work_groups ul li.selected').fadeIn(450);
      
          
    }//if
    
    return false;
  }); //work re-org
  //------------------
  
  
  //ie hover issue fix - BOO IE... BOO
  if($.browser.msie) {
    $('#employees li a').hover(function() {
      $(this).addClass('hover');
    }, function() {
      $(this).removeClass('hover');
    });
  }
  
  //preload a few select images into the DOM for fading & resizing
  var images = new Array;
  i = 0;
  $('#photo_thumbs a').each(function() {
    images[i] = new Image();
    images[i].src = $(this).attr('href');
    i++;
  });
  $('#feat_list a').each(function() {
    images[i] = new Image();
    images[i].src = '/images/zion/work/' + $(this).attr('rel') + '_home.jpg';
    i++;
  });  
  
  
  
});//doc ready



function timedRotation(){
  if($('#feat_list a').hasClass('clicked')) {} else {
    
    if($('#feat_list a.cur').next().is('a')){
      nextBlock = $('#feat_list a.cur').next();
    } else if($('#feat_list a.cur').parent().hasClass('online') && $('#feat_list a.cur').is(":last-child")){
      nextBlock = $('#feat_list a.cur').parent().next().next().find("a:first-child");
    } else {
      nextBlock = $('#feat_list .online a:first');
    }

    $('#feat_list a.cur').removeClass('cur');
    nextBlock.addClass('cur');
    animateArrow('callout_arrow', nextBlock);
  
  $('#callout_img a').attr({ href: '/work/'+nextBlock.attr('rel') });
    $('#callout_desc span:visible').fadeOut(350, function(){
      $('#callout_desc span.'+nextBlock.attr('rel')).fadeIn(350);      
    });
    var newImgSrc = '/images/zion/work/' + nextBlock.attr('rel') + '_home.jpg';
    $('#callout_img img').fadeOut(350, function(){
      $(this).attr({ src: newImgSrc }).fadeIn(350);      
    });  
  
    var t2 = setTimeout("timedRotation()",4000);    
  }
}//timedRotation



function animateArrow(arrow,clickedLink){
  offset = clickedLink.offset();
  offset_col = $('.center_col').offset();
  if(arrow == 'callout_arrow') {
    minusOffset = 252;
  } else {
    minusOffset = 620;
  }
  goLeft = ((offset.left - offset_col.left) - minusOffset);
  $('#'+arrow).animate({ left: goLeft }, 500);  
}






