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

var galleries = function() {

    var product_config = {
		numThumbs:                 9,
		preloadAhead:              1,
		enableTopPager:            false,
		enableBottomPager:         true,
		imageContainerSel:         '#slideshow',
		controlsContainerSel:      '#controls',
		captionContainerSel:       '.gallerific-caption',
		loadingContainerSel:       '#loading',
		renderSSControls:          true,
		renderNavControls:         true,
		playLinkText:              'Play Slideshow',
		pauseLinkText:             'Pause Slideshow',
		prevLinkText:              'Previous',
		nextLinkText:              'Next',
		nextPageLinkText:          'Next',
		prevPageLinkText:          'Previous',
		enableHistory:             true,
		enableKeyboardNavigation:  false
        };
    
    var gallery_config = {
		numThumbs:                 8,
		preloadAhead:              1,
		enableTopPager:            false,
		enableBottomPager:         true,
		imageContainerSel:         '#slideshow',
		controlsContainerSel:      '#controls',
		captionContainerSel:       '.gallerific-caption',
		loadingContainerSel:       '#loading',
		renderSSControls:          false,
		renderNavControls:         false,
		playLinkText:              'Play Slideshow',
		pauseLinkText:             'Pause Slideshow',
		prevLinkText:              'Previous',
		nextLinkText:              'Next',
		nextPageLinkText:          'Next',
		prevPageLinkText:          'Previous',
		enableHistory:             true,
		enableKeyboardNavigation:  false
        };
        
        var product_thumbs = $('#products #thumbs');
        var gallery_thumbs = $('#gallery #thumbs');
        
        
        if( $(product_thumbs).length > 0 ) {
            var gallery = $(product_thumbs).galleriffic(product_config);
        } else if( $(gallery_thumbs).length > 0 ) {
            var gallery = $(gallery_thumbs).galleriffic(gallery_config);
        } else { 
            return;
        }
        
		$('.gallerific-caption a.active-left').live('click', function(e) {
			gallery.previous();
			e.preventDefault();
		});

		$('.gallerific-caption a.active-right').live('click', function(e) {
			gallery.next();
			e.preventDefault();
		});
		
		$('a#play').live('click', function(e) {
		  gallery.toggleSlideshow();
		  
		  if(gallery.isSlideshowRunning) {
		      $(this).text('Pause Slideshow');
		  } else {
		      $(this).text('Play Slideshow');
		  }
		  e.preventDefault();
		});

				/****************************************************************************************/

				/**** Functions to support integration of galleriffic with the jquery.history plugin ****/

				// PageLoad function
				// This function is called when:
				// 1. after calling $.historyInit();
				// 2. after calling $.historyLoad();
				// 3. after pushing "Go Back" button of a browser
				function pageload(hash) {
					// alert("pageload: " + hash);
					// hash doesn't contain the first # character.
					if(hash) {
						$.galleriffic.gotoImage(hash);
					} else {
						gallery.gotoIndex(0);
					}
				}

				// Initialize history plugin.
				// The callback is called at once by present location.hash. 
				$.historyInit(pageload, "advanced.html");

				// set onlick event for buttons using the jQuery 1.3 live method
				$("a[rel='history']").live('click', function(e) {
					if (e.button != 0) return true;

					var hash = this.href;
					hash = hash.replace(/^.*#/, '');

					// moves to a new page. 
					// pageload is called at once. 
					// hash don't contain "#", "?"
					$.historyLoad(hash);

					return false;
				});

}

var search_labelify = function() {

    $('input.search').labelify();
}

var home_galleries = function() {
    
    if( $('.feature-images').length == 0 ) {
        return;
    }
    
    var slide_bit_animation = function(currSlideElement, nextSlideElement, options, forwardFlag) {
	    var selected = {}
	        selected.id = '#' + $(this).attr('rel');
	        selected.$image   = $(selected.id);

	    $('li.slide').removeClass('active').animate({ width: '30px'}, { queue: false, speed: 400} );
	    $(selected.id).animate({ width: '295px' }, { queue: false, speed: 400} );
	    $(selected.id).addClass('active');

    };
    
    $('.feature-images').cycle({
		fx:      'fade',
		timeout: 18000,
		before:  slide_bit_animation
	});
	
    $('li.slide em').click(function() {
        var parent  = $(this).parents('li:first');
        var index   = $('#slides li').index(parent);
        
        $('.feature-images').cycle(index);
    });
}

var rfp_colorbox = function() {
    $('a.rfp-link').colorbox({
        iframe: true,
        height: "80%",
        width: "80%"
    });

}

    search_labelify();
    home_galleries();
    galleries();
    rfp_colorbox();

});
