window.innerShiv = (function() {
	var d, r;
	
	return function(h, u) {
		if (!d) {
			d = document.createElement('div');
			r = document.createDocumentFragment();
			/*@cc_on d.style.display = 'none';@*/
		}
		
		var e = d.cloneNode(true);
		/*@cc_on document.body.appendChild(e);@*/
		e.innerHTML = h;
		/*@cc_on document.body.removeChild(e);@*/
		
		if (u === false) return e.childNodes;
		
		var f = r.cloneNode(true), i = e.childNodes.length;
		while (i--) f.appendChild(e.firstChild);
		
		return f;
	}
}());

var workSlideshow = {
	getSlidesHTML: 	function() {
 		var $figure = $('#content > figure:first');
		var $figure_nav = $figure.find('nav.figures li');
		var number_of_slides = $figure_nav.length;
		var client_path = $figure_nav.find('a').get(0).pathname.replace(/(^.*)\/\d+\//, "$1/");
		var slides_html = '<div id="ajax-wrapper">';
		
		for (var i = 1; i <= number_of_slides; i++) {
			slides_html += '<figure><img alt="" height="400" src="' + client_path + i + '/images/figure.jpg" width="870" />';
			slides_html += '<nav class="sequential"><ul>';
			if (i > 1) {
				slides_html += '<li class="previous"><a href="' + client_path + (i - 1) + '/" title="Previous">Previous</a></li>';
			}
			if (i < number_of_slides) {
				slides_html+= '<li class="next"><a href="' + client_path + (i + 1) + '/" title="Next">Next</a></li>';
			}	
			slides_html+= '</ul></nav>';	
			slides_html+= '<nav class="figures"><ol>';
			for (var j = 1; j <= number_of_slides; j++) {
				if (i == j) {
					slides_html+= '<li>' + j + '</li>';
				}
				else {
					slides_html += '<li><a href="' + client_path + j + '/">' + j + '</a></li>';
				}
			}
			slides_html += '</ol></nav>';
			slides_html += '</figure>';
		}
		slides_html += '</div>';
		return slides_html;
	},
	createSlideWrapper: function() {
		return $(innerShiv(workSlideshow.getSlidesHTML(), false));						 
	},
	massageSlideWrapper: function($slidewrapper) {
		/* Cache some jQuery objects */
    	var $slides = $slidewrapper.find('> figure');
	    var $nav = $slidewrapper.find('nav a');

		/* Add helpful IDs to figures */ 
      	$slides.each(function(i){
        	this.id='slide-' + ++i;										  									  
     	});
	
		/* Replace URLs with helpful hashes to links that correspond to figure IDs */
      	$nav.each(function(){
        	var index = this.pathname.lastIndexOf('/') - 1,
	        	hash = '#slide-' + this.pathname.substring(index++, index);
	    	this.href = hash;		  
      	});

	/* Make all slides transparent, set their z-index to a small enough number, and hide them */
		$slides
	   	 .css({opacity:0,zIndex:10})
	   	 .hide();
	},
	replaceFigure: function($slidewrapper) {
		  $slidewrapper.replaceAll('#content > figure:first');	
	},
	initHashChange: function($slides) {
	/* Here is where the magic happens */
		$(window).bind('hashchange', function () {

	/* Need to add figure out which is the deafult slide */
			var hash = window.location.hash,
        		re = /#slide\-\d/;	
	    	if (!(hash.length && re.test(hash))) {
	    		var index = window.location.pathname.lastIndexOf('/') - 1,
            		slide = window.location.pathname.substring(index++, index);  
	    			hash = '#slide-' + (isNaN(slide) ? '1' : slide);		 
	    	}  

	/* Hide all of the slide */
			$slides
	     	  .stop()
	     	  .css({zIndex:10})
	     	  .animate({opacity:0}, 500, function(){ $(this).hide() });

	/* Display the selected slide */
        	$slides
	     	 .filter(hash)
	     	 .stop()
	     	 .show()
	     	 .css({zIndex:1000})
	     	 .animate({opacity:1}, 500);

	/* Prevent the default click behavior. */
			return false;
		});	

  	// Since the event is only triggered when the hash changes, we need
  	// to trigger the event now, to handle the hash the page may have
  	// loaded with.
		$(window).trigger( "hashchange" );
	 },
	 init: function() {
		 var $slidewrapper = workSlideshow.createSlideWrapper();
		 workSlideshow.massageSlideWrapper($slidewrapper);
		 workSlideshow.replaceFigure($slidewrapper);
		 workSlideshow.initHashChange($slidewrapper.find('> figure')); 
	 }
}

jQuery(document).ready(function() {
  (function($) {

    var $body = $('body');


    if ($body.is('.work')) {	
     	if ($body.is('#work')) { }
	 	else if (!$.browser.msie) {
			workSlideshow.init();
		}
	}
  



/* Hover Effects */

 /* Global Navigation */
    $('#nav-global ul li a').each(function() {
	  var $this = $(this),
	      color = $this.css('color');
	  $this
	   .css({color:color})
	   .hover(function() {
		 $this.stop().animate({color:'#fff'}, 500);
        },
	    function() {
  		 $this.stop().animate({color:color}, 500);
        }
	  );
     });

 /* Home */

    if ($body.is('#home')) {	
     $('#featured article a')
	  .css({backgroundPosition:'top left'})	
	  .each(function() {
	    var $this = $(this),
	        $span = $('<span class="hover" />').css({opacity:0}).appendTo(this),
	        $headline = $this.find('h1'),
	        color = {
		    'anchor': $this.css('color'),
		    'headline': $headline.css('color')
		    };
	    $headline.css({color:color.headline});
	    $this
	     .css({color:color.anchor})
	     .hover(
          function() {
		   $this.stop().animate({color:'#ccc'}, 500); /* Go to #fff? */
		   $headline.stop().animate({color:'#fff'}, 500);
		   $span.stop().animate({opacity:1}, 500); 	
          },
	      function() {
  		   $this.stop().animate({color:color.anchor}, 500);
		   $headline.stop().animate({color:color.headline}, 500);
		   $span.stop().animate({opacity:0}, 500); 	
          }
	     );
	  });
    }

/* News */
    if ($body.is('.news') || $body.is('.articles')) {
     if ($body.is('#news') || $body.is('#articles')) {	
      $('article a').each(function() {
	    var $this = $(this),
		    $header = $this.find('header'),
			$time = $header.find('time'),
		    $figure = $this.find('figure'),
			$img = $figure.find('img'),
	        color = {
		    'anchor': $this.css('color'),
		    'headline': $header.css('color'),
			'time': $time.css('color')
		    };
        $header.css({color:color.headline});
		$time.css({color:color.time});
		$figure.css({backgroundImage:'url(' + $img.attr('src') + ')'});
	    $img.css({left:'auto', opacity:0, right:0});
	    $this
	     .css({color:color.anchor})
	     .hover(
          function() {
		   $this.stop().animate({color:'#333'}, 500);
		   $header.stop().animate({color:'#000'}, 500);
		   $time.stop().animate({color:'#e50'}, 500);
		   $img.stop().animate({opacity:1}, 500); 	
          },
	      function() {
		   $this.stop().animate({color:color.anchor}, 500);
		   $header.stop().animate({color:color.headline}, 500);
		   $time.stop().animate({color:color.time}, 500);
		   $img.stop().animate({opacity:0}, 500); 
          }
	     );			 
					  
	  });

     }
     else {
		 
     }
    }
	
/* Work */

    if ($body.is('.work')) {
     if ($body.is('#work')) {	
      $('nav.work a')
       .css({backgroundPosition:'bottom center'})	 
	   .each(function() {
	     var $this = $(this),
	         $span = $('<span class="hover" />').css({opacity:0}).appendTo(this),	   
	         color = $this.css('color');
	     $this
	      .css({color:color})	   
	      .hover(
           function() {
		    $this.stop().animate({color:'#000'}, 500);
            $span.stop().animate({opacity:1}, 500); 		 
           },
	       function() {
  		    $this.stop().animate({color:color}, 500);
            $span.stop().animate({opacity:0}, 500); 
           }
	      );
	   });	
     }
     else {
      $('nav.work a')
       .css({backgroundPosition:'bottom left'})
	   .each(function() {
	     var $this = $(this),
	          $span = $('<span class="hover" />').css({opacity:0}).appendTo(this),
	          color = $this.css('color');
         $this
	      .css({color:color})
	      .hover(
           function() {
		    $this.stop().animate({color:'#000'}, 500);
            $span.stop().animate({opacity:1}, 500); 
           },
	       function() {
  		    $this.stop().animate({color:color}, 500);
            $span.stop().animate({opacity:0}, 500); 
           }
	      );
	   });	


if (!$.browser.msie) {
     $('#content')
	  .find('nav.sequential li a')
	   .animate({opacity:0.5}, 0)
	   .end()
      .delegate('nav.sequential a', 'mouseenter', function() {
        $(this).stop().animate({opacity:1}, 500); 
      })
      .delegate('nav.sequential a', 'mouseleave', function() {
        $(this).stop().animate({opacity:0.5}, 500);  
      });						
}

     }
    }



/*
    if ($body.is('#news')) {	
	
var group = 8;	 
var $articles = $('#content article');
var total = $articles.size();
var groups = Math.ceil(total/group);
var list = [];
list[0] = '<nav class="pagination"><ul>'; 
var i = 1;
$articles.hide().slice(0,8).show();	 
for (x=1; x <= groups; x++) {
 list[i++] = '<li><a href="#page-';
 list[i++] = x;
 list[i++] = '">';
 list[i++] = x;
 list[i++] = '</a></li>'; 
}
list[i++] = '</ul></nav>';
$('#content').prepend(list.join(''));
	 
	 
	 
	}

*/



  })(jQuery);
});
