$(function(){
    
    /*
     * Home page fader
     */
    if ($('#homeplayer').length > 0) {
        
        $('#homeplayer').prepend($('#homeplayerhidden').html());
        
        $('#homeplayer').cycle({
            fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
        });
    }
    
    /*
     * Galleries
     */
    if (FlashDetect.versionAtLeast(9)) {
        // do nothing
    } else {
        $('.gallerybackup').each(function(){
            var gallery = $('#' + $(this).attr('ref'));
            $(gallery).empty().addClass('htmlGallery');
            $(gallery).prepend('<div id="gallery-images"></div><div id="gallery-control"><div id="gallery-prev"></div><div id="gallery-slider"><div id="thumbs"></div></div><div id="gallery-next"></div></div>');
            $(gallery).find('#gallery-images').html($(this).html());
            $(this).remove();
            if ($('#gallery-images').length > 0) {
                $('#gallery-images') 
                .cycle({ 
                    fx:      'fade',
                    speed:  'fast', 
                    timeout: 0, 
                    pager:  '#thumbs',
                    pagerAnchorBuilder: function(idx, slide) {
                        //console.log(slide);
                        return '<li><a href="#"><img src="' + $(slide).attr('rel') + '" /></a></li>';
                    },
                    prev: $('#image-prev'),
                    next: $('#image-next')
                });
            }
        })
        $('.htmlGallery #thumbs').css('left',0);
        
        $('.htmlGallery #thumbs').css('width',($('.htmlGallery #thumbs li').length*198) + (($('.htmlGallery #thumbs li').length-1)*4) + 'px'); //set width
        $('.htmlGallery #gallery-prev').click(function(){
            if (parseInt($('.htmlGallery #thumbs').css('left')) < 0) {
                $('.htmlGallery #thumbs').css('left',parseInt($('.htmlGallery #thumbs').css('left'))+198 + 'px');
            }
            return false;
        });
        $('.htmlGallery #gallery-next').click(function(){
            if ((parseInt($('.htmlGallery #thumbs').css('left'))*-1) + (4*198) < $('.htmlGallery #thumbs').width()) {
                $('.htmlGallery #thumbs').css('left',parseInt($('.htmlGallery #thumbs').css('left'))-198 + 'px');
            }
            return false;
        });
    }
    
    
    /*
     * Pretty forms
     */
    $('form.prettyform input[@type*=radio]').css('border','none');
    $('form.prettyform input[@type*=checkbox]').css('border','none');
    if($.browser.mozilla) { prettyform(); }
    
    /*
     * LI hover fix for IE6
     */
    $('#navigation ul li').each( function(){
        $(this).mouseover( function(){ $(this).addClass('hover'); } ).mouseout( function(){ $(this).removeClass('hover'); } );
    });
    
    /*
     * Blur on focus
     */
    $('a').focus(function() { this.blur(); });
    $('input[@type*=submit]').focus(function() { this.blur(); });
    $('input[@type=text], input[@type=password]').addClass("input");
    
    $('.paper').height($('#wrapper').height());
    $('#inner').height($('#wrapper').height());
	
    // Get rid of top level gallery page title
	var ele = $('div#content h1:first');
	if (ele.html() == "Weddings" || ele.html() == "Portraits") {
		ele.remove();
	}
});


$(window).load(function(){
	$('.paper').height($('#wrapper').height());
	$('#inner').height($('#wrapper').height());
});

/*
 * Pretty forms
 */
function prettyform(){
  /*
   * Hide forms
   */
  $( 'form.prettyform' ).hide().end();
  /*
   * Label & list formatting
   */
  $( 'form.prettyform' ).find( 'label' ).not( '.noprettyform' ).each( function( i ){
    var labelContent = this.innerHTML;
    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = '';
    this.appendChild( labelSpan );
  } ).end();
  
  /*
   * Show forms
   */
  $( 'form.prettyform' ).show().end();
}
