jQuery(document).ready(function(){

    //
    // CUSTOM CYCLE TRANSITION
    //
    jQuery.fn.cycle.transitions.scrollVertUp = function($cont, $slides, opts) {
    	$cont.css('overflow','hidden');
    	opts.before.push(function(curr, next, opts, fwd) {
    		jQuery.fn.cycle.commonReset(curr,next,opts);
    		opts.cssBefore.top = fwd ?  (next.cycleH-1) : (1-next.cycleH) ;
    		opts.animOut.top = fwd ?  -curr.cycleH : curr.cycleH ;
    	});
    	opts.cssFirst = { top: 0 };
    	opts.cssBefore= { left: 0 };
    	opts.animIn   = { top: 0 };
    	opts.animOut  = { left: 0 };
    };




    
    // -------------------------------------------------------------------------
    // LANDING PAGE BILLBOARDS
    // -------------------------------------------------------------------------

    jQuery('.billboard .items').cycle({
		fx: 'fade' , // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		pager: '.billboard .gallery-buttons div',
		pagerAnchorBuilder: function (idx, el) {
		  return '<a class="cr-m">'+(idx+1)+'</a>';
		},
		pauseOnPagerHover: 1,
		pause: 1,
		timeout: 6000
	});


    // -------------------------------------------------------------------------
    // SITE HEADER SEARCH BOX
    // -------------------------------------------------------------------------

    jQuery('#site-header .search-box a').click(function(ev){
        ev.preventDefault();
        jQuery('#site-header .search-box a').removeClass('active');
        jQuery(this).addClass('active');
        jQuery('#site-header .search-box form').hide()
        jQuery(jQuery(this).attr('rel')).show();
    });


    // -------------------------------------------------------------------------
            
    jQuery('#site-header .search-box input.text').labelify({text: 'label', labelledClass: 'labeled'});

   // -------------------------------------------------------------------------
            
    // Firefox fix - FF doesn't vertically align the input text
    if (jQuery.browser.mozilla) {
        // this appears to be fixed in FF3.6
      //  jQuery('#site-header .search-box input.text').wrap('<div class="search-wrap"></div>');
    }


    // -------------------------------------------------------------------------
    // GLOBAL MESSAGE BOX HANDLING
    // -------------------------------------------------------------------------
            
    jQuery('.notification .close').click(function(ev){
        ev.preventDefault();
        jQuery(this).parent('.notification').fadeTo(200, 0, function() {
            jQuery(this).slideUp(400); 
        });
    });            
    
    // -------------------------------------------------------------------------

    jQuery(".notification").delay(5000).fadeTo(200, 0, function() {
        jQuery(this).slideUp(400);
	});

    		



    // -------------------------------------------------------------------------
    // SIDE NAVIGATION MENU COLLAPSING
    // -------------------------------------------------------------------------
        
    jQuery('.cms-page-view .sidebar  ul.groups > li:not(.active) ul').hide();

    jQuery('.cms-page-view .sidebar  ul.groups > li > a').click( function(ev) {
        ev.preventDefault();
        jQuery(this).parent().siblings().children('ul').slideUp();
        jQuery(this).parent().children('ul').slideDown();        
    });
     
	
	
    // -------------------------------------------------------------------------
    // POP Gallery
    // -------------------------------------------------------------------------
      
    var pop_thumbs = 
        '<div class="main-col">' +
            '<div id="pop-thumbs-wrap">' +
                '<div id="pop-thumbs" class="cr-m"></div>' +
                '<div id="pop-thumbs-ctrl"></div>'+
                '<div class="prev-next"><a href="#" id="prev"></a><a href="#" id="next"></a></div>'+
            '</div>' +
        '</div>';
      
      
    // Only add this to the source when there are contact links
    if(jQuery('#pop-gallery')) { jQuery('#pop-gallery').before(pop_thumbs); }
       
    //only show the first gallery item initially
  	jQuery('#pop-gallery div.item:gt(0)').hide();

	               	                      
    // create the gallery nav
    jQuery('#pop-gallery div.item').each(function(index) {
        // pull content from the pop-gallery div
	    var preview = jQuery(this).find('.item-preview img').attr('src');
	    var thumb=jQuery(this).find('.item-preview img').attr('src');
        var info = '<h3>'+jQuery(this).find('.item-info h2.title').text();+'</h3>';
        var promokey = '<p class="promokey">'+jQuery(this).find('p.promokey').html();+'</p>';
       
        
        //thumbs
         if(jQuery(this).find('.item-preview img.thumb').attr('src') != undefined) {  

       		 thumb=jQuery(this).find('.item-preview img.thumb').attr('src');
             
        
        };
        
	    jQuery(this).addClass('id'+index);
        jQuery("#pop-thumbs").append('<div class="thumb" id="id'+index+'"><img src="'+thumb+'" width="40px" /> '+info+promokey+'</div>');
    });
  
     
    // controls what is displayed in pop-gallery when the thumbs are clicked
    jQuery('div.thumb').click(function(){
  		var thisId = jQuery(this).attr('id');
  		jQuery(this).addClass('active');
  		jQuery(this).siblings().removeClass('active');
  		jQuery('#pop-gallery div.'+thisId).siblings().hide();
  		jQuery('#pop-gallery div.'+thisId).fadeIn('slow');
        return false;
    });
    
    
    //set hover styles for thumbs
    jQuery('div.thumb').hover( function(){
        jQuery(this).addClass('hover');
	}, function() {
        jQuery(this).removeClass('hover');
    });

    // add the pop up links to each item
    jQuery('#pop-gallery div.item').each(function(index) {
       
        var gpreview = jQuery(this).find('.item-preview img').attr('src');
        var ginfo = jQuery(this).find('.item-info h2.title').text();
        
        jQuery(this).find('.actions').prepend('<a href="'+gpreview+'"  title="'+ginfo+'" class="enlarge zoom">Click to Enlarge</a>');
    });
  
    jQuery('a.enlarge').colorbox({donkey: 'kick',opacity:"0.1",maxWidth:jQuery(window).width()-40,maxHeight:jQuery(window).height()-40, 'height': 400, 'width': 400});

    // chunk the nav items into scrollable groups
    var popNumThumbs = 5
    
    jQuery('#pop-thumbs').wrapChildren({ 
        childElem : 'div' , 
        sets: popNumThumbs, 
        wrapper: 'div class="thumb-block"'
    });   


 // Move the slide matched from the location hash
	var popIndex = 0;
	var popName  = window.location.hash.replace('#','');
    

 
	if (popName!='') 
	{
		//jQuery('#pop-gallery .item>a[name="butterfly-garden"]');
		popIndex = jQuery('#pop-gallery .item > a').index(jQuery('#pop-gallery a[name="'+popName+'"]'));
		popIndex = popIndex >= 0 ? popIndex : 0;
	}
	
	   // jQuery('p.test').text(popName+' '+popIndex+' '+popNumThumbs);

    // gallery nav cycle (does not power slideshow, only display of nav)
    jQuery('#pop-thumbs').cycle({
        fx: 'scrollVertUp',
        startingSlide: Math.floor(popIndex/popNumThumbs),
    	timeout: 0,
    	speed: 1000,	
    	prev:   '#prev', 
    	next:   '#next',
    	pager:  'div #pop-thumbs-ctrl',
    	pagerAnchorBuilder: function(idx, slide) { 
            return '<a href="#"></a>';
        }
    });
    


	//hide the prev / next buttons if only one page of 
	var numPopThumbBlocks=jQuery('#pop-thumbs .thumb-block').size();	
	if(numPopThumbBlocks==1) { jQuery('.prev-next').hide(); }
	
	

	//highlight default thumb row
  	jQuery('#pop-thumbs div.thumb:eq(0)').addClass('active');	


   jQuery('#pop-thumbs div.thumb:eq('+popIndex+')').click();

	
   jQuery(".pop-video").colorbox({iframe:true,opacity:"0.1", innerWidth:640, innerHeight:385});
   
   
   
    // -------------------------------------------------------------------------
    // Calendar Popup
    // -------------------------------------------------------------------------
    
 jQuery(".pop-calendar").colorbox({width:"600px",height:"550px",opacity:"0.1",inline:true, href:"#calendar"});
  

    // -------------------------------------------------------------------------
    // MY ACCOUNT HOVER MENU
    // -------------------------------------------------------------------------  
  
   jQuery("a.my-account").parents("li").hover(
 	 function () {
    	
    	jQuery(this).find("ul").show();
  	}, 
 	 function () {
     
     	jQuery(this).find("ul").hide();
    }
);



    // -------------------------------------------------------------------------
    // IMAGE LIBRARY
    // -------------------------------------------------------------------------  

// results: view tabs

     jQuery('#view-thumb').click(function(){	
  		jQuery(this).addClass('active');
  		jQuery(this).siblings('a').removeClass('active'); 	
  		jQuery('.view-list').hide();
  		jQuery('.view-thumb').show();	
        return false;
    });
    
    
 jQuery('#view-list').click(function(){	
  		jQuery(this).addClass('active');
  		jQuery(this).siblings('a').removeClass('active'); 
  		jQuery('.view-thumb').hide();
  		jQuery('.view-list').show();		
        return false;
    });
    
 


     
}); // end document.ready

