$(document).ready(function(){
    
    
    
    
    // jCarousel for gallery thumbs
        $('#mycarousel').jcarousel({
            easing: 'easeOutExpo',
            animation: 800,
            buttonNextHTML : null,
            buttonPrevHTML : null,
            itemFallbackDimension: 86
        });
        
    //    resizeGallery();

        /* resize the gallery initially on load and send to resize function */
        var pageWidth = $(window).width();
        resize(pageWidth);
        

        
        /* resize gallery on window size change */
        $(window).resize(function() {
            var pageWidth = $(window).width();
            
            resize(pageWidth);
            resetValue();
            
        });
        
        
        /*-----------------------------------------------------------------------
          PULL CATEGORIES ON PHOTOGRAPHER HOVER
        ----------------------------------------------------------------------- */
            /* set initial list of categories */
            categories = $('#categories-list ul').html();
            $('#categories-list').find('ul').html(categories);
          
            
            
            $('#photographers ul').find('a').mouseenter(function(){
                   

                   
                   $('#categories-list').find('ul').empty();
                   $('#categories-list').find('ul').append('<li></li>');
                   
                    $('#categories-list').find('li').clearQueue();
                    
                    var photographer = $(this).attr('rel');
                
                        $.ajax({
                		  url: "/_partials/pull_categories"+ '?v=' + Math.random()*Math.random() + '&photographer=' + photographer,
                		  success: function(p){

                		     $('#categories-list').find('li').animate({opacity: 0}, 200).queue(function(){
                		         
                		         $(this).css({'display': 'none'});
            		          
                             });

                             $('#categories-list').find('ul').prepend(p).show(400);
                        

        		          
            		  

                          }  // ajax
            		  
            		 });
        		
        		 }).mouseleave(function(){
                     
                      $('#categories-list').find('li').delay(1500).animate({opacity: 0}, 200).queue(function(){
         		          $(this).css({'display': 'none'});
         		          $('#categories-list').find('ul').html(categories);
                      });
                      
                    
                
            });
            
            // when hovering over the list of categories
            $('#categories-list').find('li').live('mouseenter mouseleave', function(event){
                if ( event.type == "mouseenter" ) {$('#categories-list').find('li').clearQueue();} else {  
                    $('#categories-list').find('li').delay(1500).animate({opacity: 0}, 200).queue(function(){
         		          $(this).css({'display': 'none'});
         		          $('#categories-list').find('ul').html(categories);
                      });
                }
            });

        
        /*-----------------------------------------------------------------------
          REPLACE GALLERY IMAGE ON SELECTION
        ----------------------------------------------------------------------- */
        
        /* load initial image */
        var image_id = $('#gallery-thumbs li').first().find('a').attr('rel');
        var video_id = $('.motion #gallery-thumbs li').first().find('a').attr('rel');
        
        /* only run if not motion section */
        if ( !$('html').hasClass('motion') ){pullLargeImage(image_id);}    
        pullVideo(video_id);

        
        
        /* load image on thumb click */        
        $('#gallery-thumbs a').click(function(event){
            if ( !$('html').hasClass('motion') ){
                event.preventDefault();
                $('#gallery-image div img').clearQueue();
            
                var image_id = $(this).attr('rel');
            
                pullLargeImage(image_id);
                
            }    
            
        });
        
         /* load video on thumb click */
            $('.motion #gallery-thumbs a').click(function(event){
                
                event.preventDefault();
                $('.motion #gallery-image div article').clearQueue();
                video_id = $(this).attr('rel');
                pullVideo(video_id);
                
            });
  
            

        /*-----------------------------------------------------------------------
          jQUERY SLIDER
        ----------------------------------------------------------------------- */
    /* add line behind slider */
    $('#gallery-navigation').append("<div id='sliderLine'><span id='leftPlus'>+</span><span id='rightPlus'>+</span></div>");
        
    $(function() {
    		//scrollpane parts
    		var scrollPane = $( "#gallery-thumbs" ),
    			scrollContent = $( "#gallery-thumbs ul" );

    		//build slider
    		var scrollbar = $( "#slider" ).slider({
    			slide: function( event, ui ) {
    				if ( scrollContent.width() > scrollPane.width() ) {
    					scrollContent.css( "margin-left", Math.round(
    						ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
    					) + "px" );
    				} else {
    					scrollContent.css( "margin-left", 0 );
    				}
    			}
    		});
            

            //reset slider value based on scroll content position
            		function resetValue() {
            			var remainder = scrollPane.width() - scrollContent.width();
            			var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
            				parseInt( scrollContent.css( "margin-left" ) );
            			var percentage = Math.round( leftVal / remainder * 100 );
            			scrollbar.slider( "value", percentage );
            		}
    	
    	});
    	
    	/*-----------------------------------------------------------------------
    	  SEARCH BOX
    	----------------------------------------------------------------------- */
    	$('#search input').val("Search using tags/keywords").css({color: '#dddddd'});
    	$(function(){
        		$('#search input')
        			.bind('click',function(){
        				$(this).val('');
        				$(this).css({color: '#70bdd3'});
        			})
        			.bind('blur',function(){
        				var a = $(this);
        				if(a.val() == ''){
        					a.val('Search using tags/keywords');
        					$(this).css({color: '#dddddd'});
        				}
        		});
        	});


        /*-----------------------------------------------------------------------
          BLOG SCRIPTS
        ----------------------------------------------------------------------- */
            /* remove img style, width, and height attr if present on view page */
            $('.blog.view #main').find('img').removeAttr('class').removeAttr('width').removeAttr('height');
            // ensure first image has margin-top
            $('.blog.view img:first').css({'margin-top': '20px'});


        /*-----------------------------------------------------------------------
          INITIALIZE CINEMAGRAPH
        ----------------------------------------------------------------------- */    
        /* get framerate from rel attr */
        $(window).load(
            function() {
                
                Flipbook.frameRate = 8;

                Flipbook.init();
            }
        );
        
       
            
            
// end dom ready
});

