$(document).ready(function()
{
	// 14 is for padding
	var displayBlock = $('.contentBlock').height() + 14;
	var hideblock = $.cookie('hide');
	
	$('input[type="text"]').click(function() {
	   $(this).attr('value', '');
	});
    
    $(".blogLink").colorbox({width:"25%", inline:true, href:"#hiddenBlog"});
    // $(".twitterLink").colorbox({width:"25%", inline:true, href:"#hiddenTwitter"});
    
    if (hideblock == 'yes') {
        $('#noticeBanner').hide();
    } else {
        $('#noticeBanner').show();
    };
    
    $('.hideBanner').click(function() {
        $('#noticeBanner').slideUp();
        $.cookie( 'hide', 'yes', { path: '/', expires: 10 } );
        return false;
    });
    
	$(".slider").each(function()
	{
	    //var scrollBlock  = $(this).parent().prev().height();
	    var scrollBlock = $('.contentViewer').height();
	    var maxScroll    = scrollBlock - displayBlock;

		// alert(scrollBlock +':'+displayBlock);
        
		if (scrollBlock <= displayBlock)
		{
			// alert('noscroll');
			$(this).parent().addClass('noContent');
		}
		else
		{
			$(this).slider({
				orientation: "vertical",
				range: "min",
				min: 0,
				max: maxScroll,
				value: maxScroll,
				slide: function (ev, ui) {
					//$(this).parent().prev().css('bottom', '-' + (ui.value+14) + 'px');
					$('.contentViewer').css('bottom', '-' + (ui.value+28) + 'px');
				},
			});			
		}	
	});
    
    // Setting the actual width for the Collection UI
    var imageCount = $('ul.collectionImages li').size();
    
    $('ul.collectionImages').css('width', imageCount * 260 + "px");
        
    $('a.imageGallery').colorbox({transition:"fade", height:"600px"});
    $('#press a.imageGallery').colorbox({transition:"fade", height:"100%", width:"100%"});
    
    
    var horzScrollBlock = $('.collectionImages').width();
    var horzDisplayBlock = $('.collectionBlock').width();
    var maxHorzScroll = horzScrollBlock - horzDisplayBlock;
    
    $('#horzSlider').slider({
        min: 0,
        max: maxHorzScroll,
        slide: function (ev, ui) {
            $('.collectionImages').css('left', '-' + ui.value + 'px');
        },
    }); 
    
    // Stephen's Code	
	// Change mouse pointer to a hand
	$('.sliderImage').mouseover(function()
	{
		$(this).css({cursor:"pointer"});
	});

	// Change mouse pointer to a hand
	$('.seedToStoreTabs div').mouseover(function()
	{
		$(this).css({cursor:"pointer"});
	});
	
	// Make the tabs clickable
	$('.seedToStoreTabs div').click(function()
	{
		colNumber = this.id.replace('Clicker', '');
		colImage = $('#'+colNumber+'Image');
		
		// Run animation function
		animateColumns(colImage);
		selectTab($(this));
	});

	// Make images clickable
	$('.sliderImage').click(function()
	{
		colImage = $(this);

		colNumber = this.id.replace('Image', '');
		
		// Run animation function
		animateColumns(colImage);
		selectTab($('#'+colNumber+'Clicker'));
	});
	
	// Select/unselect tabs
	function selectTab(selectedDiv)
	{
		selectedLi = selectedDiv.parent();
		
		$('.seedToStoreTabs li').each(function()
		{
			if (this == selectedLi.get(0))
			{
				selectedLi.addClass('selected')
			}
			else
			{
				$(this).removeClass('selected');
			}
		});
		
	}
	
	// Animate the column transition
	function animateColumns(colImage)
	{		
		prevImage = '';
		
		$('.sliderImage').each(function(i)
		{
			// Close everything but the clicked image
			if (this != colImage.get(0))
			{
				// Hide the slider
				$(this).find('.sliderBlock').hide();

				// Fadeout the content
				$(this).find('.contentViewer').fadeOut();
				
				// Column animation
				$(this).animate({width:"146px"}).css({position:'absolute','z-index':1,top:0,left:(146*i)+"px"});
			}
			// Open the clicked image
			else
			{   
				// Fade in the content
				colImage.find('.sliderBlock:not(.noContent)').show();
				colImage.find('.contentViewer').fadeIn();
				
				// Column animation
				colImage.animate({width:"876px"}, function()
				{
					// Show slider after animation is done
				}).css({position:"absolute",left:0,top:0,'z-index':100});				
			}
			
			prevImage = this;
		});
	}
	
	// Auto select tab
	var columnToKey = {"#materials":"col1","#denim":"col2","#tailoring":"col3","#construction":"col4","#details":"col5","#finishing":"col6"};

	if (location.hash != '' && columnToKey[location.hash])
	{
		colImage = $('#'+columnToKey[location.hash]+'Image');
		animateColumns(colImage);
		selectTab($('#'+columnToKey[location.hash]+'Clicker'));
	}
	
    // End Stephen's Code

});
