/* InnerFade on homepage*/
$(document).ready( function(){ 
	$('#mainImages').innerfade({ 
		animationtype: 'fade',
		speed: 4000, 
		timeout: 3000, 
		type: 'sequence', 
		containerheight: '428px' 
	}); 
}); 


//  Form Label //
$(document).ready(function() {
	$("body.resultsPage #sort .input").removeAttr('title');
	$("input:text, .textarea").labelify();
});


// Form Active //
$(document).ready(function() { 
  $('input[type="text"], textarea').addClass("defaultField"); 
  $('input[type="text"], textarea').focus(function() { 
    $(this).removeClass("defaultField").addClass("selectedField"); 
    if (this.value == this.defaultValue){ 
      this.value = ''; 
    } 
    if(this.value != this.defaultValue){ 
      this.select(); 
    } 
  }); 
  $('input[type="text"], textarea').blur(function() { 
    $(this).removeClass("selectedField").addClass("defaultField"); 
    if ($.trim(this.value == '')){ 
      this.value = (this.defaultValue ? this.defaultValue : ''); 
      } 
    }); 
}); 

/* gallery */
$(function(){
    $('#photogallery li').each(function(idx) {
        $(this).data('index', (++idx));
    });

    $('#photogallery').jcarousel({
        scroll: 1,
		vertical: true,
        initCallback: initCallbackFunction
    })
    
    function initCallbackFunction(carousel) {
        $('#img').bind('image-loaded',function() {
            var idx =  $('#photogallery li.active').data('index') - 2;
            carousel.scroll(idx);
            return false;
        });
        
        // hotkeys plugin: use arrows to control the gallery
        $(document).bind('keydown', 'down', function (evt){ $.galleria.next(); });
        $(document).bind('keydown', 'up', function (evt){ $.galleria.prev(); });
	 };

    // load and fade-in thumbnails
    $('#photogallery li img').css('opacity', 0).each(function() {    
        if (this.complete || this.readyState == 'complete') { $(this).animate({'opacity': 1}, 300) } 
        else { $(this).load(function() { $(this).animate({'opacity': 1}, 300) }); }
    });
	
    $('#photogallery').galleria({
        insert: '#img', // #img is the empty div which holds full size images
		clickNext : false, 	// Remove click to next on large image. Needs to be removed for lightbox to work.
        history: false,  // disable history plugin
        onImage: function(image, caption, thumb) { // function fired when the image is displayed
            image.hide().fadeIn(500);
            thumb.parent().fadeTo(200, 1).siblings().fadeTo(200, 0.6);
			// Lightbox
			var lightboxurl = image.attr('src'); // grab src from image 
				if(lightboxurl){ 
					image.wrap('<a href="' + lightboxurl + '" rel="lightbox"></a>'); // wrap the image in a lightbox enabled link. simples.
				}
			$('.galleria_wrapper a').lightbox();		
        },
		
        
        // function similar to onImage, but fired when thumbnail is displayed
        onThumb: function(thumb) {
            var $li = thumb.parent(),
                opacity = $li.is('.active') ? 1 : 0.6;
            
            // hover effects for list elements
            $li.hover(
                function() { $li.fadeTo(200, 1); },
                function() { $li.not('.active').fadeTo(200, opacity); }
            )
        }        
    }).find('li:first').addClass('active') // display first image when Galleria is loaded
    
    $('#img .caption').css('height', 0)
    
    $('#slideshow').hide()
    
    // this one is for Firefox, which loves to leave fields checked after page refresh
    $('#toggle-slideshow, #show-caption').removeAttr('checked')
    
    $('#show-caption').change(function(){
    	if (this.checked) {
    		$('#img .caption').stop().animate({height: 50}, 250)
    	} else {
            $('#img .caption').stop().animate({height: 0}, 250)   
        }
    })

    var slideshow,
        slideshowPause =  $('#slideshow-pause').val() 

    $('#slideshow-pause').change(function(){
        slideshowPause = this.value
        
        // clear interval when timeout is changed
        window.clearInterval(slideshow)

        // and set new interval with new timeout value
        slideshow = window.setInterval(function(){
            $.galleria.next()
        }, slideshowPause * 1000) // must be set in milisecond
    })

    $('input#toggle-slideshow').change(function(){
        if (this.checked) {
            $('#slideshow').fadeIn()
            
            // set interval when slideshow is enabled
            slideshow = window.setInterval(function(){
                $.galleria.next()
            }, slideshowPause * 1000)
        } else {
            $('#slideshow').fadeOut()
            
            // clear interval when slideshow if disabled
            window.clearInterval(slideshow)
        }
    })
});


/* flowplayer on galleries page */
$(function() {
	$("a[rel*='overlay']").overlay({
		onBeforeLoad: function() {
			this.expose();	
		},				
		onLoad: function(content) {
			this.getContent().find("a.player").flowplayer(0).load();
		},
		onClose: function(content) {
			$f().unload();
			$.expose.close();
		}
	});				
	$("a.player").flowplayer("media/swf/flowplayer-3.1.1.swf"); 
});	


