// functions

function fetchFolio(slug) {

    jQuery.ajax({
    	
  		url: "/wp-content/themes/mirrorweb4/fetchFolio.php?slug=" + slug,
  		cache: false,
  		success: function(html){
  		
  			if( $.trim(html) != '' ) {
  			
  				jQuery("#chosenwork").slideUp( 'slow' , function() { 
  					
  					jQuery('#slideshow').cycle('reset');
  					jQuery("#chosenwork").empty();
  					jQuery("#chosenwork").append(html);
  					
  					jQuery("#chosenwork").slideDown( 'slow' , function() {
  						
  						jQuery("#close-folio").fadeIn( function() {
  							
  							jQuery("#close-folio").click(function() {
								
								jQuery('#slideshow').cycle('stop');
								jQuery("#chosenwork").slideUp('slow');
								window.location.hash = "";
							
							});
  						
  						});
  					
  					});
  					
  					jQuery('#slideshow').before('<div id="close-folio"><a title="Luk">x</a></div>').before('<div id="slide-nav">').cycle({ 
  					
					    fx:     'fade', 
					    speed:  1000, 
					    timeout: 6000, 
					    pager:  '#slide-nav',
					    pause: 1
					    
					});
  				
  				});
  			
  			} else {
  			
  				jQuery("#chosenwork").slideUp( 'slow' , function() {
  					
  					
  				
  				});
  				
  			
  			}
            
  		}
  		
	});
	
}

// When document ready, let's go!

jQuery(document).ready(function() {
	
	jQuery("a.folio").hover(function() {
	
		jQuery(this).find("img:first").stop().animate({"opacity": "0"}, "fast");
		
	},function() {
	
		jQuery(this).find("img:first").stop().animate({"opacity": "1"}, "fast");
		
	});
  
  	// Bind an event to window.onhashchange that, when the hash changes, gets the
  	// hash and adds the class "selected" to any matching nav link.
  	jQuery(window).hashchange( function(){
  	
  	  	var hash = location.hash;
  	  	hash = hash.replace( '#' , '' );
  	  	
  	  	if(hash != '') {
  	  	 	  		
  	  		fetchFolio( hash );
  	  	
  	  	} else {
  	  	
  	  		jQuery("#slideshow").cycle('stop').cycle({ startingSlide: 1 });
  	  	
  	  	}
  	  	
  	  	
  	})
  	
  	jQuery(window).hashchange();
	
});


