// Sloppy JS. Needs some attention.

$(document).ready(function() {
    stopper();
    defaultInputs('input');
    
    // Add click events
    $('#nope').click(notifyReveal);
    $('#press-reveal').click(pressReveal);
    $('.reveal').mouseenter(homeReveal);
    
    $('#buy').click(function () {
        $('#form-purchase').submit();
    } );
    
    $('#notify-submit').click(function () {
        $('#notify-me').submit();
    } );
    
    $('#select-colours').change(function(){
        var optionSelectedValue = $('#select-colours option:selected').val();
        top.location.href = optionSelectedValue;
    });
});


// Show the notify me chick.
// =============================================================================
function notifyReveal() {
    $('#nope').fadeOut(100, function () {
        $('#notify').fadeIn(100);
    });
    
    return false;
}

// Show the press pass.
// =============================================================================
function pressReveal() {
    $('#press-login').fadeIn(100);
    return false;
}

// Show the product names.
// =============================================================================
function homeReveal() {
    $('.smallname').delay(1000).fadeIn(300);
    return false;
}

// Clear the text in input elements on click
// =============================================================================
// add .input class to input to enable
function defaultInputs(className) {
	$('input.' + className).each( function () {
		$(this).bind('focus', function () {
			$this = $(this);
			if($this.val() == this.defaultValue) {
				$this.val(''); 
			}
		});
		$(this).bind('blur', function () {
			$this = $(this);
			if($this.val().length == 0) {
				$this.val(this.defaultValue);
			}
		})
	});
}


// Sticky scroller
// =============================================================================

// scroll txtpage sidebar
function stopper() {
    var $sidebar_top_value = $('#subnav').offset().top;
    $(window).scroll(function (event) {
    
        // what the y position of the scroll is
        var y = $(this).scrollTop();
        var h = $('#subnav').height();
        var d = $(document).height();
        var scroll_stop = (d-h) - 30; // 150px is combined visual height of the footer. (value wont change)
        
        // whether that's below the top threshold
        if (y >= $sidebar_top_value) {
            
            // if so, check to see if it's reached the bottom
            if (  y >= scroll_stop  ) {
            
            // if it's reached the bottom, 'glue' it there with position absolute
            $('#subnav').css({'position' : 'relative'}); // was sidebar
            $('#subnav').css({position : 'absolute', top : scroll_stop }); // was sidebar UL
            } else {
            // otherwise let it scroll
            $('#subnav').removeAttr('style');
            $('#subnav').addClass('scroll');
            }
            
        } else {
            // otherwise remove scrolling
            $('#subnav').removeClass('scroll');
        }
    
    });
}



// Masonry Launcher
// =============================================================================
$(function(){

    $('#secondary').masonry({
        columnWidth: 235,
        itemSelector: '.box'
    });

    $('#append_primary').click(function() {
        var $moreContent = makeNewBoxes(3);

        $('#primary').append( $moreContent )
            .masonry({ appendedContent: $moreContent }
                // using a callback to style the new elements
                , function() { $(this).css({background: '#222', color: '#EEE' })}
            )
        ;
    });

    $('#append_secondary').click(function() {
        var $moreContent = makeNewBoxes(1);

        $('#secondary').append( $moreContent )
            .masonry({ appendedContent: $moreContent }
                // using a callback to style the new elements
                , function() { $(this).css({background: '#222', color: '#EEE' })}
            )
        ;
    });

    // Resolve any incoming anchor links
    $(window).load(function(){
        if ( window.location.hash ) {
            var destination = $( window.location.hash ).offset().top;
            $("html:not(:animated),body:not(:animated)").scrollTop( destination );
        }
    });
});
